mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 18:22:37 +02:00
Use pathlib internally in dzutil
This commit is contained in:
parent
2da6ce02c8
commit
bbce13d9f8
1 changed files with 26 additions and 22 deletions
|
@ -19,29 +19,33 @@ def dev_mode() -> bool:
|
||||||
return hasattr(sys, "dangerzone_dev")
|
return hasattr(sys, "dangerzone_dev")
|
||||||
|
|
||||||
|
|
||||||
def get_resource_path(filename):
|
def _dev_root_path() -> pathlib.Path:
|
||||||
if getattr(sys, "dangerzone_dev", False):
|
""":returns: path to the project root (e.g., /home/user/dangerzone)"""
|
||||||
# Look for resources directory relative to python file
|
frame = inspect.currentframe()
|
||||||
prefix = os.path.join(
|
if frame is None:
|
||||||
os.path.dirname(
|
raise SystemError("This Python implementation is missing stack frame support.")
|
||||||
os.path.dirname(
|
frame_file = inspect.getfile(frame) # get the file which defined the current frame
|
||||||
os.path.abspath(inspect.getfile(inspect.currentframe()))
|
frame_path = pathlib.Path(frame_file) # concrete path to frame_file
|
||||||
)
|
frame_abspath = frame_path.resolve() # resolve any symlinks in frame_path
|
||||||
),
|
project_root = frame_abspath.parent.parent # grandparent directory of frame_abspath
|
||||||
"share",
|
return project_root
|
||||||
)
|
|
||||||
|
|
||||||
|
def get_resource_path(filename: str | os.PathLike[str]) -> str:
|
||||||
|
if dev_mode():
|
||||||
|
# Look for ./share relative to python file
|
||||||
|
prefix = _dev_root_path().joinpath("share") # e.g., /home/user/dangerzone/share
|
||||||
|
elif SYSTEM == "Darwin":
|
||||||
|
bin_path = pathlib.Path(sys.executable) # /path/to/Dangerzone.app/Contents/MacOS/dangerzone[-cli]
|
||||||
|
app_path = bin_path.parent.parent # /path/to/Dangerzone.app/Contents
|
||||||
|
prefix = app_path.joinpath("Resources", "share") # /path/to/Dangerzone.app/Contents/Resources/share
|
||||||
|
elif SYSTEM == "Linux":
|
||||||
|
prefix = pathlib.Path(sys.prefix).joinpath("share", "dangerzone")
|
||||||
|
elif SYSTEM == "Windows":
|
||||||
|
prefix = pathlib.Path(sys.executable).parent.joinpath("share")
|
||||||
else:
|
else:
|
||||||
if platform.system() == "Darwin":
|
raise NotImplementedError(f"Unsupported system {SYSTEM}")
|
||||||
prefix = os.path.join(
|
return str(prefix.joinpath(filename))
|
||||||
os.path.dirname(os.path.dirname(sys.executable)), "Resources/share"
|
|
||||||
)
|
|
||||||
elif platform.system() == "Linux":
|
|
||||||
prefix = os.path.join(sys.prefix, "share", "dangerzone")
|
|
||||||
else:
|
|
||||||
# Windows
|
|
||||||
prefix = os.path.join(os.path.dirname(sys.executable), "share")
|
|
||||||
resource_path = os.path.join(prefix, filename)
|
|
||||||
return resource_path
|
|
||||||
|
|
||||||
|
|
||||||
def get_subprocess_startupinfo():
|
def get_subprocess_startupinfo():
|
||||||
|
|
Loading…
Reference in a new issue