From 23e980fc74fe9c8c71938b9e8a38a2fc1fecf833 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 6 May 2020 11:25:43 -0400 Subject: [PATCH] setup.py: use importlib.resources from python 3.7 to find core.sr.ht Makefile importlib.resources will guarantee to find any importable module, and check that the resource ('Makefile') can be found within it. This lets us get rid of the current getsitepackages() hack and environment variable fallback, which is both verbose and badly handles some edge cases when installing modules, and instead use the recommended way to find data files shipped with a python module. --- setup.py | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/setup.py b/setup.py index fecc0df..a866db3 100755 --- a/setup.py +++ b/setup.py @@ -2,31 +2,11 @@ from distutils.core import setup import subprocess import os -import site import sys +import importlib.resources -if hasattr(site, 'getsitepackages'): - pkg_dirs = site.getsitepackages() - if site.getusersitepackages(): - pkg_dirs.append(site.getusersitepackages()) - for pkg_dir in pkg_dirs: - srht_path = os.path.join(pkg_dir, "srht") - if os.path.isdir(srht_path): - break - else: - raise Exception("Can't find core srht module in your site packages " - "directories. Please install it first.") -else: - srht_path = os.getenv("SRHT_PATH") - if not srht_path: - raise Exception("You're running inside a virtual environment. " - "Due to virtualenv limitations, you need to set the " - "$SRHT_PATH environment variable to the path of the " - "core srht module.") - elif not os.path.isdir(srht_path): - raise Exception( - "The $SRHT_PATH environment variable points to an invalid " - "directory: {}".format(srht_path)) +with importlib.resources.path('srht', 'Makefile') as f: + srht_path = f.parent.as_posix() make = os.environ.get("MAKE", "make") subp = subprocess.run([make, "SRHT_PATH=" + srht_path])