-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate Repo Manager #148
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f1baa6
copy DependencyWriter from @drdavella
clavedeluna 04bd362
add basic req txt writer
clavedeluna 9083c6d
create RequirementsWriter, call PRM
clavedeluna dde9a57
update and add new unit tests
clavedeluna 0c49bce
fix setup parser
clavedeluna 6e40abb
change types
clavedeluna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 +1 @@ | ||
from .dependency_manager import DependencyManager, Requirement | ||
from .dependency_manager import DependencyManager |
32 changes: 32 additions & 0 deletions
32
src/codemodder/dependency_management/base_dependency_writer.py
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,32 @@ | ||
from abc import ABCMeta, abstractmethod | ||
from pathlib import Path | ||
from typing import Optional | ||
from codemodder.project_analysis.file_parsers.package_store import PackageStore | ||
from codemodder.change import ChangeSet | ||
from codemodder.dependency import Dependency | ||
from packaging.requirements import Requirement | ||
|
||
|
||
class DependencyWriter(metaclass=ABCMeta): | ||
dependency_store: PackageStore | ||
|
||
def __init__(self, dependency_store: PackageStore, parent_directory: Path): | ||
self.dependency_store = dependency_store | ||
self.path = Path(dependency_store.file) | ||
self.parent_directory = parent_directory | ||
|
||
@abstractmethod | ||
def write( | ||
self, dependencies: list[Dependency], dry_run: bool = False | ||
) -> Optional[ChangeSet]: | ||
pass | ||
|
||
def add(self, dependencies: list[Dependency]) -> Optional[list[Dependency]]: | ||
"""add any number of dependencies to the end of list of dependencies.""" | ||
new = [] | ||
for new_dep in dependencies: | ||
requirement: Requirement = new_dep.requirement | ||
if requirement not in self.dependency_store.dependencies: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.dependency_store.dependencies.append(requirement) | ||
new.append(new_dep) | ||
return new |
118 changes: 20 additions & 98 deletions
118
src/codemodder/dependency_management/dependency_manager.py
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,110 +1,32 @@ | ||
from functools import cached_property | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from packaging.requirements import Requirement | ||
|
||
from codemodder.change import Action, Change, ChangeSet, PackageAction, Result | ||
from codemodder.diff import create_diff | ||
from codemodder.change import ChangeSet | ||
from codemodder.dependency import Dependency | ||
from codemodder.dependency_management.requirements_txt_writer import ( | ||
RequirementsTxtWriter, | ||
) | ||
from codemodder.project_analysis.file_parsers.package_store import PackageStore | ||
from pathlib import Path | ||
|
||
|
||
class DependencyManager: | ||
dependencies_store: PackageStore | ||
parent_directory: Path | ||
_lines: list[str] | ||
_new_requirements: list[Dependency] | ||
|
||
def __init__(self, parent_directory: Path): | ||
def __init__(self, dependencies_store: PackageStore, parent_directory: Path): | ||
self.dependencies_store = dependencies_store | ||
self.parent_directory = parent_directory | ||
self.dependency_file_changed = False | ||
self._lines = [] | ||
self._new_requirements = [] | ||
|
||
@property | ||
def new_requirements(self) -> list[str]: | ||
return [str(x.requirement) for x in self._new_requirements] | ||
|
||
def add(self, dependencies: list[Dependency]): | ||
"""add any number of dependencies to the end of list of dependencies.""" | ||
for dep in dependencies: | ||
if dep.requirement.name not in self.dependencies: | ||
self.dependencies.update({dep.requirement.name: dep.requirement}) | ||
self._new_requirements.append(dep) | ||
|
||
def write(self, dry_run: bool = False) -> Optional[ChangeSet]: | ||
def write( | ||
self, dependencies: list[Dependency], dry_run: bool = False | ||
) -> Optional[ChangeSet]: | ||
""" | ||
Write the updated dependency files if any changes were made. | ||
Write `dependencies` to the appropriate location in the project. | ||
""" | ||
if not (self.dependency_file and self._new_requirements): | ||
return None | ||
|
||
original_lines = self._lines.copy() | ||
if not original_lines[-1].endswith("\n"): | ||
original_lines[-1] += "\n" | ||
|
||
requirement_lines = [f"{req}\n" for req in self.new_requirements] | ||
|
||
updated = original_lines + requirement_lines | ||
diff = create_diff(self._lines, updated) | ||
|
||
changes = [ | ||
Change( | ||
lineNumber=len(original_lines) + i + 1, | ||
description=dep.build_description(), | ||
# Contextual comments should be added to the right side of split diffs | ||
properties={ | ||
"contextual_description": True, | ||
"contextual_description_position": "right", | ||
}, | ||
packageActions=[ | ||
PackageAction(Action.ADD, Result.COMPLETED, str(dep.requirement)) | ||
], | ||
) | ||
for i, dep in enumerate(self._new_requirements) | ||
] | ||
|
||
if not dry_run: | ||
with open(self.dependency_file, "w", encoding="utf-8") as f: | ||
f.writelines(original_lines) | ||
f.writelines(requirement_lines) | ||
|
||
self.dependency_file_changed = True | ||
return ChangeSet( | ||
str(self.dependency_file.relative_to(self.parent_directory)), | ||
diff, | ||
changes=changes, | ||
) | ||
|
||
@property | ||
def found_dependency_file(self) -> bool: | ||
return self.dependency_file is not None | ||
|
||
@cached_property | ||
def dependency_file(self) -> Optional[Path]: | ||
try: | ||
# For now for simplicity only return the first file | ||
return next(Path(self.parent_directory).rglob("requirements.txt")) | ||
except StopIteration: | ||
pass | ||
match self.dependencies_store.type: | ||
case "requirements.txt": | ||
return RequirementsTxtWriter( | ||
self.dependencies_store, self.parent_directory | ||
).write(dependencies, dry_run) | ||
case "setup.py": | ||
pass | ||
return None | ||
|
||
@cached_property | ||
def dependencies(self) -> dict[str, Requirement]: | ||
""" | ||
Extract list of dependencies from requirements.txt file. | ||
Same order of requirements is maintained, no alphabetical sorting is done. | ||
""" | ||
if not self.dependency_file: | ||
return {} | ||
|
||
with open(self.dependency_file, "r", encoding="utf-8") as f: | ||
self._lines = f.readlines() | ||
|
||
return { | ||
requirement.name: requirement | ||
for x in self._lines | ||
# Skip empty lines and comments | ||
if (line := x.strip()) | ||
and not line.startswith("#") | ||
and (requirement := Requirement(line)) | ||
} |
57 changes: 57 additions & 0 deletions
57
src/codemodder/dependency_management/requirements_txt_writer.py
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,57 @@ | ||
from typing import Optional | ||
from codemodder.dependency_management.base_dependency_writer import DependencyWriter | ||
from codemodder.change import Action, Change, ChangeSet, PackageAction, Result | ||
from codemodder.diff import create_diff | ||
from codemodder.dependency import Dependency | ||
from packaging.requirements import Requirement | ||
|
||
|
||
class RequirementsTxtWriter(DependencyWriter): | ||
def write( | ||
self, dependencies: list[Dependency], dry_run: bool = False | ||
) -> Optional[ChangeSet]: | ||
new_dependencies = self.add(dependencies) | ||
if new_dependencies: | ||
return self.add_to_file(new_dependencies, dry_run) | ||
return None | ||
|
||
def add_to_file(self, dependencies: list[Requirement], dry_run: bool): | ||
lines = self._parse_file() | ||
original_lines = lines.copy() | ||
if not original_lines[-1].endswith("\n"): | ||
original_lines[-1] += "\n" | ||
|
||
requirement_lines = [f"{dep.requirement}\n" for dep in dependencies] | ||
updated_lines = original_lines + requirement_lines | ||
|
||
diff = create_diff(original_lines, updated_lines) | ||
|
||
changes = [ | ||
Change( | ||
lineNumber=len(original_lines) + i + 1, | ||
description=dep.build_description(), | ||
# Contextual comments should be added to the right side of split diffs | ||
properties={ | ||
"contextual_description": True, | ||
"contextual_description_position": "right", | ||
}, | ||
packageActions=[ | ||
PackageAction(Action.ADD, Result.COMPLETED, str(dep.requirement)) | ||
], | ||
) | ||
for i, dep in enumerate(dependencies) | ||
] | ||
|
||
if not dry_run: | ||
with open(self.path, "w", encoding="utf-8") as f: | ||
f.writelines(updated_lines) | ||
|
||
return ChangeSet( | ||
str(self.path.relative_to(self.parent_directory)), | ||
diff, | ||
changes=changes, | ||
) | ||
|
||
def _parse_file(self): | ||
with open(self.path, "r", encoding="utf-8") as f: | ||
return f.readlines() |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional[list[Dependency]]
->list[Dependency]
.I usually expect a
None
withOptional[...]
but here it just returns an empty list.