8acb571aa161a4cb4df82d2a237d2cd77deb0521
[mass-build.git] / vcs.py
1 import os, git, subprocess
2
3 '''A VCS must have an "update" method with an optional "force" parameter.'''
4
5 # Fetch updates from git
6 class Git:
7         def __init__(self, folder, url, commit):
8                 self.folder = os.path.abspath(folder)
9                 self.url = url
10                 self.commit = commit
11         
12         class _ProgressPrinter(git.remote.RemoteProgress):
13                 def update(self, op_code, cur_count, max_count=None, message=''):
14                         print self._cur_line+(" "*30)+"\r",
15         
16         def update(self):
17                 print "Updating",self.folder
18                 isBranch = (self.commit.startswith('origin/'))
19                 if isBranch:
20                         branchname = self.commit[len('origin/'):]
21                 else:
22                         branchname = "tag"
23                 # get us a git repository, and the "origin" remote
24                 if os.path.exists(self.folder):
25                         # load existing repo
26                         repo = git.Repo(self.folder)
27                         origin = repo.remotes.origin
28                 else:
29                         # create a new one
30                         os.makedirs(self.folder)
31                         repo = git.Repo.init(self.folder)
32                         origin = repo.create_remote('origin', self.url)
33                 origin.fetch(progress=Git._ProgressPrinter()) # download new data
34                 print " "*80+"\r", # clean the line we are in
35                 # create/find correct branch
36                 if branchname in repo.heads:
37                         branch = repo.heads[branchname]
38                 else:
39                         branch = repo.create_head(branchname, self.commit)
40                         if isBranch:
41                                 branch.set_tracking_branch(origin.refs[branchname])
42                 # update it to the latest remote commit
43                 branch.checkout()
44                 repo.git.rebase(self.commit)
45                 print "...done",
46                 if repo.head.reference.commit != repo.refs[self.commit].commit:
47                         print "(keeping local patches around)",
48                 print
49
50 class KDEGit(Git):
51         def __init__(self, folder, name, commit):
52                 Git.__init__(self, folder, 'kde:'+name, commit)
53
54 # Fetch updates via SVN
55 class SVN:
56         def __init__(self, folder, svnPath):
57                 self.folder = os.path.abspath(folder)
58                 self.svnPath = svnPath
59         
60         def update(self):
61                 print "Updating",self.folder
62                 if os.path.exists(self.folder):
63                         os.chdir(self.folder) # go into repository
64                         subprocess.check_call(['svn', 'switch', self.svnPath]) # and update to the URL we got
65                 else:
66                         subprocess.check_call(['svn', 'co', self.svnPath, self.folder])# just download it
67
68 class KDESVN(SVN):
69         def __init__(self, folder, svnPath):
70                 SVN.__init__(self, folder, 'svn://svn.kde.org/home/kde/'+svnPath)