1#!/usr/bin/env python3
2# coding: utf-8
3# PYTHON_ARGCOMPLETE_OK
4# Copyright: (c) 2019, Ansible Project
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7# Make coding more python3-ish
8from __future__ import (absolute_import, division, print_function)
9__metaclass__ = type
10
11
12import argparse
13import os.path
14import sys
15
16from straight.plugin import load
17
18try:
19    import argcomplete
20except ImportError:
21    argcomplete = None
22
23
24def build_lib_path(this_script=__file__):
25    """Return path to the common build library directory."""
26    hacking_dir = os.path.dirname(this_script)
27    libdir = os.path.abspath(os.path.join(hacking_dir, 'build_library'))
28
29    return libdir
30
31
32def ansible_lib_path(this_script=__file__):
33    """Return path to the common build library directory."""
34    hacking_dir = os.path.dirname(this_script)
35    libdir = os.path.abspath(os.path.join(hacking_dir, '..', 'lib'))
36
37    return libdir
38
39
40sys.path.insert(0, ansible_lib_path())
41sys.path.insert(0, build_lib_path())
42
43
44from build_ansible import commands, errors
45
46
47def create_arg_parser(program_name):
48    """
49    Creates a command line argument parser
50
51    :arg program_name: The name of the script.  Used in help texts
52    """
53    parser = argparse.ArgumentParser(prog=program_name,
54                                     description="Implements utilities to build Ansible")
55    return parser
56
57
58def main():
59    """
60    Start our run.
61
62    "It all starts here"
63    """
64    subcommands = load('build_ansible.command_plugins', subclasses=commands.Command)
65
66    arg_parser = create_arg_parser(os.path.basename(sys.argv[0]))
67    arg_parser.add_argument('--debug', dest='debug', required=False, default=False,
68                            action='store_true',
69                            help='Show tracebacks and other debugging information')
70    subparsers = arg_parser.add_subparsers(title='Subcommands', dest='command',
71                                           help='for help use build-ansible.py SUBCOMMANDS -h')
72    subcommands.pipe('init_parser', subparsers.add_parser)
73
74    if argcomplete:
75        argcomplete.autocomplete(arg_parser)
76
77    args = arg_parser.parse_args(sys.argv[1:])
78    if args.command is None:
79        print('Please specify a subcommand to run')
80        sys.exit(1)
81
82    for subcommand in subcommands:
83        if subcommand.name == args.command:
84            command = subcommand
85            break
86    else:
87        # Note: We should never trigger this because argparse should shield us from it
88        print('Error: {0} was not a recognized subcommand'.format(args.command))
89        sys.exit(1)
90
91    try:
92        retval = command.main(args)
93    except (errors.DependencyError, errors.MissingUserInput, errors.InvalidUserInput) as e:
94        print(e)
95        if args.debug:
96            raise
97        sys.exit(2)
98
99    sys.exit(retval)
100
101
102if __name__ == '__main__':
103    main()
104