Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
#54: Consider looking for resources in package, venv, user and global…
Browse files Browse the repository at this point in the history
… directories
  • Loading branch information
blackandred committed Nov 26, 2020
1 parent 4803c4c commit 791d477
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
3 changes: 2 additions & 1 deletion rkd/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .api.inputoutput import SystemIO
from .api.inputoutput import UnbufferedStdout
from .aliasgroups import parse_alias_groups_from_env
from .packaging import find_resource_file


class RiotKitDoApplication(object):
Expand Down Expand Up @@ -91,7 +92,7 @@ def main(self, argv: list):

@staticmethod
def print_banner_and_exit():
with open(os.path.dirname(os.path.realpath(__file__)) + '/misc/banner.txt', 'rb') as banner_file:
with open(find_resource_file('banner.txt'), 'rb') as banner_file:
print(banner_file.read().replace(b'\\x1B', b'\x1B').decode('utf-8'))

sys.exit(0)
Expand Down
2 changes: 2 additions & 0 deletions rkd/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .exception import PythonContextFileNotFoundException
from .exception import NotImportedClassException
from .exception import ContextException
from .packaging import get_user_site_packages
from .yaml_context import YamlSyntaxInterpreter
from .yaml_parser import YamlFileLoader

Expand Down Expand Up @@ -251,6 +252,7 @@ def create_unified_context(self, chdir: str = '', additional_imports: List[str]

paths = [
CURRENT_SCRIPT_PATH + '/misc/internal',
get_user_site_packages() + '/usr/share/rkd/internal',
'/usr/lib/rkd',
'/usr/share/rkd/internal',
os.path.expanduser('~/.rkd'),
Expand Down
64 changes: 64 additions & 0 deletions rkd/packaging.py
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()

6 changes: 2 additions & 4 deletions rkd/standardlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ..inputoutput import clear_formatting
from ..aliasgroups import parse_alias_groups_from_env, AliasGroup
from .shell import ShellCommandTask
from ..packaging import find_resource_directory


class InitTask(TaskInterface):
Expand Down Expand Up @@ -446,10 +447,7 @@ def execute(self, ctx: ExecutionContext) -> bool:
'please commit them or stash first')
return False

template_structure_path = os.path.dirname(os.path.realpath(__file__)) + '/../misc/initial-structure'

if not os.path.isdir(template_structure_path):
template_structure_path = '/usr/lib/rkd/initial-structure/'
template_structure_path = find_resource_directory('initial-structure')

self.on_startup(ctx)

Expand Down
3 changes: 3 additions & 0 deletions rkd/yaml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from jsonschema import ValidationError
from jsonschema import draft7_format_checker
from .exception import YAMLFileValidationError
from .packaging import get_user_site_packages

CURRENT_SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))

Expand Down Expand Up @@ -89,6 +90,8 @@ def get_lookup_paths(self, subdirectory: str) -> List[str]:
paths.append(path + '/' + subdirectory)

paths.append(CURRENT_SCRIPT_PATH + '/misc/internal/' + subdirectory)
paths.append(get_user_site_packages() + '/usr/share/rkd/internal')
paths.append(get_user_site_packages() + '/usr/share/rkd/internal/' + subdirectory)
paths.append('/usr/share/rkd/internal')

return list(dict.fromkeys(paths))
5 changes: 4 additions & 1 deletion test/test_standardlib_jinja_file_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class TestFileRendererTask(BasicTestingCase):
"""

@staticmethod
def _execute_mocked_task(params: dict, envs: dict = {}) -> BufferedSystemIO:
def _execute_mocked_task(params: dict, envs: dict = None) -> BufferedSystemIO:
if envs is None:
envs = {}

io = BufferedSystemIO()

task: FileRendererTask = FileRendererTask()
Expand Down

0 comments on commit 791d477

Please sign in to comment.