diff mbox series

[02/10] package: Move get_conffiles/files_from_filevars functions to lib

Message ID 20230104154714.1168535-2-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit 424e65627c018b3119050f515b0c7cfb43be5573
Headers show
Series [01/10] package: Move fixup_perms function to bb function library | expand

Commit Message

Richard Purdie Jan. 4, 2023, 3:47 p.m. UTC
To avoid reparsing the bbclass code all the time, move the functions
to the python function library code which is more efficient.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 meta/classes-global/package.bbclass     | 78 +------------------------
 meta/classes-global/package_deb.bbclass |  2 +-
 meta/classes-global/package_ipk.bbclass |  2 +-
 meta/classes-global/package_rpm.bbclass |  2 +-
 meta/lib/oe/package.py                  | 77 ++++++++++++++++++++++++
 5 files changed, 81 insertions(+), 80 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-global/package.bbclass b/meta/classes-global/package.bbclass
index 29f0c80abdc..a31224f243b 100644
--- a/meta/classes-global/package.bbclass
+++ b/meta/classes-global/package.bbclass
@@ -257,82 +257,6 @@  python () {
         d.appendVarFlag('do_package', 'deptask', " do_packagedata")
 }
 
-# Get a list of files from file vars by searching files under current working directory
-# The list contains symlinks, directories and normal files.
-def files_from_filevars(filevars):
-    import os,glob
-    cpath = oe.cachedpath.CachedPath()
-    files = []
-    for f in filevars:
-        if os.path.isabs(f):
-            f = '.' + f
-        if not f.startswith("./"):
-            f = './' + f
-        globbed = glob.glob(f)
-        if globbed:
-            if [ f ] != globbed:
-                files += globbed
-                continue
-        files.append(f)
-
-    symlink_paths = []
-    for ind, f in enumerate(files):
-        # Handle directory symlinks. Truncate path to the lowest level symlink
-        parent = ''
-        for dirname in f.split('/')[:-1]:
-            parent = os.path.join(parent, dirname)
-            if dirname == '.':
-                continue
-            if cpath.islink(parent):
-                bb.warn("FILES contains file '%s' which resides under a "
-                        "directory symlink. Please fix the recipe and use the "
-                        "real path for the file." % f[1:])
-                symlink_paths.append(f)
-                files[ind] = parent
-                f = parent
-                break
-
-        if not cpath.islink(f):
-            if cpath.isdir(f):
-                newfiles = [ os.path.join(f,x) for x in os.listdir(f) ]
-                if newfiles:
-                    files += newfiles
-
-    return files, symlink_paths
-
-# Called in package_<rpm,ipk,deb>.bbclass to get the correct list of configuration files
-def get_conffiles(pkg, d):
-    pkgdest = d.getVar('PKGDEST')
-    root = os.path.join(pkgdest, pkg)
-    cwd = os.getcwd()
-    os.chdir(root)
-
-    conffiles = d.getVar('CONFFILES:%s' % pkg);
-    if conffiles == None:
-        conffiles = d.getVar('CONFFILES')
-    if conffiles == None:
-        conffiles = ""
-    conffiles = conffiles.split()
-    conf_orig_list = files_from_filevars(conffiles)[0]
-
-    # Remove links and directories from conf_orig_list to get conf_list which only contains normal files
-    conf_list = []
-    for f in conf_orig_list:
-        if os.path.isdir(f):
-            continue
-        if os.path.islink(f):
-            continue
-        if not os.path.exists(f):
-            continue
-        conf_list.append(f)
-
-    # Remove the leading './'
-    for i in range(0, len(conf_list)):
-        conf_list[i] = conf_list[i][1:]
-
-    os.chdir(cwd)
-    return conf_list
-
 def checkbuildpath(file, d):
     tmpdir = d.getVar('TMPDIR')
     with open(file) as f:
@@ -1209,7 +1133,7 @@  python populate_packages () {
             filesvar.replace("//", "/")
 
         origfiles = filesvar.split()
-        files, symlink_paths = files_from_filevars(origfiles)
+        files, symlink_paths = oe.package.files_from_filevars(origfiles)
 
         if autodebug and pkg.endswith("-dbg"):
             files.extend(debug)
diff --git a/meta/classes-global/package_deb.bbclass b/meta/classes-global/package_deb.bbclass
index ec7e10dbc99..c3ae7d574dc 100644
--- a/meta/classes-global/package_deb.bbclass
+++ b/meta/classes-global/package_deb.bbclass
@@ -269,7 +269,7 @@  def deb_write_pkg(pkg, d):
             scriptfile.close()
             os.chmod(os.path.join(controldir, script), 0o755)
 
-        conffiles_str = ' '.join(get_conffiles(pkg, d))
+        conffiles_str = ' '.join(oe.package.get_conffiles(pkg, d))
         if conffiles_str:
             conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
             for f in conffiles_str.split():
diff --git a/meta/classes-global/package_ipk.bbclass b/meta/classes-global/package_ipk.bbclass
index c43592af7e3..0207ea874b1 100644
--- a/meta/classes-global/package_ipk.bbclass
+++ b/meta/classes-global/package_ipk.bbclass
@@ -226,7 +226,7 @@  def ipk_write_pkg(pkg, d):
             scriptfile.close()
             os.chmod(os.path.join(controldir, script), 0o755)
 
-        conffiles_str = ' '.join(get_conffiles(pkg, d))
+        conffiles_str = ' '.join(oe.package.get_conffiles(pkg, d))
         if conffiles_str:
             conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
             for f in conffiles_str.split():
diff --git a/meta/classes-global/package_rpm.bbclass b/meta/classes-global/package_rpm.bbclass
index 39efcc328eb..7ba73f48e7d 100644
--- a/meta/classes-global/package_rpm.bbclass
+++ b/meta/classes-global/package_rpm.bbclass
@@ -341,7 +341,7 @@  python write_specfile () {
 
         localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
 
-        conffiles = get_conffiles(pkg, d)
+        conffiles = oe.package.get_conffiles(pkg, d)
         dirfiles = localdata.getVar('DIRFILES')
         if dirfiles is not None:
             dirfiles = dirfiles.split()
diff --git a/meta/lib/oe/package.py b/meta/lib/oe/package.py
index 05447f88084..b4c8ab7222a 100644
--- a/meta/lib/oe/package.py
+++ b/meta/lib/oe/package.py
@@ -4,6 +4,8 @@ 
 # SPDX-License-Identifier: GPL-2.0-only
 #
 
+import os
+import glob
 import stat
 import mmap
 import subprocess
@@ -532,5 +534,80 @@  def fixup_perms(d):
                     each_file = os.path.join(root, f)
                     fix_perms(each_file, fs_perms_table[dir].fmode, fs_perms_table[dir].fuid, fs_perms_table[dir].fgid, dir)
 
+# Get a list of files from file vars by searching files under current working directory
+# The list contains symlinks, directories and normal files.
+def files_from_filevars(filevars):
+    import oe.cachedpath
+
+    cpath = oe.cachedpath.CachedPath()
+    files = []
+    for f in filevars:
+        if os.path.isabs(f):
+            f = '.' + f
+        if not f.startswith("./"):
+            f = './' + f
+        globbed = glob.glob(f)
+        if globbed:
+            if [ f ] != globbed:
+                files += globbed
+                continue
+        files.append(f)
+
+    symlink_paths = []
+    for ind, f in enumerate(files):
+        # Handle directory symlinks. Truncate path to the lowest level symlink
+        parent = ''
+        for dirname in f.split('/')[:-1]:
+            parent = os.path.join(parent, dirname)
+            if dirname == '.':
+                continue
+            if cpath.islink(parent):
+                bb.warn("FILES contains file '%s' which resides under a "
+                        "directory symlink. Please fix the recipe and use the "
+                        "real path for the file." % f[1:])
+                symlink_paths.append(f)
+                files[ind] = parent
+                f = parent
+                break
+
+        if not cpath.islink(f):
+            if cpath.isdir(f):
+                newfiles = [ os.path.join(f,x) for x in os.listdir(f) ]
+                if newfiles:
+                    files += newfiles
+
+    return files, symlink_paths
+
+# Called in package_<rpm,ipk,deb>.bbclass to get the correct list of configuration files
+def get_conffiles(pkg, d):
+    pkgdest = d.getVar('PKGDEST')
+    root = os.path.join(pkgdest, pkg)
+    cwd = os.getcwd()
+    os.chdir(root)
+
+    conffiles = d.getVar('CONFFILES:%s' % pkg);
+    if conffiles == None:
+        conffiles = d.getVar('CONFFILES')
+    if conffiles == None:
+        conffiles = ""
+    conffiles = conffiles.split()
+    conf_orig_list = files_from_filevars(conffiles)[0]
+
+    # Remove links and directories from conf_orig_list to get conf_list which only contains normal files
+    conf_list = []
+    for f in conf_orig_list:
+        if os.path.isdir(f):
+            continue
+        if os.path.islink(f):
+            continue
+        if not os.path.exists(f):
+            continue
+        conf_list.append(f)
+
+    # Remove the leading './'
+    for i in range(0, len(conf_list)):
+        conf_list[i] = conf_list[i][1:]
 
+    os.chdir(cwd)
+    return conf_list