From 1e24a4d60930a4508ea55f09e91e9e63bcf774b0 Mon Sep 17 00:00:00 2001 From: Olaf Pichler Date: Tue, 14 Mar 2023 16:10:25 +0100 Subject: [PATCH 1/7] use sys.executable --- selinux/__init__.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index ef94efd..1a99633 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -71,11 +71,7 @@ def add_location(location): def get_system_sitepackages(): """Get sitepackage locations from system python""" - # Do not ever use sys.executable here - # See https://github.com/pycontribs/selinux/issues/17 for details - system_python = "/usr/bin/python%s" % ".".join( - [str(item) for item in platform.python_version_tuple()[0:2]] - ) + system_python = sys.executable system_sitepackages = json.loads( subprocess.check_output( From f3378ccbccb0732790eeea8bc779e92ebeddd860 Mon Sep 17 00:00:00 2001 From: Olaf Pichler Date: Wed, 15 Mar 2023 09:19:56 +0100 Subject: [PATCH 2/7] more elaborate fix --- selinux/__init__.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index 1a99633..b8b5796 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -71,9 +71,14 @@ def add_location(location): def get_system_sitepackages(): """Get sitepackage locations from system python""" - system_python = sys.executable - system_sitepackages = json.loads( + python_paths = [ + "/usr/bin/python%s", + "/usr/local/bin/python%s" + ] + path_check_count = 0 + + getsitepackages_subprocess = lambda path: json.loads( subprocess.check_output( [ system_python, @@ -82,7 +87,17 @@ def get_system_sitepackages(): ] ).decode("utf-8") ) - return system_sitepackages + + while path_check_count < len(python_paths): + try: + system_python = python_paths[path_check_count] % ".".join( + [str(item) for item in platform.python_version_tuple()[0:2]] + ) + return getsitepackages_subprocess(system_python) + except FileNotFoundError: + path_check_count += 1 + else: + return getsitepackages_subprocess(sys.executable) def check_system_sitepackages(): """Try add selinux module from any of the python site-packages""" From 592bee2b54873483cb98415f29a5c3cab7dc3c2a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 08:23:47 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- selinux/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index b8b5796..f87839f 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -72,10 +72,7 @@ def add_location(location): def get_system_sitepackages(): """Get sitepackage locations from system python""" - python_paths = [ - "/usr/bin/python%s", - "/usr/local/bin/python%s" - ] + python_paths = ["/usr/bin/python%s", "/usr/local/bin/python%s"] path_check_count = 0 getsitepackages_subprocess = lambda path: json.loads( From ac77d816977685c103a11bc31ebd6dbe6ff2fa56 Mon Sep 17 00:00:00 2001 From: Olaf Pichler Date: Wed, 15 Mar 2023 13:39:05 +0100 Subject: [PATCH 4/7] reworked fix --- selinux/__init__.py | 58 ++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index f87839f..0092abc 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -69,46 +69,60 @@ def add_location(location): return True return False - def get_system_sitepackages(): - """Get sitepackage locations from system python""" + def check_system_sitepackages(): + """Try add selinux module from any of the python site-packages of probably installed python variants""" + + # Here we can add other possible python installation paths + # The %s will be replaced with the curren python version + python_paths = [ + "/usr/bin/python%s", + "/usr/local/bin/python%s" + ] - python_paths = ["/usr/bin/python%s", "/usr/local/bin/python%s"] path_check_count = 0 + success = False - getsitepackages_subprocess = lambda path: json.loads( + getsitepackages_subprocess = lambda python_path: json.loads( subprocess.check_output( [ - system_python, + python_path, "-c", "import json, site; print(json.dumps(site.getsitepackages()))", ] ).decode("utf-8") ) - while path_check_count < len(python_paths): - try: - system_python = python_paths[path_check_count] % ".".join( + # First we'll check for the currently executed python + tmp = [sys.executable] + # Then we'll try other commonly used python installation places for the current python version + for python_path in python_paths: + tmp.append( + python_path % ".".join( [str(item) for item in platform.python_version_tuple()[0:2]] ) - return getsitepackages_subprocess(system_python) - except FileNotFoundError: - path_check_count += 1 - else: - return getsitepackages_subprocess(sys.executable) + ) + python_paths = tmp + del tmp - def check_system_sitepackages(): - """Try add selinux module from any of the python site-packages""" + # We try to find selinux in all provided paths + while path_check_count < len(python_paths) and not success: + try: + system_sitepackages = getsitepackages_subprocess(python_paths[path_check_count]) + for candidate in system_sitepackages: + success = add_location(candidate) + if success: + break - success = False - system_sitepackages = get_system_sitepackages() - for candidate in system_sitepackages: - success = add_location(candidate) - if success: - break + except FileNotFoundError: + # We could not find python under the provided path so we just try the next one + pass + + finally: + path_check_count += 1 if not success: raise Exception( - "Failed to detect selinux python bindings at %s" % system_sitepackages + "Failed to detect selinux python bindings at %s" % " or ".join(python_paths) ) check_system_sitepackages() From 5f966db4de79c2e0a3e1cc54baac4e2026d5a59c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 12:40:17 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- selinux/__init__.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index 0092abc..8179bdb 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -74,10 +74,7 @@ def check_system_sitepackages(): # Here we can add other possible python installation paths # The %s will be replaced with the curren python version - python_paths = [ - "/usr/bin/python%s", - "/usr/local/bin/python%s" - ] + python_paths = ["/usr/bin/python%s", "/usr/local/bin/python%s"] path_check_count = 0 success = False @@ -97,9 +94,8 @@ def check_system_sitepackages(): # Then we'll try other commonly used python installation places for the current python version for python_path in python_paths: tmp.append( - python_path % ".".join( - [str(item) for item in platform.python_version_tuple()[0:2]] - ) + python_path + % ".".join([str(item) for item in platform.python_version_tuple()[0:2]]) ) python_paths = tmp del tmp @@ -107,7 +103,9 @@ def check_system_sitepackages(): # We try to find selinux in all provided paths while path_check_count < len(python_paths) and not success: try: - system_sitepackages = getsitepackages_subprocess(python_paths[path_check_count]) + system_sitepackages = getsitepackages_subprocess( + python_paths[path_check_count] + ) for candidate in system_sitepackages: success = add_location(candidate) if success: @@ -122,7 +120,8 @@ def check_system_sitepackages(): if not success: raise Exception( - "Failed to detect selinux python bindings at %s" % " or ".join(python_paths) + "Failed to detect selinux python bindings at %s" + % " or ".join(python_paths) ) check_system_sitepackages() From 1dd1b7ca8ee30fe2dc03440984bb05e20efa855d Mon Sep 17 00:00:00 2001 From: Olaf Pichler Date: Wed, 15 Mar 2023 13:47:52 +0100 Subject: [PATCH 6/7] make linter happy --- selinux/__init__.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index 8179bdb..b164a3a 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -69,17 +69,9 @@ def add_location(location): return True return False - def check_system_sitepackages(): - """Try add selinux module from any of the python site-packages of probably installed python variants""" - - # Here we can add other possible python installation paths - # The %s will be replaced with the curren python version - python_paths = ["/usr/bin/python%s", "/usr/local/bin/python%s"] - - path_check_count = 0 - success = False - - getsitepackages_subprocess = lambda python_path: json.loads( + def get_system_sitepackages(python_path): + """Extract python site-package locations for a provided python installation path""" + return json.loads( subprocess.check_output( [ python_path, @@ -89,13 +81,27 @@ def check_system_sitepackages(): ).decode("utf-8") ) + def check_system_sitepackages(): + """Try add selinux module from any of the python site-packages of probably installed python variants""" + + # Here we can add other possible python installation paths + # The %s will be replaced with the curren python version + python_paths = [ + "/usr/bin/python%s", + "/usr/local/bin/python%s" + ] + + path_check_count = 0 + success = False + # First we'll check for the currently executed python tmp = [sys.executable] # Then we'll try other commonly used python installation places for the current python version for python_path in python_paths: tmp.append( - python_path - % ".".join([str(item) for item in platform.python_version_tuple()[0:2]]) + python_path % ".".join( + [str(item) for item in platform.python_version_tuple()[0:2]] + ) ) python_paths = tmp del tmp @@ -103,9 +109,7 @@ def check_system_sitepackages(): # We try to find selinux in all provided paths while path_check_count < len(python_paths) and not success: try: - system_sitepackages = getsitepackages_subprocess( - python_paths[path_check_count] - ) + system_sitepackages = get_system_sitepackages(python_paths[path_check_count]) for candidate in system_sitepackages: success = add_location(candidate) if success: @@ -120,8 +124,7 @@ def check_system_sitepackages(): if not success: raise Exception( - "Failed to detect selinux python bindings at %s" - % " or ".join(python_paths) + "Failed to detect selinux python bindings at %s" % " or ".join(python_paths) ) check_system_sitepackages() From 011ab6e39f8c66d13483dd73f5acc702b359a039 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 12:48:13 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- selinux/__init__.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/selinux/__init__.py b/selinux/__init__.py index b164a3a..1b4f4b3 100644 --- a/selinux/__init__.py +++ b/selinux/__init__.py @@ -86,10 +86,7 @@ def check_system_sitepackages(): # Here we can add other possible python installation paths # The %s will be replaced with the curren python version - python_paths = [ - "/usr/bin/python%s", - "/usr/local/bin/python%s" - ] + python_paths = ["/usr/bin/python%s", "/usr/local/bin/python%s"] path_check_count = 0 success = False @@ -99,9 +96,8 @@ def check_system_sitepackages(): # Then we'll try other commonly used python installation places for the current python version for python_path in python_paths: tmp.append( - python_path % ".".join( - [str(item) for item in platform.python_version_tuple()[0:2]] - ) + python_path + % ".".join([str(item) for item in platform.python_version_tuple()[0:2]]) ) python_paths = tmp del tmp @@ -109,7 +105,9 @@ def check_system_sitepackages(): # We try to find selinux in all provided paths while path_check_count < len(python_paths) and not success: try: - system_sitepackages = get_system_sitepackages(python_paths[path_check_count]) + system_sitepackages = get_system_sitepackages( + python_paths[path_check_count] + ) for candidate in system_sitepackages: success = add_location(candidate) if success: @@ -124,7 +122,8 @@ def check_system_sitepackages(): if not success: raise Exception( - "Failed to detect selinux python bindings at %s" % " or ".join(python_paths) + "Failed to detect selinux python bindings at %s" + % " or ".join(python_paths) ) check_system_sitepackages()