forked from sagemath/sage
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements on package discovery for setup.py (sagemath#41)
* Improvements on package discovery for setup.py * Exclude test file from flake8 * Add stdlib-list to azure
- Loading branch information
1 parent
8381bde
commit 622630a
Showing
13 changed files
with
159 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[flake8] | ||
max-line-length=88 | ||
extend-ignore=E203,D104,D100 | ||
exclude=tests/data/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import ast | ||
from functools import lru_cache | ||
from typing import List | ||
|
||
|
||
@lru_cache(maxsize=10) | ||
def get_std_modules() -> List: | ||
from stdlib_list import stdlib_list | ||
|
||
all_libs = set() | ||
for py_ver in ("2.7", "3.6", "3.7", "3.8"): | ||
all_libs.update(stdlib_list(py_ver)) | ||
return list(all_libs) | ||
|
||
|
||
def get_all_modules_imported_script(script_file: str) -> set: | ||
modules = set() | ||
|
||
def visit_Import(node): | ||
for name in node.names: | ||
modules.add(name.name.split(".")[0]) | ||
|
||
def visit_ImportFrom(node): | ||
# if node.module is missing it's a "from . import ..." statement | ||
# if level > 0 it's a "from .submodule import ..." statement | ||
if node.module is not None and node.level == 0: | ||
modules.add(node.module.split(".")[0]) | ||
|
||
node_iter = ast.NodeVisitor() | ||
node_iter.visit_Import = visit_Import | ||
node_iter.visit_ImportFrom = visit_ImportFrom | ||
with open(script_file, "r") as f: | ||
node_iter.visit(ast.parse(f.read())) | ||
return modules | ||
|
||
|
||
def get_vendored_dependencies(script_file: str) -> List: | ||
all_std_modules = get_std_modules() | ||
all_modules_used = get_all_modules_imported_script(script_file) | ||
vendored_modules = [] | ||
for dep in all_modules_used: | ||
if dep in all_std_modules: | ||
continue | ||
vendored_modules.append(dep.lower()) | ||
return vendored_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
|
||
from pytest import fixture | ||
|
||
|
||
@fixture | ||
def data_dir() -> str: | ||
return os.path.join(os.path.dirname(__file__), "data") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os | ||
import sys | ||
|
||
import numpy as np | ||
|
||
|
||
def foo(): | ||
import pandas | ||
|
||
|
||
def bar(): | ||
from requests import get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import os | ||
|
||
from grayskull.utils import ( | ||
get_all_modules_imported_script, | ||
get_std_modules, | ||
get_vendored_dependencies, | ||
) | ||
|
||
|
||
def test_get_std_modules(): | ||
std_modules = get_std_modules() | ||
assert "sys" in std_modules | ||
assert "os" in std_modules | ||
assert "ast" in std_modules | ||
assert "typing" in std_modules | ||
|
||
|
||
def test_get_all_modules_imported_script(data_dir): | ||
all_imports = get_all_modules_imported_script( | ||
os.path.join(data_dir, "foo_imports.py") | ||
) | ||
assert sorted(all_imports) == sorted(["numpy", "pandas", "requests", "os", "sys"]) | ||
|
||
|
||
def test_get_vendored_dependencies(data_dir): | ||
all_deps = get_vendored_dependencies(os.path.join(data_dir, "foo_imports.py")) | ||
assert sorted(all_deps) == sorted(["numpy", "pandas", "requests"]) |