Prettify error messages for invalid component names
[matrix.git] / matrix / matrix.py
index 8d0edf9..4bf796a 100644 (file)
@@ -4,36 +4,37 @@
 #          Kalle Vahlman <kalle.vahlman@movial.fi>
 #          Tuomas Kulve <tuomas.kulve@movial.fi>
 
+import optparse
 import os
 import sys
 from sets import Set as set
 
 import cache
 import components
-import config
 import git
 import log
-from build import build
-from rootfs import RootFS
+from config import config
+from config import parse as config_parse
+from build import build, build_only
 
 Error = RuntimeError
 
-def main():
-       parse_config('config.local')
+def merge_config(name, value):
+       if isinstance(value, list):
+               value = getattr(config, name, []) + value
+       setattr(config, name, value)
 
-       command, params, targets = parse_args(sys.argv)
+def main(version):
+       command, targets, options = parse_args(version)
 
-       parse_config('config')
-       parse_config('boards')
-       parse_config('components')
+       for name in ('debug', 'initial_config_dirs', 'pull_config_dirs'):
+               if name in options:
+                       merge_config(name, options[name])
 
-       if 'global-cache' in config.flags:
-               config.cache_dir = config.global_cache_dir
+       config_parse('main', True)
 
-       if not os.path.exists(config.cache_dir):
-               os.makedirs(config.cache_dir)
-
-       all_targets = components.init()
+       for name in options:
+               merge_config(name, options[name])
 
        for i in xrange(len(targets)):
                name = targets[i]
@@ -41,139 +42,151 @@ 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
-
-       if command == 'install':
-               build(targets)
-       elif command == 'clone':
-               clone(targets)
-       elif command == 'clean':
-               clean(targets)
-       elif command == 'rebase':
-               rebase(targets)
-       elif command == 'pull':
-               pull(targets)
-       elif command == 'source-dist':
-               source_dist(targets)
-       elif command == 'rootfs':
-               build_rootfs(**params)
-       else:
-               raise Error('Invalid command: ' + command)
-
-def help(file, args):
+               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,
+       }
+
+       func = command_funcs.get(command)
+       if not func:
+               raise Error('Invalid command: "%s"' % command)
+
+       func(targets)
+
+def print_help(file, parser):
+       parser.print_help(file=file)
        print >>file, '''
-Copyright (C) 2006-2008 Movial Oy
-
-Usage: %(progname)s [<options>] <command> [<params>] [<components>]
-
-If no components are specified, all of them will be targeted.  All component
-metadata will be downloaded regardless of the specified command and components.
-
-Options:
-       -v              Verbose output.
-       -d              Debug output.
-       -r URL          Specify the root for component git repos to clone from.
-                       If this option is not given, the roots specified in the
-                       components file will be used.  This option may be
-                       specified multiple times.
-       -f              Build components with dirty files.
-       -k              Build as much as possible after an error.
-       -j N            Execute N build jobs in parallel.
-       -mj N           Run component builds with make -j N.
-       -h, --help      Print this help text.
-
-Commands and parameters:
-       clone           Download the components' git repositories.
-       install         Download, build and install the components.
-       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.
-       source-dist     Download and package the component sources.
-       rootfs          Build a rootfs from the current target installation.
-               -n              Do not clean env.faked and stripped directory.
-               -r              Only generate a stripped rootfs.
-               -j              Only generate a jffs2 image
-               -d              Only generate a rootstrap (development rootfs).
-''' % {'progname': args[0]}
-
-def parse_args(args):
-       def parse_jobs(arg):
-               jobs = int(arg)
-               if jobs <= 0:
-                       raise Error('Please specify a valid number of jobs')
-               return jobs
-
-       command = None
-       params = {}
-       targets = []
-
-       i = 1
-       while i < len(args):
-               if args[i].startswith('-'):
-                       if not command:
-                               if args[i] == '-v':
-                                       config.verbose = True
-                               elif args[i] == '-d':
-                                       config.debug = True
-                               elif args[i] == '-r':
-                                       i += 1
-                                       config.roots.append(args[i])
-                               elif args[i] == '-f':
-                                       config.force = True
-                               elif args[i] == '-k':
-                                       config.keep_going = True
-                               elif args[i] == '-j':
-                                       i += 1
-                                       config.jobs = parse_jobs(args[i])
-                               elif args[i] == '-mj':
-                                       i += 1
-                                       config.make_jobs = parse_jobs(args[i])
-                               elif args[i] in ('-h', '--help'):
-                                       help(sys.stdout, args)
-                                       sys.exit(0)
-                               else:
-                                       raise Error('Bad option: ' + args[i])
-                       elif command == 'rootfs':
-                               if args[i] == '-n':
-                                       i += 1
-                                       params['clean'] = False
-                               elif args[i] == '-r':
-                                       i += 1
-                                       params['rootfs_only'] = True
-                               elif args[i] == '-j':
-                                       i += 1
-                                       params['jffs2_only'] = True
-                               elif args[i] == '-d':
-                                       i += 1
-                                       params['devrootfs_only'] = True
-                       else:
-                               raise Error('Command takes no parameters')
-               elif not command:
-                       command = args[i]
-               else:
-                       targets.append(args[i])
-               i += 1
-
-       if not command:
-               help(sys.stderr, args)
+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
+  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(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
+
+       def set_jobs(option, opt, value, parser):
+               if value < 1:
+                       raise optparse.OptionValueError(
+                               'option %s: invalid job count: %d' % \
+                               (opt, 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>]',
+               add_help_option=False)
+
+       parser.add_option(
+               '-h', '--help',
+               help='show this help message and exit',
+               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)
+
+       parser.add_option(
+               '-d', '--debug',
+               help='debug output',
+               dest='debug', action='callback', callback=set_flag)
+
+       parser.add_option(
+               '-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 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='callback', callback=set_flag)
+
+       parser.add_option(
+               '-k', '--keep-going',
+               help='build as much as possible after an error',
+               dest='keep_going', action='callback', callback=set_flag)
+
+       parser.add_option(
+               '-j', '--jobs',
+               help='execute up to N build jobs in parallel', metavar='N',
+               dest='jobs', action='callback', callback=set_jobs,
+               type='int')
+
+       parser.add_option(
+               '-J', '--make-jobs',
+               help='run component builds with make -j N', metavar='N',
+               dest='make_jobs', action='callback', callback=set_jobs,
+               type='int')
+
+       values, args = parser.parse_args(values=None)
+       if not args:
+               print_help(sys.stderr, parser)
                sys.exit(1)
 
-       return command, params, targets
-
-def parse_config(path):
-       if os.path.isabs(path):
-               path = os.path.join(config.top_dir, path)
-
-       if os.path.exists(path):
-               if config.debug:
-                       print 'Reading', path
-
-               execfile(path, config.__dict__, config.__dict__)
+       return args[0], args[1:], options
 
 def execute(args):
        if config.debug:
@@ -185,70 +198,134 @@ 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):
-       have_repo = c.repo.exists()
+       have_source = c.source.exists()
        have_meta = c.meta.exists()
 
-       if not overwrite and have_repo and have_meta:
+       if not overwrite and have_source and have_meta:
                return
 
-       if overwrite and os.path.exists(c.repo.path):
-               print 'Removing', c.repo
+       if overwrite and os.path.exists(c.source.path):
+               print 'Removing', c
 
-               remove_tree(c.repo.path)
-               have_repo = False
+               remove_tree(c.source.path)
+               have_source = False
                have_meta = False
 
-       if not have_repo:
-               c.repo.clone()
+       if not have_source:
+               c.source.clone()
        if not have_meta:
                c.meta.clone()
 
 def clean(targets):
+       changed = False
+
        for name in targets:
-               c = config.components[name]
-               c.repo.clean()
+               c = find_component(name)
+               c.source.clean()
                cache.remove(c)
 
-               for repo in (c.repo, c.meta):
-                       if repo.dirty_files():
-                               log.message('Dirty files left in %s' % \
-                                           repo.path)
+               if not config.force:
+                       changed = check_dirty(c)
+
+       if changed:
+               raise Error()
 
 def for_each_repository(func, targets=None):
+       ret = None
+
        for name in targets:
-               c = config.components[name]
-               if c.repo.exists():
-                       func(c.repo)
-               func(c.meta)
+               c = find_component(name)
+               if c.source.exists():
+                       value = func(c.source)
+                       if value:
+                               ret = value
+               value = func(c.meta)
+               if value:
+                       ret = value
+
+       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)
 
+def changes(targets):
+       changed = False
+
+       for name in targets:
+               c = find_component(name)
+
+               if c.source.exists():
+                       if check_changes(c.source):
+                               changed = True
+
+               if check_changes(c.meta):
+                       changed = True
+
+               if not config.force and check_dirty(c):
+                       changed = True
+
+       if changed:
+               raise Error()
+
+def check_changes(repo):
+       ret = repo.changes()
+       if ret:
+               log.message('Changes in %s' % repo)
+       return ret
+
+def check_dirty(c):
+       ret = c.is_dirty()
+       if ret:
+               log.message('Dirty files in %s' % c.name)
+       return ret
+
 def source_dist(targets):
        location = 'dist'
        if not os.path.exists(location):
                os.makedirs(location)
 
        for name in targets:
-               c = config.components[name]
+               c = find_component(name)
                dist_changes(c, location)
                dist_sources(c, location)
 
 def dist_changes(c, location):
        path = os.path.join(location, c.name) + '.changes'
-       c.repo.dump_log(path)
+       c.source.dump_log(path)
 
 def dist_sources(c, location):
-       rev = c.repo.describe()
+       rev = c.source.describe()
        if rev:
                rev = '_' + rev
        else:
@@ -259,32 +336,4 @@ def dist_sources(c, location):
        if os.path.exists(path):
                os.remove(path)
 
-       c.repo.archive(name, path)
-
-def build_rootfs(clean=True, rootfs_only=False, jffs2_only=False, devrootfs_only=False):
-       parse_config('rootfs')
-
-       f = os.popen('sb-conf cu')
-       target = f.read()
-       f.close()
-
-       rootfs = RootFS(target.strip())
-       rootfs.include_paths(config.include_paths)
-       rootfs.include_files(config.include_files)
-       rootfs.filter_paths(config.exclude_paths)
-       rootfs.filter_files(config.exclude_files)
-       rootfs.filter_expressions(config.exclude_expressions)
-       rootfs.add_paths(config.created_paths)
-       rootfs.set_devices(config.devices)
-       rootfs.set_change_owner(config.change_owner)
-       rootfs.set_erase_size(config.boards[config.board].flash_erase_size)
-       rootfs.set_pad_size(config.boards[config.board].flash_pad_size)
-
-       if rootfs_only:
-               rootfs.generate(clean, build_target="rootfs")
-       elif jffs2_only:
-               rootfs.generate(clean, build_target="jffs2")
-       elif devrootfs_only:
-               rootfs.generate(clean, build_target="devrootfs")
-       else:
-               rootfs.generate(clean, build_target="all")
+       c.source.archive(name, path)