Skip to content

Commit

Permalink
Merge pull request #47 from nikromen/few-fixes
Browse files Browse the repository at this point in the history
Sync code with thesis expectations
  • Loading branch information
nikromen authored May 11, 2023
2 parents 4f0a2ea + 04f2b02 commit f157617
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 110 deletions.
3 changes: 2 additions & 1 deletion alpa.spec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: alpa
Version: 0.8.0
Version: 0.9.0
Release: 1%{?dist}
Summary: Integration tool with Alpa repository

Expand All @@ -16,6 +16,7 @@ BuildRequires: python3-pygithub
BuildRequires: python3-specfile

Requires: mock
Requires: pre-commit


%description
Expand Down
2 changes: 0 additions & 2 deletions alpa/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
pull,
push,
list_,
genspec,
add,
get_pkg_archive,
mockbuild,
Expand Down Expand Up @@ -45,7 +44,6 @@ def clone(url: str, fork: bool) -> None:
entry_point.add_command(pull)
entry_point.add_command(push)
entry_point.add_command(list_)
entry_point.add_command(genspec)
entry_point.add_command(add)
entry_point.add_command(get_pkg_archive)
entry_point.add_command(mockbuild)
Expand Down
92 changes: 59 additions & 33 deletions alpa/cli/local_repo.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
"""
These commands need to create LocalRepo -> no GH token required
"""
import os
import subprocess
from os import getcwd
from pathlib import Path
from shutil import which

import click
from click import ClickException, Choice
from click import ClickException

from alpa.config import MetadataConfig
from alpa.repository.branch import LocalRepoBranch, AlpaRepoBranch

from alpa.messages import NO_PRE_COMMIT

pkg_name = click.argument("name", type=str)


Expand Down Expand Up @@ -63,6 +68,21 @@ def add(to_add: str) -> None:
LocalRepoBranch(Path(getcwd())).add(to_add)


def _skip_pre_commit_checks_for_non_rpm_os() -> None:
process = subprocess.run(
["rpm", "--help"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
if process.returncode != 0:
# no special pre-commit hooks for non-RPM OS :/
disabled_checks = ["source0-uses-version-macro", "check-packit-file"]
click.secho(
"Warning! You don't have RPM based OS, these checks are "
f"disabled: {disabled_checks}",
fg="yellow",
)
os.environ["SKIP"] = ",".join(disabled_checks)


@click.command("push")
@click.option(
"-p",
Expand All @@ -72,14 +92,25 @@ def add(to_add: str) -> None:
default=False,
help="This will create pull request on GitHub for you.",
)
def push(pull_request: bool) -> None:
@click.option("-n", "--no-verify", is_flag=True, help="Do not run pre-commit")
def push(pull_request: bool, no_verify: bool) -> None:
"""Pushes your commited changes to the Alpa repo so you can make PR"""
if not no_verify:
if which("pre-commit") is None:
click.secho(NO_PRE_COMMIT, fg="red", err=True)
return

_skip_pre_commit_checks_for_non_rpm_os()
ret = subprocess.run(["pre-commit", "run", "--all-files"])
if ret.returncode != 0:
# pre-commit already gives info about fail
return

repo_path = Path(getcwd())
local_repo = LocalRepoBranch(repo_path)
local_repo.push(local_repo.branch)

if not pull_request:
local_repo.git_cmd(["branch", "-d", local_repo.feat_branch])
return

alpa = AlpaRepoBranch(repo_path)
Expand Down Expand Up @@ -110,25 +141,26 @@ def list_(pattern: str) -> None:
click.echo(pkg)


@click.command("genspec")
@click.option(
"--lang",
type=Choice(["python", "java"], case_sensitive=False),
required=True,
help="Choose the programming language for which the generator is designed",
)
@click.option(
"-t",
"--test",
default=False,
help=(
"Send package with generated spec file to "
"packit to test whether build will succeed."
),
)
def genspec(lang: str, test: bool) -> None:
"""This command uses some existing spec file generators for you"""
raise NotImplementedError("Not implemented yet (1.0 goal)")
# TODO: please implement me
# @click.command("genspec")
# @click.option(
# "--lang",
# type=Choice(["python", "java"], case_sensitive=False),
# required=True,
# help="Choose the programming language for which the generator is designed",
# )
# @click.option(
# "-t",
# "--test",
# default=False,
# help=(
# "Send package with generated spec file to "
# "packit to test whether build will succeed."
# ),
# )
# def genspec(lang: str, test: bool) -> None:
# """This command uses some existing spec file generators for you"""
# raise NotImplementedError("Not implemented yet (1.0 goal)")


@click.command("create-packit-config")
Expand All @@ -147,14 +179,6 @@ def create_packit_config(override: bool) -> None:
)


def _get_chroots_to_build(meta: MetadataConfig, distros: list[str]) -> list[str]:
chroots = []
for arch in meta.arch:
for distro in distros:
chroots.append(f"{distro}-{arch}")
return chroots


@click.command("mockbuild")
@click.option(
"--chroot",
Expand All @@ -171,9 +195,11 @@ def mockbuild(chroot: str) -> None:
"""
from alpa.upstream_integration import UpstreamIntegration

meta = MetadataConfig.get_config()
distros = [chroot] if chroot else list(meta.targets)
chroots = _get_chroots_to_build(meta, distros)
if chroot:
chroots = [chroot]
else:
chroots = MetadataConfig.get_config().chroots

UpstreamIntegration(Path(getcwd())).mockbuild(chroots)


Expand Down
3 changes: 1 addition & 2 deletions alpa/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from alpa.config.alpa_repo import AlpaRepoConfig
from alpa.config.metadata import MetadataConfig
from alpa.config.packit import PackitConfig


__all__ = [AlpaRepoConfig.__name__, MetadataConfig.__name__, PackitConfig.__name__]
__all__ = [AlpaRepoConfig.__name__, MetadataConfig.__name__]
20 changes: 19 additions & 1 deletion alpa/config/alpa_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,38 @@ def __init__(
copr_owner: str,
copr_repo: str,
allow_foreign_contributing: bool = False,
targets: Optional[set[str]] = None,
arch: Optional[set[str]] = None,
) -> None:
self.repo_type = repo_type
self.copr_owner = copr_owner
self.copr_repo = copr_repo

# optional parameters
self.allow_foreign_contributing = allow_foreign_contributing
self.targets = targets
self.arch = arch

@classmethod
def _config_from_dict(cls, d: dict) -> "AlpaRepoConfig":
for mandatory_key in ["repo_type", "copr_owner", "copr_repo"]:
cls._check_for_mandatory_key(d, mandatory_key, "alpa.yaml")

targets = d.get("targets")
if targets is not None:
targets = set(targets)

arch = d.get("arch")
if arch is not None:
arch = set(arch)

return AlpaRepoConfig(
repo_type=AlpaRepoType[d["repo_type"]],
copr_owner=d["copr_owner"],
copr_repo=d["copr_repo"],
allow_foreign_contributing=d.get("allow_foreign_contributing", False),
targets=targets,
arch=arch,
)

@classmethod
Expand All @@ -64,7 +80,9 @@ def get_config(cls) -> "AlpaRepoConfig":
if process.returncode != 0:
raise AlpaConfException(process.stderr)

alpa_config = cls._load_alpa_config(Path(process.stdout.decode().strip()))
alpa_config = cls._load_alpa_config(
Path("/home/jkyjovsk/Documents/git/github/alpa/test-branch-repo")
)
if alpa_config is None:
raise FileNotFoundError("No alpa config file in git root found")

Expand Down
25 changes: 22 additions & 3 deletions alpa/config/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from alpa.constants import METADATA_FILE_NAMES
from alpa.config.base import Config

from alpa.config.alpa_repo import AlpaRepoConfig


@dataclass
class User:
Expand Down Expand Up @@ -61,13 +63,22 @@ def _fill_metadata_from_dict(cls, d: dict) -> "MetadataConfig":
)
users_list.append(User(**maintainer))

targets = cls._check_for_mandatory_key(d, "targets", "metadata.yaml")
repo_config = AlpaRepoConfig.get_config()
if repo_config.targets is None:
targets = set(cls._check_for_mandatory_key(d, "targets", "metadata.yaml"))
else:
targets = set(d.get("targets", set())) | repo_config.targets

if repo_config.arch is None:
arch = set(d.get("arch", ["x86_64"]))
else:
arch = set(d.get("arch", [])) | repo_config.arch

return MetadataConfig(
autoupdate=autoupdate_dataclass,
maintainers=users_list,
targets=set(targets),
arch=set(d.get("arch", ["x86_64"])),
targets=targets,
arch=arch,
)

@classmethod
Expand All @@ -89,3 +100,11 @@ def get_config(cls, working_dir: Optional[Path] = None) -> "MetadataConfig":
raise FileNotFoundError("No metadata file found in package")

return metadata_data_class

@property
def chroots(self) -> list[str]:
chroots = []
for arch in self.arch:
for distro in self.targets:
chroots.append(f"{distro}-{arch}")
return chroots
5 changes: 3 additions & 2 deletions alpa/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ def clone_url(self) -> str:
if self.has_write_access(self._api.get_user().login):
return self._repo.ssh_url

click.echo(
click.secho(
RETURNING_CLONE_URL_MSG.format(
user=self._api.get_user().login, repo=self._repo.full_name
)
),
fg="bright_yellow",
)
return self._repo.clone_url

Expand Down
19 changes: 17 additions & 2 deletions alpa/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ class GitCmdResult:
stderr: str
retval: int

@property
def stderr_and_stdout(self) -> str:
result = ""
if self.stdout:
result += self.stdout

if not self.stderr:
return result

if result:
result += "\n"

result += self.stderr
return result


class GitCMD:
def __init__(self, cwd: str) -> None:
Expand All @@ -40,8 +55,8 @@ def git_cmd(
)
process = subprocess.run(
["git"] + arguments,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=context,
)
stdout = process.stdout.decode()
Expand Down
4 changes: 4 additions & 0 deletions alpa/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
"User {user} does not have access to {repo} repository. "
"Using https url for cloning instead of ssh url."
)

NO_PRE_COMMIT = (
"pre-commit is not installed! Please install " "it via `pip install pre-commit`"
)
Loading

0 comments on commit f157617

Please sign in to comment.