-
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
Showing
10 changed files
with
152 additions
and
129 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
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
54 changes: 26 additions & 28 deletions
54
src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.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,41 +1,39 @@ | ||
from typing import Optional | ||
from pathlib import Path | ||
|
||
import chardet | ||
|
||
from packaging.requirements import InvalidRequirement | ||
from codemodder.logging import logger | ||
from codemodder.project_analysis.file_parsers.package_store import ( | ||
PackageStore, | ||
FileType, | ||
) | ||
from pathlib import Path | ||
from .base_parser import BaseParser | ||
import chardet | ||
from codemodder.logging import logger | ||
|
||
|
||
class RequirementsTxtParser(BaseParser): | ||
@property | ||
def file_type(self): | ||
return FileType.REQ_TXT | ||
|
||
def _parse_file(self, file: Path) -> Optional[PackageStore]: | ||
try: | ||
with open(file, "rb") as f: | ||
whole_file = f.read() | ||
enc = chardet.detect(whole_file) | ||
if enc["confidence"] > 0.9: | ||
encoding = enc.get("encoding") | ||
decoded = whole_file.decode(encoding.lower()) if encoding else "" | ||
lines = decoded.splitlines() if decoded else [] | ||
else: | ||
raise UnicodeError() | ||
return PackageStore( | ||
type=self.file_type, | ||
file=str(file), | ||
dependencies=set(self._parse_dependencies(lines)), | ||
# requirements.txt files do not declare py versions explicitly | ||
# though we could create a heuristic by analyzing each dependency | ||
# and extracting py versions from them. | ||
py_versions=[], | ||
) | ||
except (UnicodeError, OSError, InvalidRequirement): | ||
logger.debug("Error parsing file: %s", file) | ||
return None | ||
def _parse_file(self, file: Path) -> PackageStore | None: | ||
with open(file, "rb") as f: | ||
whole_file = f.read() | ||
|
||
enc = chardet.detect(whole_file) | ||
if enc["confidence"] > 0.9: | ||
encoding = enc.get("encoding") | ||
decoded = whole_file.decode(encoding.lower()) if encoding else "" | ||
lines = decoded.splitlines() if decoded else [] | ||
else: | ||
logger.debug("Unknown encoding for file: %s", file) | ||
return None | ||
|
||
return PackageStore( | ||
type=self.file_type, | ||
file=file, | ||
dependencies=set(self._parse_dependencies(lines)), | ||
# requirements.txt files do not declare py versions explicitly | ||
# though we could create a heuristic by analyzing each dependency | ||
# and extracting py versions from them. | ||
py_versions=[], | ||
) |
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
Oops, something went wrong.