3 import optparse
, os
, re
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
):
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).
21 class namespace_type(object):
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
__)
36 def filter_kernels(files
, exprs
):
37 # don't filter anything if no filtering expressions given
41 compiled
= [re
.compile(r
) for r
in exprs
]
44 for pattern
in compiled
:
45 match
= pattern
.match(fname
)
47 return match
.groupdict().get('arg', fname
)
51 # return a sequence of (possibly transformed) matched filenames
52 return set(filter(None, map(matches
, files
)))
56 # import configuration file
57 config
= import_config_file(options
.config
)
59 new_files
= config
.source
.get_new_files()
61 print 'Found %d new files...' % len(new_files
)
66 # commit the new files to the database
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',
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',
87 help='Do not print anything except errors.')
88 (options
, args
) = parser
.parse_args()