#!/usr/pkg/bin/python3.12

# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""main entry point."""

from __future__ import print_function

import argparse
import os
import sys
from importlib.metadata import version

from importlab import environment
from importlab import graph
from importlab import output
from importlab import utils


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('inputs', metavar='input', type=str, nargs='*',
                        help=('Input files or directories. Directories will be '
                              'recursively scanned for .py files'))
    parser.add_argument('--tree', dest='tree', action='store_true',
                        default=False,
                        help='Display import tree.')
    parser.add_argument('--unresolved', dest='unresolved', action='store_true',
                        default=False,
                        help='Display unresolved dependencies.')
    default_python_version = '%d.%d' % sys.version_info[:2]
    parser.add_argument('-V', '--python_version', type=str, action='store',
                        dest='python_version', default=default_python_version,
                        help='Python version of target code, e.g. "2.7"')
    parser.add_argument('-P', '--pythonpath', type=str, action='store',
                        dest='pythonpath', default='',
                        help=('Directories for reading dependencies - a list '
                              'of paths separated by "%s".') % os.pathsep)
    parser.add_argument('--trim', dest='trim', action='store_true',
                        default=False,
                        help=('Trim the dependencies of builtin and system '
                              'files.'))
    parser.add_argument('-v', '--version', action='version', version=version('importlab'),
                        help='Script version')
    return parser.parse_args()


def main():
    args = parse_args()

    # Exit early if we don't have any output args.
    if not (args.tree or args.unresolved):
        print('Nothing to do!')
        sys.exit(0)

    args.inputs = utils.expand_source_files(args.inputs)
    print('Reading %d files' % len(args.inputs))
    env = environment.create_from_args(args)
    import_graph = graph.ImportGraph.create(env, args.inputs, args.trim)

    if args.tree:
        print('Source tree:')
        output.print_tree(import_graph)
        output.maybe_show_unreadable(import_graph)
        sys.exit(0)

    if args.unresolved:
        print('Unresolved dependencies:')
        output.print_unresolved_dependencies(import_graph)
        output.maybe_show_unreadable(import_graph)
        sys.exit(0)


if __name__ == "__main__":
    sys.exit(main())
