-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python requirements.txt support (#56)
* Various refactoring to support Python requirements.txt with forward-looking infra for other formats. * WIP. * Refactor comparisons to support more complex specifications. * Fix dependencies. * Update integration tests. * Add some coverage. * Add Python support.
- Loading branch information
1 parent
07d0f87
commit 6d81191
Showing
26 changed files
with
426 additions
and
125 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,24 @@ | ||
from rubrical.enum import PackageCheck, PackageTypes | ||
from rubrical.schemas.configuration import PackageRequirement | ||
from rubrical.schemas.package import Package | ||
|
||
from . import semversion, string | ||
|
||
|
||
def check_package( | ||
package_requirement: PackageRequirement, package: Package | ||
) -> PackageCheck: | ||
result = None | ||
|
||
if package_requirement.type == PackageTypes.SEMVER: | ||
result = semversion.compare_package_semver( | ||
package=package, package_requirement=package_requirement | ||
) | ||
elif package_requirement.type == PackageTypes.GENERIC: | ||
result = string.compare_package_str( | ||
package=package, package_requirement=package_requirement | ||
) | ||
else: | ||
raise NotImplementedError("Comparison type not implemented.") | ||
|
||
return result |
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,56 @@ | ||
import semver | ||
|
||
from rubrical.comparisons.utils import results_to_status | ||
from rubrical.enum import ( | ||
DependencySpecifications, | ||
PackageCheck, | ||
SemverComparison, | ||
) | ||
from rubrical.schemas.configuration import PackageRequirement | ||
from rubrical.schemas.package import Package | ||
|
||
|
||
def __semver_conversion(version: str): | ||
return version[1:] if version.startswith("v") else version | ||
|
||
|
||
def compare_package_semver( | ||
package_requirement: PackageRequirement, package: Package | ||
) -> PackageCheck: | ||
result = None | ||
|
||
if package.specifier in [ | ||
DependencySpecifications.EQ, | ||
DependencySpecifications.LT, | ||
DependencySpecifications.LTE, | ||
]: | ||
package_semver = __semver_conversion(package.version) | ||
elif package.specifier in [ | ||
DependencySpecifications.NE, | ||
DependencySpecifications.GT, | ||
DependencySpecifications.GTE, | ||
]: | ||
result = PackageCheck.NOOP | ||
elif package.specifier == DependencySpecifications.COMPATIBLE: | ||
parsed_version = __semver_conversion(package.version).split(".") | ||
if len(parsed_version) < 3: | ||
parsed_version += ["999999"] * (3 - len(parsed_version)) | ||
package_semver = ".".join(parsed_version) | ||
|
||
if not result: | ||
try: | ||
warn_signal = SemverComparison.GT.value != semver.compare( | ||
package_semver, | ||
__semver_conversion(package_requirement.warn), | ||
) | ||
block_signal = SemverComparison.GT.value != semver.compare( | ||
package_semver, | ||
__semver_conversion(package_requirement.block), | ||
) | ||
except ValueError: | ||
warn_signal = False | ||
block_signal = False | ||
|
||
result = results_to_status(warn_signal=warn_signal, block_signal=block_signal) | ||
|
||
return result |
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,13 @@ | ||
from rubrical.comparisons.utils import results_to_status | ||
from rubrical.enum import PackageCheck | ||
from rubrical.schemas.configuration import PackageRequirement | ||
from rubrical.schemas.package import Package | ||
|
||
|
||
def compare_package_str( | ||
package_requirement: PackageRequirement, package: Package | ||
) -> PackageCheck: | ||
warn_signal = package_requirement.warn >= package.version | ||
block_signal = package_requirement.block >= package.version | ||
|
||
return results_to_status(warn_signal=warn_signal, block_signal=block_signal) |
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 @@ | ||
from rubrical.enum import PackageCheck | ||
|
||
|
||
def results_to_status(warn_signal: bool, block_signal: bool) -> PackageCheck: | ||
if block_signal: | ||
status = PackageCheck.BLOCK | ||
elif warn_signal: | ||
status = PackageCheck.WARN | ||
else: | ||
status = PackageCheck.OK | ||
|
||
return status |
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,53 @@ | ||
import requirements | ||
|
||
from rubrical.enum import DependencySpecifications, SupportedPackageManagers | ||
from rubrical.package_managers.base_package_manager import BasePackageManager | ||
from rubrical.schemas.package import Package | ||
|
||
|
||
class Python(BasePackageManager): | ||
target_file = "requirements.txt" | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
self.name = SupportedPackageManagers.PYTHON.value | ||
|
||
self.specification_symbols = self.specification_symbols | { | ||
DependencySpecifications.COMPATIBLE.value: ["~="] | ||
} | ||
|
||
def _set_package(self, package_file_filename: str, requirement, spec): | ||
for key, value in self.specification_symbols.items(): | ||
if spec[0] in value: | ||
self.append_package( | ||
package_file_filename, | ||
Package( | ||
name=requirement.name, | ||
version=spec[1], | ||
specifier=DependencySpecifications(key), | ||
), | ||
) | ||
|
||
def parse_package_manager_file( | ||
self, package_file_filename: str, package_file_contents: str | ||
) -> None: | ||
self.packages[package_file_filename] = [] | ||
|
||
for req in requirements.parse(package_file_contents): | ||
if req.specifier: | ||
# Handle cases for single specifiers. | ||
if len(req.specs) == 1: | ||
[spec] = req.specs | ||
self._set_package(package_file_filename, req, spec) | ||
elif len(req.specs) > 1: | ||
[spec] = [ | ||
x | ||
for x in req.specs | ||
if x[0] | ||
in self.specification_symbols["GT"] | ||
+ self.specification_symbols["GTE"] | ||
] | ||
self._set_package(package_file_filename, req, spec) | ||
else: | ||
pass |
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from rubrical.enum import DependencySpecifications | ||
|
||
|
||
class Package(BaseModel): | ||
name: str | ||
version: str | ||
specifier: Optional[DependencySpecifications] = DependencySpecifications.EQ |
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.