This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#54: Consider looking for resources in package, venv, user and global…
… directories
- Loading branch information
1 parent
4803c4c
commit 791d477
Showing
6 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Packaging | ||
========= | ||
Utilities related to finding resources of installed RKD | ||
""" | ||
|
||
import os | ||
import sys | ||
from distutils.sysconfig import get_python_lib | ||
from typing import List, Optional, Callable | ||
|
||
|
||
def find_resource_directory(path: str, additional_paths: list = None) -> Optional[str]: | ||
return _find(path, os.path.isdir) | ||
|
||
|
||
def find_resource_file(path: str) -> Optional[str]: | ||
return _find(path, os.path.isfile) | ||
|
||
|
||
def get_possible_paths(path: str) -> List[str]: | ||
""" | ||
Finds possible paths to resources, considering PACKAGE and USER directories first, then system-wide directories | ||
:param path: | ||
:return: | ||
""" | ||
|
||
return [ | ||
# eg. ./rkd/misc/banner.txt | ||
os.path.dirname(os.path.realpath(__file__)) + '/misc/' + path, | ||
|
||
# eg. /home/andrew/.local/lib/python3.8/site-packages/usr/share/rkd/banner.txt | ||
get_user_site_packages() + '/usr/share/rkd/' + path, | ||
|
||
# eg. /usr/lib/python3.8/site-packages/usr/share/rkd/banner.txt | ||
_get_global_site_packages() + '/usr/share/rkd/' + path, | ||
|
||
# eg. /usr/share/rkd/banner.txt | ||
'/usr/share/rkd/' + path | ||
] | ||
|
||
|
||
def get_user_site_packages() -> str: | ||
""" | ||
Finds a user or venv site-packages directory | ||
:return: | ||
""" | ||
|
||
return next(p for p in sys.path if 'site-packages' in p) | ||
|
||
|
||
def _find(path: str, method: Callable) -> Optional[str]: | ||
for checked_path in get_possible_paths(path): | ||
if method(checked_path): | ||
return checked_path | ||
|
||
return None | ||
|
||
|
||
def _get_global_site_packages() -> str: | ||
return get_python_lib() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters