add motd generator script
[freebsd-maintainer-scripts/.git] / up.py
CommitLineData
efd73192
SB
1#!/usr/bin/env python3
2
3"""
4One updater script to pull them all
5"""
6
7import configparser
8import io
9import os
10import shlex
11import subprocess
12import sys
13
14user = os.environ['USER']
15pwd = os.path.expanduser('~{0}'.format(user) or '/home/{0}'.format(user))
16
17config = """
18[default]
19scm=git
20git_update_cmd=git pull
21git_clone_cmd=git clone
22svn_update_cmd=svn update
23svn_clone_cmd=svn checkout
24
25[freebsd-src]
26scm=svn
27remote=svn+ssh://{1}@svn.freebsd.org/base/head
28local={0}/freebsd/svn/src
29
30[freebsd-ports]
31scm=svn
32remote=svn+ssh://{1}@svn.freebsd.org/ports/head
33local={0}/freebsd/svn/ports
34
35[freebsd-doc]
36scm=svn
37remote=svn+ssh://{1}@svn.freebsd.org/doc/head
38local={0}/freebsd/svn/doc
39
40[netbsd-src]
41remote=http://git.edgebsd.org/EdgeBSD/netbsd-src.git
42local={0}/netbsd/git/src
43
44[netbsd-pkgsrc]
45remote=http://git.edgebsd.org/EdgeBSD/netbsd-pkgsrc.git
46local={0}/netbsd/git/pkgsrc
47
48[openbsd-src]
49remote=git://anoncvs.estpak.ee/openbsd-src
50local={0}/openbsd/git/src
51""".format(pwd, user)
52
53cfg = configparser.ConfigParser()
54cfg.read_string(config)
55
56def call(cmd):
57 subprocess.call(shlex.split(cmd))
58
59class UpdaterFactory(object):
60
61 class BaseUpdater(object):
62 def __init__(self):
63 self.scm = "none"
64 self.already_cloned = False
65
66 def setCloned(self):
67 self.already_clone = True
68
69 def isCloned(self):
70 return self.already_cloned
71
72 def up(self): raise NotImplemented("up() method to implement")
73
74 def clone(self): raise NotImplemented("clone() method to implement")
75
76 class SVNUpdater(BaseUpdater):
77 def __init__(self, p):
78 super().__init__()
79 self.p = p
80 self.scm = 'svn'
81
82 def up(self):
83 os.chdir(self.p.get('local'))
84 cmd = cfg.get('default', 'svn_update_cmd')
85 call(cmd)
86
87 def clone(self):
88 cmd = cfg.get('default', 'svn_clone_cmd')
89 rc = call('svn checkout {0} {1}'.format(self.p.get('remote'), self.p.get('local')))
90 if rc == 0:
91 self.setCloned()
92
93 class GitUpdater(BaseUpdater):
94 def __init__(self, p):
95 super().__init__()
96 self.p = p
97 self.scm = 'git'
98
99 def up(self):
100 os.chdir(self.p.get('local'))
101 cmd = cfg.get('default', 'git_update_cmd')
102 call(cmd)
103
104 def clone(self):
105 cmd = cfg.get('default', 'git_clone_cmd')
106 rc = call('{0} {1} {2}'.format(cmd , self.p.get('remote'), self.p.get('local')))
107 if rc == 0:
108 self.setCloned()
109
110 @classmethod
111 def fromdict(cls, p):
112 scm_type = p.get('scm')
113 if scm_type == 'git':
114 return cls.GitUpdater(p)
115 elif scm_type == 'svn':
116 return cls.SVNUpdater(p)
117 else:
118 raise Exception("scm_type '{0}' unknown".format(scm_type))
119
120class Config(object):
121 def __init__(self, cp):
122 self.cp = cp
123
124 def getSection(self, s, k):
125 if self.cp is None:
126 return ""
127 return self.cp._sections[s].get(k)
128
129 def getProjects(self):
130 projects = []
131 for s in self.cp._sections:
132 if s == 'default':
133 continue
134 remote = self.getSection(s, 'remote')
135 local = self.getSection(s, 'local')
136 scm = self.getSection(s, 'scm') or self.getSection('default', 'scm')
137 projects.append(
138 {'name': s, 'remote': remote, 'local': local, 'scm': scm}
139 )
140
141 return projects
142
143if __name__ == '__main__':
144
145 for p in Config(cfg).getProjects():
146 u = UpdaterFactory.fromdict(p)
147 if not u.isCloned():
148 u.clone()
149 else:
150 u.up()