-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08d64ef
commit 490cf62
Showing
8 changed files
with
109 additions
and
0 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
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,37 @@ | ||
import libcst as cst | ||
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand | ||
from codemodder.codemods.utils import is_setup_py_file | ||
from codemodder.codemods.utils_mixin import NameResolutionMixin | ||
|
||
|
||
class SetupPyAddDependencies(VisitorBasedCodemodCommand, NameResolutionMixin): | ||
def __init__(self, context: CodemodContext, dependencies): | ||
""" | ||
:param dependencies: | ||
""" | ||
super().__init__(context) | ||
self.filename = self.context.filename | ||
self.dependencies = dependencies | ||
|
||
def visit_Module(self, _: cst.Module) -> bool: | ||
""" | ||
Only visit module with this codemod if it's a setup.py file. | ||
""" | ||
return is_setup_py_file(self.filename) | ||
|
||
def leave_Call(self, original_node: cst.Call, updated_node: cst.Call): | ||
true_name = self.find_base_name(original_node.func) | ||
if true_name != "setuptools.setup": | ||
return original_node | ||
|
||
# todo: add self.dependencies to install_requires arg | ||
breakpoint() | ||
return updated_node | ||
|
||
|
||
# filename = "tests/samples/pkg_w_setuppy/setup.py" | ||
# with open(filename, "r", encoding="utf-8") as f: | ||
# source_tree = cst.parse_module(f.read()) | ||
# | ||
# codemod = SetupPyAddDependencies(CodemodContext(filename=filename), ["dep1"]) | ||
# codemod.transform_module(source_tree) |
Empty file.
File renamed without changes.
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,41 @@ | ||
from codemodder.dependency_management.setup_py_codemod import SetupPyAddDependencies | ||
from libcst.codemod import CodemodTest, CodemodContext | ||
from packaging.requirements import Requirement | ||
|
||
TEST_DEPENDENCIES = [Requirement("defusedxml==0.7.1"), Requirement("security~=1.2.0")] | ||
|
||
|
||
class TestSetupPyCodemod(CodemodTest): | ||
TRANSFORM = SetupPyAddDependencies | ||
CONTEXT = CodemodContext(filename="pkg/setup.py") | ||
|
||
def test_setup_call(self): | ||
before = """ | ||
from setuptools import setup | ||
setup( | ||
name="test pkg", | ||
description="testing", | ||
long_description="...", | ||
author="Pixee", | ||
packages=find_packages("src"), | ||
package_dir={"": "src"}, | ||
python_requires=">3.6", | ||
install_requires=[ | ||
"protobuf>=3.12,<3.18; python_version < '3'", | ||
"protobuf>=3.12,<4; python_version >= '3'", | ||
"psutil>=5.7,<6", | ||
"requests>=2.4.2,<3", | ||
], | ||
entry_points={}, | ||
) | ||
""" | ||
|
||
after = "" | ||
|
||
self.assertCodemod( | ||
before, after, TEST_DEPENDENCIES, context_override=self.CONTEXT | ||
) | ||
|
||
# def test_different_setup_call(self): | ||
# test does not call install_requires | ||
# test with no dependencies inside install_requires |
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,25 @@ | ||
from os import path | ||
from setuptools import find_packages, setup | ||
|
||
root_dir = path.abspath(path.dirname(__file__)) | ||
|
||
print(root_dir) | ||
|
||
setup( | ||
name="test pkg", | ||
description="testing", | ||
long_description="...", | ||
# The project's main homepage. | ||
# Author details | ||
author="Pixee", | ||
packages=find_packages("src"), | ||
package_dir={"": "src"}, | ||
python_requires=">3.6", | ||
install_requires=[ | ||
"protobuf>=3.12,<3.18; python_version < '3'", | ||
"protobuf>=3.12,<4; python_version >= '3'", | ||
"psutil>=5.7,<6", | ||
"requests>=2.4.2,<3", | ||
], | ||
entry_points={}, | ||
) |
Empty file.
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 @@ | ||
print("hello world") |