2016-04-10 04:00:00 +00:00
|
|
|
#!/usr/bin/python3
|
2013-01-23 17:28:19 +00:00
|
|
|
# python-deps - find the dependencies of a given python script.
|
2016-04-10 04:00:00 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
# Author(s): Will Woods <wwoods@redhat.com>
|
|
|
|
#
|
2015-05-30 11:20:59 +00:00
|
|
|
import copy
|
2013-01-23 17:28:19 +00:00
|
|
|
import os, sys
|
|
|
|
from modulefinder import ModuleFinder
|
2016-04-10 04:00:00 +00:00
|
|
|
from distutils.sysconfig import get_python_lib, get_config_var, get_makefile_filename, get_config_h_filename
|
2013-01-23 17:28:19 +00:00
|
|
|
|
|
|
|
sitedir = get_python_lib()
|
|
|
|
libdir = get_config_var('LIBDEST')
|
|
|
|
|
2016-04-10 04:00:00 +00:00
|
|
|
# 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"}
|
2015-05-30 11:20:59 +00:00
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
# 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)
|
|
|
|
|
2015-03-23 11:36:12 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
2013-01-23 17:28:19 +00:00
|
|
|
def pyfiles(moddir):
|
|
|
|
'''basically, "find $moddir -type f -name "*.py"'''
|
2015-03-23 11:36:12 +00:00
|
|
|
for curdir, _dirs, files in os.walk(moddir):
|
2013-01-23 17:28:19 +00:00
|
|
|
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 = []
|
2015-05-30 11:20:59 +00:00
|
|
|
|
|
|
|
scripts = copy.copy(sys.argv[1:])
|
2016-04-10 04:00:00 +00:00
|
|
|
scripts.append(os.path.join(libdir,'site.py'))
|
|
|
|
scripts.append(os.path.join(libdir,'sysconfig.py'))
|
2015-05-30 11:20:59 +00:00
|
|
|
|
|
|
|
while scripts:
|
|
|
|
script = scripts.pop()
|
|
|
|
|
2014-04-07 12:38:09 +00:00
|
|
|
finder = ModuleFinder()
|
|
|
|
finder.run_script(script) # parse the script
|
2016-04-10 04:00:00 +00:00
|
|
|
for mod in finder.modules.values():
|
2014-04-07 12:38:09 +00:00
|
|
|
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)
|
2013-01-23 17:28:19 +00:00
|
|
|
|
2015-05-30 11:20:59 +00:00
|
|
|
if mod.__file__ in alsoNeeded and alsoNeeded[mod.__file__] not in deps:
|
|
|
|
scripts.append(alsoNeeded[mod.__file__])
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
# Include some bits that the python install itself needs
|
2015-03-23 11:36:12 +00:00
|
|
|
print(get_makefile_filename())
|
|
|
|
print(get_config_h_filename())
|
2013-01-23 17:28:19 +00:00
|
|
|
|
2016-04-10 04:00:00 +00:00
|
|
|
# And print the list of unique deps.
|
|
|
|
for d in set(deps):
|
2015-03-23 11:36:12 +00:00
|
|
|
print(d)
|