diff mbox series

meta/lib: Clean up python library imports

Message ID 20260905090637.2747518-1-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit 7b265fc0e01ca35c03029e8ac48affb63633b204
Headers show
Series meta/lib: Clean up python library imports | expand

Commit Message

Richard Purdie Sept. 5, 2026, 9:06 a.m. UTC
There are a number of places os isn't imported as in class code, bitbake
has always handled this. Some external usages of the modules are triggering
exceptions from missing imports after bitbake's own imports were cleaned up.

Add the easily identified missing import os calls and move other imports
to the start of the files whilst there.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 meta/lib/oe/bootfiles.py        | 11 ++++++-----
 meta/lib/oe/buildstats.py       |  1 +
 meta/lib/oe/copy_buildsystem.py |  8 +++-----
 meta/lib/oe/distro_check.py     |  5 +++--
 meta/lib/oe/kernel.py           |  1 +
 meta/lib/oe/license.py          |  1 +
 meta/lib/oe/npm_registry.py     |  1 +
 meta/lib/oe/path.py             |  1 +
 meta/lib/oe/sanity.py           |  1 +
 meta/lib/oe/spdx_common.py      |  1 +
 meta/lib/oe/sstatesig.py        |  1 +
 meta/lib/oe/terminal.py         |  1 +
 meta/lib/oe/utils.py            |  1 +
 meta/lib/oeqa/utils/__init__.py | 10 +++++-----
 meta/lib/oeqa/utils/metadata.py |  2 ++
 15 files changed, 29 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/meta/lib/oe/bootfiles.py b/meta/lib/oe/bootfiles.py
index 7ee148c4e25..c69db1ad582 100644
--- a/meta/lib/oe/bootfiles.py
+++ b/meta/lib/oe/bootfiles.py
@@ -12,11 +12,12 @@ 
 #
 # Heavily inspired of bootimg_partition.py
 #
-def get_boot_files(deploy_dir, boot_files):
-    import re
-    import os
-    from glob import glob
 
+import os
+import re
+import glob
+
+def get_boot_files(deploy_dir, boot_files):
     if boot_files is None:
         return None
 
@@ -45,7 +46,7 @@  def get_boot_files(deploy_dir, boot_files):
                                 os.path.join(dst,
                                              os.path.basename(name))
 
-            srcs = glob(os.path.join(deploy_dir, src))
+            srcs = glob.glob(os.path.join(deploy_dir, src))
 
             for entry in srcs:
                 src = os.path.relpath(entry, deploy_dir)
diff --git a/meta/lib/oe/buildstats.py b/meta/lib/oe/buildstats.py
index 2700245ec69..db3d26afb8b 100644
--- a/meta/lib/oe/buildstats.py
+++ b/meta/lib/oe/buildstats.py
@@ -7,6 +7,7 @@ 
 # Because it is a real Python module, it can hold persistent state,
 # like open log files and the time of the last sampling.
 
+import os
 import time
 import re
 import bb.event
diff --git a/meta/lib/oe/copy_buildsystem.py b/meta/lib/oe/copy_buildsystem.py
index ced751b8356..73b868ff2ec 100644
--- a/meta/lib/oe/copy_buildsystem.py
+++ b/meta/lib/oe/copy_buildsystem.py
@@ -12,11 +12,13 @@ 
 #                              e.g.: ".*-downloads closed-.*"
 #
 
+import os
+import re
 import stat
 import shutil
+import subprocess
 
 def _smart_copy(src, dest):
-    import subprocess
     # smart_copy will choose the correct function depending on whether the
     # source is a file or a directory.
     mode = os.stat(src).st_mode
@@ -38,7 +40,6 @@  class BuildSystem(object):
         self.layers_exclude_pattern = d.getVar('SDK_LAYERS_EXCLUDE_PATTERN')
 
     def copy_bitbake_and_layers(self, destdir, workspace_name=None):
-        import re
         # Copy in all metadata layers + bitbake (as repositories)
         copied_corebase = None
         layers_copied = []
@@ -261,7 +262,6 @@  def merge_lockedsigs(copy_tasks, lockedsigs_main, lockedsigs_extra, merged_outpu
         write_sigs_file(merged_output, arch_order, merged)
 
 def create_locked_sstate_cache(lockedsigs, input_sstate_cache, output_sstate_cache, d, fixedlsbstring="", filterfile=None):
-    import shutil
     bb.note('Generating sstate-cache...')
 
     nativelsbstring = d.getVar('NATIVELSBSTRING')
@@ -282,8 +282,6 @@  def create_locked_sstate_cache(lockedsigs, input_sstate_cache, output_sstate_cac
                         shutil.move(src, dest)
 
 def check_sstate_task_list(d, targets, filteroutfile, cmdprefix='', cwd=None, logfile=None):
-    import subprocess
-
     bb.note('Generating sstate task list...')
 
     if not cwd:
diff --git a/meta/lib/oe/distro_check.py b/meta/lib/oe/distro_check.py
index 3494520f40d..1d78348ff3d 100644
--- a/meta/lib/oe/distro_check.py
+++ b/meta/lib/oe/distro_check.py
@@ -4,6 +4,9 @@ 
 # SPDX-License-Identifier: GPL-2.0-only
 #
 
+import os
+import shutil
+
 def create_socket(url, d):
     import urllib
     from bb.utils import export_proxies
@@ -150,8 +153,6 @@  def get_latest_released_ubuntu_source_package_list(d):
     return latest, package_names
 
 def create_distro_packages_list(distro_check_dir, d):
-    import shutil
-
     pkglst_dir = os.path.join(distro_check_dir, "package_lists")
     bb.utils.remove(pkglst_dir, True)
     bb.utils.mkdirhier(pkglst_dir)
diff --git a/meta/lib/oe/kernel.py b/meta/lib/oe/kernel.py
index ff88c41b218..624eace10ec 100644
--- a/meta/lib/oe/kernel.py
+++ b/meta/lib/oe/kernel.py
@@ -4,6 +4,7 @@ 
 # SPDX-License-Identifier: MIT
 #
 
+import os
 import re
 
 # Return a value for the ARCH environment variable for kernel compilation (including
diff --git a/meta/lib/oe/license.py b/meta/lib/oe/license.py
index adcfc827cfc..bd1d85ad5fd 100644
--- a/meta/lib/oe/license.py
+++ b/meta/lib/oe/license.py
@@ -6,6 +6,7 @@ 
 """Code for parsing OpenEmbedded license strings"""
 
 import ast
+import os
 import re
 import oe.qa
 from fnmatch import fnmatchcase as fnmatch
diff --git a/meta/lib/oe/npm_registry.py b/meta/lib/oe/npm_registry.py
index d97ced7cdaf..6d8ad903cfd 100644
--- a/meta/lib/oe/npm_registry.py
+++ b/meta/lib/oe/npm_registry.py
@@ -4,6 +4,7 @@ 
 # SPDX-License-Identifier: MIT
 #
 
+import os
 import bb
 import json
 import subprocess
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
index 7255d99bc2f..88d256f00b4 100644
--- a/meta/lib/oe/path.py
+++ b/meta/lib/oe/path.py
@@ -6,6 +6,7 @@ 
 
 import errno
 import glob
+import os
 import shutil
 import subprocess
 import os.path
diff --git a/meta/lib/oe/sanity.py b/meta/lib/oe/sanity.py
index 387d745024c..c9149ec974d 100644
--- a/meta/lib/oe/sanity.py
+++ b/meta/lib/oe/sanity.py
@@ -4,6 +4,7 @@ 
 # SPDX-License-Identifier: MIT
 #
 
+import os
 import oe.utils
 
 def bblayers_conf_file(d):
diff --git a/meta/lib/oe/spdx_common.py b/meta/lib/oe/spdx_common.py
index f885f1832ae..ad2bc0ffda3 100644
--- a/meta/lib/oe/spdx_common.py
+++ b/meta/lib/oe/spdx_common.py
@@ -8,6 +8,7 @@  import bb
 import collections
 import json
 import oe.packagedata
+import os
 import re
 import shutil
 
diff --git a/meta/lib/oe/sstatesig.py b/meta/lib/oe/sstatesig.py
index bd509800726..9928f3af495 100644
--- a/meta/lib/oe/sstatesig.py
+++ b/meta/lib/oe/sstatesig.py
@@ -7,6 +7,7 @@  import bb.parse
 import bb.siggen
 import bb.runqueue
 import oe
+import os
 import netrc
 
 def sstate_rundepfilter(siggen, fn, recipename, task, dep, depname, dataCaches):
diff --git a/meta/lib/oe/terminal.py b/meta/lib/oe/terminal.py
index ec65b616ab6..a8d3840153f 100644
--- a/meta/lib/oe/terminal.py
+++ b/meta/lib/oe/terminal.py
@@ -5,6 +5,7 @@ 
 #
 import logging
 import oe.classutils
+import os
 import shlex
 from bb.process import Popen, ExecutionError
 
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
index d24f49b875a..66f6323addf 100644
--- a/meta/lib/oe/utils.py
+++ b/meta/lib/oe/utils.py
@@ -7,6 +7,7 @@ 
 import subprocess
 import traceback
 import errno
+import os
 
 import bb.parse
 from bb import multiprocessing
diff --git a/meta/lib/oeqa/utils/__init__.py b/meta/lib/oeqa/utils/__init__.py
index e03f7e33bb0..89e24e6492e 100644
--- a/meta/lib/oeqa/utils/__init__.py
+++ b/meta/lib/oeqa/utils/__init__.py
@@ -3,6 +3,11 @@ 
 #
 # SPDX-License-Identifier: MIT
 #
+
+import importlib
+import os
+import sys
+
 # Enable other layers to have modules in the same named directory
 from pkgutil import extend_path
 __path__ = extend_path(__path__, __name__)
@@ -24,8 +29,6 @@  def avoid_paths_in_environ(paths):
 
         Returns new PATH without avoided PATHs.
     """
-    import os
-
     new_path = ''
     for p in os.environ['PATH'].split(':'):
        avoid = False
@@ -56,9 +59,6 @@  def make_logger_bitbake_compatible(logger):
     return logger
 
 def load_test_components(logger, executor):
-    import sys
-    import os
-    import importlib
 
     from oeqa.core.context import OETestContextExecutor
 
diff --git a/meta/lib/oeqa/utils/metadata.py b/meta/lib/oeqa/utils/metadata.py
index b320df67e0b..e68cb5ce19f 100644
--- a/meta/lib/oeqa/utils/metadata.py
+++ b/meta/lib/oeqa/utils/metadata.py
@@ -5,6 +5,8 @@ 
 # Functions to get metadata from the testing host used
 # for analytics of test results.
 
+import os
+
 from collections import OrderedDict
 from collections.abc import MutableMapping
 from xml.dom.minidom import parseString