3
# client showing use of command_hook_mirror
5
from simplestreams import mirrors
6
from simplestreams.mirrors import command_hook
14
LOG = logging.getLogger('sstream-sync')
15
LOG.setLevel(logging.ERROR)
16
LOG.addHandler(logging.StreamHandler())
20
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
22
fpath, _fname = os.path.split(program)
27
for path in os.environ["PATH"].split(os.pathsep):
28
path = path.strip('"')
29
exe_file = os.path.join(path, program)
37
sys.stderr.write("WARN: %s" % msg)
41
parser = argparse.ArgumentParser()
42
defhook = command_hook.DEFAULT_HOOK_NAME
44
hooks = [("--hook-%s" % hook.replace("_", "-"), hook, False)
45
for hook in command_hook.HOOK_NAMES]
46
hooks.append(('--hook', defhook, False,))
48
parser.add_argument('--config', '-c',
49
help='read config file',
50
type=argparse.FileType('rb'))
52
for (argname, cfgname, required) in hooks:
53
parser.add_argument(argname, dest=cfgname, required=False)
55
parser.add_argument('--keep', action='store_true', default=False,
56
help='keep items in target up to MAX items '
57
'even after they have fallen out of the source')
58
parser.add_argument('--max', type=int, default=None,
59
help='store at most MAX items in the target')
60
parser.add_argument('--item-skip-download', action='store_true',
62
help='Do not download items that are to be inserted.')
64
parser.add_argument('--path', default="streams/v1/index.sjs",
65
help='sync from index or products file in mirror')
66
parser.add_argument('mirror_url')
67
cmdargs = parser.parse_args()
69
known_cfg = [('--item-skip-download', 'item_skip_download', False),
70
('--max', 'max', False),
71
('--keep', 'keep', False),
72
('mirror_url', 'mirror_url', True),
73
('--path', 'path', True)]
74
known_cfg.extend(hooks)
78
cfg = yaml.safe_load(cmdargs.config)
82
known_names = [i[1] for i in known_cfg]
83
unknown = [key for key in cfg if key not in known_names]
85
warn("unknown keys in config: %s\n" % str(unknown))
88
fallback = cfg.get(defhook, getattr(cmdargs, defhook, None))
90
for (argname, cfgname, required) in known_cfg:
91
val = getattr(cmdargs, cfgname)
98
if ((cfgname in command_hook.HOOK_NAMES or cfgname == defhook)
99
and cfg.get(cfgname) is not None):
100
if which(cfg[cfgname]) is None:
101
msg = "invalid input for %s. '%s' is not executable\n"
102
sys.stderr.write(msg % (argname, val))
105
if (cfgname in command_hook.REQUIRED_FIELDS and
106
cfg.get(cfgname) is None and not fallback):
107
missing.append((argname, cfgname,))
110
sys.stderr.write("must provide input for (--hook/%s for default):\n"
112
for (flag, cfg) in missing:
113
sys.stderr.write(" cmdline '%s' or cfgname '%s'\n" % (flag, cfg))
116
smirror = mirrors.UrlMirrorReader(cfg['mirror_url'])
117
tmirror = command_hook.CommandHookMirror(config=cfg)
118
tmirror.sync(smirror.reader, cfg['path'])
120
if __name__ == '__main__':