~ubuntu-branches/ubuntu/wily/simplestreams/wily

« back to all changes in this revision

Viewing changes to bin/sstream-sync

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2013-03-26 01:10:01 UTC
  • Revision ID: package-import@ubuntu.com-20130326011001-342bcgb65worw4r8
Tags: upstream-0.1.0~bzr191
ImportĀ upstreamĀ versionĀ 0.1.0~bzr191

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# client showing use of command_hook_mirror
 
4
 
 
5
from simplestreams import mirrors
 
6
from simplestreams.mirrors import command_hook
 
7
 
 
8
import argparse
 
9
import logging
 
10
import sys
 
11
import os
 
12
import yaml
 
13
 
 
14
LOG = logging.getLogger('sstream-sync')
 
15
LOG.setLevel(logging.ERROR)
 
16
LOG.addHandler(logging.StreamHandler())
 
17
 
 
18
def which(program):
 
19
    def is_exe(fpath):
 
20
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
 
21
 
 
22
    fpath, _fname = os.path.split(program)
 
23
    if fpath:
 
24
        if is_exe(program):
 
25
            return program
 
26
    else:
 
27
        for path in os.environ["PATH"].split(os.pathsep):
 
28
            path = path.strip('"')
 
29
            exe_file = os.path.join(path, program)
 
30
            if is_exe(exe_file):
 
31
                return exe_file
 
32
 
 
33
    return None
 
34
 
 
35
 
 
36
def warn(msg):
 
37
    sys.stderr.write("WARN: %s" % msg)
 
38
 
 
39
 
 
40
def main():
 
41
    parser = argparse.ArgumentParser()
 
42
    defhook = command_hook.DEFAULT_HOOK_NAME
 
43
 
 
44
    hooks = [("--hook-%s" % hook.replace("_", "-"), hook, False)
 
45
             for hook in command_hook.HOOK_NAMES]
 
46
    hooks.append(('--hook', defhook, False,))
 
47
    
 
48
    parser.add_argument('--config', '-c',
 
49
                        help='read config file',
 
50
                        type=argparse.FileType('rb'))
 
51
 
 
52
    for (argname, cfgname, required) in hooks:
 
53
        parser.add_argument(argname, dest=cfgname, required=False)
 
54
 
 
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',
 
61
                        default=False,
 
62
                        help='Do not download items that are to be inserted.')
 
63
 
 
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()
 
68
 
 
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)
 
75
 
 
76
    cfg = {}
 
77
    if cmdargs.config:
 
78
        cfg = yaml.safe_load(cmdargs.config)
 
79
        if not cfg:
 
80
            cfg = {}
 
81
 
 
82
    known_names = [i[1] for i in known_cfg]
 
83
    unknown = [key for key in cfg if key not in known_names]
 
84
    if unknown:
 
85
        warn("unknown keys in config: %s\n" % str(unknown))
 
86
 
 
87
    missing = []
 
88
    fallback = cfg.get(defhook, getattr(cmdargs, defhook, None))
 
89
 
 
90
    for (argname, cfgname, required) in known_cfg:
 
91
        val = getattr(cmdargs, cfgname)
 
92
        if val is not None:
 
93
            cfg[cfgname] = val
 
94
        if val == "":
 
95
            cfg[cfgname] = None
 
96
        print cfgname, val
 
97
 
 
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))
 
103
                sys.exit(1)
 
104
 
 
105
        if (cfgname in command_hook.REQUIRED_FIELDS and
 
106
            cfg.get(cfgname) is None and not fallback):
 
107
            missing.append((argname, cfgname,))
 
108
 
 
109
    if missing:
 
110
        sys.stderr.write("must provide input for (--hook/%s for default):\n"
 
111
                         % defhook)
 
112
        for (flag, cfg) in missing:
 
113
            sys.stderr.write("  cmdline '%s' or cfgname '%s'\n" % (flag, cfg))
 
114
        sys.exit(1)
 
115
 
 
116
    smirror = mirrors.UrlMirrorReader(cfg['mirror_url'])
 
117
    tmirror = command_hook.CommandHookMirror(config=cfg)
 
118
    tmirror.sync(smirror.reader, cfg['path'])
 
119
 
 
120
if __name__ == '__main__':
 
121
    main()