Skip to content

Commit

Permalink
Merge pull request #494 from mulkieran/issue_ci_493
Browse files Browse the repository at this point in the history
Insert bundled provides in stratisd spec file when creating artifacts for testing
  • Loading branch information
mulkieran authored Nov 9, 2023
2 parents 333fa28 + a74a7e6 commit 1ac8550
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 44 deletions.
47 changes: 47 additions & 0 deletions release_management/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# isort: STDLIB
import os
import subprocess
import tarfile
import tomllib
from datetime import datetime
from getpass import getpass
Expand Down Expand Up @@ -76,6 +77,52 @@ def calc_pre_release_suffix():
return f"{datetime.today():%Y%m%d%H%M}git{commit_hash}"


def get_bundled_provides(vendor_tarfile):
"""
Given absolute path of vendor tarfile generate bundled provides.
"""
with tarfile.open(vendor_tarfile, "r") as tar:
for member in tar.getmembers():
components = member.name.split("/")

if (
len(components) == 3
and components[0] == "vendor"
and components[2] == "Cargo.toml"
):
manifest = tar.extractfile(member)
metadata = tomllib.load(manifest)
directory_name = components[1]
package = metadata["package"]
package_version = package["version"]
package_name = package["name"]
if directory_name != package_name and (
not directory_name.startswith(package_name)
and directory_name[-len(package_version) :] != package_version
):
raise RuntimeError(
"Unexpected disagreement between directory name "
f"{directory_name} and package name in Cargo.toml, "
f"{package_name}"
)
continue

if (
len(components) == 4
and components[0] == "vendor"
and components[2] == "src"
and components[3] == "lib.rs"
):
size = member.size
if size != 0:
if components[1] == directory_name:
yield f"Provides: bundled(crate({package_name})) = {package_version}"
else:
raise RuntimeError(
"Found an entry for bundled provides, but no version information"
)


def edit_specfile(specfile_path, *, release_version=None, sources=None, arbitrary=None):
"""
Edit the specfile in place
Expand Down
13 changes: 13 additions & 0 deletions release_management/create_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
ReleaseVersion,
calc_pre_release_suffix,
edit_specfile,
get_bundled_provides,
get_package_info,
get_python_package_info,
make_source_tarball,
Expand Down Expand Up @@ -142,13 +143,25 @@ def _stratisd_artifacts(namespace):

os.rename(vendor_tarfile_name, vendor_tarfile_path)

def insert_bundled_provides(spec):
"""
Insert bundled provides in the spec file.
"""
with spec.sections() as sections:
preamble = sections.package
preamble.append("%if 0%{?rhel}")
preamble.extend(get_bundled_provides(vendor_tarfile_path))
preamble.append("%endif")
preamble.append("")

edit_specfile(
specfile_path,
release_version=release_version,
sources=[
os.path.basename(path)
for path in [source_tarfile_path, vendor_tarfile_path, crate_path]
],
arbitrary=insert_bundled_provides,
)


Expand Down
49 changes: 5 additions & 44 deletions release_management/gen_bundled_provides.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import argparse
import os
import sys
import tarfile
import tomllib

# isort: LOCAL
from _utils import get_bundled_provides


def main():
Expand All @@ -47,48 +48,8 @@ def main():

namespace = parser.parse_args()

with tarfile.open(namespace.vendor_tarfile, "r") as tar:
for member in tar.getmembers():
components = member.name.split("/")

if (
len(components) == 3
and components[0] == "vendor"
and components[2] == "Cargo.toml"
):
manifest = tar.extractfile(member)
metadata = tomllib.load(manifest)
directory_name = components[1]
package = metadata["package"]
package_version = package["version"]
package_name = package["name"]
if directory_name != package_name and (
not directory_name.startswith(package_name)
and directory_name[-len(package_version) :] != package_version
):
raise RuntimeError(
"Unexpected disagreement between directory name "
f"{directory_name} and package name in Cargo.toml, "
f"{package_name}"
)
continue

if (
len(components) == 4
and components[0] == "vendor"
and components[2] == "src"
and components[3] == "lib.rs"
):
size = member.size
if size != 0:
if components[1] == directory_name:
print(
f"Provides: bundled(crate({package_name})) = {package_version}"
)
else:
raise RuntimeError(
"Found an entry for bundled provides, but no version information"
)
for line in get_bundled_provides(namespace.vendor_tarfile):
print(line)

return 0

Expand Down

0 comments on commit 1ac8550

Please sign in to comment.