Remember whether we already ran the unit-tests
[0release.git] / scm.py
blob06389a192e6b643d3b593d91404bb1178de0db3c
1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os, subprocess, tempfile
5 from zeroinstall import SafeException
6 from logging import info
7 from support import unpack_tarball
9 class SCM:
10 def __init__(self, root_dir, options):
11 self.options = options
12 self.root_dir = root_dir
13 assert type(root_dir) == str, root_dir
15 class GIT(SCM):
16 def _run(self, args, **kwargs):
17 info("Running git %s (in %s)", ' '.join(args), self.root_dir)
18 return subprocess.Popen(["git"] + args, cwd = self.root_dir, **kwargs)
20 def _run_check(self, args, **kwargs):
21 child = self._run(args, **kwargs)
22 code = child.wait()
23 if code:
24 raise SafeException("Git %s failed with exit code %d" % (repr(args), code))
26 def _run_stdout(self, args, **kwargs):
27 child = self._run(args, stdout = subprocess.PIPE, **kwargs)
28 stdout, unused = child.communicate()
29 if child.returncode:
30 raise SafeException('Failed to get current branch! Exit code %d: %s' % (child.returncode, stdout))
31 return stdout
33 def ensure_versioned(self, path):
34 """Ensure path is a file tracked by the version control system.
35 @raise SafeException: if file is not tracked"""
36 out = self._run_stdout(['ls-tree', 'HEAD', path]).strip()
37 if not out:
38 raise SafeException("File '%s' is not under version control, according to git-ls-tree" % path)
40 def reset_hard(self, revision):
41 self._run_check(['reset', '--hard', revision])
43 def ensure_committed(self):
44 child = self._run(["status", "-a"], stdout = subprocess.PIPE)
45 stdout, unused = child.communicate()
46 if not child.returncode:
47 raise SafeException('Uncommitted changes! Use "git-commit -a" to commit them. Changes are:\n' + stdout)
48 for scm in self._submodules():
49 scm.ensure_committed()
51 def _submodules(self):
52 for line in self._run_stdout(['submodule', 'status']).split('\n'):
53 if not line: continue
54 r, subdir = line.strip().split(' ')[:2]
55 scm = GIT(os.path.join(self.root_dir, subdir), self.options)
56 scm.rev = r
57 scm.rel_path = subdir
58 yield scm
60 def make_tag(self, version):
61 return 'v' + version
63 def tag(self, version, revision):
64 tag = self.make_tag(version)
65 if self.options.key:
66 key_opts = ['-u', self.options.key]
67 else:
68 key_opts = []
69 self._run_check(['tag', '-s'] + key_opts + ['-m', 'Release %s' % version, tag, revision])
70 print "Tagged as %s" % tag
72 def get_current_branch(self):
73 current_branch = self._run_stdout(['symbolic-ref', 'HEAD']).strip()
74 info("Current branch is %s", current_branch)
75 return current_branch
77 def delete_branch(self, branch):
78 self._run_check(['branch', '-D', branch])
80 def push_head_and_release(self, version):
81 self._run_check(['push', self.options.public_scm_repository, self.make_tag(version), self.get_current_branch()])
83 def ensure_no_tag(self, version):
84 tag = self.make_tag(version)
85 child = self._run(['tag', '-l', tag], stdout = subprocess.PIPE)
86 stdout, unused = child.communicate()
87 if tag in stdout.split('\n'):
88 raise SafeException(("Release %s is already tagged! If you want to replace it, do\n" +
89 "git-tag -d %s") % (version, tag))
91 def export(self, prefix, archive_file, revision):
92 child = self._run(['archive', '--format=tar', '--prefix=' + prefix + '/', revision], stdout = subprocess.PIPE)
93 subprocess.check_call(['bzip2', '-'], stdin = child.stdout, stdout = file(archive_file, 'w'))
94 status = child.wait()
95 if status:
96 if os.path.exists(archive_file):
97 os.unlink(archive_file)
98 raise SafeException("git-archive failed with exit code %d" % status)
100 def export_submodules(self, target):
101 # Export all sub-modules under target
102 cwd = os.getcwd()
103 target = os.path.abspath(target)
104 for scm in self._submodules():
105 tmp = tempfile.NamedTemporaryFile(prefix = '0release-')
106 try:
107 scm.export(prefix = '.', archive_file = tmp.name, revision = scm.rev)
108 os.chdir(os.path.join(target, scm.rel_path))
109 unpack_tarball(tmp.name)
110 finally:
111 tmp.close()
112 os.chdir(cwd)
114 def commit(self, message, branch, parent):
115 self._run_check(['add', '-u']) # Commit all changed tracked files to index
116 tree = self._run_stdout(['write-tree']).strip()
117 child = self._run(['commit-tree', tree, '-p', parent], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
118 stdout, unused = child.communicate(message)
119 commit = stdout.strip()
120 info("Committed as %s", commit)
121 self._run_check(['branch', '-f', branch, commit])
122 return commit
124 def get_head_revision(self):
125 proc = self._run(['rev-parse', 'HEAD'], stdout = subprocess.PIPE)
126 stdout, unused = proc.communicate()
127 if proc.returncode:
128 raise Exception("git rev-parse failed with exit code %d" % proc.returncode)
129 head = stdout.strip()
130 assert head
131 return head
133 def export_changelog(self, last_release_version, head, stream):
134 if last_release_version:
135 self._run_check(['log', 'refs/tags/v' + last_release_version + '..' + head], stdout = stream)
136 else:
137 self._run_check(['log', head], stdout = stream)
139 def grep(self, pattern):
140 self._run_check(['grep', pattern])
142 def has_submodules(self):
143 return os.path.isfile(os.path.join(self.root_dir, '.gitmodules'))
145 def get_scm(local_iface, options):
146 start_dir = os.path.dirname(os.path.abspath(local_iface.uri))
147 current = start_dir
148 while True:
149 if os.path.exists(os.path.join(current, '.git')):
150 return GIT(current, options)
151 parent = os.path.dirname(current)
152 if parent == current:
153 raise SafeException("Unable to determine which version control system is being used. Couldn't find .git in %s or any parent directory." % start_dir)
154 current = parent