Skip to content

Commit

Permalink
add gup-git-tree builder
Browse files Browse the repository at this point in the history
  • Loading branch information
timbertson committed Jan 29, 2017
1 parent a27f2ff commit 6e3d690
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ from within a build script. So you may wish to place it in
its own directory to avoid accidentally adding other scripts
to `$PATH`.

# Bundled builders

Gup includes some generic builder scripts (in [./builders][]), which are
typically installed alongside `gup`. These are intended to be used as builders
in a `Gupfile` (prefixed with `!`). They are provided as-is, and will not
necessarily be as portable or as stable as gup itself. If you bundle `gup` into
your project, you should also include any scripts you make use of (you can
place them next to the `gup` binary, as gup will automatically add its
directory to your `$PATH` when building).

----

### Python version requirements
Expand Down
71 changes: 71 additions & 0 deletions builders/gup-git-tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# Export the current stash (or a specific revision) as a .tgz archive
from __future__ import print_function

import os, sys, subprocess, shutil
from optparse import OptionParser

def resolve_commit(rev):
return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()

def get_commit(cwd, rev):
if rev is not None:
return resolve_commit(rev)

env = os.environ.copy()
env['GIT_AUTHOR_NAME'] = 'nobody'
env['GIT_AUTHOR_EMAIL'] = 'nobody@example.org'
env['GIT_AUTHOR_DATE'] = '1970-01-01T00:00:00Z'
env['GIT_COMMITTER_NAME'] = 'nobody'
env['GIT_COMMITTER_EMAIL'] = 'nobody@example.org'
env['GIT_COMMITTER_DATE'] ='1970-01-01T00:00:00Z'
out = subprocess.check_output(['git', 'stash', 'create'], env=env).strip()
if out == '':
return resolve_commit('HEAD')
else:
return out

def get_current_tar_commit(path):
gunzip = subprocess.Popen(['gunzip', '-c', path], stdout = subprocess.PIPE)
get_tar_commit = subprocess.Popen(['git', 'get-tar-commit-id'], stdin=gunzip.stdout)
current_commit, _ = get_tar_commit.communicate()
if not all([status == 0 for status in [ gunzip.wait(), get_tar_commit.wait() ]]):
print("WARN: unable to determine commit of existing file: %s" % (path,), file=sys.stderr)
return None
else:
return current_commit.strip()

def export_commit(commit, dest, target, cwd):
subprocess.check_call(['gup', '--always'])
checksum_proc = subprocess.Popen(['gup', '--contents'], stdin=subprocess.PIPE)
checksum_proc.communicate(commit)
assert checksum_proc.wait() == 0, "gup --contents failed"

if os.path.exists(target):
current_commit = get_current_tar_commit(target)
if current_commit == commit:
# Don't regenerate archive; we just end up with the same contents but different mtimes
# (https://blog.lnx.cx/2015/05/15/getting-consistent-fingerprints-from-git-archive/)
shutil.copyfile(target, dest)
else:
with open(dest, 'w') as dest_f:
subprocess.check_call(['git', 'archive', commit, '--format=tar.gz', '--prefix=git-export/'], stdout=dest_f, cwd=cwd)

def main():
try:
p = OptionParser()
p.add_option('--rev', help='export specific commit')
opts, args = p.parse_args()
dest, target = args

dest_dir = os.path.dirname(dest) or '.'
git_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], cwd=dest_dir).strip()
commit = get_commit(cwd=git_root, rev=opts.rev)
export_commit(commit, dest, target, cwd=git_root)
except KeyboardInterrupt:
sys.exit(1)
except subprocess.CalledProcessError as e:
print(str(e), file=sys.stderr)
sys.exit(1)

main()
1 change: 1 addition & 0 deletions nix/gup-ocaml.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ stdenv.mkDerivation {
installPhase = ''
mkdir -p $out
cp -r ocaml/bin $out/bin
cp builders/* $out/bin
cp -r share $out/share
'';
}
1 change: 1 addition & 0 deletions nix/gup-python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ stdenv.mkDerivation {
installPhase = ''
mkdir $out
cp -r python/bin $out/bin
cp builders/* $out/bin/
'';
}

0 comments on commit 6e3d690

Please sign in to comment.