diff options
author | Johannes Berg <johannes@sipsolutions.net> | 2013-04-04 16:50:00 +0200 |
---|---|---|
committer | Johannes Berg <johannes@sipsolutions.net> | 2013-04-04 18:21:15 +0200 |
commit | 01717eda0d7756d8bc03d5c3ab2de7a17c202a87 (patch) | |
tree | 2e6df843f4816252d1340c6adb4d0dfab67c26a2 /lib | |
parent | f0f61f73f8eb1343295aca85063e4296a0df8d57 (diff) |
devel: add git tracker helper script
This allows creating a git tree based on the backport
output automatically, to track an upstream kernel tree.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/git.py | 143 | ||||
-rw-r--r-- | lib/tempdir.py | 28 |
2 files changed, 170 insertions, 1 deletions
@@ -1,4 +1,4 @@ -import subprocess, re, os +import subprocess, re, os, tempfile class GitError(Exception): pass @@ -85,3 +85,144 @@ def get_blob(blob, outf, tree=None): stdout=outf, close_fds=True, cwd=tree) process.wait() _check(process) + +def clone(gittree, outputdir, options=[]): + process = subprocess.Popen(['git', 'clone'] + options + [gittree, outputdir]) + process.wait() + _check(process) + +def set_origin(giturl, gitdir): + process = subprocess.Popen(['git', 'remote', 'rm', 'origin'], + close_fds=True, universal_newlines=True, cwd=gitdir) + process.wait() + + process = subprocess.Popen(['git', 'remote', 'add', 'origin', giturl], + close_fds=True, universal_newlines=True, cwd=gitdir) + process.wait() + _check(process) + +def remote_update(gitdir): + process = subprocess.Popen(['git', 'remote', 'update'], + close_fds=True, universal_newlines=True, cwd=gitdir) + process.wait() + _check(process) + +def shortlog(from_commit, to_commit, tree=None): + process = subprocess.Popen(['git', 'shortlog', from_commit + '..' + to_commit], + stdout=subprocess.PIPE, + close_fds=True, universal_newlines=True, + cwd=tree) + stdout = process.communicate()[0] + process.wait() + _check(process) + return stdout + +def commit_env_vars(commitid, tree=None): + process = subprocess.Popen(['git', 'show', '--name-only', + '--format=format:GIT_AUTHOR_NAME=%an%nGIT_AUTHOR_EMAIL=%ae%nGIT_AUTHOR_DATE=%aD%x00', + commitid], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + close_fds=True, universal_newlines=True, + cwd=tree) + stdout = process.communicate()[0] + process.wait() + _check(process) + data = stdout.split('\x00')[0] + vals = data.split('\n') + d = {} + for k, v in map(lambda x: x.split('=', 1), vals): + d[k] = v + return d + +def commit_message(commitid, tree=None): + process = subprocess.Popen(['git', 'show', '--name-only', + '--format=format:%s%n%n%b%x00', commitid], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + close_fds=True, universal_newlines=True, + cwd=tree) + stdout = process.communicate()[0] + process.wait() + _check(process) + return stdout.split('\x00')[0] + +def remove_config(cfg, tree=None): + process = subprocess.Popen(['git', 'config', '--unset-all', cfg], + close_fds=True, universal_newlines=True, cwd=tree) + process.wait() + _check(process) + +def ls_remote(branch, tree=None, remote='origin'): + process = subprocess.Popen(['git', 'ls-remote', '--exit-code', remote, 'refs/heads/' + branch], + stdout=subprocess.PIPE, + close_fds=True, universal_newlines=True, cwd=tree) + stdout = process.communicate()[0] + process.wait() + _check(process) + sha = stdout.split()[0] + if not _sha_re.match(sha): + raise SHAError() + return sha + +def add(fn, tree=None): + process = subprocess.Popen(['git', 'add', fn], cwd=tree, + close_fds=True, universal_newlines=True) + process.wait() + _check(process) + +def commit(msg, tree=None, env = {}, opts=[]): + stdin = tempfile.NamedTemporaryFile(mode='wr') + stdin.write(msg) + stdin.seek(0) + process = subprocess.Popen(['git', 'commit', '--file=-'] + opts, + stdin=stdin.file, universal_newlines=True, env=env, + cwd=tree) + process.wait() + _check(process) + +def push(opts=[], tree=None): + process = subprocess.Popen(['git', 'push'] + opts, + close_fds=True, universal_newlines=True, cwd=tree) + process.wait() + _check(process) + +def log_commits(from_commit, to_commit, tree=None): + process = subprocess.Popen(['git', 'log', '--first-parent', '--format=format:%H', + from_commit + '..' + to_commit], + stdout=subprocess.PIPE, + close_fds=True, universal_newlines=True, + cwd=tree) + stdout = process.communicate()[0] + process.wait() + _check(process) + vals = stdout.split() + vals.reverse() + return vals + +def commit_env_vars(commitid, tree=None): + process = subprocess.Popen(['git', 'show', '--name-only', + '--format=format:GIT_AUTHOR_NAME=%an%nGIT_AUTHOR_EMAIL=%ae%nGIT_AUTHOR_DATE=%aD%x00', + commitid], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + close_fds=True, universal_newlines=True, + cwd=tree) + stdout = process.communicate()[0] + process.wait() + _check(process) + data = stdout.split('\x00')[0] + vals = data.split('\n') + d = {} + for k, v in map(lambda x: x.split('=', 1), vals): + d[k] = v + return d + +def rm(opts=[], tree=None): + process = subprocess.Popen(['git', 'rm'] + opts, + close_fds=True, universal_newlines=True, cwd=tree) + process.wait() + _check(process) + +def reset(opts=[], tree=None): + process = subprocess.Popen(['git', 'reset'] + opts, + close_fds=True, universal_newlines=True, cwd=tree) + process.wait() + _check(process) diff --git a/lib/tempdir.py b/lib/tempdir.py new file mode 100644 index 00000000..43c6945e --- /dev/null +++ b/lib/tempdir.py @@ -0,0 +1,28 @@ +# simple tempdir wrapper object for 'with' statement +# +# Usage: +# with tempdir.tempdir() as tmpdir: +# os.chdir(tmpdir) +# do something +# + +import tempfile, shutil + +class tempdir(object): + def __init__(self, suffix='', prefix='', dir=None, nodelete=False): + self.suffix = '' + self.prefix = '' + self.dir = dir + self.nodelete = nodelete + + def __enter__(self): + self._name = tempfile.mkdtemp(suffix=self.suffix, + prefix=self.prefix, + dir=self.dir) + return self._name + + def __exit__(self, type, value, traceback): + if self.nodelete: + print 'not deleting directory %s!' % self._name + else: + shutil.rmtree(self._name) |