#!/usr/libexec/platform-python
# python-deps - find the dependencies of a given python script.
#
# Copyright (C) 2012-2015 by Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import copy
import os, sys
import sysconfig
from modulefinder import ModuleFinder
from distutils.sysconfig import get_python_lib, get_config_var, get_makefile_filename, get_config_h_filename

sitedir = get_python_lib()
libdir = get_config_var('LIBDEST')

# stringprep is needed by the idna encoding, and idna is needed by requests.
# The encoding import is implicit so ModuleFinder doesn't find it right.
alsoNeeded = {sitedir+"/requests/__init__.py": libdir+"/stringprep.py"}

# A couple helper functions...
def moduledir(pyfile):
    '''Given a python file, return the module dir it belongs to, or None.'''
    for topdir in sitedir, libdir:
        relpath = os.path.relpath(pyfile, topdir)
        if '/' not in relpath: continue
        modname = relpath.split('/')[0]
        if modname not in ('..', 'site-packages'):
            return os.path.join(topdir, modname)

# pylint: disable=redefined-outer-name
def pyfiles(moddir):
    '''basically, "find $moddir -type f -name "*.py"'''
    for curdir, _dirs, files in os.walk(moddir):
        for f in files:
            if f.endswith(".py"):
                yield os.path.join(curdir, f)

# OK. Use modulefinder to find all the modules etc. this script uses!
mods = []
deps = []

scripts = copy.copy(sys.argv[1:])
scripts.append(os.path.join(libdir,'site.py'))
scripts.append(os.path.join(libdir,'sysconfig.py'))
# platform-specific sysconfigdata module, needed by sysconfig, not
# detected by ModuleFinder - RHBZ #1409177, Python #29113
# this will only work and is only needed on Python 3.6+, so hedge it
try:
    sysconfmod = os.path.join(libdir, sysconfig._get_sysconfigdata_name() + '.py')
    if os.path.exists(sysconfmod):
        scripts.append(sysconfmod)
except AttributeError:
    # _get_sysconfigdata_name won't exist on older Pythons
    pass

while scripts:
    script = scripts.pop()

    finder = ModuleFinder()
    finder.run_script(script) # parse the script
    for mod in finder.modules.values():
        if not mod.__file__: # this module is builtin, so we can skip it
            continue

        if mod.__file__ not in deps: # grab the file itself
            deps.append(mod.__file__)

        moddir = moduledir(mod.__file__)  # if it's part of a module...
        if moddir and moddir not in mods: #
            deps += list(pyfiles(moddir)) # ...get the whole module
            mods.append(moddir)

        if mod.__file__ in alsoNeeded and alsoNeeded[mod.__file__] not in deps:
            scripts.append(alsoNeeded[mod.__file__])

# Include some bits that the python install itself needs
print(get_makefile_filename())
print(get_config_h_filename())

# And print the list of unique deps.
for d in set(deps):
    print(d)
