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

Commit

Permalink
#64: Fix task that create structure - usage of wrapper was invalid, i…
Browse files Browse the repository at this point in the history
…mprove error handling, add debug mode, improve docs, add virtualenv as dependency
  • Loading branch information
blackandred committed Jun 24, 2021
1 parent 2756653 commit 04d33f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/core/requirements-external.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ python-dotenv==0.13.*
tabulate==0.8.*
setuptools>=45.0.0
riotkit.pbs >= 1.0
virtualenv >= 20

42 changes: 31 additions & 11 deletions src/core/rkd/core/misc/initial-structure/rkdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@
Bootstraps a virtual environment transparently and starts RKD.
Environment:
ENVIRONMENT_TYPE: venv, pipenv or auto
VENV_CREATION_ARGS: Additional arguments for pipenv or virtualenv
WRAPPER_MODULE: Name of the python module to proxy (defaults to 'rkd.core')
PYTHON_BIN: Python binary path or command name (defaults to 'python')
LOCK_CACHE_TIME: How long to cache the .venv-lock (defaults to '3600'), when expired will again calculate checksums.
If checksums will differ then virtualenv will be recreated
IS_DEBUG: Debug mode, set to "1" to enable
</docs>
Designed and developed by Riotkit, an anarchist tech-collective supporting various grassroot movements, including
anarchosyndicalist workers unions like IWA-AIT, tenants rights organizations, anti-repression Anarchist Black Cross and
Food Not Bombs.
:url: https://github.com/riotkit-org/riotkit-do
:license: Apache-2
:author:
"""

Expand All @@ -26,6 +37,7 @@
WRAPPER_MODULE = os.getenv('RKD_ENV_WRAPPER_MODULE', 'rkd.core')
PYTHON_BIN = os.getenv('RKD_ENV_PYTHON_BIN', 'python')
LOCK_CACHE_TIME = int(os.getenv('RKD_ENV_LOCK_TIME', 3600))
IS_DEBUG = os.getenv('RKD_ENV_DEBUG') == '1'

CALL_ARGS = [PYTHON_BIN, '-m', WRAPPER_MODULE] + sys.argv[1:]
VENV_PATH = '.venv'
Expand Down Expand Up @@ -115,16 +127,20 @@ def create(self) -> None:
for requirement_file in requirements:
args += f" -r {requirement_file} "

subprocess.check_call(f'{PYTHON_BIN} -m virtualenv {VENV_CREATION_ARGS} {VENV_PATH}', shell=True)
subprocess.check_call(f'''
source "{VENV_PATH}/bin/activate";
pip install {args}
''')
/bin/bash -c 'set -e; pwd; find ./; source "{VENV_PATH}/bin/activate"; pip install {args}'
''', shell=True)

@staticmethod
def get_name() -> str:
return 'venv'


def debug(msg: str) -> None:
print(f'DEBUG >> {msg}')


def lock_exists() -> bool:
return os.path.isfile(LOCK_PATH)

Expand Down Expand Up @@ -176,17 +192,21 @@ def main():
os.putenv('PYTHONPATH', os.getenv('PYTHONPATH', '') + f':{cwd}/../process:{cwd}/../pythonic')

if lock_cache_is_outdated():
debug('Lock file outdated')
checksum = environment.calculate_checksum()
write_lock(checksum)

if should_create_virtualenv(checksum):
debug('Creating virtual environment')
environment.create()

try:
environment.wrap()
except subprocess.CalledProcessError as err:
sys.exit(err.returncode)
write_lock(checksum)

debug('Running command')
environment.wrap()


if __name__ == '__main__':
main()
try:
main()
except subprocess.CalledProcessError as err:
sys.exit(err.returncode)
2 changes: 1 addition & 1 deletion src/core/rkd/core/standardlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def _setup_venv(self, use_pipenv: bool, template_structure_path: str, use_latest

self.sh('cp %s/rkdw.py ./rkdw' % template_structure_path)
self.sh('chmod +x ./rkdw')
self.sh('./rkdw')
self.sh('./rkdw :init', env={'ENVIRONMENT_TYPE': 'pipenv' if use_pipenv else 'venv'})

@staticmethod
def _get_development_pipenv_install_str(dev_dir: str):
Expand Down

0 comments on commit 04d33f5

Please sign in to comment.