Make bin script compatible with Python 3.6

This commit is contained in:
D. Berge
2023-09-09 16:38:51 +02:00
parent c4b330b2bb
commit 2b364bbff7

View File

@@ -12,6 +12,18 @@ surveys should be under $HOME/etc/surveys/*.yaml. In both cases,
$HOME is the home directory of the user running this script. $HOME is the home directory of the user running this script.
""" """
def is_relative_to(it, other):
"""
is_relative_to() is not present version Python 3.9, so we
need this kludge to get Dougal to run on OpenSUSE 15.4
"""
if "is_relative_to" in dir(it):
return it.is_relative_to(other)
return str(it.absolute()).startswith(str(other.absolute()))
prefix = os.environ.get("DOUGAL_ROOT", os.environ.get("HOME", ".")+"/software") prefix = os.environ.get("DOUGAL_ROOT", os.environ.get("HOME", ".")+"/software")
DOUGAL_ROOT = os.environ.get("DOUGAL_ROOT", os.environ.get("HOME", ".")+"/software") DOUGAL_ROOT = os.environ.get("DOUGAL_ROOT", os.environ.get("HOME", ".")+"/software")
@@ -142,7 +154,7 @@ def untranslate_path (file):
if filepath.is_absolute(): if filepath.is_absolute():
if type(import_paths) == str: if type(import_paths) == str:
if filepath.is_relative_to(import_paths): if is_relative_to(filepath, import_paths):
physical_root = pathlib.Path("/") physical_root = pathlib.Path("/")
physical_prefix = pathlib.Path(import_paths) physical_prefix = pathlib.Path(import_paths)
return str(root.joinpath(filepath.relative_to(physical_prefix))) return str(root.joinpath(filepath.relative_to(physical_prefix)))
@@ -152,7 +164,7 @@ def untranslate_path (file):
for key, value in import_paths.items(): for key, value in import_paths.items():
value = dougal_root.joinpath(value) value = dougal_root.joinpath(value)
physical_prefix = pathlib.Path(value) physical_prefix = pathlib.Path(value)
if filepath.is_relative_to(physical_prefix): if is_relative_to(filepath, physical_prefix):
logical_prefix = physical_root.joinpath(pathlib.Path(key)).resolve() logical_prefix = physical_root.joinpath(pathlib.Path(key)).resolve()
return str(logical_prefix.joinpath(filepath.relative_to(physical_prefix))) return str(logical_prefix.joinpath(filepath.relative_to(physical_prefix)))