KVM test: Turning off debug output for frequently called monitor commands
[autotest-zwu.git] / mirror / mirror
blobf4c07be25da9d5d168f31841c38e89f411ee6959
1 #!/usr/bin/python
3 import optparse, os, re
5 import common
6 from autotest_lib.mirror import trigger
8 default_config_path = os.path.expanduser(
9 os.path.join(os.path.dirname(__file__), 'config.py'))
11 options = optparse.Values()
14 def import_config_file(path):
15 """
16 Import the config file executing it as python code inside a class
17 instance namespace to isolate the defined names inside it and return
18 the the namespace (ie it emulates __import__ but does not require
19 proper packages to the path of the module to import, just a full filename).
20 """
21 class namespace_type(object):
22 def __init__(self):
23 # hardcoded default values
24 self.filter_exprs = ()
25 self.trigger = trigger.trigger()
26 self.__file__ = os.path.abspath(path)
28 namespace = namespace_type()
30 # execute the file using the given namespace
31 execfile(path, namespace.__dict__)
33 return namespace
36 def filter_kernels(files, exprs):
37 # don't filter anything if no filtering expressions given
38 if not exprs:
39 return files
41 compiled = [re.compile(r) for r in exprs]
43 def matches(fname):
44 for pattern in compiled:
45 match = pattern.match(fname)
46 if match:
47 return match.groupdict().get('arg', fname)
49 return None
51 # return a sequence of (possibly transformed) matched filenames
52 return set(filter(None, map(matches, files)))
55 def main():
56 # import configuration file
57 config = import_config_file(options.config)
59 new_files = config.source.get_new_files()
60 if options.verbose:
61 print 'Found %d new files...' % len(new_files)
63 if not new_files:
64 return
66 # commit the new files to the database
67 if options.verbose:
68 print 'Committing to the known files database...'
69 config.source.store_files(new_files)
71 if not options.dry_run:
72 config.trigger.run(filter_kernels(new_files, config.filter_exprs))
75 if __name__ == '__main__':
76 usage = 'mirror [options]'
77 parser = optparse.OptionParser(usage=usage)
78 parser.add_option('-c', '--config-file', dest='config',
79 help='Location of the configuration file; defaults \
80 to %s' % default_config_path, default=default_config_path)
81 parser.add_option('-n', '--dry-run', dest='dry_run', action='store_true',
82 default=False,
83 help='Perform a dry run without scheduling jobs but \
84 updating the known files database.')
85 parser.add_option('-q', '--quiet', dest='verbose', action='store_false',
86 default=True,
87 help='Do not print anything except errors.')
88 (options, args) = parser.parse_args()
90 main()