525cdf546b0b0f948680b9c129c3ec958cb7097c
[freebsd-maintainer-scripts/.git] / up.py
1 #!/usr/bin/env python3
2
3 """
4 One updater script to pull them all
5 """
6
7 import configparser
8 import io
9 import os
10 import shlex
11 import subprocess
12 import sys
13
14 user = os.environ['USER']
15 pwd = os.path.expanduser('~{0}'.format(user) or '/home/{0}'.format(user))
16
17 config = """
18 [default]
19 scm=git
20 git_update_cmd=git pull
21 git_clone_cmd=git clone
22 svn_update_cmd=svn update
23 svn_clone_cmd=svn checkout
24
25 [freebsd-src]
26 scm=svn
27 remote=svn+ssh://{1}@svn.freebsd.org/base/head
28 local={0}/freebsd/svn/src
29
30 [freebsd-ports]
31 scm=svn
32 remote=svn+ssh://{1}@svn.freebsd.org/ports/head
33 local={0}/freebsd/svn/ports
34
35 [freebsd-doc]
36 scm=svn
37 remote=svn+ssh://{1}@svn.freebsd.org/doc/head
38 local={0}/freebsd/svn/doc
39
40 [netbsd-src]
41 remote=http://git.edgebsd.org/EdgeBSD/netbsd-src.git
42 local={0}/netbsd/git/src
43
44 [netbsd-pkgsrc]
45 remote=http://git.edgebsd.org/EdgeBSD/netbsd-pkgsrc.git
46 local={0}/netbsd/git/pkgsrc
47
48 [openbsd-src]
49 remote=git://anoncvs.estpak.ee/openbsd-src
50 local={0}/openbsd/git/src
51 """.format(pwd, user)
52
53 cfg = configparser.ConfigParser()
54 cfg.read_string(config)
55
56 def call(cmd):
57     subprocess.call(shlex.split(cmd))
58
59 class 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
120 class 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
143 if __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()