scripts/install.py

branch
eric7-maintenance
changeset 10004
983477114d3c
parent 9940
a57c188857e9
parent 9986
2c571c1c7b0d
child 10079
0222a480e93d
--- a/scripts/install.py	Thu Apr 06 09:32:12 2023 +0200
+++ b/scripts/install.py	Tue May 02 10:20:02 2023 +0200
@@ -29,6 +29,8 @@
 import sysconfig
 import time
 
+from functools import partial
+
 # Define the globals.
 progName = None
 currDir = os.getcwd()
@@ -1923,13 +1925,80 @@
     return py_dir, "Ui_{0}".format(py_file)
 
 
-def compileUiFiles():
+def __compileOneUi(ui_path, mapFunc=None):
+    """
+    Function to compile a single form file to Python code.
+
+    @param ui_path path of the Qt form file
+    @type str
+    @param mapFunc function to change directory and/or name of the resulting Python file
+        (defaults to None)
+    @type func (optional)
     """
-    Compile the .ui files to Python sources.
+    from PyQt6.uic import compileUi
+
+    py_dir, py_file = os.path.split(ui_path[:-3] + ".py")
+
+    # Allow the caller to change the name of the .py file or generate
+    # it in a different directory.
+    if mapFunc is not None:
+        py_dir, py_file = mapFunc(py_dir, py_file)
+
+    # Make sure the destination directory exists.
+    os.makedirs(py_dir, exist_ok=True)
+
+    py_path = os.path.join(py_dir, py_file)
+
+    with open(py_path, "w", encoding="utf-8") as py_file:
+        compileUi(ui_path, py_file, execute=False, indent=4)
+
+
+def compileUiDir(root, recurse=False, mapFunc=None, workers=1):
     """
-    from PyQt6.uic import compileUiDir
+    Function to compile all Qt form files of a directory or directory tree
+    to Python code.
 
-    compileUiDir(eric7SourceDir, True, __pyName)
+    @param root directory to scan for Qt form files (i.e. files ending with '.ui'
+    @type str
+    @param recurse flag indicating to recurse into sub-directories (defaults to False)
+    @type bool (optional)
+    @param mapFunc function to change directory and/or name of the resulting Python file
+        (defaults to None)
+    @type func (optional)
+    @param workers number of worker processes to be used to compile (defaults to 1)
+    @type int (optional)
+    """
+    if recurse:
+        ui_files = []
+        for rootDir, _, files in os.walk(root):
+            ui_files.extend(
+                os.path.join(rootDir, ui) for ui in files if ui.endswith(".ui")
+            )
+    else:
+        ui_files = [
+            os.path.join(root, ui)
+            for ui in os.listdir(root)
+            if os.path.isfile(os.path.join(root, ui) and ui.endswith(".ui"))
+        ]
+
+    ProcessPoolExecutor = None
+    if workers != 1:
+        try:
+            from concurrent.futures import ProcessPoolExecutor  # __IGNORE_WARNING__
+        except NotImplementedError:
+            workers = 1
+
+    if workers != 1 and ProcessPoolExecutor is not None:
+        # If workers == 0, let ProcessPoolExecutor determine worker count.
+        workers = workers or None
+        with ProcessPoolExecutor(max_workers=workers) as executor:
+            executor.map(
+                partial(__compileOneUi, mapFunc=mapFunc),
+                ui_files,
+            )
+    else:
+        for ui_file in ui_files:
+            __compileOneUi(ui_file, mapFunc=mapFunc)
 
 
 def prepareInfoFile(fileName):
@@ -2274,7 +2343,7 @@
         for file in [f for f in files if fnmatch.fnmatch(f, "Ui_*.py")]:
             os.remove(os.path.join(root, file))
     # step 2: compile the forms
-    compileUiFiles()
+    compileUiDir(eric7SourceDir, recurse=True, mapFunc=__pyName, workers=0)
     print(" Done")
 
     if doCompile:

eric ide

mercurial