Skip to content

Commit

Permalink
Added custom variables (OSPARC_SIMCORE_REPO_ROOTDIR) for the config file
Browse files Browse the repository at this point in the history
Fixes config folder key
  • Loading branch information
Pedro Crespo committed Nov 13, 2018
1 parent f1a0a42 commit c034676
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 23 deletions.
26 changes: 25 additions & 1 deletion services/web/server/src/simcore_service_webserver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
"""
import argparse
import logging
import os
import sys

from .application import run_service
from .application_config import CLI_DEFAULT_CONFIGFILE, CONFIG_SCHEMA
from .cli_config import add_cli_options, config_from_options
from .utils import search_osparc_repo_dir

log = logging.getLogger(__name__)


def create_default_parser():
return argparse.ArgumentParser(description='Service to manage data storage in simcore.')


def setup_parser(parser):
""" Adds all options to a parser"""
#parser.add_argument('names', metavar='NAME', nargs=argparse.ZERO_OR_MORE,
Expand All @@ -37,14 +40,35 @@ def setup_parser(parser):

return parser


def create_environ():
"""
Build environment with substitutable variables
"""
# system's environment variables
environ = dict(os.environ)

# project-related environment variables
rootdir = search_osparc_repo_dir()
if rootdir is not None:
environ.update({
'OSPARC_SIMCORE_REPO_ROOTDIR': str(rootdir),
})

return environ



def parse(args, parser):
""" Parse options and returns a configuration object """
if args is None:
args = sys.argv[1:]

# ignore unknown options
options, _ = parser.parse_known_args(args)
config = config_from_options(options, CONFIG_SCHEMA)

config = config_from_options(options, CONFIG_SCHEMA, vars=create_environ())

# TODO: check whether extra options can be added to the config?!
return config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import trafaret_config
import trafaret_config.commandline as commandline

from .resources import resources, RSC_CONFIG_DIR_KEY
from .resources import resources

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -40,7 +40,7 @@ def config_from_options(options, schema, vars=None): # pylint: disable=W0622
if resources.exists(resource_name):
options.config = resources.get_path(resource_name)
else:
resource_name = RSC_CONFIG_DIR_KEY + '/' + resource_name
resource_name = resources.config_folder + '/' + resource_name
if resources.exists(resource_name):
options.config = resources.get_path(resource_name)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '1.0'
main:
client_outdir: ~/devp/osparc-simcore/services/web/client/source-output
client_outdir: ${OSPARC_SIMCORE_REPO_ROOTDIR}/services/web/client/source-output
host: 127.0.0.1
log_level: INFO
port: 8080
Expand Down Expand Up @@ -40,7 +40,7 @@ smtp:
# secret_key: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
rest:
version: v0
location: ~/devp/osparc-simcore/api/specs/webserver/v0/openapi.yaml
location: ${OSPARC_SIMCORE_REPO_ROOTDIR}/api/specs/webserver/v0/openapi.yaml
#location: http://localhost:8043/api/specs/webserver/v0/openapi.yaml
extra_urls:
- http://localhost:8080
13 changes: 0 additions & 13 deletions services/web/server/src/simcore_service_webserver/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,12 @@
"""
from servicelib.resources import ResourcesFacade

# RSC=resource
RSC_CONFIG_DIR_KEY = __name__ + ".config"

resources = ResourcesFacade(
package_name=__name__,
distribution_name="simcore-service-webserver",
config_folder='config',
)


__all__ = (
'resources',
'RSC_CONFIG_DIR_KEY',
)


#TODO: from servicelib import resources

# resources names
# TODO: import all RSC_* within .settings.constants!
#RESOURCE_OPENAPI = "oas3"
#RESOURCE_CONFIG = "config"
3 changes: 1 addition & 2 deletions services/web/server/src/simcore_service_webserver/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_server(servers, url):

#-----------------------


def setup(app: web.Application, *, debug=False):
log.debug("Setting up %s ...", __name__)

Expand All @@ -42,8 +43,6 @@ def setup(app: web.Application, *, debug=False):
#specs = await create_openapi_specs(location=cfg["location"])
loop = asyncio.get_event_loop()
location = cfg["location"]

# FIXME: resolve if location is relative location path
specs = loop.run_until_complete( create_openapi_specs(location) )

# sets servers variables to current server's config
Expand Down
27 changes: 27 additions & 0 deletions services/web/server/src/simcore_service_webserver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,39 @@
"""
import os
import sys
from pathlib import Path

from typing import Iterable, List

from aiohttp.web import HTTPFound


CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent

def is_osparc_repo_dir(path: Path) -> bool:
# TODO: implement with git cli
expected = (".github", "packages", "services")
got = [p.name for p in path.iterdir() if p.is_dir()]
return all(d in got for d in expected)

def search_osparc_repo_dir():
""" Returns path to root repo dir or None
NOTE: assumes this file within repo, i.e. only happens in edit mode!
"""
MAX_ITERATIONS = 8
root_dir = CURRENT_DIR
if "services/web/server" in str(root_dir):
it = 1
while not is_osparc_repo_dir(root_dir) and it<MAX_ITERATIONS:
root_dir = root_dir.parent
it += 1

if is_osparc_repo_dir(root_dir):
return root_dir
return None


def as_list(obj) -> List:
if isinstance(obj, Iterable):
return list(obj)
Expand Down
5 changes: 2 additions & 3 deletions services/web/server/tests/unit/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import pytest

# under test
from simcore_service_webserver.resources import (RSC_CONFIG_DIR_KEY,
resources)
from simcore_service_webserver.resources import resources

log = logging.getLogger(__name__)

@pytest.fixture
def app_resources(package_dir):
resource_names = []
base = package_dir
for name in (RSC_CONFIG_DIR_KEY,):
for name in (resources.config_folder,):
folder = base / name
resource_names += [ str(p.relative_to(base)) for p in folder.rglob("*.y*ml") ]

Expand Down

0 comments on commit c034676

Please sign in to comment.