11import os
22import shutil
3- import sys
43import unittest
54from typing import List
65
76import click
8- import pkg_resources
97
108from spaceone .core import config , pygrpc , fastapi , utils
119from spaceone .core import scheduler as scheduler_v1
@@ -48,6 +46,7 @@ def grpc(package, port=None, config_file=None, module_path=None):
4846@click .option ('-m' , '--module-path' , type = click .Path (exists = True ), multiple = True , help = 'Module path' )
4947def rest (package , host = None , port = None , config_file = None , module_path = None ):
5048 """Run a FastAPI REST server"""
49+ _set_environment (package , host , port , config_file , module_path )
5150 _set_server_config (package , module_path , port , config_file = config_file )
5251 fastapi .serve ()
5352
@@ -107,35 +106,9 @@ def test(config_file=None, dir=None, failfast=False, scenario: str = None, param
107106 RichTestRunner (verbosity = verbose , failfast = failfast ).run (full_suite )
108107
109108
110- def _set_file_config (conf_file ):
111- if conf_file :
112- config .set_file_conf (conf_file )
113-
114-
115- def _set_python_path (package , module_path ):
116- current_path = os .getcwd ()
117-
118- if current_path not in sys .path :
119- sys .path .insert (0 , current_path )
120-
121- if isinstance (module_path , tuple ):
122- for path in module_path :
123- if path not in sys .path :
124- sys .path .insert (0 , path )
125-
126- if '.' in package :
127- pkg_resources .declare_namespace (package )
128-
129- try :
130- __import__ (package )
131- except Exception :
132- raise Exception (f'The package({ package } ) can not imported. '
133- 'Please check the module path.' )
134-
135-
136109def _set_server_config (package , module_path = None , port = None , config_file = None ):
137110 # 1. Set a python path
138- _set_python_path (package , module_path )
111+ utils . set_python_path (package , module_path )
139112
140113 # 2. Initialize config from command argument
141114 config .init_conf (
@@ -147,7 +120,8 @@ def _set_server_config(package, module_path=None, port=None, config_file=None):
147120 config .set_service_config ()
148121
149122 # 4. Merge file conf
150- _set_file_config (config_file )
123+ if config_file :
124+ config .set_file_conf (config_file )
151125
152126
153127def init_project_file (path , text ):
@@ -191,5 +165,13 @@ def _print_config(output):
191165 print (utils .dump_yaml (data ))
192166
193167
168+ def _set_environment (package , host , port , config_file , module_path ):
169+ os .environ ['SPACEONE_PACKAGE' ] = package
170+ os .environ ['SPACEONE_HOST' ] = host
171+ os .environ ['SPACEONE_MODULE_PATH' ] = ', ' .join (module_path )
172+ os .environ ['SPACEONE_PORT' ] = str (port )
173+ os .environ ['SPACEONE_CONFIG_FILE' ] = config_file
174+
175+
194176if __name__ == '__main__' :
195177 cli ()
0 commit comments