-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python: script to build python package
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2023-2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import re | ||
import subprocess | ||
import sys | ||
import traceback | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
def main(): | ||
repo_root_dir = Path(__file__).parent.parent.parent | ||
python_root_dir = repo_root_dir / "python" | ||
dist_output_dir = python_root_dir / "dist" | ||
rust_root_dir = repo_root_dir / "rust" | ||
rust_cli_readme_path = rust_root_dir / "cli" / "README.md" | ||
rust_cli_cargo_path = rust_root_dir / "cli" / "Cargo.toml" | ||
|
||
r = subprocess.run( | ||
["git", "status", "--porcelain"], capture_output=True, cwd=repo_root_dir | ||
) | ||
if len(r.stdout) > 0 or len(r.stderr) > 0: | ||
print("ERROR: the git repository is in a dirty state.") | ||
# TODO: add this back once we are done developing this script | ||
# sys.exit(1) | ||
|
||
# delete readme as it causes issues with sdist; we'll restore it later | ||
rust_cli_readme_path.unlink() | ||
|
||
try: | ||
with_exception = False | ||
|
||
# patch the version in Cargo.toml with magika.__version__ | ||
version = get_python_module_version(python_root_dir) | ||
patch_cargo_toml_with_version(rust_cli_cargo_path, version) | ||
|
||
# FIXME: this currently fails due to patched Cargo.toml | ||
r = subprocess.run( | ||
["uv", "build", "--out-dir", str(dist_output_dir)], | ||
cwd=python_root_dir, | ||
) | ||
except Exception: | ||
with_exception = True | ||
print(f"There was an exception: {traceback.format_exc()}") | ||
finally: | ||
# restore what we modified, no matter what happened | ||
r = subprocess.run( | ||
[ | ||
"git", | ||
"restore", | ||
"--", | ||
str(rust_cli_readme_path), | ||
str(rust_cli_cargo_path), | ||
], | ||
capture_output=True, | ||
cwd=repo_root_dir, | ||
) | ||
|
||
if with_exception: | ||
sys.exit(1) | ||
else: | ||
sys.exit(0) | ||
|
||
|
||
def get_python_module_version(python_root_dir: Path) -> str: | ||
init_path = python_root_dir / "src" / "magika" / "__init__.py" | ||
lines = init_path.read_text().split("\n") | ||
for line in lines: | ||
m = re.fullmatch('__version__ = "([A-Za-z0-9.-]+)"', line) | ||
if m: | ||
return m.group(1) | ||
raise Exception("python module version not found") | ||
|
||
|
||
def patch_cargo_toml_with_version(cargo_path: Path, version: str) -> None: | ||
lines = cargo_path.read_text().split("\n") | ||
version_already_found = False | ||
for line_idx in range(len(lines)): | ||
line = lines[line_idx] | ||
if line.startswith("version = "): | ||
if version_already_found: | ||
print('ERROR: Found more than one "version" lines in Cargo.toml') | ||
sys.exit(1) | ||
line = f'version = "{version}"' | ||
lines[line_idx] = line | ||
|
||
cargo_path.write_text("\n".join(lines)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |