Prettify error messages for invalid component names
[matrix.git] / matrix / matrix.py
index 265f2cb..4bf796a 100644 (file)
@@ -19,22 +19,22 @@ from build import build, build_only
 
 Error = RuntimeError
 
-def main():
-       command, targets, options = parse_args()
+def merge_config(name, value):
+       if isinstance(value, list):
+               value = getattr(config, name, []) + value
+       setattr(config, name, value)
 
-       if 'debug' in options:
-               config.debug = options['debug']
+def main(version):
+       command, targets, options = parse_args(version)
 
-       config_parse('config', True)
+       for name in ('debug', 'initial_config_dirs', 'pull_config_dirs'):
+               if name in options:
+                       merge_config(name, options[name])
 
-       for name in options:
-               value = options[name]
-               if type(value) == list:
-                       value = value + getattr(config, name, [])
-               setattr(config, name, value)
+       config_parse('main', True)
 
-       cache.init()
-       all_targets = components.init()
+       for name in options:
+               merge_config(name, options[name])
 
        for i in xrange(len(targets)):
                name = targets[i]
@@ -42,18 +42,22 @@ def main():
                        name = name.split(os.path.sep)[1]
                        targets[i] = name
 
-               if name not in config.components:
-                       raise Error('Component "%s" does not exist' % name)
+       cache.init()
+       components.init(targets)
 
        if not targets:
-               targets = all_targets
+               targets = components.by_name.keys()
+               targets.sort()
 
        command_funcs = {
                'install':      build,
                'install-only': build_only,
+               'meta':         meta,
                'clone':        clone,
                'clean':        clean,
+               'update':       update,
                'rebase':       rebase,
+               'update-rebase':update_rebase,
                'pull':         pull,
                'changes':      changes,
                'source-dist':  source_dist,
@@ -69,38 +73,48 @@ def print_help(file, parser):
        parser.print_help(file=file)
        print >>file, '''
 commands:
+  meta          download the components' meta repositories
   clone         download the components' git repositories
   install       download, build and install the components
   install-only  download, build and install the specified components without
                 dependencies
   clean         remove all non-tracked files from the component git repository
                 directories
-  rebase        update repositories from server by rebasing
-  pull          update repositories from server by merging
+  update        download remote repositories from server
+  rebase        rebase local repositories against remote repositories (remotes
+                won't be updated first)
+  update-rebase update remote repositories and rebase local repositories
+  pull          update remote repositories and merge with local repositories
   changes       show commits which are not on the server
   source-dist   download and package the component sources
 '''
 
-def parse_args():
-       values = dict(roots=[])
-
-       class ValueProxy(object):
-               def __setattr__(self, name, value):
-                       values[name] = value
+def parse_args(matrix_version):
+       options = {}
 
        def help(option, opt, value, parser):
                print_help(sys.stdout, parser)
                sys.exit(0)
 
-       def add_root(option, opt, value, parser):
-               values['roots'].insert(0, value)
+       def version(option, opt, value, parser):
+               print matrix_version
+               sys.exit(0)
+
+       def set_flag(option, opt, value, parser):
+               options[option.dest] = True
 
        def set_jobs(option, opt, value, parser):
                if value < 1:
                        raise optparse.OptionValueError(
                                'option %s: invalid job count: %d' % \
                                (opt, value))
-               values[option.dest] = value
+               options[option.dest] = value
+
+       def append(option, opt, value, parser):
+               l = options.get(option.dest, [])
+               if not l:
+                       options[option.dest] = l
+               l.append(value)
 
        parser = optparse.OptionParser(
                usage='%prog [<options>] <command> [<components>]',
@@ -112,31 +126,48 @@ def parse_args():
                action='callback', callback=help)
 
        parser.add_option(
+               '--version',
+               help='print Matrix version',
+               action='callback', callback=version)
+
+       parser.add_option(
                '-v', '--verbose',
                help='show build output',
-               dest='verbose', action='store_true')
+               dest='verbose', action='callback', callback=set_flag)
 
        parser.add_option(
                '-d', '--debug',
                help='debug output',
-               dest='debug', action='store_true')
+               dest='debug', action='callback', callback=set_flag)
 
        parser.add_option(
-               '-r', '--root',
+               '-c', '--config-dir', metavar='DIR',
+               help='add a directory for finding config files (may be set ' \
+                    'multiple times)',
+               dest='initial_config_dirs', action='callback', callback=append,
+               type='string')
+
+       parser.add_option(
+               '-p', '--pull-config-dirs',
+               help='git-pull config dirs which are backed by Git repositories',
+               dest='pull_config_dirs', action='callback', callback=set_flag)
+
+       parser.add_option(
+               '-r', '--root', metavar='URL',
                help='add a location for finding git repos when cloning ' \
-                    '(may be specified multiple times)', metavar='URL',
-               dest='roots', action='callback', callback=add_root,
+                    '(may be set multiple times)',
+               dest='roots', action='callback', callback=append,
                type='string')
 
        parser.add_option(
                '-f', '--force',
                help='build (or be quiet about) components with dirty files',
-               dest='force', action='store_true')
+               dest='force', action='callback', callback=set_flag)
 
        parser.add_option(
                '-k', '--keep-going',
                help='build as much as possible after an error',
-               dest='keep_going', action='store_true')
+               dest='keep_going', action='callback', callback=set_flag)
 
        parser.add_option(
                '-j', '--jobs',
@@ -150,12 +181,12 @@ def parse_args():
                dest='make_jobs', action='callback', callback=set_jobs,
                type='int')
 
-       proxy, args = parser.parse_args(values=ValueProxy())
+       values, args = parser.parse_args(values=None)
        if not args:
                print_help(sys.stderr, parser)
                sys.exit(1)
 
-       return args[0], args[1:], values
+       return args[0], args[1:], options
 
 def execute(args):
        if config.debug:
@@ -167,9 +198,21 @@ def execute(args):
 def remove_tree(path):
        execute(['rm', '-rf', path])
 
+def find_component(name):
+       try:
+               return components.by_name[name]
+       except KeyError:
+               raise Error("Component " + name + " not found");
+
+def meta(targets):
+       for name in targets:
+               c = find_component(name)
+               if not c.meta.exists():
+                       c.meta.clone()
+       
 def clone(targets):
        for name in targets:
-               c = config.components[name]
+               c = find_component(name)
                clone_component(c)
 
 def clone_component(c, overwrite=False):
@@ -195,7 +238,7 @@ def clean(targets):
        changed = False
 
        for name in targets:
-               c = config.components[name]
+               c = find_component(name)
                c.source.clean()
                cache.remove(c)
 
@@ -209,7 +252,7 @@ def for_each_repository(func, targets=None):
        ret = None
 
        for name in targets:
-               c = config.components[name]
+               c = find_component(name)
                if c.source.exists():
                        value = func(c.source)
                        if value:
@@ -220,9 +263,19 @@ def for_each_repository(func, targets=None):
 
        return ret
 
+def update(targets):
+       for_each_repository(lambda repo: repo.update(), targets)
+
 def rebase(targets):
        for_each_repository(lambda repo: repo.rebase(), targets)
 
+def update_rebase(targets):
+       def action(repo):
+               repo.update()
+               repo.rebase()
+
+       for_each_repository(action, targets)
+
 def pull(targets):
        for_each_repository(lambda repo: repo.pull(), targets)
 
@@ -230,7 +283,7 @@ def changes(targets):
        changed = False
 
        for name in targets:
-               c = config.components[name]
+               c = find_component(name)
 
                if c.source.exists():
                        if check_changes(c.source):
@@ -263,7 +316,7 @@ def source_dist(targets):
                os.makedirs(location)
 
        for name in targets:
-               c = config.components[name]
+               c = find_component(name)
                dist_changes(c, location)
                dist_sources(c, location)