Skip to content

Commit

Permalink
Merge branch 'packages-root-variable'
Browse files Browse the repository at this point in the history
  • Loading branch information
palikar committed Jun 15, 2020
2 parents 0746754 + 494d740 commit 76b1af8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
56 changes: 53 additions & 3 deletions code_manager/core/configuration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import json
import logging
import os
import re

import requests

from code_manager.utils.utils import flatten
from code_manager.utils.utils import recursive_items
from code_manager.utils.utils import sanitize_input_variable


class CofigurationResolver:
Expand Down Expand Up @@ -121,6 +125,38 @@ def resolve_nodes(self, config):


class ConfigurationAware:

URL_REGEX = re.compile(r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))")

@staticmethod
def _load_extra_pack(primary_config, config):
primary_config.setdefault('vars', {})
for var, value in config.get('vars', {}).items():
primary_config['vars'][var] = value

primary_config.setdefault('packages_list', {})
for group_name, group_list in config.get('packages_list', {}).items():
primary_config['packages_list'].setdefault(group_name, group_list)

primary_config.setdefault('debian_packages', {})
for list_name, list_list in config.get('debian_packages', {}).items():
primary_config['debian_packages'].setdefault(list_name, list_list)

primary_config.setdefault('packages', {})
for pack_name, pack_node in config.get('packages', {}).items():
primary_config['packages'].setdefault(pack_name, pack_node)

return primary_config

@staticmethod
def _load_pack_form_link(link):
r = requests.get(link)
try:
config = json.loads(r.content)
except json.JSONDecodeError:
return None
return config

@staticmethod
def var(name):
if name in ConfigurationAware.resovler.variables.keys():
Expand All @@ -142,13 +178,27 @@ def variables():
@staticmethod
def set_configuration(config, install_scripts_dir, cache_file, opt, extra_configs=[]):

ConfigurationAware.config_dir = sanitize_input_variable('${HOME}/.config/code_manager/')
ConfigurationAware.usr_dir = sanitize_input_variable(opt['Config']['usr'])
ConfigurationAware.code_dir = sanitize_input_variable(opt['Config']['code'])

ConfigurationAware.opt = opt
ConfigurationAware.usr_dir = os.path.expandvars(opt['Config']['usr'])
ConfigurationAware.code_dir = os.path.expandvars(opt['Config']['code'])

ConfigurationAware.resolver = CofigurationResolver()

# TODO: merge the dicts here "somehow"
if extra_configs:
for pack in extra_configs:

if re.match(pack, ConfigurationAware.URL_REGEX):
con = ConfigurationAware._load_pack_form_link(pack)
elif os.path.exists(pack):
with open(pack) as config_file:
con = json.load(config_file)
else:
con = None

if con is not None:
config = ConfigurationAware._load_extra_pack(config, con)

ConfigurationAware.config = ConfigurationAware.resolver.configuration_dict(
config,
Expand Down
5 changes: 3 additions & 2 deletions code_manager/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from code_manager.core.installation import Installation
from code_manager.utils.utils import flatten

# TODO: extract each step in function per package


class Manager(ConfigurationAware):

Expand Down Expand Up @@ -279,3 +277,6 @@ def remove_package(self, packs):
cache.set_root(pack, '')
cache.set_built(pack, False)
cache.set_installed(pack, False)

def run_command(self, command, args):
pass
17 changes: 13 additions & 4 deletions code_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ def get_arg_parser():
dest='packages_file',
action='store',
help='File to read the packages from',
metavar='packages.json',
metavar='PACK_FILE',
)

parser.add_argument(
'--pack',
'-p',
dest='packs',
action='append',
default=[],
help='Extra packages.json files to load packages from.',
)

parser.add_argument(
Expand Down Expand Up @@ -195,7 +204,7 @@ def get_arg_parser():
parser_remove = subparsers.add_parser(
'remove',
description='Remove a package',
help='Remove a package and its sources from the code directory',
help='Remove a package and its sources',
)

parser_remove.add_argument(
Expand Down Expand Up @@ -490,7 +499,7 @@ def venv_check(args, opt):
def venv_setup(args, opt):

python_ver = promt('Python executable', 'python3')
env_root = promt('Python executable', sanitize_input_variable(opt['Config']['venv']))
env_root = promt('Virtual environment location', sanitize_input_variable(opt['Config']['venv']))
opt['Config']['venv'] = env_root

if not os.path.isdir(os.path.abspath(os.path.join(env_root, os.pardir))):
Expand Down Expand Up @@ -558,7 +567,7 @@ def main():

commands = get_commands_map()

ConfigurationAware.set_configuration(CONFIG, INSTALL_SCRIPTS_DIR, CACHE, opt)
ConfigurationAware.set_configuration(CONFIG, INSTALL_SCRIPTS_DIR, CACHE, opt, extra_configs=args.packs)

logging.debug('Code dir: %s', CODE_DIR)
logging.debug('Usr dir: %s', USR_DIR)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[metadata]
description-file = README.md


[pep8]
ignore=E111,E114,E501
max-line-length=100
Expand Down

0 comments on commit 76b1af8

Please sign in to comment.