#!/usr/bin/python
import argparse
import importlib
import inspect
import functools
import logging
import os
import sys
import traceback
import types


logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)


class DepGenerator(object):

	def __init__(self):
		# Maps a module (filename) to set of module (filenames) its dependent on.
		self._dependency_dict = {}

	def generateDepGraph(self, path):
		assert path, 'path must be provided.' 
		assert os.path.exists(path), 'path %s must refer to file/dir.' % path
		if os.path.isdir(path):
			logger.debug('path %s is dir.', path)
			for filename in os.listdir(path):
				self.generateDepGraph(os.path.join(path, filename))
		else:
			if path.endswith('.py'):
				self._GenerateDepGraphForOnePyFile(path)
		return self._dependency_dict

	def _GenerateDepGraphForOnePyFile(self, path):
		if self._dependency_dict.get(path, None) is None:
			self._dependency_dict[path] = set()
		base_path = os.path.dirname(os.path.abspath(path))
		module_name = os.path.basename(path)[:-3]
		sys.path.append(base_path)
		try:
			module = importlib.import_module(module_name)
			for (member_name, member_obj) in inspect.getmembers(module):
				if type(member_obj) == types.ModuleType:
					if hasattr(member_obj, '__file__') and not (
							# I wish I had a better way to do this.
							member_obj.__file__.startswith('/usr/')):
							dependency = member_obj.__file__.replace('.pyc', '.py')
					else:
							dependency = member_name
					self._dependency_dict[path].add(dependency)
		except ImportError as e:
			logger.error('Failed to import %s', path)
		except Exception as e:
			logger.error('Error happened while importing %s', path)

	@staticmethod
	def _ComparisonFunction(x, y):
		if x.startswith('/'):
			if y.startswith('/'):
				return (x > y) - (x < y)
			else:
				return +1
		elif y.startswith('/'):
			return -1
		else:
			return (x > y) - (x < y)

	@staticmethod
	def prettyPrint(dep_dict, hide_system_packages=False):
		for (k, deps) in dep_dict.items():
			if len(deps):
				print
				print(k)
				for dep in sorted(deps, key=functools.cmp_to_key(DepGenerator._ComparisonFunction)):
					if not hide_system_packages or dep.startswith('/'):
						print('%s%s' %('|_', dep))


def main():
	parser = argparse.ArgumentParser()
	parser.add_argument(dest='path',
			help='path (or dir) of the python file (or all python files under it).')
	parser.add_argument('-s', '--small_list', action='store_true', default=False,
			help='Pass -s to hide '
			'system	packages and get a smaller list.')
	args = parser.parse_args()
	dep_dict = DepGenerator().generateDepGraph(args.path)
	DepGenerator.prettyPrint(dep_dict, hide_system_packages=args.small_list)


if __name__ == '__main__':
	main()

7e9058d9b6ad598af157fcdb37fcb16dff228d6a
<mask token>


class DepGenerator(object):
    <mask token>

    def generateDepGraph(self, path):
        assert path, 'path must be provided.'
        assert os.path.exists(path), 'path %s must refer to file/dir.' % path
        if os.path.isdir(path):
            logger.debug('path %s is dir.', path)
            for filename in os.listdir(path):
                self.generateDepGraph(os.path.join(path, filename))
        elif path.endswith('.py'):
            self._GenerateDepGraphForOnePyFile(path)
        return self._dependency_dict

    def _GenerateDepGraphForOnePyFile(self, path):
        if self._dependency_dict.get(path, None) is None:
            self._dependency_dict[path] = set()
        base_path = os.path.dirname(os.path.abspath(path))
        module_name = os.path.basename(path)[:-3]
        sys.path.append(base_path)
        try:
            module = importlib.import_module(module_name)
            for member_name, member_obj in inspect.getmembers(module):
                if type(member_obj) == types.ModuleType:
                    if hasattr(member_obj, '__file__'
                        ) and not member_obj.__file__.startswith('/usr/'):
                        dependency = member_obj.__file__.replace('.pyc', '.py')
                    else:
                        dependency = member_name
                    self._dependency_dict[path].add(dependency)
        except ImportError as e:
            logger.error('Failed to import %s', path)
        except Exception as e:
            logger.error('Error happened while importing %s', path)

    @staticmethod
    def _ComparisonFunction(x, y):
        if x.startswith('/'):
            if y.startswith('/'):
                return (x > y) - (x < y)
            else:
                return +1
        elif y.startswith('/'):
            return -1
        else:
            return (x > y) - (x < y)
    <mask token>


<mask token>

<mask token>


class DepGenerator(object):

    def __init__(self):
        self._dependency_dict = {}

    def generateDepGraph(self, path):
        assert path, 'path must be provided.'
        assert os.path.exists(path), 'path %s must refer to file/dir.' % path
        if os.path.isdir(path):
            logger.debug('path %s is dir.', path)
            for filename in os.listdir(path):
                self.generateDepGraph(os.path.join(path, filename))
        elif path.endswith('.py'):
            self._GenerateDepGraphForOnePyFile(path)
        return self._dependency_dict

    def _GenerateDepGraphForOnePyFile(self, path):
        if self._dependency_dict.get(path, None) is None:
            self._dependency_dict[path] = set()
        base_path = os.path.dirname(os.path.abspath(path))
        module_name = os.path.basename(path)[:-3]
        sys.path.append(base_path)
        try:
            module = importlib.import_module(module_name)
            for member_name, member_obj in inspect.getmembers(module):
                if type(member_obj) == types.ModuleType:
                    if hasattr(member_obj, '__file__'
                        ) and not member_obj.__file__.startswith('/usr/'):
                        dependency = member_obj.__file__.replace('.pyc', '.py')
                    else:
                        dependency = member_name
                    self._dependency_dict[path].add(dependency)
        except ImportError as e:
            logger.error('Failed to import %s', path)
        except Exception as e:
            logger.error('Error happened while importing %s', path)

    @staticmethod
    def _ComparisonFunction(x, y):
        if x.startswith('/'):
            if y.startswith('/'):
                return (x > y) - (x < y)
            else:
                return +1
        elif y.startswith('/'):
            return -1
        else:
            return (x > y) - (x < y)

    @staticmethod
    def prettyPrint(dep_dict, hide_system_packages=False):
        for k, deps in dep_dict.items():
            if len(deps):
                print
                print(k)
                for dep in sorted(deps, key=functools.cmp_to_key(
                    DepGenerator._ComparisonFunction)):
                    if not hide_system_packages or dep.startswith('/'):
                        print('%s%s' % ('|_', dep))


<mask token>

<mask token>


class DepGenerator(object):

    def __init__(self):
        self._dependency_dict = {}

    def generateDepGraph(self, path):
        assert path, 'path must be provided.'
        assert os.path.exists(path), 'path %s must refer to file/dir.' % path
        if os.path.isdir(path):
            logger.debug('path %s is dir.', path)
            for filename in os.listdir(path):
                self.generateDepGraph(os.path.join(path, filename))
        elif path.endswith('.py'):
            self._GenerateDepGraphForOnePyFile(path)
        return self._dependency_dict

    def _GenerateDepGraphForOnePyFile(self, path):
        if self._dependency_dict.get(path, None) is None:
            self._dependency_dict[path] = set()
        base_path = os.path.dirname(os.path.abspath(path))
        module_name = os.path.basename(path)[:-3]
        sys.path.append(base_path)
        try:
            module = importlib.import_module(module_name)
            for member_name, member_obj in inspect.getmembers(module):
                if type(member_obj) == types.ModuleType:
                    if hasattr(member_obj, '__file__'
                        ) and not member_obj.__file__.startswith('/usr/'):
                        dependency = member_obj.__file__.replace('.pyc', '.py')
                    else:
                        dependency = member_name
                    self._dependency_dict[path].add(dependency)
        except ImportError as e:
            logger.error('Failed to import %s', path)
        except Exception as e:
            logger.error('Error happened while importing %s', path)

    @staticmethod
    def _ComparisonFunction(x, y):
        if x.startswith('/'):
            if y.startswith('/'):
                return (x > y) - (x < y)
            else:
                return +1
        elif y.startswith('/'):
            return -1
        else:
            return (x > y) - (x < y)

    @staticmethod
    def prettyPrint(dep_dict, hide_system_packages=False):
        for k, deps in dep_dict.items():
            if len(deps):
                print
                print(k)
                for dep in sorted(deps, key=functools.cmp_to_key(
                    DepGenerator._ComparisonFunction)):
                    if not hide_system_packages or dep.startswith('/'):
                        print('%s%s' % ('|_', dep))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(dest='path', help=
        'path (or dir) of the python file (or all python files under it).')
    parser.add_argument('-s', '--small_list', action='store_true', default=
        False, help='Pass -s to hide system\tpackages and get a smaller list.')
    args = parser.parse_args()
    dep_dict = DepGenerator().generateDepGraph(args.path)
    DepGenerator.prettyPrint(dep_dict, hide_system_packages=args.small_list)


<mask token>

<mask token>
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)


class DepGenerator(object):

    def __init__(self):
        self._dependency_dict = {}

    def generateDepGraph(self, path):
        assert path, 'path must be provided.'
        assert os.path.exists(path), 'path %s must refer to file/dir.' % path
        if os.path.isdir(path):
            logger.debug('path %s is dir.', path)
            for filename in os.listdir(path):
                self.generateDepGraph(os.path.join(path, filename))
        elif path.endswith('.py'):
            self._GenerateDepGraphForOnePyFile(path)
        return self._dependency_dict

    def _GenerateDepGraphForOnePyFile(self, path):
        if self._dependency_dict.get(path, None) is None:
            self._dependency_dict[path] = set()
        base_path = os.path.dirname(os.path.abspath(path))
        module_name = os.path.basename(path)[:-3]
        sys.path.append(base_path)
        try:
            module = importlib.import_module(module_name)
            for member_name, member_obj in inspect.getmembers(module):
                if type(member_obj) == types.ModuleType:
                    if hasattr(member_obj, '__file__'
                        ) and not member_obj.__file__.startswith('/usr/'):
                        dependency = member_obj.__file__.replace('.pyc', '.py')
                    else:
                        dependency = member_name
                    self._dependency_dict[path].add(dependency)
        except ImportError as e:
            logger.error('Failed to import %s', path)
        except Exception as e:
            logger.error('Error happened while importing %s', path)

    @staticmethod
    def _ComparisonFunction(x, y):
        if x.startswith('/'):
            if y.startswith('/'):
                return (x > y) - (x < y)
            else:
                return +1
        elif y.startswith('/'):
            return -1
        else:
            return (x > y) - (x < y)

    @staticmethod
    def prettyPrint(dep_dict, hide_system_packages=False):
        for k, deps in dep_dict.items():
            if len(deps):
                print
                print(k)
                for dep in sorted(deps, key=functools.cmp_to_key(
                    DepGenerator._ComparisonFunction)):
                    if not hide_system_packages or dep.startswith('/'):
                        print('%s%s' % ('|_', dep))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(dest='path', help=
        'path (or dir) of the python file (or all python files under it).')
    parser.add_argument('-s', '--small_list', action='store_true', default=
        False, help='Pass -s to hide system\tpackages and get a smaller list.')
    args = parser.parse_args()
    dep_dict = DepGenerator().generateDepGraph(args.path)
    DepGenerator.prettyPrint(dep_dict, hide_system_packages=args.small_list)


if __name__ == '__main__':
    main()

#!/usr/bin/python
import argparse
import importlib
import inspect
import functools
import logging
import os
import sys
import traceback
import types


logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)


class DepGenerator(object):

	def __init__(self):
		# Maps a module (filename) to set of module (filenames) its dependent on.
		self._dependency_dict = {}

	def generateDepGraph(self, path):
		assert path, 'path must be provided.' 
		assert os.path.exists(path), 'path %s must refer to file/dir.' % path
		if os.path.isdir(path):
			logger.debug('path %s is dir.', path)
			for filename in os.listdir(path):
				self.generateDepGraph(os.path.join(path, filename))
		else:
			if path.endswith('.py'):
				self._GenerateDepGraphForOnePyFile(path)
		return self._dependency_dict

	def _GenerateDepGraphForOnePyFile(self, path):
		if self._dependency_dict.get(path, None) is None:
			self._dependency_dict[path] = set()
		base_path = os.path.dirname(os.path.abspath(path))
		module_name = os.path.basename(path)[:-3]
		sys.path.append(base_path)
		try:
			module = importlib.import_module(module_name)
			for (member_name, member_obj) in inspect.getmembers(module):
				if type(member_obj) == types.ModuleType:
					if hasattr(member_obj, '__file__') and not (
							# I wish I had a better way to do this.
							member_obj.__file__.startswith('/usr/')):
							dependency = member_obj.__file__.replace('.pyc', '.py')
					else:
							dependency = member_name
					self._dependency_dict[path].add(dependency)
		except ImportError as e:
			logger.error('Failed to import %s', path)
		except Exception as e:
			logger.error('Error happened while importing %s', path)

	@staticmethod
	def _ComparisonFunction(x, y):
		if x.startswith('/'):
			if y.startswith('/'):
				return (x > y) - (x < y)
			else:
				return +1
		elif y.startswith('/'):
			return -1
		else:
			return (x > y) - (x < y)

	@staticmethod
	def prettyPrint(dep_dict, hide_system_packages=False):
		for (k, deps) in dep_dict.items():
			if len(deps):
				print
				print(k)
				for dep in sorted(deps, key=functools.cmp_to_key(DepGenerator._ComparisonFunction)):
					if not hide_system_packages or dep.startswith('/'):
						print('%s%s' %('|_', dep))


def main():
	parser = argparse.ArgumentParser()
	parser.add_argument(dest='path',
			help='path (or dir) of the python file (or all python files under it).')
	parser.add_argument('-s', '--small_list', action='store_true', default=False,
			help='Pass -s to hide '
			'system	packages and get a smaller list.')
	args = parser.parse_args()
	dep_dict = DepGenerator().generateDepGraph(args.path)
	DepGenerator.prettyPrint(dep_dict, hide_system_packages=args.small_list)


if __name__ == '__main__':
	main()
