diff options
author | Mark McLoughlin <markmc@redhat.com> | 2012-01-27 16:27:25 +0000 |
---|---|---|
committer | Mark McLoughlin <markmc@redhat.com> | 2012-01-27 16:27:25 +0000 |
commit | 7633bef9382bc6038837d19eb535bfbdcac76f3f (patch) | |
tree | a4bce55749a9280b86242455f059d3e4f0651454 /update.py | |
parent | 9d5ac5225d5daad3e0ab49f494641281f8fb9613 (diff) | |
download | oslo-7633bef9382bc6038837d19eb535bfbdcac76f3f.tar.gz oslo-7633bef9382bc6038837d19eb535bfbdcac76f3f.tar.xz oslo-7633bef9382bc6038837d19eb535bfbdcac76f3f.zip |
Re-factor update.py to avoid breaking tests
nosetests tries to import update.py and fails.
Re-factor the code so that we have a main function that doesn't get
executed on import.
Note: setup.py would break nose similarly, except nose ignores setup.py
by default.
Change-Id: I609019d492f6fc2d7f2e8b6165228f879cf1ddb7
Diffstat (limited to 'update.py')
-rw-r--r-- | update.py | 130 |
1 files changed, 74 insertions, 56 deletions
@@ -63,6 +63,7 @@ import sys from openstack.common import cfg + opts = [ cfg.ListOpt('modules', default=[], @@ -75,83 +76,100 @@ opts = [ help='Destination project directory'), ] -conf = cfg.ConfigOpts(usage='Usage: %prog [config-file|dest-dir]') -conf.register_cli_opts(opts) -args = conf() -if len(args) == 1: - def def_config_file(dest_dir): - return os.path.join(dest_dir, 'openstack-common.conf') +def _parse_args(argv): + conf = cfg.ConfigOpts(usage='Usage: %prog [config-file|dest-dir]') + conf.register_cli_opts(opts) + args = conf(argv) + + if len(args) == 1: + def def_config_file(dest_dir): + return os.path.join(dest_dir, 'openstack-common.conf') - argv = sys.argv[1:] - i = argv.index(args[0]) + i = argv.index(args[0]) - config_file = None - if os.path.isfile(argv[i]): - config_file = argv[i] - elif os.path.isdir(argv[i]) and os.path.isfile(def_config_file(argv[i])): - config_file = def_config_file(argv[i]) + config_file = None + if os.path.isfile(argv[i]): + config_file = argv[i] + elif os.path.isdir(argv[i]) and os.path.isfile(def_config_file(argv[i])): + config_file = def_config_file(argv[i]) - if config_file: - argv[i:i+1] = ['--config-file', config_file] - args = conf(argv) + if config_file: + argv[i:i+1] = ['--config-file', config_file] + args = conf(argv) + if args: + conf.print_usage(file=sys.stderr) + sys.exit(1) -if args: - conf.print_usage(file=sys.stderr) - sys.exit(1) + return conf -dest_dir = conf.dest_dir -if not dest_dir: - dest_dir = os.path.dirname(conf.config_file[-1]) -if not dest_dir or not os.path.isdir(dest_dir): - print >> sys.stderr, "A valid destination dir is required" - sys.exit(1) +def _mod_to_path(mod): + return os.path.join(*mod.split('.')) -if not conf.modules: - print >> sys.stderr, "A list of modules to copy is required" - sys.exit(1) -if not conf.base: - print >> sys.stderr, "A destination base module is required" - sys.exit(1) +def _dest_path(path, base, dest_dir): + return os.path.join(dest_dir, _mod_to_path(base), path) -print "Copying the %s modules under the %s module in %s" % \ - (','.join(conf.modules), conf.base, dest_dir) -for mod in conf.modules: +def _replace(path, pattern, replacement): + with open(path, "r+") as f: + lines = f.readlines() + f.seek(0) + f.truncate() + for line in lines: + f.write(re.sub(pattern, replacement, line)) - def mod_to_path(mod): - return os.path.join(*mod.split('.')) - def dest_path(path): - return os.path.join(dest_dir, mod_to_path(conf.base), path) +def _copy_file(path, base, dest_dir): + dest = _dest_path(path, base, dest_dir) - def replace(path, pattern, replacement): - with open(path, "r+") as f: - lines = f.readlines() - f.seek(0) - f.truncate() - for line in lines: - f.write(re.sub(pattern, replacement, line)) + if not os.path.isdir(os.path.dirname(dest)): + os.makedirs(os.path.dirname(dest)) - def copy_file(path): - dest = dest_path(path) + shutil.copy2(path, dest) - if not os.path.isdir(os.path.dirname(dest)): - os.makedirs(os.path.dirname(dest)) + _replace(dest, + '^from openstack.common', + 'from ' + base + '.openstack.common') - shutil.copy2(path, dest) - replace(dest, - '^from openstack.common', - 'from ' + conf.base + '.openstack.common') +def _copy_module(mod, base, dest_dir): + print "Copying openstack.common.%s under the %s module in %s" % \ + (mod, base, dest_dir) if '.' in mod: - path = mod_to_path('openstack.common') + path = _mod_to_path('openstack.common') for d in mod.split('.')[:-1]: path = os.path.join(path, d) - copy_file(os.path.join(path, '__init__.py')) + _copy_file(os.path.join(path, '__init__.py'), base, dest_dir) + + _copy_file(_mod_to_path('openstack.common.' + mod) + '.py', base, dest_dir) + + +def main(argv): + conf = _parse_args(argv) + + dest_dir = conf.dest_dir + if not dest_dir and conf.config_file: + dest_dir = os.path.dirname(conf.config_file[-1]) + + if not dest_dir or not os.path.isdir(dest_dir): + print >> sys.stderr, "A valid destination dir is required" + sys.exit(1) + + if not conf.modules: + print >> sys.stderr, "A list of modules to copy is required" + sys.exit(1) + + if not conf.base: + print >> sys.stderr, "A destination base module is required" + sys.exit(1) + + for mod in conf.modules: + _copy_module(mod, conf.base, dest_dir) + - copy_file(mod_to_path('openstack.common.' + mod) + '.py') +if __name__ == "__main__": + main(sys.argv[1:]) |