Skip to content

Commit

Permalink
Pass distro to create methods. Do not instantiate dataclassed directl…
Browse files Browse the repository at this point in the history
…y but call their proper create method. refs: #97
  • Loading branch information
spanezz committed May 19, 2023
1 parent 700d32f commit 784b303
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion moncic/cli/moncic.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def source(self, distro: Distro, source: str) -> Generator[Source, None, None]:
input_source = input_source.branch(self.args.branch)
if self.args.source_type:
source_cls = get_source_class(self.args.source_type)
source = source_cls.create(input_source)
source = source_cls.create(distro, input_source)
else:
source = input_source.detect_source(distro)
self.moncic.privs.regain()
Expand Down
54 changes: 36 additions & 18 deletions moncic/source/debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ def detect(cls, distro: Distro, source: LocalGit) -> "DebianGitSource":
return DebianGBPTestDebian._create_from_repo(distro, source)

@classmethod
def create(cls, source: InputSource) -> "DebianGitSource":
def create(cls, distro: Distro, source: InputSource) -> "DebianGitSource":
if isinstance(source, LocalGit):
return cls(source.source, source.repo.working_dir)
return cls._create_from_repo(distro, source)
elif isinstance(source, URL):
return cls.create(source.clone())
return cls._create_from_repo(distro, source.clone())
else:
raise RuntimeError(
f"cannot create {cls.__name__} instances from an input source of type {source.__class__.__name__}")
Expand Down Expand Up @@ -139,7 +139,9 @@ def _create_from_repo(cls, distro: Distro, source: LocalGit) -> "DebianPlainGit"
source.repo.working_dir)
source = source.clone()

return cls(source, source.repo.working_dir)
res = cls(source, source.repo.working_dir)
res.add_trace_log("git", "clone", "-b", source.repo.active_branch.name, source.source)
return res

@host_only
def gather_sources_from_host(self, build: Build, container: Container) -> None:
Expand Down Expand Up @@ -299,18 +301,30 @@ def _create_from_repo(cls, distro: Distro, source: LocalGit) -> "DebianGBPTestUp
log.info("%s: cloning repository to avoid mangling the original version", source.repo.working_dir)
source = source.clone()

res = cls(source, source.repo.working_dir)

# Make a temporary merge of active_branch on the debian branch
log.info("merge packaging branch %s for test build", branch)
active_branch = source.repo.active_branch.name
if active_branch is None:
log.info("repository is in detached head state, creating a 'moncic-ci' working branch from it")
run(["git", "checkout", "-b", "moncic-ci"], cwd=source.repo.working_dir)
res.add_trace_log("git", "clone", source.source)
cmd = ["git", "checkout", source.repo.head.commit.hexsha, "-b", "moncic-ci"]
res.add_trace_log(*cmd)
run(cmd, cwd=source.repo.working_dir)
active_branch = "moncic-ci"
run(["git", "checkout", "--quiet", branch], cwd=source.repo.working_dir)
run(["git", "-c", "[email protected]", "-c",
"user.name=Moncic-CI", "merge", active_branch, "--quiet", "-m", "CI merge"], cwd=source.repo.working_dir)
else:
res.add_trace_log("git", "clone", "-b", active_branch, source.source)

cmd = ["git", "checkout", "--quiet", branch]
res.add_trace_log(*cmd)
run(cmd, cwd=source.repo.working_dir)

cmd = ["git", "-c", "[email protected]", "-c",
"user.name=Moncic-CI", "merge", str(active_branch), "--quiet", "-m", "CI merge"]
res.add_trace_log(*cmd)
run(cmd, cwd=source.repo.working_dir)

res = cls(source, source.repo.working_dir)
res.gbp_args.append("--git-upstream-tree=branch")
res.gbp_args.append("--git-upstream-branch=" + active_branch)
return res
Expand Down Expand Up @@ -372,12 +386,16 @@ def _create_from_repo(cls, distro: Distro, source: LocalGit) -> "DebianGBPTestDe

cls.ensure_local_branch_exists(source.repo, upstream_branch)

res = cls(source, source.repo.working_dir)
res.add_trace_log("git", "clone", "-b", cls.repo.active_branch, source.source)

# Merge the upstream branch into the debian branch
log.info("merge upstream branch %s into build branch", upstream_branch)
run(["git", "-c", "[email protected]", "-c",
"user.name=Moncic-CI", "merge", upstream_branch, "--quiet", "-m", "CI merge"], cwd=source.repo.working_dir)
cmd = ["git", "-c", "[email protected]", "-c", "user.name=Moncic-CI",
"merge", upstream_branch, "--quiet", "-m", "CI merge"]
res.add_trace_log(*cmd)
run(cmd, cwd=source.repo.working_dir)

res = cls(source, source.repo.working_dir)
res.gbp_args.append("--git-upstream-tree=branch")
return res

Expand All @@ -391,15 +409,15 @@ class DebianSourceDir(DebianDirMixin, DebianSource):
NAME = "debian-dir"

@classmethod
def create(cls, source: InputSource) -> "DebianSourceDir":
def create(cls, distro: Distro, source: InputSource) -> "DebianSourceDir":
if isinstance(source, LocalDir):
return cls(source, source.path)
return cls._create_from_dir(distro, source)
else:
raise RuntimeError(
f"cannot create {cls.__name__} instances from an input source of type {source.__class__.__name__}")

@classmethod
def _create_from_dir(cls, source: LocalDir) -> "DebianSourceDir":
def _create_from_dir(cls, distro: Distro, source: LocalDir) -> "DebianSourceDir":
return cls(source, source.path)

@host_only
Expand Down Expand Up @@ -439,15 +457,15 @@ class DebianDsc(DebianSource):
NAME = "debian-dsc"

@classmethod
def create(cls, source: InputSource) -> "DebianDsc":
def create(cls, distro: Distro, source: InputSource) -> "DebianDsc":
if isinstance(source, LocalFile):
return cls(source, source.path)
return cls._create_from_file(distro, source)
else:
raise RuntimeError(
f"cannot create {cls.__name__} instances from an input source of type {source.__class__.__name__}")

@classmethod
def _create_from_file(cls, source: LocalFile) -> "DebianDsc":
def _create_from_file(cls, distro: Distro, source: LocalFile) -> "DebianDsc":
return cls(source, source.path)

@host_only
Expand Down
4 changes: 2 additions & 2 deletions moncic/source/inputsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def detect_source(self, distro: Distro) -> "Source":
if isinstance(distro, DebianDistro):
if self.source.endswith(".dsc"):
from .debian import DebianDsc
return DebianDsc._create_from_file(self)
return DebianDsc._create_from_file(distro, self)
else:
raise Fail(f"{self.source!r}: cannot detect source type")
else:
Expand All @@ -126,7 +126,7 @@ def detect_source(self, distro: Distro) -> "Source":

if isinstance(distro, DebianDistro):
if os.path.isdir(os.path.join(self.path, "debian")):
return DebianSourceDir._create_from_dir(self)
return DebianSourceDir._create_from_dir(distro, self)
else:
raise Fail(f"{self.source!r}: cannot detect source type")
else:
Expand Down
2 changes: 1 addition & 1 deletion moncic/source/rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _create_from_repo(cls, source: Union[LocalGit, LocalDir]) -> "ARPASource":
return cls(source, source.path)

@classmethod
def create(cls, source: InputSource) -> "ARPASource":
def create(cls, distro: Distro, source: InputSource) -> "ARPASource":
if isinstance(source, (LocalGit, LocalDir)):
return cls(source, source.path)
elif isinstance(source, URL):
Expand Down

0 comments on commit 784b303

Please sign in to comment.