diff --git a/README.md b/README.md index cf421aa..61d597e 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,22 @@ Until proper documentation is in place, please refer to: to RTFS +## Command Line Interface + +A simple CLI is provided in `scripts/di.py` which can be used to test basic +`distroinfo` functionality, profile, dump parsed data, etc. + +An example of dumping parsed rdoinfo into both YAML and JSON files: + + $> ./scripts/di.py dump -y rdoinfo.yaml -j rdoinfo.json -f git \ + 'https://github.com/redhat-openstack/rdoinfo' rdo-full.yml + + Dumping YAML to: rdoinfo.yaml + Dumping JSON to: rdoinfo.json + +Additional `docopt` module is required to run the CLI. + + ## Bugs Please use the diff --git a/scripts/di.py b/scripts/di.py new file mode 100755 index 0000000..c42d356 --- /dev/null +++ b/scripts/di.py @@ -0,0 +1,143 @@ +#!/usr/bin/python +""" +Usage: di.py fetch [-C ] [-f remote|git|local] ... + di.py dump [-y ] [-j ] [-C ] [-f remote|git|local] ... + di.py --help | --version + +Fetch, parse and dump remote distroinfo metadata. + +Commands: + fetch fetch specified info into + dump dump parsed info as YAML and/or JSON + +Arguments: + distroinfo repo URL + info file(s) to parse + +Options: + -f, --fetcher remote|git|local + choose a distroinfo fetcher to use + -y, --yaml-out dump parsed info into specified YAML file + -j, --json-out dump parsed info into specified YAML file + -C, --cache-dir directory to store cached distroinfo metadata + --version show distroinfo version + -h, --help show usage help +""" # noqa +# NOTE: docopt module is required to run this +# -*- encoding: utf-8 -*- +from __future__ import print_function +from docopt import docopt +import json +from timeit import default_timer as timer +import sys +import yaml + +from distroinfo import __version__ +from distroinfo import exception +from distroinfo.info import DistroInfo + + +def get_distroinfo(info_url, info_files, fetcher=None, cache_dir=None): + kwargs = {} + # use desired distroinfo fetcher + if fetcher == 'local': + kwargs['local_info'] = info_url + elif fetcher == 'git': + kwargs['remote_git_info'] = info_url + else: + kwargs['remote_info'] = info_url + + if cache_dir: + kwargs['cache_base_path'] = cache_dir + + return DistroInfo(info_files, **kwargs) + + +def fetch(info_url, info_files, fetcher=None, cache_dir=None): + if fetcher: + fetch_str = ' %s' % fetcher + else: + fetch_str = '' + print("Starting%s info fetch: %s" % (fetch_str, info_url)) + di = get_distroinfo(info_url, info_files, + fetcher=fetcher, cache_dir=cache_dir) + cache_path = getattr(di.fetcher, 'cache_path', None) + if cache_path: + print("Cache: %s" % cache_path) + t_start = timer() + info = di.get_info() + t_end = timer() + t = t_end - t_start + print("Fetched and parsed info with %d packages in %.2f s." + % (len(info['packages']), t)) + return 0 + + +def dump(info_url, info_files, fetcher=None, cache_dir=None, + yaml_out=None, json_out=None): + di = get_distroinfo(info_url, info_files, + fetcher=fetcher, cache_dir=cache_dir) + info = di.get_info() + if yaml_out or json_out: + if yaml_out: + print("Dumping YAML to: %s" % yaml_out) + f = open(yaml_out, 'w') + yaml.dump(info, f) + if json_out: + print("Dumping JSON to: %s" % json_out) + f = open(json_out, 'w') + json.dump(info, f) + else: + print(yaml.dump(info)) + return 0 + + +def distroinfo(cargs, version=__version__): + """ + distroinfo Command-Line Interface + + """ + code = 1 + args = docopt(__doc__, argv=cargs) + try: + if args['--version']: + if not version: + version = 'N/A' + print(version) + code = 0 + elif args['fetch']: + code = fetch( + info_url=args[''], + info_files=args[''], + cache_dir=args['--cache-dir'], + fetcher=args['--fetcher'], + ) + elif args['dump']: + code = dump( + info_url=args[''], + info_files=args[''], + yaml_out=args['--yaml-out'], + json_out=args['--json-out'], + cache_dir=args['--cache-dir'], + fetcher=args['--fetcher'], + ) + except ( + exception.InvalidInfoFormat, + KeyboardInterrupt, + ) as ex: + code = getattr(ex, 'exit_code', code) + print("") + print(str(ex) or type(ex).__name__) + + return code + + +def main(): + """ + distroinfo CLI entry point + """ + sys.exit(distroinfo(sys.argv[1:])) + + +if __name__ == '__main__': + main() diff --git a/setup.cfg b/setup.cfg index 2691373..6258a2b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,7 +8,8 @@ classifiers = Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 Environment :: OpenStack Intended Audience :: Information Technology Intended Audience :: System Administrators