← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~allenap/maas/find-modules into lp:maas

 

Gavin Panella has proposed merging lp:~allenap/maas/find-modules into lp:maas.

Commit message:
New script, utilities/find-imported-module-files, that finds the files of imported modules for a range of input files.

This is intended to help get correct dependency lists for use with packaging.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~allenap/maas/find-modules/+merge/129946

New script, utilities/find-imported-module-files, that finds the files of imported modules for a range of input files.

This is intended to help get correct dependency lists for use with packaging.
-- 
https://code.launchpad.net/~allenap/maas/find-modules/+merge/129946
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/maas/find-modules into lp:maas.
=== added file 'utilities/find-imported-module-files'
--- utilities/find-imported-module-files	1970-01-01 00:00:00 +0000
+++ utilities/find-imported-module-files	2012-10-16 17:03:21 +0000
@@ -0,0 +1,83 @@
+#!/usr/bin/env python2.7
+# -*- mode: python -*-
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Find imported modules.
+
+For example, to find dependency packages for the provisioning
+server, try the following:
+
+  $ find src/provisioningserver \\
+  >     -name '*.py' ! -path '*/test*' -print0 | \\
+  >   xargs -r0 utilities/finder.py --null | \\
+  >   xargs -r0 dpkg -S | cut -d: -f1 | sort -u
+
+"""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+
+import argparse
+from modulefinder import ModuleFinder
+from os import (
+    getcwd,
+    path,
+    )
+import sys
+
+sys.path.insert(0, path.dirname(__file__))
+from python_standard_libs import python_standard_libs
+
+
+def find_standard_library_modules(seed=python_standard_libs):
+    """Find all standard-library modules."""
+    finder = ModuleFinder()
+    for name in seed:
+        finder.import_module(name, name, None)
+    return set(finder.modules)
+
+
+argument_parser = argparse.ArgumentParser(
+    formatter_class=argparse.RawDescriptionHelpFormatter,
+    description=__doc__)
+argument_parser.add_argument(
+    "-0", "--null", help="delimit output with null bytes",
+    action="store_true", default=False)
+argument_parser.add_argument(
+    "filenames", nargs="+", metavar="FILENAME")
+
+
+if __name__ == '__main__':
+    options = argument_parser.parse_args()
+    standard_libs = find_standard_library_modules()
+    finder = ModuleFinder()
+    for filename in options.filenames:
+        finder.load_file(filename)
+    # Collect modules from the finder, eliminating those from the standard
+    # library.
+    modules = (
+        module for name, module in finder.modules.items()
+        if name not in standard_libs
+        )
+    # Collect the absolute paths for each module, eliminating those modules
+    # with no filename.
+    filenames = (
+        path.abspath(module.__file__) for module in modules
+        if module.__file__ is not None
+        )
+    # Narrow down to those modules not in the nearby source tree.
+    here = getcwd()
+    filenames = (
+        filename for filename in filenames
+        if not filename.startswith(here)
+        )
+    # Write it all out.
+    end = b"\0" if options.null else None
+    for filename in sorted(filenames):
+        print(filename, end=end)