implement a version checker
[mass-build.git] / vcs.py
1 # mass-build - Easily Build Software Involving a Large Amount of Source Repositories
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 import os, git, subprocess, re
19
20 '''A VCS must have an "update" method with an optional "mode" parameter taking one of the three values below,
21    a "version" method returning a version name (or None),
22    and a "newVersions" method which checks for new versions and prints the result to standard output.'''
23 MODE_FETCH = 0
24 MODE_REBASE = 1
25 MODE_RESET = 2
26
27 def natural_sort_key(val):
28         return [ (int(c) if c.isdigit() else c) for c in re.split('([0-9]+)', val) ]
29
30 # Fetch updates from git
31 class Git:
32         
33         def __init__(self, folder, url, commit):
34                 self.folder = os.path.abspath(folder)
35                 self.url = url
36                 self.commit = commit
37
38         class _ProgressPrinter(git.remote.RemoteProgress):
39                 def update(self, op_code, cur_count, max_count=None, message=''):
40                         print self._cur_line+(" "*30)+"\r",
41
42         def update(self, mode = MODE_REBASE):
43                 isBranch = (self.commit.startswith('origin/'))
44                 if isBranch:
45                         branchname = self.commit[len('origin/'):]
46                 else:
47                         branchname = "tag"
48                 # get us a git repository, and the "origin" remote
49                 if os.path.exists(self.folder):
50                         # load existing repo
51                         repo = git.Repo(self.folder)
52                         origin = repo.remotes.origin
53                         origin.config_writer.set_value("url", self.url) # make sure we use the current URL
54                 else:
55                         # create a new one
56                         os.makedirs(self.folder)
57                         repo = git.Repo.init(self.folder)
58                         origin = repo.create_remote('origin', self.url)
59                 origin.fetch(progress=Git._ProgressPrinter()) # download new data
60                 print " "*80+"\r", # clean the line we are in
61                 if mode == MODE_FETCH:
62                         return
63                 # create/find correct branch
64                 if branchname in repo.heads:
65                         branch = repo.heads[branchname]
66                 else:
67                         branch = repo.create_head(branchname, self.commit)
68                         if isBranch: # track remote branch
69                                 branch.set_tracking_branch(origin.refs[branchname])
70                 # update it to the latest remote commit
71                 branch.checkout()
72                 if mode == MODE_RESET:
73                         repo.head.reset(self.commit, working_tree=True)
74                 else:
75                         repo.git.rebase(self.commit)
76                 # update submodules
77                 repo.git.submodule("update", "--init", "--recursive", "--rebase")
78                 # done
79                 print "...done",
80                 if repo.head.reference.commit != repo.commit(self.commit):
81                         print "(keeping local patches around)",
82                 print
83
84         def version(self):
85                 repo = git.Repo(self.folder)
86                 v = repo.git.describe()
87                 if v.startswith('v'): v = v[1:]
88                 return v
89
90         def checkVersions(self):
91                 repo = git.Repo(self.folder)
92                 # get tag for current commit, if any
93                 commit = repo.commit(self.commit)
94                 commitTag = filter(lambda t: t.commit == commit, repo.tags)
95                 if not commitTag:
96                         print "Version is not a tag"
97                         return
98                 currentVersion = str(commitTag[0])
99                 # get sorted list of tag names
100                 tags = map(str, repo.tags)
101                 tags = filter(lambda t: natural_sort_key(t) > natural_sort_key(currentVersion), tags)
102                 if not tags: return
103                 tags.sort(key = natural_sort_key)
104                 print "Versions newer than "+self.commit+" available:"
105                 print tags
106
107 # Fetch updates via SVN
108 class SVN:
109         def __init__(self, folder, url):
110                 self.folder = os.path.abspath(folder)
111                 self.url = url
112
113         def update(self, forceVersion=False):
114                 if os.path.exists(self.folder):
115                         os.chdir(self.folder) # go into repository
116                         if forceVersion: subprocess.check_call(['svn', 'revert', '-R', '.'])
117                         subprocess.check_call(['svn', 'switch', self.url]) # and update to the URL we got
118                 else:
119                         os.makedirs(self.folder) # if even the parent folder does not exist, svn fails
120                         subprocess.check_call(['svn', 'co', self.url, self.folder]) # just download it
121         
122         def version(self):
123                 return None
124         
125         def checkVersions(self):
126                 print "Version checking not supporting with SVN"