Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of the srpm prepare #646

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 6 additions & 61 deletions packit/api.py
Expand Up @@ -127,7 +127,7 @@ def sync_pr(self, pr_id, dist_git_branch: str, upstream_version: str = None):

description = (
f"Upstream pr: {pr_id}\n"
f"Upstream commit: {self.up.local_project.git_repo.head.commit}\n"
f"Upstream commit: {self.up.local_project.commit_hexsha}\n"
)

self._handle_sources(add_new_sources=True, force_new_sources=False)
Expand Down Expand Up @@ -215,7 +215,7 @@ def sync_release(

description = (
f"Upstream tag: {upstream_tag}\n"
f"Upstream commit: {self.up.local_project.git_repo.head.commit}\n"
f"Upstream commit: {self.up.local_project.commit_hexsha}\n"
)

path = os.path.join(self.dg.local_project.working_dir, "README.packit")
Expand Down Expand Up @@ -332,9 +332,7 @@ def sync_from_downstream(
sync_files(reverse_raw_sync_files, fail_on_missing=False)

if not no_pr:
description = (
f"Downstream commit: {self.dg.local_project.git_repo.head.commit}\n"
)
description = f"Downstream commit: {self.dg.local_project.commit_hexsha}\n"

commit_msg = f"sync from downstream branch {dist_git_branch!r}"
pr_title = f"Update from downstream branch {dist_git_branch!r}"
Expand Down Expand Up @@ -454,62 +452,9 @@ def create_srpm(
"""
self.up.run_action(actions=ActionName.post_upstream_clone)

current_git_describe_version = self.up.get_current_version()
upstream_ref = upstream_ref or self.package_config.upstream_ref

if self.up.local_project.git_repo.head.is_detached:
commit = self.up.local_project.git_repo.head.commit.hexsha[:8]
else:
commit = self.up.local_project.git_repo.active_branch.commit.hexsha[:8]

if self.up.running_in_service():
relative_to = Path(self.config.command_handler_work_dir)
else:
relative_to = Path.cwd()

if upstream_ref:
# source-git code: fetch the tarball and don't check out the upstream ref
self.up.fetch_upstream_archive()
source_dir = self.up.absolute_specfile_dir.relative_to(relative_to)
if self.up.with_action(action=ActionName.create_patches):
patches = self.up.create_patches(
upstream=upstream_ref,
destination=str(self.up.absolute_specfile_dir),
)
self.up.add_patches_to_specfile(patches)

old_release = self.up.specfile.get_release_number()
try:
old_release_int = int(old_release)
new_release = old_release_int + 1
except ValueError:
new_release = old_release
release_to_update = f"{new_release}.g{commit}"
msg = f"Downstream changes ({commit})"
self.up.specfile.set_spec_version(
release=release_to_update, changelog_entry=f"- {msg}"
)
else:
archive = self.up.create_archive(version=current_git_describe_version)
env = {
"PACKIT_PROJECT_VERSION": current_git_describe_version,
"PACKIT_PROJECT_COMMIT": commit,
"PACKIT_PROJECT_ARCHIVE": archive,
}
if self.up.with_action(action=ActionName.fix_spec, env=env):
self.up.fix_spec(
archive=archive, version=current_git_describe_version, commit=commit
)
if self.up.local_project.working_dir.startswith(str(relative_to)):
source_dir = Path(self.up.local_project.working_dir).relative_to(
relative_to
)
else:
source_dir = Path(self.up.local_project.working_dir)

# > Method that iterates over all sources and downloads ones,
# > which contain URL instead of just a file.
self.up.specfile.download_remote_sources()
source_dir = self.up.prepare_upstream_for_srpm_creation(
upstream_ref=upstream_ref
)

srpm_path = self.up.create_srpm(
srpm_path=output_file, srpm_dir=srpm_dir, source_dir=source_dir
Expand Down
12 changes: 12 additions & 0 deletions packit/local_project.py
Expand Up @@ -135,6 +135,18 @@ def ref(self) -> Optional[str]:
return self._get_ref_from_git_repo()
return None

@property
def commit_hexsha(self) -> str:
"""
Get the short commit hash for the current commit.

:return: first 8 characters of the current commit
"""
if self.git_repo.head.is_detached:
return self.git_repo.head.commit.hexsha[:8]
else:
return self.git_repo.active_branch.commit.hexsha[:8]

def clean(self):
if self.working_dir_temporary:
logger.debug(f"Cleaning: {self.working_dir}")
Expand Down
96 changes: 96 additions & 0 deletions packit/upstream.py
Expand Up @@ -563,3 +563,99 @@ def create_srpm(
if self.running_in_service():
return Path(self.local_project.working_dir).joinpath(the_srpm)
return Path(the_srpm)

def prepare_upstream_for_srpm_creation(self, upstream_ref: str = None) -> Path:
"""
1. determine version
2. create an archive or download upstream and create patches for sourcegit
3. fix/update the specfile to use the right archive
4. download the remote sources

:param upstream_ref: str, needed for the sourcegit mode
:return: source directory where you can start the SRPM build
"""
current_git_describe_version = self.get_current_version()
upstream_ref = upstream_ref or self.package_config.upstream_ref

if self.running_in_service():
relative_to = Path(self.config.command_handler_work_dir)
else:
relative_to = Path.cwd()

if upstream_ref:
source_dir = self.prepare_upstream_using_source_git(
relative_to, upstream_ref
)
else:
created_archive = self.create_archive(version=current_git_describe_version)
self.fix_specfile_to_use_local_archive(
archive=created_archive, archive_version=current_git_describe_version
)
if self.local_project.working_dir.startswith(str(relative_to)):
source_dir = Path(self.local_project.working_dir).relative_to(
relative_to
)
else:
source_dir = Path(self.local_project.working_dir)

# > Method that iterates over all sources and downloads ones,
# > which contain URL instead of just a file.
self.specfile.download_remote_sources()

return source_dir

def fix_specfile_to_use_local_archive(self, archive, archive_version) -> None:
"""
Update specfile to use the archive with the right version.

:param archive: path to the archive
:param archive_version: package version of the archive
"""
current_commit = self.local_project.commit_hexsha
env = {
"PACKIT_PROJECT_VERSION": archive_version,
"PACKIT_PROJECT_COMMIT": current_commit,
"PACKIT_PROJECT_ARCHIVE": archive,
}
if self.with_action(action=ActionName.fix_spec, env=env):
self.fix_spec(
archive=archive, version=archive_version, commit=current_commit
)

def prepare_upstream_using_source_git(self, relative_to, upstream_ref) -> Path:
"""
Fetch the tarball and don't check out the upstream ref.

:param relative_to: for the purpose of Sandcastle
:param upstream_ref: the base git ref for the source git
:return: the source directory where we can build the SRPM
"""
self.fetch_upstream_archive()
source_dir = self.absolute_specfile_dir.relative_to(relative_to)
self.create_patches_and_update_specfile(upstream_ref)
old_release = self.specfile.get_release_number()
try:
old_release_int = int(old_release)
new_release = str(old_release_int + 1)
except ValueError:
new_release = str(old_release)

current_commit = self.local_project.commit_hexsha
release_to_update = f"{new_release}.g{current_commit}"
msg = f"Downstream changes ({current_commit})"
self.specfile.set_spec_version(
release=release_to_update, changelog_entry=f"- {msg}"
)
return source_dir

def create_patches_and_update_specfile(self, upstream_ref) -> None:
"""
Create patches for the sourcegit and add them to the specfile.

:param upstream_ref: the base git ref for the source git
"""
if self.with_action(action=ActionName.create_patches):
patches = self.create_patches(
upstream=upstream_ref, destination=str(self.absolute_specfile_dir)
)
self.add_patches_to_specfile(patches)