Script 'mail_helper' called by obssrc
Hello community,

here is the log from the commit of package 
obs-service-replace_using_package_version for openSUSE:Factory checked in at 
2022-11-11 14:36:18
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Comparing /work/SRC/openSUSE:Factory/obs-service-replace_using_package_version 
(Old)
 and      
/work/SRC/openSUSE:Factory/.obs-service-replace_using_package_version.new.1597 
(New)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Package is "obs-service-replace_using_package_version"

Fri Nov 11 14:36:18 2022 rev:4 rq:1035098 version:0.0.6

Changes:
--------
--- 
/work/SRC/openSUSE:Factory/obs-service-replace_using_package_version/obs-service-replace_using_package_version.changes
      2019-06-26 16:03:56.263621219 +0200
+++ 
/work/SRC/openSUSE:Factory/.obs-service-replace_using_package_version.new.1597/obs-service-replace_using_package_version.changes
    2022-11-11 14:36:22.094250871 +0100
@@ -1,0 +2,55 @@
+Thu Nov 10 14:26:46 UTC 2022 - [email protected]
+
+- Update to version 1668090256.71deb28:
+  * Bump version: 0.0.5 → 0.0.6
+  * Link to the official poetry installation guide
+  * Update replace_using_package_version.service
+  * Update README.md
+  * Update README.md
+  * Update README.md
+  * Update integration_tests/test_version_extraction.py
+  * Update replace_using_package_version.service
+  * Attempt to read version from obsinfo if no version is found
+  * Add Leap 15.4 & SLE 15 SP4 to the CI
+
+-------------------------------------------------------------------
+Thu Nov 10 13:11:18 UTC 2022 - [email protected]
+
+- Update to version 1668085498.ab6de1e:
+  * Bump version: 0.0.4 → 0.0.5
+  * Remove the rpm signkeys in the CI environment
+  * Adapt tests to mock subprocess.check_output
+  * Ignore stderr on successful command executions
+
+-------------------------------------------------------------------
+Wed Nov 09 10:11:30 UTC 2022 - [email protected]
+
+- Update to version 1667988064.d72b7ba:
+  * Bump version: 0.0.3 → 0.0.4
+  * Update obs-service-replace_using_package_version.spec
+  * Adapt spec to the current project setup
+  * Bump actions/setup-python from 4.1.0 to 4.3.0
+  * Bump actions/setup-python from 3 to 4.1.0
+  * Bump pytest-cov from 3.0.0 to 4.0.0
+  * Bump flake8 from 4.0.1 to 5.0.1
+  * Bump tox from 3.25.0 to 3.25.1
+  * Bump tox from 3.24.5 to 3.25.0
+  * Bump actions/cache from 2 to 3
+  * Bump actions/checkout from 2 to 3
+  * Bump actions/setup-python from 2 to 3
+  * Bump pytest from 7.0.0 to 7.0.1
+  * Bump pytest from 6.2.5 to 7.0.0
+  * Switch from custom run_command to using check_output
+  * Remove additional space from pyproject.toml
+  * Replace travis CI badge with GitHub Actions badge
+  * Make the file parameter default to the build recipe
+  * Add integration tests using pytest_container
+  * Bump coverage from 6.3 to 6.3.1
+  * Switch from travis-ci to github actions
+  * Remove trailing whitespace
+  * Switch to poetry instead of using setup.py
+  * Correct spelling & gramar in .service
+  * Add missing closing description element to .service
+  * Follow the python package naming conventions
+
+-------------------------------------------------------------------

Old:
----
  replace_using_package_version.tar

New:
----
  LICENSE
  replace_using_package_version.py
  replace_using_package_version.service

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Other differences:
------------------
++++++ obs-service-replace_using_package_version.spec ++++++
--- /var/tmp/diff_new_pack.7i3YFJ/_old  2022-11-11 14:36:22.678254244 +0100
+++ /var/tmp/diff_new_pack.7i3YFJ/_new  2022-11-11 14:36:22.682254266 +0100
@@ -1,7 +1,7 @@
 #
-# spec file for package obs-service-replace_with_package_version
+# spec file
 #
-# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany.
+# Copyright (c) 2022 SUSE LLC
 #
 # All modifications and additions to the file contributed by third parties
 # remain the property of their copyright owners, unless otherwise agreed
@@ -12,27 +12,25 @@
 # license that conforms to the Open Source Definition (Version 1.9)
 # published by the Open Source Initiative.
 
-# Please submit bugfixes or comments via http://bugs.opensuse.org/
+# Please submit bugfixes or comments via https://bugs.opensuse.org/
 #
+
+
 %define service replace_using_package_version
 
 Name:           obs-service-%{service}
-Version:        0.0.3
+Version:        0.0.6
 Release:        0
 Summary:        An OBS service: Replaces a regex  with the version value of a 
package
 License:        GPL-3.0-or-later
 Group:          Development/Tools/Building
-Url:            https://github.com/openSUSE/obs-service-%{service}
-Source0:        %{service}.tar
-%if 0%{?suse_version} > 1315
-BuildRequires:  python3-setuptools
-Requires:       python3-setuptools
+URL:            https://github.com/openSUSE/obs-service-%{service}
+Source0:        %{service}.py
+Source1:        %{service}.service
+Source2:        LICENSE
+BuildRequires:  sed
 Requires:       python3-docopt
-%else
-BuildRequires:  python-setuptools
-Requires:       python-setuptools
-Requires:       python-docopt
-%endif
+Requires:       python3-setuptools
 BuildRoot:      %{_tmppath}/%{name}-%{version}-build
 BuildArch:      noarch
 
@@ -42,40 +40,23 @@
 to the version of another package.
 
 %prep
-%setup -q -n %{service}
+cp %{S:0} .
+cp %{S:1} .
+cp %{S:2} .
 
 %build
-%if 0%{?suse_version} > 1315
-# Build Python 3 version
-python3 setup.py build
-%else
-# Build Python 2 version
-python2 setup.py build
-%endif
+sed -i "s|#!/usr/bin/env python3|#!/usr/bin/python3|g" %{service}.py
 
 %install
-%if 0%{?suse_version} > 1315
-# Install Python 3 version
-python3 setup.py install --root %{buildroot} \
-    --install-script %{_prefix}/lib/obs/service \
-    --install-data %{_prefix}/lib/obs/service
-%else
-# Install Python 2 version
-python2 setup.py install --root %{buildroot} \
-    --install-script %{_prefix}/lib/obs/service \
-    --install-data %{_prefix}/lib/obs/service
-%endif
+
+install -D -m 755 %{service}.py 
%{buildroot}%{_prefix}/lib/obs/service/%{service}
+install -D -m 644 %{service}.service 
%{buildroot}%{_prefix}/lib/obs/service/%{service}.service
 
 %files
 %defattr(-,root,root)
 %dir %{_prefix}/lib/obs
 %dir %{_prefix}/lib/obs/service
 %{_prefix}/lib/obs/service
-
-%if 0%{?suse_version} < 1500
-%doc LICENSE
-%else
 %license LICENSE
-%endif
 
 %changelog

++++++ LICENSE ++++++
++++ 675 lines (skipped)

++++++ _service ++++++
--- /var/tmp/diff_new_pack.7i3YFJ/_old  2022-11-11 14:36:22.746254636 +0100
+++ /var/tmp/diff_new_pack.7i3YFJ/_new  2022-11-11 14:36:22.746254636 +0100
@@ -1,10 +1,13 @@
 <services>
-    <service name="tar_scm" mode="disabled">
+    <service name="obs_scm" mode="disabled">
         <param 
name="url">https://github.com/openSUSE/obs-service-replace_using_package_version.git</param>
 
         <param name="scm">git</param>
         <param name="filename">replace_using_package_version</param>
         <param name="extension">tar</param>
         <param 
name="extract">obs-service-replace_using_package_version.spec</param>
+        <param name="extract">LICENSE</param>
+        <param 
name="extract">replace_using_package_version/replace_using_package_version.py</param>
+        <param name="extract">replace_using_package_version.service</param>
         <param name="version">_none_</param>
         <param name="changesgenerate">enable</param>
         <param name="changesauthor">[email protected]</param>

++++++ _servicedata ++++++
--- /var/tmp/diff_new_pack.7i3YFJ/_old  2022-11-11 14:36:22.766254751 +0100
+++ /var/tmp/diff_new_pack.7i3YFJ/_new  2022-11-11 14:36:22.766254751 +0100
@@ -1,6 +1,6 @@
 <servicedata>
 <service name="tar_scm">
                 <param 
name="url">https://github.com/openSUSE/obs-service-replace_using_package_version.git</param>
-              <param 
name="changesrevision">079b75f7ce19941a04a22fa00055867497e7812e</param></service></servicedata>
+              <param 
name="changesrevision">71deb28fee3d2bd774404ee4018080fbb47a6247</param></service></servicedata>
 (No newline at EOF)
 

++++++ replace_using_package_version.py ++++++
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018 SUSE Linux GmbH.  All rights reserved.
#
# This file is part of obs-service-replace_using_package_version.
#
#   obs-service-replace_using_package_version is free software: you can
#   redistribute it and/or modify it under the terms of the GNU General
#   Public License as published by the Free Software Foundation, either
#   version 3 of the License, or (at your option) any later version.
#
#   obs-service-replace_using_package_version is distributed in the hope
#   that it will be useful, but WITHOUT ANY WARRANTY; without even the
#   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#   See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with obs-service-replace_using_package_version.  If not,
#   see <http://www.gnu.org/licenses/>.
#
"""
replace_with_package_version.py

Usage:
    replace_using_package_version.py -h
    replace_using_package_version.py --regex=REGEX --outdir=DIR
        [--file=FILE]
        (--package=PACKAGE | --replacement=REPLACEMENT)
        [--parse-version=DEPTH]

Options:
    -h,--help                   : show this help message
    --outdir=DIR                : output directory
    --file=FILE                 : file to update
                                    The default build recipe file
                                    (e.g. Dockerfile) is used when this
                                    parameter is omitted.
    --package=PACKAGE           : package to check
    --replacement=REPLACEMENT   : replacement string for any match
    --regex=REGEX               : regular expression for parsing file
    --parse-version=DEPTH       : parse the package version string to match
                                    major.minor.patch.patch_update format.
                                    It can be set to 'major', 'minor',
                                    'patch', 'patch_update' and 'offset'.
"""
from typing import Optional
import docopt
import re
import os
import subprocess
from pkg_resources import parse_version
from typing import List

version_regex = {
    'major': r'^(\d+)',
    'minor': r'^(\d+(\.\d+){0,1})',
    'patch': r'^(\d+(\.\d+){0,2})',
    'patch_update': r'^(\d+(\.\d+){0,3})',
    'offset': r'^(?:\d+(?:\.\d+){0,3})[+-.~](?:git|svn|cvs)(\d+)'
}
obsinfo_regex = r'version: (.+)'


def guess_recipe_filename_from_env() -> Optional[str]:
    """Try to infer the default build recipe file from the current build
    environment.

    The `build <https://github.com/openSUSE/obs-build>`_ script sets the
    environment variable ``BUILD_DIST`` to the location of the
    :file:`build.dist`. The same directory contains the file
    :file:`build.data`, which contains environment variables to be sourced via
    :command:`bash` or :command:`sh`. One of these is ``RECIPEFILE`` which
    contains the name of the current build recipe, with the tiny catch that it
    will be called :file:`_service:actual_name` although the build script
    already renamed the actual file to :file:`actual_name`.

    """
    build_dist = os.getenv("BUILD_DIST")
    if build_dist is None or build_dist[-5:] != ".dist":
        return None

    # Extract the variable RECIPEFILE from `build.data`
    recipefile = None
    with open(build_dist[:-5] + ".data") as data:
        for line in data:
            # lines are:
            # FOOBAR='baz'
            # => need to also remove the ' or " from the second column
            var, val = line.strip().split("=")
            if var == "RECIPEFILE":
                recipefile = val.replace("'", "").replace('"', '')

    if recipefile is None:
        return None

    # strip the leading `_service:` part
    return recipefile.split(":")[-1]


def main():
    """
    main-entry point for program, expects dict with arguments from docopt()
    """
    # TODO: probably there is a better way to set the repositories path
    rpm_dir = './repos'

    command_args = docopt.docopt(__doc__)

    src_file = command_args['--file']

    if src_file is None:
        src_file = guess_recipe_filename_from_env()
        if src_file is None:
            raise RuntimeError(
                "No file was provided and could not infer a default build file"
            )

    if not os.path.isfile(src_file):
        raise RuntimeError('File {0} not found'.format(src_file))

    if not os.path.isdir(command_args['--outdir']):
        raise Exception(
            'Output directory {0} not found'.format(command_args['--outdir'])
        )

    filecopy = os.path.join(
        command_args['--outdir'], os.path.basename(src_file)
    )

    if command_args['--package']:
        parse_version = command_args['--parse-version']
        version = find_package_version(command_args['--package'], rpm_dir)
        if parse_version and parse_version not in version_regex.keys():
            raise Exception((
                'Invalid value for this flag. Expected format is: '
                '--parse-version=[major|minor|patch|patch_update|offset]'
            ))
        elif parse_version:
            version = find_match_in_version(
                version_regex[parse_version], version
            )
        replacement = version
    else:
        replacement = command_args['--replacement']

    apply_regex_to_file(
        src_file,
        filecopy,
        command_args['--regex'],
        replacement
    )


def apply_regex_to_file(input_file, output_file, regex, replacement):
    with open(input_file, 'r') as in_file:
        contents = in_file.read()

    with open(output_file, 'w') as out_file:
        out_file.write(re.sub(regex, replacement, contents))


def find_package_version(package, rpm_dir):
    version = parse_version('')
    try:
        version = get_pkg_version(package)
    except Exception:
        version = find_package_version_in_local_repos(rpm_dir, package)

    if not str(version):
        version = find_package_version_in_obsinfo('.', package)

    if not str(version):
        raise Exception('Package version not found')
    return str(version)


def find_package_version_in_local_repos(repo_path, package):
    version = parse_version('')
    for root, _, files in os.walk(repo_path):
        packages = [
            f for f in files if f.endswith('rpm') and package in f
        ]
        for pkg in packages:
            rpm_file = os.path.join(root, pkg)
            if get_pkg_name_from_rpm(rpm_file) == package:
                rpm_ver = get_pkg_version_from_rpm(rpm_file)
                if rpm_ver >= version:
                    version = rpm_ver
    return version


def find_package_version_in_obsinfo(path, package):
    version = parse_version('')
    for f in os.listdir(path):
        if f.endswith('obsinfo') and package in f:
            obsinfo_ver = get_pkg_version_from_obsinfo(f)
            if obsinfo_ver >= version:
                version = obsinfo_ver
    return version


def find_match_in_version(regexpr, version):
    search = re.search(regexpr, version)
    if search is None:
        return version
    else:
        return search.group(1)


def run_command(command: List[str]) -> str:
    return subprocess.check_output(command).decode()


def get_pkg_version_from_obsinfo(obsinfo_file):
    regex = re.compile(obsinfo_regex)
    with open(obsinfo_file) as f:
        for line in f:
            match = regex.match(line)
            if match:
                return parse_version(match[1])
    return parse_version('')


def get_pkg_name_from_rpm(rpm_file):
    command = [
        'rpm', '-qp', '--queryformat', '%{NAME}', rpm_file
    ]
    return run_command(command)


def get_pkg_version_from_rpm(rpm_file):
    command = [
        'rpm', '-qp', '--queryformat', '%{VERSION}', rpm_file
    ]
    return parse_version(run_command(command))


def get_pkg_version(package):
    command = [
        'rpm', '-q', '--queryformat', '%{VERSION}', package
    ]
    return parse_version(run_command(command))


def init(__name__):
    if __name__ == '__main__':
        main()


init(__name__)

++++++ replace_using_package_version.service ++++++
<service name="replace_using_package_version">
  <summary>Replaces a regex  with the version value of a package</summary>
  <description>This service replaces a given regex with the version value of
a given package. Can be used to align the version of you package or image
to the version of another package.</description>
  <parameter name="file">
    <description>This is the file where the change will be applied</description>
  </parameter>
  <parameter name="regex">
    <description>This is the regular expression used to parse the input file.
It uses python re module syntax</description>
  </parameter>
  <parameter name="package">
    <description>This is the package which version will be used as a replacement
for the regex. If it is not found as a build dependency it will fallback to
look for an *.obsinfo file with the package name (the *obsinfo file is 
generated by the obs_scm service). It fails if no version is found.
</description>
  </parameter>
  <parameter name="parse-version">
    <description>Parses the package version string to match the
major.minor.patch format. Then any found match is used as the replacement
string. It can be set to three different values: major, minor or patch.
If set to major only the first numeric value will be used as the
replacement (e.g. if version is 3.1.3 only 3 will be used). If set to minor
only the first and second numeric values separated by a dot will be used
(e.g. if version is 3.1~git_r125 only 3.1 will be used). If set to patch
it will reach up to the first three numeric values separated with a 
dots.</description>
  </parameter>
  <parameter name="replacement">
    <description>This parameter is an alternative to the package parameter,
instead to look for a package version it will just use the given string
for the regex replacement. It is mostly used to debug or test the
regex.</description>
  </parameter>
</service>

Reply via email to