diff options
-rw-r--r-- | morphlib/builder.py | 41 | ||||
-rwxr-xr-x | tests/hello-chunk.script | 2 | ||||
-rwxr-xr-x | tests/hello-stratum.script | 7 |
3 files changed, 37 insertions, 13 deletions
diff --git a/morphlib/builder.py b/morphlib/builder.py index f28ccca0..8eb7d4fa 100644 --- a/morphlib/builder.py +++ b/morphlib/builder.py @@ -30,6 +30,11 @@ class Builder(object): self.tempdir = tempdir self.msg = msg self.settings = settings + self.cachedir = morphlib.cachedir.CacheDir(settings['cachedir']) + + @property + def arch(self): + return os.uname()[4] def build(self, morph): '''Build a binary based on a morphology.''' @@ -52,7 +57,7 @@ class Builder(object): self.ex.run(morph.build_commands) self.ex.run(morph.test_commands) self.ex.run(morph.install_commands) - self.create_chunk(morph) + self.create_chunk(morph, repo, ref) self.tempdir.clear() def create_build_tree(self, morph, repo, ref): @@ -68,15 +73,14 @@ class Builder(object): self.ex.runv(['tar', '-C', self._build, '-xf', tarball]) os.remove(tarball) - def create_chunk(self, morph): + def create_chunk(self, morph, repo, ref): '''Create a Baserock chunk from the ``self._inst`` directory. The directory must be filled in with all the relevant files already. ''' - dirname = self.settings['cachedir'] - filename = os.path.join(dirname, '%s.chunk' % morph.name) + filename = self.get_cached_name('chunk', repo, ref) logging.debug('Creating chunk %s at %s' % (morph.name, filename)) self.ex.runv(['tar', '-C', self._inst, '-czf', filename, '.']) @@ -85,7 +89,9 @@ class Builder(object): os.mkdir(self._inst) self.ex = morphlib.execute.Execute(self.tempdir.dirname, self.msg) for chunk_name in morph.sources: - filename = self._chunk_filename(morph, chunk_name) + chunk_repo = self.settings['chunk-repo'] + chunk_ref = self.settings['chunk-ref'] + filename = self.get_cached_name('chunk', chunk_repo, chunk_ref) self.unpack_chunk(filename) self.create_stratum(morph) self.tempdir.clear() @@ -100,8 +106,7 @@ class Builder(object): ''' - dirname = os.path.dirname(morph.filename) - filename = os.path.join(dirname, '%s.stratum' % morph.name) + filename = self.get_cached_name('stratum', '', '') logging.debug('Creating stratum %s at %s' % (morph.name, filename)) self.ex.runv(['tar', '-C', self._inst, '-czf', filename, '.']) @@ -113,7 +118,23 @@ class Builder(object): def _inst(self): return self.tempdir.join('inst') - def _chunk_filename(self, morph, chunk_name): - dirname = os.path.dirname(morph.filename) - return os.path.join(dirname, '%s.chunk' % chunk_name) + def get_cached_name(self, kind, repo, ref): + '''Return the cached name of a binary blob, if and when it exists.''' + abs_ref = self.get_git_commit_id(repo, ref) + dict_key = { + 'kind': kind, + 'arch': self.arch, + 'repo': repo, + 'ref': abs_ref, + } + return self.cachedir.name(dict_key) + + def get_git_commit_id(self, repo, ref): + '''Return the full SHA-1 commit id for a repo+ref.''' + if repo and ref: + ex = morphlib.execute.Execute(repo, self.msg) + out = ex.runv(['git', 'rev-list', '-n1', ref]) + return out.strip() + else: + return '' diff --git a/tests/hello-chunk.script b/tests/hello-chunk.script index 60839abb..11cef4ef 100755 --- a/tests/hello-chunk.script +++ b/tests/hello-chunk.script @@ -23,4 +23,4 @@ tar -C "$DATADIR" -xf tests/hello-chunk.tar.gz ./morph build "$DATADIR/hello/hello.morph" \ --chunk-repo="file://$DATADIR/hello" --chunk-ref=master \ --cachedir="$DATADIR" -tar -tf "$DATADIR/hello.chunk" | LC_ALL=C sort +tar -tf "$DATADIR/"*.chunk | LC_ALL=C sort diff --git a/tests/hello-stratum.script b/tests/hello-stratum.script index 76055ba9..ec7d1b12 100755 --- a/tests/hello-stratum.script +++ b/tests/hello-stratum.script @@ -25,5 +25,8 @@ tar -C "$DATADIR" -xf tests/hello-chunk.tar.gz --cachedir="$DATADIR" cp tests/hello-stratum.morph "$DATADIR" -./morph build "$DATADIR/hello-stratum.morph" --git-base-url "file://$DATADIR/" -tar -tf "$DATADIR/hello.stratum" | LC_ALL=C sort +./morph build "$DATADIR/hello-stratum.morph" \ + --git-base-url "file://$DATADIR/" \ + --chunk-repo="file://$DATADIR/hello" --chunk-ref=master \ + --cachedir="$DATADIR" +tar -tf "$DATADIR/"*.stratum | LC_ALL=C sort |