File: pyi_pkgutil_iter_modules.py

package info (click to toggle)
pyinstaller 6.18.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,820 kB
  • sloc: python: 41,828; ansic: 12,123; makefile: 171; sh: 131; xml: 19
file content (61 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#-----------------------------------------------------------------------------
# Copyright (c) 2021-2023, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------

import os
import sys
import argparse
import pkgutil
import importlib

# Argument parser
parser = argparse.ArgumentParser(description="pkgutil iter_modules test")
parser.add_argument(
    'package',
    type=str,
    help="Package to test.",
)
parser.add_argument(
    '--prefix',
    type=str,
    default='',
    help="Optional prefix to pass to iter_modules.",
)
parser.add_argument(
    '--resolve-pkg-path',
    action='store_true',
    default=False,
    help="Resolve symbolic links in package path before passing it to pkgutil.iter_modules.",
)
parser.add_argument(
    '--output-file',
    default=None,
    type=str,
    help="Output file.",
)
args = parser.parse_args()

# Output file (optional)
if args.output_file:
    fp = open(args.output_file, 'w')
else:
    fp = sys.stdout

# Iterate over package's module
package = importlib.import_module(args.package)
pkg_path = package.__path__
if args.resolve_pkg_path:
    pkg_path = [os.path.realpath(path) for path in pkg_path]
for module in pkgutil.iter_modules(pkg_path, args.prefix):
    print("%s;%d" % (module.name, module.ispkg), file=fp)

# Cleanup
if args.output_file:
    fp.close()