Skip to content

Commit

Permalink
Add support for exporting poetry.lock to requirements.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
carlcsaposs-canonical committed Sep 23, 2024
1 parent 28cbdec commit 4322c95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
38 changes: 30 additions & 8 deletions charmcraftcache/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,36 @@ def pack(context: typer.Context, verbose: Verbose = False):
f'Passing unrecognized arguments to `charmcraft pack`: {" ".join(context.args)}'
)
logger.info("Resolving dependencies")
if not pathlib.Path("requirements.txt").exists():
if not pathlib.Path("charmcraft.yaml").exists():
raise FileNotFoundError(
"requirements.txt not found. `cd` into the directory with charmcraft.yaml"
charmcraft_yaml = pathlib.Path("charmcraft.yaml")
poetry_lock_exists = pathlib.Path("poetry.lock").exists()
if not charmcraft_yaml.exists():
raise FileNotFoundError(
"charmcraft.yaml not found. `cd` into the directory with charmcraft.yaml"
)
if poetry_lock_exists:
# Convert subset of poetry.lock to requirements.txt
try:
subprocess.run(
[
"poetry",
"export",
"--only",
"main,charm-libs",
"--output",
"requirements.txt",
],
check=True,
)
else:
raise FileNotFoundError(
"requirements.txt not found. Are you using a pack wrapper (e.g. `tox run -e build-dev`)? If so, call charmcraftcache via the wrapper."
except FileNotFoundError:
raise Exception(
"poetry.lock detected but poetry not installed. Install poetry"
)
# TODO: add handling if poetry installed but poetry-plugin-export not installed
# (only applicable once https://github.com/python-poetry/poetry/pull/5980 merged)
except subprocess.CalledProcessError:
raise Exception("Failed to create requirements.txt from poetry.lock")
elif not pathlib.Path("requirements.txt").exists():
raise FileNotFoundError("requirements.txt not found")
report_file = cache_directory / "report.json"
subprocess.run(
[
Expand All @@ -239,10 +260,11 @@ def pack(context: typer.Context, verbose: Verbose = False):
stdout=None if state.verbose else subprocess.DEVNULL,
check=True,
)
if poetry_lock_exists:
pathlib.Path("requirements.txt").unlink()
with open(report_file, "r") as file:
report = json.load(file)
dependencies = []
charmcraft_yaml = pathlib.Path("charmcraft.yaml")
architecture = platform.machine()
bases = get_charmcraft_yaml_bases(
charmcraft_yaml=charmcraft_yaml, architecture=architecture
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "charmcraftcache"
version = "0.3.8"
version = "0.4.0"
description = "Fast first-time builds for charmcraft"
authors = ["Carl Csaposs <carl.csaposs@canonical.com>"]
readme = "README.md"
Expand Down

0 comments on commit 4322c95

Please sign in to comment.