Skip to content
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

Pip freeze fix #104

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions tools/build_post_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,60 @@ def get_runtime_modules(root):
def get_packages_info(build_root):
"""Read lock file to get packages.

Retruns:
Combine requirements freeze from venv and from poetry export. Requirements
freezed with pip 24 do not contain git urls, instead are pointing to
source folder on a disk. In that case is used poetry export which contains
the git url.

Notes:
This is not ideal solution. The most ideal would be to get all the
information from poetry. But poetry export is limited only to
requirements.txt and using custom export logic would require
to implement it on our own.
The custom logic would also require to run venv located inside poetry
because poetry does not have mechanism to run internal python
via poetry executable. Parsing poetry lock on our own is also not
ideal because it can change based on poetry version.
We might hit an issue that newer poetry export won't be able to export
the urls either.

Returns:
list[tuple[str, Union[str, None]]]: List of tuples containing package
name and version.
"""

requirements_path = build_root / "requirements.txt"
poetry_requirements_path = build_root / "poetry_requirements.txt"
if not requirements_path.exists():
raise RuntimeError(
"Failed to get packages info -> couldn't find 'requirements.txt'."
)

if not poetry_requirements_path.exists():
raise RuntimeError(
"Failed to get packages info"
" -> couldn't find 'poetry_requirements.txt'."
)

with open(str(requirements_path), "r", encoding="utf-8") as stream:
content = stream.read()

with open(str(poetry_requirements_path), "r", encoding="utf-8") as stream:
poetry_content = stream.read()

poetry_packages = {}
for line in poetry_content.split("\n"):
line = line.strip()
if not line:
continue

match = re.match(r"^(.+?)(?:==|>=|<=|~=|!=|@)(.+)$", line)
if not match:
raise ValueError(f"Cannot parse package info '{line}'.")
package_name, version = match.groups()
version = version.split(";")[0].strip()
poetry_packages[package_name.rstrip()] = version

packages = {}
for line in content.split("\n"):
line = line.strip()
Expand All @@ -388,7 +428,12 @@ def get_packages_info(build_root):
if not match:
raise ValueError(f"Cannot parse package info '{line}'.")
package_name, version = match.groups()
packages[package_name.rstrip()] = version.lstrip()
package_name = package_name.rstrip()
version = version.lstrip()

if version.startswith("file:"):
version = poetry_packages[package_name]
packages[package_name] = version.lstrip()

return packages

Expand Down
1 change: 1 addition & 0 deletions tools/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ build_ayon () {
fi
echo -e "${BIGreen}>>>${RST} Building ..."
"$POETRY_HOME/bin/poetry" run python -m pip --no-color freeze > "$repo_root/build/requirements.txt"
"$POETRY_HOME/bin/poetry" export --without-urls --without-hashes -f requirements.txt -n --no-ansi > "$repo_root/build/poetry_requirements.txt"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
"$POETRY_HOME/bin/poetry" run python "$repo_root/setup.py" build &> "$repo_root/build/build.log" || { echo -e "${BIRed}------------------------------------------${RST}"; cat "$repo_root/build/build.log"; echo -e "${BIRed}------------------------------------------${RST}"; echo -e "${BIRed}!!!${RST} Build failed, see the build log."; return 1; }
elif [[ "$OSTYPE" == "darwin"* ]]; then
Expand Down
2 changes: 2 additions & 0 deletions tools/manage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ function Build-Ayon($MakeInstaller = $false) {
$startTime = [int][double]::Parse((Get-Date -UFormat %s))

$FreezeContent = & "$($env:POETRY_HOME)\bin\poetry" run python -m pip --no-color freeze
$FreezeContentPoetry = & "$($env:POETRY_HOME)\bin\poetry" export --without-urls --without-hashes -f requirements.txt -n --no-ansi
# Make sure output is UTF-8 without BOM
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines("$($repo_root)\build\requirements.txt", $FreezeContent, $Utf8NoBomEncoding)
[System.IO.File]::WriteAllLines("$($repo_root)\build\poetry_requirements.txt", $FreezeContentPoetry, $Utf8NoBomEncoding)

$out = & "$($env:POETRY_HOME)\bin\poetry" run python setup.py build 2>&1
Set-Content -Path "$($repo_root)\build\build.log" -Value $out
Expand Down