-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
11 changed files
with
206 additions
and
89 deletions.
There are no files selected for viewing
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
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 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 @@ | ||
# Packages |
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,18 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
__doc__ = r""" | ||
Created on 31/07/2023 | ||
""" | ||
|
||
__author__ = "Christian Heider Nielsen" | ||
|
||
from pathlib import Path | ||
|
||
with open(Path(__file__).parent / "README.md", "r") as this_init_file: | ||
__doc__ += this_init_file.read() | ||
|
||
from .pip_parsing import * | ||
from .reloading import * | ||
from .editable import * |
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,93 @@ | ||
import json | ||
from importlib.metadata import Distribution | ||
|
||
__all__ = [ | ||
"dist_is_editable", | ||
"package_is_editable", | ||
"get_dist_package_location", | ||
"get_package_location", | ||
] | ||
|
||
from pathlib import Path | ||
|
||
|
||
def dist_is_editable(dist: Distribution) -> bool: | ||
""" | ||
Return True if given Distribution is an editable installation. | ||
""" | ||
|
||
top_level_name = dist.read_text("top_level.txt").split("\n")[0].strip() | ||
|
||
if dist._read_files_egginfo() is not None: | ||
if top_level_name == dist._path.parent.stem: | ||
return True | ||
|
||
if dist._read_files_distinfo() is not None: | ||
direct_url_str = dist.read_text("direct_url.json") | ||
if direct_url_str is not None: | ||
direct_url_json = json.loads(direct_url_str) | ||
if "dir_info" in direct_url_json: | ||
if "editable" in direct_url_json["dir_info"]: | ||
return direct_url_json["dir_info"]["editable"] | ||
|
||
return False | ||
|
||
|
||
def package_is_editable(package_name: str) -> bool: | ||
""" | ||
Return True if given Package is an editable installation. | ||
""" | ||
return dist_is_editable(Distribution.from_name(package_name)) | ||
|
||
|
||
def get_package_location(package_name: str) -> Path: | ||
dist = Distribution.from_name(package_name) | ||
if dist: | ||
return get_dist_package_location(dist) | ||
|
||
|
||
def get_dist_package_location(dist: Distribution) -> Path: | ||
""" | ||
FULL OF ASSUMPTIONS! | ||
:param dist: | ||
:return: | ||
""" | ||
|
||
top_level_name = dist.read_text("top_level.txt").split("\n")[0].strip() | ||
|
||
if dist._read_files_egginfo() is not None: | ||
if top_level_name == dist._path.parent.stem: | ||
return dist._path.parent | ||
|
||
if dist._read_files_distinfo() is not None: | ||
direct_url_str = dist.read_text("direct_url.json") | ||
if direct_url_str is not None: | ||
direct_url_json = json.loads(direct_url_str) | ||
if "dir_info" in direct_url_json: | ||
if "editable" in direct_url_json["dir_info"]: | ||
return Path(direct_url_json["url"]) | ||
|
||
if top_level_name: | ||
package_location = dist._path.parent / top_level_name | ||
if package_location.exists() and package_location.is_dir(): | ||
return package_location | ||
|
||
return None | ||
|
||
|
||
if __name__ == "__main__": | ||
print(package_is_editable(package_name="draugr")) | ||
print(get_package_location(package_name="draugr")) | ||
|
||
print(package_is_editable(package_name="warg")) | ||
print(get_package_location(package_name="warg")) | ||
|
||
print(package_is_editable(package_name="apppath")) | ||
print(get_package_location(package_name="apppath")) | ||
|
||
print(get_package_location(package_name="numpy")) | ||
|
||
print(package_is_editable(package_name="Pillow")) | ||
print(get_package_location(package_name="Pillow")) | ||
print(get_package_location(package_name="pillow")) |
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,38 @@ | ||
from pathlib import Path | ||
from typing import List, Union | ||
|
||
from packaging.requirements import Requirement | ||
from pip._internal.network.session import PipSession | ||
from pip._internal.req import parse_requirements | ||
from pip._internal.req.req_file import ParsedRequirement | ||
from pip._internal.utils.packaging import get_requirement | ||
|
||
|
||
from urllib.parse import urlparse | ||
|
||
__all__ = ["get_requirements_from_file"] | ||
|
||
|
||
def get_reqed(req: ParsedRequirement) -> Requirement: | ||
req_ = req.requirement | ||
if req.is_editable: # parse out egg=... fragment from VCS URL | ||
parsed = urlparse(req_) | ||
egg_name = parsed.fragment.partition("egg=")[-1] | ||
without_fragment = parsed._replace(fragment="").geturl() | ||
req_parsed = f"{egg_name} @ {without_fragment}" | ||
else: | ||
req_parsed = req_ | ||
return get_requirement(req_parsed) | ||
|
||
|
||
def get_requirements_from_file( | ||
file_path: Union[str, Path], session: Union[str, PipSession] = "test" | ||
) -> List[Requirement]: | ||
"""Turn requirements.txt into a list""" | ||
if isinstance(file_path, Path): | ||
file_path = str(file_path) | ||
return [get_reqed(ir) for ir in parse_requirements(file_path, session=session)] | ||
|
||
|
||
if __name__ == "__main__": | ||
print(get_requirements_from_file(Path(__file__).parent.parent.parent / "requirements.txt")) |
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.