#183 Enable fedpkg/rpkg more
Merged 2 years ago by scoady. Opened 2 years ago by nphilipp.
fedora-infra/ nphilipp/rpmautospec main--enable-rpkg-more  into  main

@@ -2,7 +2,7 @@ 

  

  from koji.plugin import callback

  

- from rpmautospec.process_distgit import process_distgit

+ from rpmautospec import process_distgit

  

  

  log = logging.getLogger(__name__)
@@ -16,5 +16,5 @@ 

          # i.e. maven and image builds don't have spec-files

          return

  

-     if not process_distgit(srcdir):

+     if not process_distgit(srcdir, enable_caching=False):

          log.info("No %autorelease/%autochangelog features used, skipping.")

@@ -1,4 +1,8 @@ 

- %autorelease(e:s:pb:) %{?-p:0.}%{?-b*}%{!?-b:1}%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{?dist}

+ %autorelease(e:s:pb:) %{?-p:0.}%{lua:

+     release_number = tonumber(rpm.expand("%{?_rpmautospec_release_number}%{!?_rpmautospec_release_number:1}"));

+     base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));

+     print(release_number + base_release_number - 1);

+ }%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{?dist}

  %autochangelog %{lua:

      locale = os.setlocale(nil)

      os.setlocale("C.utf8")

file modified
+2
@@ -1,1 +1,3 @@ 

  from .misc import specfile_uses_rpmautospec  # noqa: F401

+ from .subcommands.process_distgit import process_distgit  # noqa: F401

+ from .subcommands.release import calculate_release, calculate_release_number  # noqa: F401

file modified
+1 -1
@@ -3,7 +3,7 @@ 

  import sys

  import typing

  

- from . import changelog, process_distgit, release

+ from .subcommands import changelog, process_distgit, release

  

  

  subcmd_modules_by_name = {}

file modified
+9 -1
@@ -1,5 +1,6 @@ 

  import re

  from collections import namedtuple

+ from functools import lru_cache

  from pathlib import Path

  from typing import Union

  
@@ -18,6 +19,7 @@ 

  )

  

  

+ @lru_cache(maxsize=None)

  def check_specfile_features(specpath: Union[Path, str]) -> SpecfileFeatures:

      if not isinstance(specpath, Path):

          specpath = Path(specpath)
@@ -50,7 +52,13 @@ 

  def specfile_uses_rpmautospec(

      specpath: Union[Path, str], check_autorelease: bool = True, check_autochangelog: bool = True

  ) -> bool:

-     """Check whether or not an RPM spec file uses rpmautospec features."""

+     """Check whether or not an RPM spec file uses rpmautospec features.

+ 

+     :param specpath: Path to the RPM spec file

+     :param check_autorelease: Whether to check for use of %autorelease

+     :param check_autochangelog: Whether to check for use of %autochangelog

+     :return: Whether the spec file uses the specified features

+     """

      if not check_autorelease and not check_autochangelog:

          raise ValueError("One of check_autorelease and check_autochangelog must be set")

  

empty or binary file added
rpmautospec/subcommands/changelog.py rpmautospec/changelog.py
file renamed
+1 -1
@@ -1,7 +1,7 @@ 

  import logging

  from typing import Any, Dict, Optional, Union

  

- from .pkg_history import PkgHistoryProcessor

+ from ..pkg_history import PkgHistoryProcessor

  

  

  log = logging.getLogger(__name__)

rpmautospec/subcommands/process_distgit.py rpmautospec/process_distgit.py
file renamed
+24 -6
@@ -5,8 +5,8 @@ 

  from pathlib import Path

  from typing import Union

  

- from .misc import check_specfile_features

- from .pkg_history import PkgHistoryProcessor

+ from ..misc import check_specfile_features

+ from ..pkg_history import PkgHistoryProcessor

  

  

  log = logging.getLogger(__name__)
@@ -35,18 +35,36 @@ 

          help="Path to package worktree or the spec file within",

      )

  

+     process_distgit_parser.add_argument(

+         "target",

+         help="Path where to write processed spec file",

+     )

+ 

      return subcmd_name

  

  

- def process_distgit(spec_or_path: Union[Path, str]) -> bool:

+ def process_distgit(

+     spec_or_path: Union[Path, str], target: Union[Path, str] = None, *, enable_caching: bool = True

+ ) -> bool:

      """Process an RPM spec file in a distgit repository.

  

      :param spec_or_path: the spec file or path of the repository

+     :param enable_caching: whether or not spec file feature test results

+                            should be cached (disable in long-running

+                            processes)

      :return: whether or not the spec file needed processing

      """

      processor = PkgHistoryProcessor(spec_or_path)

  

-     features = check_specfile_features(processor.specfile)

+     if target is None:

+         target = processor.specfile

+     elif isinstance(target, Path):

+         target = Path(target)

+ 

+     if enable_caching:

+         features = check_specfile_features(processor.specfile)

+     else:

+         features = check_specfile_features.__wrapped__(processor.specfile)

      processing_necessary = (

          features.has_autorelease or features.has_autochangelog or not features.changelog_lineno

      )
@@ -98,10 +116,10 @@ 

          tmp_specfile.flush()

  

          # ...and copy it back (potentially across device boundaries)

-         shutil.copy2(tmp_specfile.name, processor.specfile)

+         shutil.copy2(tmp_specfile.name, target)

  

  

  def main(args):

      """Main method."""

      spec_or_path = args.spec_or_path.rstrip(os.path.sep)

-     process_distgit(spec_or_path)

+     process_distgit(spec_or_path, args.target)

rpmautospec/subcommands/release.py rpmautospec/release.py
file renamed
+44 -4
@@ -2,7 +2,7 @@ 

  from pathlib import Path

  from typing import Union

  

- from .pkg_history import PkgHistoryProcessor

+ from ..pkg_history import PkgHistoryProcessor

  

  

  log = logging.getLogger(__name__)
@@ -23,16 +23,56 @@ 

          help="Path to package worktree or the spec file within",

      )

  

+     complete_release_group = calc_release_parser.add_mutually_exclusive_group()

+ 

+     complete_release_group.add_argument(

+         "-c",

+         "--complete-release",

+         action="store_true",

+         default=True,

+         help="Print the complete release with flags (without dist tag)",

+     )

+ 

+     complete_release_group.add_argument(

+         "-n",

+         "--number-only",

+         action="store_false",

+         dest="complete_release",

+         default=False,

+         help="Print only the calculated release number",

+     )

+ 

      return subcmd_name

  

  

- def calculate_release(spec_or_path: Union[str, Path]) -> int:

+ def calculate_release(

+     spec_or_path: Union[str, Path], *, complete_release: bool = True

+ ) -> Union[str, int]:

+     """Calculate release value (or number) of a package.

+ 

+     :param spec_or_path: The spec file or directory it is located in.

+     :param complete_release: Whether to return the complete release (without

+                              dist tag) or just the number.

+     :return: the release value or number

+     """

      processor = PkgHistoryProcessor(spec_or_path)

      result = processor.run(visitors=(processor.release_number_visitor,))

-     return result["release-complete"]

+     return result["release-complete" if complete_release else "release-number"]

+ 

+ 

+ def calculate_release_number(spec_or_path: Union[str, Path]) -> int:

+     """Calculate release number of a package.

+ 

+     This number can be passed into the %autorelease macro as

+     %_rpmautospec_release_number.

+ 

+     :param spec_or_path: The spec file or directory it is located in.

+     :return: the release number

+     """

+     return calculate_release(spec_or_path, complete_release=False)

  

  

  def main(args):

      """Main method."""

-     release = calculate_release(args.spec_or_path)

+     release = calculate_release(args.spec_or_path, complete_release=args.complete_release)

      log.info("calculate_release release: %s", release)

empty or binary file added
tests/rpmautospec/subcommands/test_process_distgit.py tests/rpmautospec/test_process_distgit.py
file renamed
+26 -5
@@ -8,14 +8,14 @@ 

  

  import pytest

  

- from rpmautospec import process_distgit

+ from rpmautospec.subcommands import process_distgit

  

  

  __here__ = os.path.dirname(__file__)

  

  

  class TestProcessDistgit:

-     """Test the rpmautospec.process_distgit module"""

+     """Test the rpmautospec.subcommands.process_distgit module"""

  

      autorelease_autochangelog_cases = [

          (autorelease_case, autochangelog_case)
@@ -103,6 +103,7 @@ 

                  env=env,

              )

  

+     @pytest.mark.parametrize("overwrite_specfile", (False, True))

      @pytest.mark.parametrize("dirty_worktree", (False, True))

      @pytest.mark.parametrize("autorelease_case", ("unchanged", "with braces", "optional"))

      @pytest.mark.parametrize(
@@ -118,13 +119,16 @@ 

              "optional",

          ),

      )

-     def test_process_distgit(self, dirty_worktree, autorelease_case, autochangelog_case):

+     def test_process_distgit(

+         self, overwrite_specfile, dirty_worktree, autorelease_case, autochangelog_case

+     ):

          """Test the process_distgit() function"""

          with tempfile.TemporaryDirectory() as workdir:

              with tarfile.open(

                  os.path.join(

                      __here__,

                      os.path.pardir,

+                     os.path.pardir,

                      "test-data",

                      "repodata",

                      "dummy-test-package-gloster-git.tar.gz",
@@ -146,11 +150,25 @@ 

                      run_git_amend=not dirty_worktree,

                  )

  

-             process_distgit.process_distgit(unpacked_repo_dir)

+             if overwrite_specfile:

+                 target_spec_file_path = None

+             else:

+                 target_spec_file_path = os.path.join(workdir, "test-this-specfile-please.spec")

+ 

+             orig_test_spec_file_stat = os.stat(test_spec_file_path)

+             process_distgit.process_distgit(unpacked_repo_dir, target_spec_file_path)

+             if not overwrite_specfile:

+                 test_spec_file_stat = os.stat(test_spec_file_path)

+                 # we can't compare stat_results directly because st_atime has changed

+                 for attr in ("mode", "ino", "dev", "uid", "gid", "size", "mtime", "ctime"):

+                     assert getattr(test_spec_file_stat, "st_" + attr) == getattr(

+                         orig_test_spec_file_stat, "st_" + attr

+                     )

  

              expected_spec_file_path = os.path.join(

                  __here__,

                  os.path.pardir,

+                 os.path.pardir,

                  "test-data",

                  "repodata",

                  "dummy-test-package-gloster.spec.expected",
@@ -178,7 +196,10 @@ 

  

                  rpm_cmd = ["rpm", "--define", "dist .fc32", "--specfile"]

  

-                 test_cmd = rpm_cmd + [test_spec_file_path]

+                 if target_spec_file_path:

+                     test_cmd = rpm_cmd + [target_spec_file_path]

+                 else:

+                     test_cmd = rpm_cmd + [test_spec_file_path]

                  expected_cmd = rpm_cmd + [expected_spec_file_path]

  

                  q_release = ["--qf", "%{release}\n"]

tests/rpmautospec/subcommands/test_release.py tests/rpmautospec/test_release.py
file renamed
+3 -2
@@ -7,14 +7,14 @@ 

  

  import pytest

  

- from rpmautospec import release

+ from rpmautospec.subcommands import release

  

  

  __here__ = os.path.dirname(__file__)

  

  

  class TestRelease:

-     """Test the rpmautospec.release module"""

+     """Test the rpmautospec.subcommands.release module"""

  

      @pytest.mark.parametrize("method_to_test", ("calculate_release", "main"))

      def test_calculate_release(self, method_to_test, caplog):
@@ -23,6 +23,7 @@ 

                  os.path.join(

                      __here__,

                      os.path.pardir,

+                     os.path.pardir,

                      "test-data",

                      "repodata",

                      "dummy-test-package-gloster-git.tar.gz",

  • Enable not overwriting original spec file
  • Move subcommand modules into their own package
  • Expose process_distgit() in top-level package
  • Cache check_specfile_features() results

Depends-On: https://pagure.io/fedora-infra/rpmautospec/pull-request/181

This is on top of #181 which needs to be reviewed/merged first.

Build succeeded.

rebased onto 0aa96ce632bf292eed0475a1c6f053e5b75bc305

2 years ago

Build succeeded.

rebased onto 25c8f40

2 years ago

Pull-Request has been merged by scoady

2 years ago

Build succeeded.