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

Commit

Permalink
#65: Implement rkd.env module
Browse files Browse the repository at this point in the history
@todo: Unit tests
  • Loading branch information
blackandred committed Jan 11, 2021
1 parent 7e5c99c commit 63db29b
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 25 deletions.
4 changes: 2 additions & 2 deletions rkd/api/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
Core interfaces that should be changed WITH CAREFUL as those are parts of API.
Any breaking change there requires to bump RKD major version (see: Semantic Versioning)
"""


from tabulate import tabulate
from abc import abstractmethod, ABC as AbstractClass
from typing import Dict, List, Union, Optional
Expand All @@ -19,6 +17,7 @@
from ..exception import EnvironmentVariableNameNotAllowed
from ..taskutil import TaskUtilities
from .temp import TempManager
from .inputoutput import SystemIO


def env_to_switch(env_name: str) -> str:
Expand Down Expand Up @@ -118,6 +117,7 @@ def format_task_name(self, name: str) -> str:

class ContextInterface(AbstractClass):
directories: []
io: SystemIO

@abstractmethod
def merge(cls, first, second):
Expand Down
6 changes: 4 additions & 2 deletions rkd/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
Allows to keep activity logs for future analysis
"""

import os
import re

from rkd import env
from .api.syntax import TaskDeclaration
from .context import ApplicationContext



def decide_about_target_log_files(ctx: ApplicationContext, log_to_file: str, declaration: TaskDeclaration,
task_num: int):
"""Decides where to save logs"""

log_files = []
session_log: bool = os.getenv('RKD_AUDIT_SESSION_LOG', '').lower() in ['true', '1', 'yes']
session_log: bool = env.audit_session_log_enabled()

if log_to_file:
log_files.append(log_to_file)
Expand Down
5 changes: 3 additions & 2 deletions rkd/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .api.inputoutput import UnbufferedStdout
from .aliasgroups import parse_alias_groups_from_env
from .packaging import find_resource_file
from rkd import env


class RiotKitDoApplication(object):
Expand All @@ -29,7 +30,7 @@ class RiotKitDoApplication(object):

@staticmethod
def load_environment():
paths = os.getenv('RKD_PATH', '').split(':')
paths = env.rkd_paths()

for path in paths:
if os.path.isfile(path + '/.env'):
Expand All @@ -54,7 +55,7 @@ def main(self, argv: list):
# system wide IO instance with defaults, the :init task should override those settings
io = SystemIO()
io.silent = True
io.set_log_level(os.getenv('RKD_SYS_LOG_LEVEL', 'info'))
io.set_log_level(env.system_log_level())

# preparse arguments that are before tasks
preparsed_args = CommandlineParsingHelper.preparse_args(argv)
Expand Down
5 changes: 3 additions & 2 deletions rkd/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from typing import Dict, List, Union, Tuple
from importlib.machinery import SourceFileLoader
from traceback import print_exc

from rkd import env
from .api.syntax import TaskDeclaration
from .api.syntax import TaskAliasDeclaration
from .api.syntax import GroupDeclaration
Expand Down Expand Up @@ -262,8 +264,7 @@ def create_unified_context(self, chdir: str = '', additional_imports: List[str]
if chdir:
paths += chdir + '/.rkd'

if os.getenv('RKD_PATH'):
paths += os.getenv('RKD_PATH', '').split(':')
paths += env.rkd_paths()

# export for usage inside in makefiles
os.environ['RKD_PATH'] = ":".join(paths)
Expand Down
41 changes: 41 additions & 0 deletions rkd/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Centralized place for environment variables supported internally by RKD
=======================================================================
Idea is to make RKD core code clean of os.getenv() calls as much as possible.
The advantage is unified list of environment variables and always the same default values.
"""

import os
from typing import List

STR_BOOLEAN_TRUE = ['true', '1', 'yes']


def rkd_paths() -> List[str]:
return os.getenv('RKD_PATH', '').split(':')


def rkd_depth() -> int:
return int(os.getenv('RKD_DEPTH', 0))


def binary_name() -> str:
return os.getenv('RKD_BIN')


def distribution_name() -> str:
return os.getenv('RKD_DIST_NAME', 'rkd')


def audit_session_log_enabled() -> bool:
return os.getenv('RKD_AUDIT_SESSION_LOG', '').lower() in STR_BOOLEAN_TRUE


def system_log_level() -> str:
return os.getenv('RKD_SYS_LOG_LEVEL', 'info')


def is_subprocess_compat_mode() -> bool:
return os.getenv('RKD_COMPAT_SUBPROCESS', '').lower() in STR_BOOLEAN_TRUE
3 changes: 2 additions & 1 deletion rkd/execution/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import List
from ..api.contract import TaskInterface
from ..api.contract import ExecutionContext
from rkd import env


Step = namedtuple('Step', ['language', 'code', 'task_name', 'rkd_path', 'envs', 'task_num'])
Expand Down Expand Up @@ -123,7 +124,7 @@ def execute_bash_step(ctx: ExecutionContext, this: TaskInterface, step: Step) ->
process_env = OrderedDict()
process_env.update(step.envs)
process_env.update(args)
process_env.update({'RKD_PATH': step.rkd_path, 'RKD_DEPTH': int(os.getenv('RKD_DEPTH', 0))})
process_env.update({'RKD_PATH': step.rkd_path, 'RKD_DEPTH': env.rkd_depth()})

this.sh(step.code, strict=True, env=process_env)
return True
Expand Down
11 changes: 7 additions & 4 deletions rkd/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
from distutils.sysconfig import get_python_lib
from typing import List, Optional, Callable
from . import env


def find_resource_directory(path: str) -> Optional[str]:
Expand All @@ -28,18 +29,20 @@ def get_possible_paths(path: str) -> List[str]:
"""

# <sphinx_resources-get_possible_paths>
dist_name = env.distribution_name() # RKD_DIST_NAME env variable

paths = [
# eg. ~/.local/share/rkd/banner.txt
os.path.expanduser('~/.local/share/rkd/' + path),
os.path.expanduser(('~/.local/share/%s/' + path) % dist_name),

# eg. /home/andrew/.local/lib/python3.8/site-packages/rkd/misc/banner.txt
get_user_site_packages() + '/rkd/misc/' + path,
(get_user_site_packages() + '/%s/misc/' + path) % dist_name,

# eg. /usr/lib/python3.8/site-packages/rkd/misc/banner.txt
_get_global_site_packages() + '/rkd/misc/' + path,
(_get_global_site_packages() + '/%s/misc/' + path) % dist_name,

# eg. /usr/share/rkd/banner.txt
'/usr/share/rkd/' + path
('/usr/share/%s/' + path) % dist_name
]
# </sphinx_resources-get_possible_paths>

Expand Down
3 changes: 2 additions & 1 deletion rkd/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from typing import Union
from threading import Thread
from time import time
from rkd import env as rkd_env

ON_POSIX = 'posix' in sys.builtin_module_names

Expand Down Expand Up @@ -75,7 +76,7 @@ def check_call(command: str, script_to_show: Optional[str] = '', use_subprocess:
:return:
"""

if os.getenv('RKD_COMPAT_SUBPROCESS') == 'true' or use_subprocess:
if rkd_env.is_subprocess_compat_mode() or use_subprocess:
subprocess.check_call(command, shell=True)
return

Expand Down
7 changes: 3 additions & 4 deletions rkd/standardlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from ..api.contract import TaskDeclarationInterface
from ..api.contract import ArgparseArgument
from ..api.syntax import TaskDeclaration
from ..inputoutput import SystemIO
from ..inputoutput import clear_formatting
from ..aliasgroups import parse_alias_groups_from_env, AliasGroup
from .shell import ShellCommandTask
from ..packaging import find_resource_directory
from rkd import env


class InitTask(TaskInterface):
Expand Down Expand Up @@ -65,9 +65,8 @@ def execute(self, context: ExecutionContext) -> bool:
"""

# increment RKD_DEPTH
os.environ['RKD_DEPTH'] = str(int(os.getenv('RKD_DEPTH', '0')) + 1)
os.environ['RKD_DEPTH'] = str(env.rkd_depth() + 1)

self._ctx.io # type: SystemIO
self._ctx.io.silent = context.args['silent']

# log level is optional to be set
Expand All @@ -77,7 +76,7 @@ def execute(self, context: ExecutionContext) -> bool:
if context.get_env('RKD_UI'):
self._ctx.io.set_display_ui(context.get_env('RKD_UI').lower() == 'true')

if int(os.getenv('RKD_DEPTH')) >= 2 or context.args['no_ui']:
if env.rkd_depth() >= 2 or context.args['no_ui']:
self._ctx.io.set_display_ui(False)

return True
Expand Down
5 changes: 3 additions & 2 deletions rkd/taskutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from copy import deepcopy
from .api.inputoutput import IO
from .process import check_call
from rkd import env


class TaskUtilities(AbstractClass):
Expand Down Expand Up @@ -39,8 +40,8 @@ def silent_sh(self, cmd: str, verbose: bool = False, strict: bool = True,
def get_rkd_binary():
"""Gets the command how RKD was launched"""

if os.getenv('RKD_BIN'):
return os.getenv('RKD_BIN')
if env.binary_name():
return env.binary_name()

binary = sys.argv[0]
sys_executable_basename = os.path.basename(sys.executable)
Expand Down
18 changes: 13 additions & 5 deletions rkd/yaml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from jsonschema import validate
from jsonschema import ValidationError
from jsonschema import draft7_format_checker
from . import env
from .exception import YAMLFileValidationError
from .packaging import get_user_site_packages

Expand Down Expand Up @@ -70,6 +71,9 @@ def find_path_by_name(self, filename: str, subdir: str) -> str:
"""Find schema in one of RKD directories or in current path
"""

if "/" in filename and os.path.isfile(filename):
return filename

for path in self.get_lookup_paths(subdir):
file_path = path + '/' + filename

Expand All @@ -79,8 +83,12 @@ def find_path_by_name(self, filename: str, subdir: str) -> str:
return ''

def get_lookup_paths(self, subdirectory: str) -> List[str]:
paths = [os.getcwd(), os.getcwd() + '/' + subdirectory, os.getcwd() + '/.rkd/' + subdirectory]
global_paths = os.getenv('RKD_PATH', '').split(':')
paths = [
os.getcwd(),
os.getcwd() + '/' + subdirectory,
(os.getcwd() + '/.%s/' + subdirectory) % env.distribution_name()
]
global_paths = env.rkd_paths()
global_paths.reverse()

for path in self.paths:
Expand All @@ -90,8 +98,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')
paths.append(get_user_site_packages() + '/usr/share/%s/internal' % env.distribution_name())
paths.append((get_user_site_packages() + '/usr/share/%s/internal/' + subdirectory) % env.distribution_name())
paths.append('/usr/share/%s/internal' % env.distribution_name())

return list(dict.fromkeys(paths))

0 comments on commit 63db29b

Please sign in to comment.