diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2012-05-17 22:30:39 +0300 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2012-05-17 22:30:39 +0300 |
commit | 8732c86d6eb9e0efa5ce2af294a67af08c8561d0 (patch) | |
tree | 045a18798dd4e2b9f36e6f1513b4745b7c3c6026 | |
parent | a2f149c70d395003e2d36d3cdd2fdb15ee515dd7 (diff) | |
download | morph-8732c86d6eb9e0efa5ce2af294a67af08c8561d0.tar.gz |
Update refs in morphologies when doing "morph edit"
-rwxr-xr-x | morph | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -18,6 +18,7 @@ import cliapp import collections +import glob import json import logging import os @@ -725,6 +726,8 @@ class Morph(cliapp.Application): self._clone_to_directory(new_repo, repo, ref) system_branch = self._deduce_system_branch() + if system_branch is None: + raise morphlib.Error('Cannot deduce system branch') if system_branch == ref: self.runcmd(['git', 'checkout', system_branch], cwd=new_repo) @@ -732,6 +735,46 @@ class Morph(cliapp.Application): self.runcmd(['git', 'checkout', '-b', system_branch, ref], cwd=new_repo) + morphs_dirname = os.path.join(mine_directory, system_branch, 'morphs') + if morphs_dirname is None: + logging.warning('Can not find morphs directory, not updating') + return + for filename, morphology in self._load_morphologies(morphs_dirname): + if morphology['kind'] != 'stratum': + continue + changed = False + for spec in morphology['sources']: + spec_repo = self._resolve_reponame(spec['repo']) + if spec_repo == repo and spec['ref'] != system_branch: + if self.settings['verbose']: + print ('Replacing ref "%s" with "%s" in %s' % + (spec['ref'], system_branch, filename)) + spec['ref'] = system_branch + changed = True + if changed: + self._write_morphology(filename, morphology) + + def _load_morphologies(self, dirname): + pattern = os.path.join(dirname, '*.morph') + for filename in glob.glob(pattern): + with open(filename) as f: + text = f.read() + morphology = morphlib.morph2.Morphology(text) + yield filename, morphology + + def _write_morphology(self, filename, morphology): + as_dict = {} + for key in morphology.keys(): + value = morphology[key] + if value: + as_dict[key] = value + fd, tempname = tempfile.mkstemp(dir=os.path.dirname(filename)) + os.close(fd) + with open(tempname, 'w') as f: + json.dump(as_dict, fp=f, indent=4, sort_keys=True) + f.write('\n') + os.rename(tempname, filename) + def cmd_merge(self, args): '''Merge specified repositories from another system branch.''' |