diff --git a/charmcraftcache/main.py b/charmcraftcache/main.py index 7e1f4d9..e38d2cc 100644 --- a/charmcraftcache/main.py +++ b/charmcraftcache/main.py @@ -213,15 +213,38 @@ 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") + else: + logger.debug("Converted subset of poetry.lock to requirements.txt") + elif not pathlib.Path("requirements.txt").exists(): + raise FileNotFoundError("requirements.txt not found") report_file = cache_directory / "report.json" subprocess.run( [ @@ -239,10 +262,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 diff --git a/pyproject.toml b/pyproject.toml index 801c848..d251a50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] readme = "README.md"