1 # mass-build - Easily Build Software Involving a Large Amount of Source Repositories
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
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.
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.
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.
18 import os, git, subprocess, re
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.'''
27 def natural_sort_key(val):
28 return [ (int(c) if c.isdigit() else c) for c in re.split('([0-9]+)', val) ]
30 # Fetch updates from git
33 def __init__(self, folder, url, commit):
34 self.folder = os.path.abspath(folder)
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",
42 def update(self, mode = MODE_REBASE):
43 isBranch = (self.commit.startswith('origin/'))
45 branchname = self.commit[len('origin/'):]
48 # get us a git repository, and the "origin" remote
49 if os.path.exists(self.folder):
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
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:
63 # create/find correct branch
64 if branchname in repo.heads:
65 branch = repo.heads[branchname]
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
72 if mode == MODE_RESET:
73 repo.head.reset(self.commit, working_tree=True)
75 repo.git.rebase(self.commit)
77 repo.git.submodule("update", "--init", "--recursive", "--rebase")
80 if repo.head.reference.commit != repo.commit(self.commit):
81 print "(keeping local patches around)",
85 repo = git.Repo(self.folder)
86 v = repo.git.describe()
87 if v.startswith('v'): v = v[1:]
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)
96 print "Version is not a tag"
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)
103 tags.sort(key = natural_sort_key)
104 print "Versions newer than "+self.commit+" available:"
107 # Fetch updates via SVN
109 def __init__(self, folder, url):
110 self.folder = os.path.abspath(folder)
113 def update(self, mode = MODE_REBASE):
114 if mode == MODE_FETCH: raise Exception("Just fetching is not supported with SVN")
115 if os.path.exists(self.folder):
116 os.chdir(self.folder) # go into repository
117 if mode == MODE_RESET: subprocess.check_call(['svn', 'revert', '-R', '.'])
118 subprocess.check_call(['svn', 'switch', self.url]) # and update to the URL we got
120 os.makedirs(self.folder) # if even the parent folder does not exist, svn fails
121 subprocess.check_call(['svn', 'co', self.url, self.folder]) # just download it
126 def checkVersions(self):
127 print "Version checking not supporting with SVN"