| 1 |
import json |
| 2 |
import subprocess |
| 3 |
import sys |
| 4 |
|
| 5 |
from click._compat import WIN |
| 6 |
|
| 7 |
IMPORT_TEST = b"""\ |
| 8 |
import builtins |
| 9 |
|
| 10 |
found_imports = set() |
| 11 |
real_import = builtins.__import__ |
| 12 |
import sys |
| 13 |
|
| 14 |
def tracking_import(module, locals=None, globals=None, fromlist=None, |
| 15 |
level=0): |
| 16 |
rv = real_import(module, locals, globals, fromlist, level) |
| 17 |
if globals and globals['__name__'].startswith('click') and level == 0: |
| 18 |
found_imports.add(module) |
| 19 |
return rv |
| 20 |
builtins.__import__ = tracking_import |
| 21 |
|
| 22 |
import click |
| 23 |
rv = list(found_imports) |
| 24 |
import json |
| 25 |
click.echo(json.dumps(rv)) |
| 26 |
""" |
| 27 |
|
| 28 |
ALLOWED_IMPORTS = { |
| 29 |
"__future__", |
| 30 |
"codecs", |
| 31 |
"collections", |
| 32 |
"collections.abc", |
| 33 |
"configparser", |
| 34 |
"contextlib", |
| 35 |
"datetime", |
| 36 |
"enum", |
| 37 |
"errno", |
| 38 |
"fcntl", |
| 39 |
"functools", |
| 40 |
"gettext", |
| 41 |
"inspect", |
| 42 |
"io", |
| 43 |
"itertools", |
| 44 |
"os", |
| 45 |
"re", |
| 46 |
"stat", |
| 47 |
"struct", |
| 48 |
"sys", |
| 49 |
"threading", |
| 50 |
"types", |
| 51 |
"typing", |
| 52 |
"weakref", |
| 53 |
} |
| 54 |
|
| 55 |
if WIN: |
| 56 |
ALLOWED_IMPORTS.update( |
| 57 |
{ |
| 58 |
"ctypes", |
| 59 |
"ctypes._layout", |
| 60 |
"ctypes.wintypes", |
| 61 |
"msvcrt", |
| 62 |
"time", |
| 63 |
} |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
def test_light_imports(): |
| 68 |
c = subprocess.Popen( |
| 69 |
[sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE |
| 70 |
) |
| 71 |
rv = c.communicate(IMPORT_TEST)[0] |
| 72 |
rv = rv.decode("utf-8") |
| 73 |
imported = json.loads(rv) |
| 74 |
|
| 75 |
for module in imported: |
| 76 |
if module == "click" or module.startswith("click."): |
| 77 |
continue |
| 78 |
assert module in ALLOWED_IMPORTS |