1
1
import os
2
2
import shutil
3
- import sys
4
3
import unittest
5
4
from typing import List
6
5
7
6
import click
8
- import pkg_resources
9
7
10
8
from spaceone .core import config , pygrpc , fastapi , utils
11
9
from spaceone .core import scheduler as scheduler_v1
@@ -48,6 +46,7 @@ def grpc(package, port=None, config_file=None, module_path=None):
48
46
@click .option ('-m' , '--module-path' , type = click .Path (exists = True ), multiple = True , help = 'Module path' )
49
47
def rest (package , host = None , port = None , config_file = None , module_path = None ):
50
48
"""Run a FastAPI REST server"""
49
+ _set_environment (package , host , port , config_file , module_path )
51
50
_set_server_config (package , module_path , port , config_file = config_file )
52
51
fastapi .serve ()
53
52
@@ -107,35 +106,9 @@ def test(config_file=None, dir=None, failfast=False, scenario: str = None, param
107
106
RichTestRunner (verbosity = verbose , failfast = failfast ).run (full_suite )
108
107
109
108
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
-
136
109
def _set_server_config (package , module_path = None , port = None , config_file = None ):
137
110
# 1. Set a python path
138
- _set_python_path (package , module_path )
111
+ utils . set_python_path (package , module_path )
139
112
140
113
# 2. Initialize config from command argument
141
114
config .init_conf (
@@ -147,7 +120,8 @@ def _set_server_config(package, module_path=None, port=None, config_file=None):
147
120
config .set_service_config ()
148
121
149
122
# 4. Merge file conf
150
- _set_file_config (config_file )
123
+ if config_file :
124
+ config .set_file_conf (config_file )
151
125
152
126
153
127
def init_project_file (path , text ):
@@ -191,5 +165,13 @@ def _print_config(output):
191
165
print (utils .dump_yaml (data ))
192
166
193
167
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
+
194
176
if __name__ == '__main__' :
195
177
cli ()
0 commit comments