Skip to content

Commit c034676

Browse files
author
Pedro Crespo
committed
Added custom variables (OSPARC_SIMCORE_REPO_ROOTDIR) for the config file
Fixes config folder key
1 parent f1a0a42 commit c034676

File tree

7 files changed

+59
-23
lines changed

7 files changed

+59
-23
lines changed

services/web/server/src/simcore_service_webserver/cli.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
"""
1515
import argparse
1616
import logging
17+
import os
1718
import sys
1819

1920
from .application import run_service
2021
from .application_config import CLI_DEFAULT_CONFIGFILE, CONFIG_SCHEMA
2122
from .cli_config import add_cli_options, config_from_options
23+
from .utils import search_osparc_repo_dir
2224

2325
log = logging.getLogger(__name__)
2426

2527

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

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

3841
return parser
3942

43+
44+
def create_environ():
45+
"""
46+
Build environment with substitutable variables
47+
48+
"""
49+
# system's environment variables
50+
environ = dict(os.environ)
51+
52+
# project-related environment variables
53+
rootdir = search_osparc_repo_dir()
54+
if rootdir is not None:
55+
environ.update({
56+
'OSPARC_SIMCORE_REPO_ROOTDIR': str(rootdir),
57+
})
58+
59+
return environ
60+
61+
62+
4063
def parse(args, parser):
4164
""" Parse options and returns a configuration object """
4265
if args is None:
4366
args = sys.argv[1:]
4467

4568
# ignore unknown options
4669
options, _ = parser.parse_known_args(args)
47-
config = config_from_options(options, CONFIG_SCHEMA)
70+
71+
config = config_from_options(options, CONFIG_SCHEMA, vars=create_environ())
4872

4973
# TODO: check whether extra options can be added to the config?!
5074
return config

services/web/server/src/simcore_service_webserver/cli_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import trafaret_config
77
import trafaret_config.commandline as commandline
88

9-
from .resources import resources, RSC_CONFIG_DIR_KEY
9+
from .resources import resources
1010

1111
log = logging.getLogger(__name__)
1212

@@ -40,7 +40,7 @@ def config_from_options(options, schema, vars=None): # pylint: disable=W0622
4040
if resources.exists(resource_name):
4141
options.config = resources.get_path(resource_name)
4242
else:
43-
resource_name = RSC_CONFIG_DIR_KEY + '/' + resource_name
43+
resource_name = resources.config_folder + '/' + resource_name
4444
if resources.exists(resource_name):
4545
options.config = resources.get_path(resource_name)
4646

services/web/server/src/simcore_service_webserver/config/host-dev-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '1.0'
22
main:
3-
client_outdir: ~/devp/osparc-simcore/services/web/client/source-output
3+
client_outdir: ${OSPARC_SIMCORE_REPO_ROOTDIR}/services/web/client/source-output
44
host: 127.0.0.1
55
log_level: INFO
66
port: 8080
@@ -40,7 +40,7 @@ smtp:
4040
# secret_key: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
4141
rest:
4242
version: v0
43-
location: ~/devp/osparc-simcore/api/specs/webserver/v0/openapi.yaml
43+
location: ${OSPARC_SIMCORE_REPO_ROOTDIR}/api/specs/webserver/v0/openapi.yaml
4444
#location: http://localhost:8043/api/specs/webserver/v0/openapi.yaml
4545
extra_urls:
4646
- http://localhost:8080

services/web/server/src/simcore_service_webserver/resources.py

-13
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,12 @@
33
"""
44
from servicelib.resources import ResourcesFacade
55

6-
# RSC=resource
7-
RSC_CONFIG_DIR_KEY = __name__ + ".config"
8-
96
resources = ResourcesFacade(
107
package_name=__name__,
118
distribution_name="simcore-service-webserver",
129
config_folder='config',
1310
)
1411

15-
1612
__all__ = (
1713
'resources',
18-
'RSC_CONFIG_DIR_KEY',
1914
)
20-
21-
22-
#TODO: from servicelib import resources
23-
24-
# resources names
25-
# TODO: import all RSC_* within .settings.constants!
26-
#RESOURCE_OPENAPI = "oas3"
27-
#RESOURCE_CONFIG = "config"

services/web/server/src/simcore_service_webserver/rest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def get_server(servers, url):
3232

3333
#-----------------------
3434

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

@@ -42,8 +43,6 @@ def setup(app: web.Application, *, debug=False):
4243
#specs = await create_openapi_specs(location=cfg["location"])
4344
loop = asyncio.get_event_loop()
4445
location = cfg["location"]
45-
46-
# FIXME: resolve if location is relative location path
4746
specs = loop.run_until_complete( create_openapi_specs(location) )
4847

4948
# sets servers variables to current server's config

services/web/server/src/simcore_service_webserver/utils.py

+27
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,39 @@
33
"""
44
import os
55
import sys
6+
from pathlib import Path
67

78
from typing import Iterable, List
89

910
from aiohttp.web import HTTPFound
1011

1112

13+
CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent
14+
15+
def is_osparc_repo_dir(path: Path) -> bool:
16+
# TODO: implement with git cli
17+
expected = (".github", "packages", "services")
18+
got = [p.name for p in path.iterdir() if p.is_dir()]
19+
return all(d in got for d in expected)
20+
21+
def search_osparc_repo_dir():
22+
""" Returns path to root repo dir or None
23+
24+
NOTE: assumes this file within repo, i.e. only happens in edit mode!
25+
"""
26+
MAX_ITERATIONS = 8
27+
root_dir = CURRENT_DIR
28+
if "services/web/server" in str(root_dir):
29+
it = 1
30+
while not is_osparc_repo_dir(root_dir) and it<MAX_ITERATIONS:
31+
root_dir = root_dir.parent
32+
it += 1
33+
34+
if is_osparc_repo_dir(root_dir):
35+
return root_dir
36+
return None
37+
38+
1239
def as_list(obj) -> List:
1340
if isinstance(obj, Iterable):
1441
return list(obj)

services/web/server/tests/unit/test_resources.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
import pytest
99

1010
# under test
11-
from simcore_service_webserver.resources import (RSC_CONFIG_DIR_KEY,
12-
resources)
11+
from simcore_service_webserver.resources import resources
1312

1413
log = logging.getLogger(__name__)
1514

1615
@pytest.fixture
1716
def app_resources(package_dir):
1817
resource_names = []
1918
base = package_dir
20-
for name in (RSC_CONFIG_DIR_KEY,):
19+
for name in (resources.config_folder,):
2120
folder = base / name
2221
resource_names += [ str(p.relative_to(base)) for p in folder.rglob("*.y*ml") ]
2322

0 commit comments

Comments
 (0)