Prettify error messages for invalid component names
[matrix.git] / matrix / matrix.py
index 6869879..4bf796a 100644 (file)
@@ -24,10 +24,10 @@ def merge_config(name, value):
                value = getattr(config, name, []) + value
        setattr(config, name, value)
 
-def main():
-       command, targets, options = parse_args()
+def main(version):
+       command, targets, options = parse_args(version)
 
-       for name in ('debug', 'initial_config_dirs'):
+       for name in ('debug', 'initial_config_dirs', 'pull_config_dirs'):
                if name in options:
                        merge_config(name, options[name])
 
@@ -36,17 +36,14 @@ def main():
        for name in options:
                merge_config(name, options[name])
 
-       cache.init()
-       components.init(targets)
-
        for i in xrange(len(targets)):
                name = targets[i]
                if name.startswith('src' + os.path.sep):
                        name = name.split(os.path.sep)[1]
                        targets[i] = name
 
-               if name not in components.by_name:
-                       raise Error('Component "%s" does not exist' % name)
+       cache.init()
+       components.init(targets)
 
        if not targets:
                targets = components.by_name.keys()
@@ -92,13 +89,17 @@ commands:
   source-dist   download and package the component sources
 '''
 
-def parse_args():
+def parse_args(matrix_version):
        options = {}
 
        def help(option, opt, value, parser):
                print_help(sys.stdout, parser)
                sys.exit(0)
 
+       def version(option, opt, value, parser):
+               print matrix_version
+               sys.exit(0)
+
        def set_flag(option, opt, value, parser):
                options[option.dest] = True
 
@@ -125,6 +126,11 @@ 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='callback', callback=set_flag)
@@ -142,6 +148,11 @@ def parse_args():
                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 set multiple times)',
@@ -187,15 +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 = components.by_name[name]
+               c = find_component(name)
                if not c.meta.exists():
                        c.meta.clone()
        
 def clone(targets):
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                clone_component(c)
 
 def clone_component(c, overwrite=False):
@@ -221,7 +238,7 @@ def clean(targets):
        changed = False
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                c.source.clean()
                cache.remove(c)
 
@@ -235,7 +252,7 @@ def for_each_repository(func, targets=None):
        ret = None
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                if c.source.exists():
                        value = func(c.source)
                        if value:
@@ -266,7 +283,7 @@ def changes(targets):
        changed = False
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
 
                if c.source.exists():
                        if check_changes(c.source):
@@ -299,7 +316,7 @@ def source_dist(targets):
                os.makedirs(location)
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                dist_changes(c, location)
                dist_sources(c, location)