Skip to content

Commit

Permalink
Build packages
Browse files Browse the repository at this point in the history
  • Loading branch information
carlcsaposs-canonical committed Nov 3, 2023
1 parent fddf6ad commit 349316c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 14 deletions.
3 changes: 0 additions & 3 deletions charm_lock_files.json

This file was deleted.

7 changes: 7 additions & 0 deletions charms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"github_repository": "canonical/mysql-router-k8s-operator",
"ref": "main",
"relative_path_to_charmcraft_yaml": "."
}
]
112 changes: 101 additions & 11 deletions cli/cli/build.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,109 @@
import argparse
import dataclasses
import json
import tomllib
import os
import pathlib
import subprocess

import requests

@dataclasses.dataclass(frozen=True, kw_only=True)
class Charm:
github_repository: str
ref: str
relative_path_to_charmcraft_yaml: str

@property
def _repository_directory(self) -> pathlib.Path:
return pathlib.Path("repos", self.github_repository)

def checkout_repository(self):
try:
self._repository_directory.mkdir(parents=True)
except FileExistsError:
commands = [
f"git fetch origin {self.ref}",
"git checkout FETCH_HEAD",
]
else:
commands = [
"git init",
"git sparse-checkout set --sparse-index .github/workflows/",
f"git remote add --fetch origin https://github.com/{self.github_repository}.git",
f"git fetch origin {self.ref}",
"git checkout FETCH_HEAD",
]
for command in commands:
subprocess.run(
command.split(" "), cwd=self._repository_directory, check=True
)

@property
def directory(self) -> pathlib.Path:
return self._repository_directory / self.relative_path_to_charmcraft_yaml


@dataclasses.dataclass(frozen=True, kw_only=True)
class Dependency:
name: str
version: str


def main():
pip_cache = pathlib.Path("~/charmcraftcache-hub-build/").expanduser()
pip_cache.mkdir()
parser = argparse.ArgumentParser()
parser.add_argument("charm_locks_json")
parser.add_argument("charms_file")
args = parser.parse_args()
with open(args.charm_locks_json, "r") as file:
charm_lock_files = json.load(file)
for url in charm_lock_files:
response = requests.get(url)
response.raise_for_status()
poetry_lock_file = response.text
foo = tomllib.loads(poetry_lock_file)
print(foo)
with open(args.charms_file, "r") as file:
charms = [Charm(**charm) for charm in json.load(file)]
dependencies: dict[Charm, set[Dependency]] = {}
for charm in charms:
charm.checkout_repository()
assert (charm.directory / "poetry.lock").exists()
subprocess.run(
[
"poetry",
"export",
# Ignore other dependency groups (e.g. unit test, lint, etc.)
"--only",
"main",
"--output",
"requirements.txt",
],
cwd=charm.directory,
check=True,
)
# Build wheels from source
env = os.environ
env["XDG_CACHE_HOME"] = str(pip_cache)
subprocess.run(
[
"pip",
"install",
"-r",
"requirements.txt",
# Build from source
"--no-binary",
":all:",
# Cache will still be hit if exact version of wheel available
# Needed:
# - to ignore non-exact versions
# - to include all dependencies in report
"--ignore-installed",
"--report",
"report.json",
],
cwd=charm.directory,
check=True,
env=env,
)
with open(charm.directory / "report.json", "r") as file:
report = json.load(file)
dependencies[charm] = {
Dependency(
name=dependency["metadata"]["name"],
version=dependency["metadata"]["version"],
)
for dependency in report["install"]
}
print(dependencies)

0 comments on commit 349316c

Please sign in to comment.