Git commit 4f769b8417b23d27a037aede0baf5d99ad78025f by Andrew Shark, on behalf of Christoph Cullmann. Committed on 09/03/2024 at 17:55. Pushed by ashark into branch 'master'.
Use os.cpu_count() instead of running subprocess This also unifies the code, so we do not need to distinct between linux/freebsd/macos specific command to get number of threads. M +1 -3 doc/conf-options-table.docbook M +2 -4 ksblib/BuildSystem/BuildSystem.py M +1 -6 ksblib/FirstRun.py M +5 -2 tests/unit/build-system/test_empty-num-cores.py https://invent.kde.org/sdk/kde-builder/-/commit/4f769b8417b23d27a037aede0baf5d99ad78025f diff --git a/doc/conf-options-table.docbook b/doc/conf-options-table.docbook index b35d520c..a0008112 100644 --- a/doc/conf-options-table.docbook +++ b/doc/conf-options-table.docbook @@ -258,9 +258,7 @@ number, the "nicer" the program is.</para> <member>Available since</member><member>20.07</member> </simplelist> <para>This option is defined by &kdesrc-build; (when using <command>kdesrc-build --generate-config</command>), set to be the number of -available CPUs (as indicated by the external application -<application>nproc</application>). If &kdesrc-build; cannot detect the -number of CPUs, this value is set to 4.</para> +available CPUs. If &kdesrc-build; cannot detect the number of CPUs, this value is set to 4.</para> <para>See <xref linkend="make-options-example"/> for an example of this option's usage.</para> diff --git a/ksblib/BuildSystem/BuildSystem.py b/ksblib/BuildSystem/BuildSystem.py index 3fe13b77..6523bf20 100644 --- a/ksblib/BuildSystem/BuildSystem.py +++ b/ksblib/BuildSystem/BuildSystem.py @@ -97,10 +97,8 @@ class BuildSystem: if self.supportsAutoParallelism() and cores == "auto": return {} - try: - out = [line.removesuffix("\n") for line in Util.filter_program_output(None, "nproc")] - max_cores = max(1, int(out[0])) - except BuildException: + max_cores = os.cpu_count() + if not max_cores: max_cores = 1 if cores == "auto" and max_cores > 1: diff --git a/ksblib/FirstRun.py b/ksblib/FirstRun.py index 95ea6204..356aea77 100644 --- a/ksblib/FirstRun.py +++ b/ksblib/FirstRun.py @@ -194,12 +194,7 @@ class FirstRun: with open(os.path.dirname(os.path.realpath(__file__)) + "/../data/kdesrc-buildrc.in", "r") as data_file: sampleRc = data_file.read() - numCores = None - if self.oss.vendorID() in [*self.supportedDistros, "linux"]: - numCores = int(subprocess.run(["nproc"], shell=False, capture_output=True, check=True).stdout.decode("utf-8").removesuffix("\n")) - elif self.oss.vendorID() == "freebsd": - numCores = int(subprocess.run(["sysctl", "-n", "hw.ncpu"], shell=False, capture_output=True, check=True).stdout.decode("utf-8").removesuffix("\n")) - + numCores = os.cpu_count() if not numCores: numCores = 4 diff --git a/tests/unit/build-system/test_empty-num-cores.py b/tests/unit/build-system/test_empty-num-cores.py index b2ad9a03..a2c2f334 100644 --- a/tests/unit/build-system/test_empty-num-cores.py +++ b/tests/unit/build-system/test_empty-num-cores.py @@ -1,3 +1,4 @@ +import os import pytest import subprocess from ksblib.Module.Module import Module @@ -29,8 +30,10 @@ def test_empty_numcores(mock_buildsystem): # The -j logic will take off one CPU if you ask for too many so try to ensure # test cases don't ask for too many. - max_cores = int(subprocess.run(["nproc"], shell=True, capture_output=True, check=True).stdout.decode("utf-8").removesuffix("\n")) - max_cores = int(max_cores // 2) + max_cores = os.cpu_count() + if max_cores is None: + max_cores = 2 + if max_cores < 2: max_cores = 2