[projects/pld-builder.new] Detect PHP version dynamically instead of hardcoded lists

arekm arekm at pld-linux.org
Wed Apr 15 21:43:54 CEST 2026


commit 1f061bf432746451dc8360eadbbf878bf78007ac
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Wed Apr 15 21:36:46 2026 +0200

    Detect PHP version dynamically instead of hardcoded lists
    
    Replace the hardcoded DEFAULT_PHP ('5.3') and php_versions list
    (which stopped at 8.2, missing 8.3/8.4/8.5) with dynamic detection:
    
    - Query the chroot's rpm --eval '%{php_name}' to get the current
      default PHP version prefix (e.g. 'php85').  This tracks whatever
      the rpmbuild macros define, without manual updates.
    
    - Discover available PHP version prefixes by listing php*-program
      packages from the repo via poldek, instead of maintaining a
      hardcoded list.  New PHP versions are picked up automatically.
    
    - When -D php_suffix is passed in the build request, that still
      takes precedence (same as before).
    
    The previous code had DEFAULT_PHP = '5.3' and a php_versions list
    that ended at 8.2.  This meant the --ignore flags passed to poldek
    would block php54 through php82, allow php53 (the "default"), and
    accidentally allow php83/84/85 (not in the list, never ignored).
    Any new PHP version added to the distro required updating both the
    list and potentially the default.
    
    Removes: DEFAULT_PHP, php_name_to_ver(), php_ver_to_name(),
    config.php_versions — all replaced by the dynamic approach.

 PLD_Builder/config.py  |  2 --
 PLD_Builder/request.py | 73 ++++++++++++++++++++++++++------------------------
 2 files changed, 38 insertions(+), 37 deletions(-)
---
diff --git a/PLD_Builder/config.py b/PLD_Builder/config.py
index e107047..522642b 100644
--- a/PLD_Builder/config.py
+++ b/PLD_Builder/config.py
@@ -110,8 +110,6 @@ class Builder_Conf:
         self.builder_user = get("builder_user", "builder")
         self.sudo_chroot_wrapper = get("sudo_chroot_wrapper", "")
         self.nonet_chroot = get("nonet_chroot", "/usr/local/bin/chroot-nonet")
-        # Available php versions in the distro
-        self.php_versions = get("php_versions", "4 5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2").split(" ")
         self.nice = get("nice", "0")
 
         f = get("syslog", "")
diff --git a/PLD_Builder/request.py b/PLD_Builder/request.py
index f6f4667..1416042 100644
--- a/PLD_Builder/request.py
+++ b/PLD_Builder/request.py
@@ -12,6 +12,7 @@ import html
 import pytz
 import tempfile
 
+import chroot
 import util
 import log
 from acl import acl
@@ -176,16 +177,7 @@ class Group:
                 ok = 0
         return ok
 
-# transform php package name (52) to version (5.2)
-def php_name_to_ver(v):
-    return '.'.join(list(v))
-
-# transform php version (5.2) to package name (52)
-def php_ver_to_name(v):
-    return v.replace('.', '')
-
 class Batch:
-    DEFAULT_PHP = '5.3'
 
     def __init__(self, e):
         self.bconds_with = []
@@ -407,39 +399,50 @@ class Batch:
             "--define '_builddir %{_topdir}/BUILD' "
         return rpmdefs + rpmopts
 
-    def php_ignores(self, php_version):
-        php_versions = config.php_versions.copy()
-
-        # remove current php version
-        try:
-            php_versions.remove(php_version)
-        except ValueError:
-            log.notice("Attempt to remove inexistent key '%s' from %s" % (php_version, php_versions))
-            pass
+    def php_ignores(self):
+        # Determine which PHP version to keep.  If -D php_suffix was
+        # passed in the build request, use that.  Otherwise, ask the
+        # chroot what rpm --eval '%{php_name}' returns — this is the
+        # system-default PHP set by rpmbuild macros and stays current
+        # without any hardcoded version lists.
+        if 'php_suffix' in self.defines:
+            keep_prefix = "php%s" % self.defines['php_suffix']
+        else:
+            f = chroot.popen("rpm --eval '%{php_name}'", encoding = "utf-8")
+            keep_prefix = f.read().strip()
+            f.close()
+            if not re.match(r'^php\d+$', keep_prefix):
+                log.notice("php_ignores: unexpected %%{php_name} value: '%s', skipping php ignores" % keep_prefix)
+                return []
+
+        # Discover all phpNN-* version prefixes available in the repo
+        # by looking at php*-program packages.  Ignore every prefix
+        # except the one we want to keep.
+        rx = re.compile(r'^(php\d+)-program-')
+        prefixes = set()
+        f = chroot.popen("poldek -q --shcmd='ls -q php*-program'", encoding = "utf-8")
+        for l in f:
+            m = rx.match(l.strip())
+            if m:
+                prefixes.add(m.group(1))
+        f.close()
+
+        if len(prefixes) == 0:
+            log.notice("php_ignores: no php*-program packages found in repo, skipping php ignores")
+            return []
+
+        prefixes.discard(keep_prefix)
 
-        # map them to poldek ignores
-        # always ignore hhvm
         res = ['hhvm-*']
-        for v in list(map(php_ver_to_name, php_versions)):
-            res.append("php%s-*" % v)
+        for p in sorted(prefixes):
+            res.append("%s-*" % p)
 
         return res
 
-    # build ignore package list
-    # currently only php ignore is filled based on build context
+    # build ignore package list for poldek
     def ignores(self):
-        ignores = []
-
-        # add php version based ignores
-        if 'php_suffix' in self.defines:
-            # current version if -D php_suffix is present
-            php_version = php_name_to_ver(self.defines['php_suffix'])
-        else:
-            php_version = self.DEFAULT_PHP
-
-        ignores.extend(self.php_ignores(php_version))
+        ignores = self.php_ignores()
 
-        # return empty string if the list is empty
         if len(ignores) == 0:
             return ""
 
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-builder.new.git/commitdiff/1f061bf432746451dc8360eadbbf878bf78007ac



More information about the pld-cvs-commit mailing list