1 # mass-build - Easily Build Software Involving a Large Amount of Source Repositories
2 # Copyright (C) 2012-2013 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, 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 def get_non_digit_prefix(val):
31 return re.match('[^0-9]*', val).group(0)
34 def __getattr__(self, name):
35 def call(*args, get_stderr = False):
36 cmd = ["git", name.replace('_', '-')] + list(args)
37 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT if get_stderr else None)
38 return output.decode('utf-8').strip('\n')
42 # Fetch updates from git
44 def __init__(self, folder, config):
45 self.folder = os.path.abspath(folder)
46 self.url = config['url']
47 self.commit = config['version']
49 def update(self, mode = MODE_REBASE):
50 isBranch = (self.commit.startswith('origin/'))
52 branchname = self.commit[len('origin/'):]
55 # get us a git repository, and the "origin" remote
56 if os.path.exists(self.folder):
59 git.remote("set-url", "origin", self.url) # make sure we use the current URL
62 os.makedirs(self.folder)
65 git.remote("add", "origin", self.url)
67 if mode == MODE_FETCH:
69 # create/find correct branch
70 if not git.branch("--list", branchname): # the branch does not yet exit
71 git.branch(branchname, self.commit)
72 if isBranch: # make sure we track the correct remote branch
73 git.branch("-u", self.commit, branchname)
74 # update it to the latest remote commit
75 git.checkout(branchname, get_stderr=True)
76 if mode == MODE_RESET:
77 git.reset("--hard", self.commit)
79 git.rebase(self.commit)
81 git.submodule("update", "--init", "--recursive", "--rebase")
83 print("...done", end=' ')
84 if git.rev_parse("HEAD") != git.rev_parse(self.commit):
85 print("(keeping local patches around)", end=' ')
91 return v[len(get_non_digit_prefix(v)):] # remove the non-digit prefix from v (so that it starts with a number)
93 def checkVersions(self):
94 self.update(mode = MODE_FETCH)
95 currentVersion = git.describe()
96 # get sorted list of tag names with the same non-digit prefix and higher version number
97 tags = git.tag().split('\n')
98 tags = [t for t in tags if get_non_digit_prefix(t) == get_non_digit_prefix(currentVersion) and natural_sort_key(t) > natural_sort_key(currentVersion)]
100 tags.sort(key = natural_sort_key)
101 print("Versions newer than "+currentVersion+" available:")
104 # Fetch updates via SVN
106 def __init__(self, folder, url):
107 self.folder = os.path.abspath(folder)
110 def update(self, mode = MODE_REBASE):
111 if mode == MODE_FETCH: raise Exception("Just fetching is not supported with SVN")
112 if os.path.exists(self.folder):
113 os.chdir(self.folder) # go into repository
114 if mode == MODE_RESET: subprocess.check_call(['svn', 'revert', '-R', '.'])
115 subprocess.check_call(['svn', 'switch', self.url]) # and update to the URL we got
117 os.makedirs(self.folder) # if even the parent folder does not exist, svn fails
118 subprocess.check_call(['svn', 'co', self.url, self.folder]) # just download it
123 def checkVersions(self):
124 print("Version checking not supporting with SVN")