# Copyright (C) 2016 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program. If not, see . import cliapp import contextlib import glob import logging import os import shutil import morphlib class FixRefPlugin(cliapp.Plugin): def enable(self): self.app.add_subcommand( 'fix', self.fix, arg_synopsis='FIX YOUR REFS') self.app.add_subcommand( 'unfix', self.unfix, arg_synopsis='UNFIX YOUR REFS') def disable(self): pass def fix(self, args): '''Convert all chunk refs in a system branch to be fixed SHA1s. This modifies all git commit references in system and stratum morphologies, in the current system branch, to be fixed SHA commit identifiers, rather than symbolic branch or tag names. This is useful for making sure none of the components in a system branch change accidentally. Consider the following scenario: * The `master` system branch refers to `gcc` using the `baserock/morph` ref. This is appropriate, since the main line of development should use the latest curated code. * You create a system branch to prepare for a release, called `TROVE_ID/release/2.0`. The reference to `gcc` is still `baserock/morph`. * You test everything, and make a release. You deploy the release images onto devices, which get shipped to your customers. * A new version GCC is committed to the `baserock/morph` branch. * Your release branch suddenly uses a new compiler, which may or may not work for your particular system at that release. To avoid this, you need to _petrify_ all git references so that they do not change accidentally. If you've tested your release with the GCC release that is stored in commit `94c50665324a7aeb32f3096393ec54b2e63bfb28`, then you should continue to use that version of GCC, regardless of what might happen in the master system branch. If, and only if, you decide that a new compiler would be good for your release should you include it in your release branch. This way, only the things that you change intentionally change in your release branch. ''' # if args: # raise cliapp.AppException('morph petrify takes no arguments') filename = morphlib.util.sanitise_morphology_path(args[0]) definitions_repo = morphlib.definitions_repo.open( '.', search_for_root=True, app=self.app) loader = definitions_repo.get_morphology_loader() filename = definitions_repo.relative_path(filename, cwd='.') # source_pool_context = definitions_repo.source_pool( # definitions_repo.HEAD, filename) # with source_pool_context as source_pool: # bc = morphlib.buildcommand.BuildCommand(self.app) # root = bc.resolve_artifacts(source_pool) # print root def unset_defaults(morphology): '''If a field is equal to its default, delete it. The morphology is assumed to be valid. ''' kind = morphology['kind'] defaults = morphlib.morphloader.MorphologyLoader._static_defaults[kind] for key in defaults: if key in morphology and morphology[key] == defaults[key]: del morphology[key] for spec in morphology['chunks']: if 'repo' in spec and spec['repo'] == spec['name']: del spec['repo'] if 'build-mode' in spec and spec['build-mode'] == \ morphlib.morphloader.MorphologyLoader._static_defaults['chunk']['build-mode']: del spec['build-mode'] if 'prefix' in spec and spec['prefix'] == \ morphlib.morphloader.MorphologyLoader._static_defaults['chunk']['prefix']: del spec['prefix'] if 'submodules' in spec and spec['submodules'] == \ morphlib.morphloader.MorphologyLoader._static_defaults['chunk']['submodules']: del spec['submodules'] for morph in definitions_repo.load_all_morphologies(): print morph['name'] if morph['kind'] != 'stratum': continue # TODO for testing if morph['name'] != "build-essential": continue for chunk in morph['chunks']: repo = chunk.get('repo') ref = chunk.get('ref') if repo is None or ref is None: print "FAIL" continue chunk['ref'] = 'xxx' chunk['unpetrify-ref'] = ref unset_defaults(morph) loader.save_to_file(morph.filename, morph) def unfix(self, args): '''Reverse the process of petrification. This undoes the changes `morph petrify` did. ''' if args: raise cliapp.AppException('morph petrify takes no arguments') ws = morphlib.workspace.open('.') sb = morphlib.sysbranchdir.open_from_within('.') loader = morphlib.morphloader.MorphologyLoader() morphs = self._load_all_sysbranch_morphologies(sb, loader) # Restore the ref for each stratum and chunk morphs.unpetrify_all() # Write morphologies back out again. self._save_dirty_morphologies(loader, sb, morphs.morphologies)