1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
23 from .config import config
24 from .logger import Logging
25 from .. import (application_model_storage, application_resource_storage)
26 from ..orchestrator.plugin import PluginManager
27 from ..storage.sql_mapi import SQLAlchemyModelAPI
28 from ..storage.filesystem_rapi import FileSystemResourceAPI
31 ARIA_DEFAULT_WORKDIR_NAME = '.aria'
34 class _Environment(object):
36 def __init__(self, workdir):
38 self._workdir = workdir
41 self._config = config.CliConfig.create_config(workdir)
42 self._logging = Logging(self._config)
44 self._model_storage_dir = os.path.join(workdir, 'models')
45 self._resource_storage_dir = os.path.join(workdir, 'resources')
46 self._plugins_dir = os.path.join(workdir, 'plugins')
49 self._model_storage = None
50 self._resource_storage = None
51 self._plugin_manager = None
66 def model_storage(self):
67 if not self._model_storage:
68 self._model_storage = self._init_sqlite_model_storage()
69 return self._model_storage
72 def resource_storage(self):
73 if not self._resource_storage:
74 self._resource_storage = self._init_fs_resource_storage()
75 return self._resource_storage
78 def plugin_manager(self):
79 if not self._plugin_manager:
80 self._plugin_manager = self._init_plugin_manager()
81 return self._plugin_manager
83 def reset(self, reset_config):
85 shutil.rmtree(self._workdir)
87 _, dirs, files = next(os.walk(self._workdir))
88 files.remove(config.CONFIG_FILE_NAME)
91 shutil.rmtree(os.path.join(self._workdir, dir_))
93 os.remove(os.path.join(self._workdir, file_))
95 def _init_workdir(self):
96 if not os.path.exists(self._workdir):
97 os.makedirs(self._workdir)
99 def _init_sqlite_model_storage(self):
100 if not os.path.exists(self._model_storage_dir):
101 os.makedirs(self._model_storage_dir)
103 initiator_kwargs = dict(base_dir=self._model_storage_dir)
104 return application_model_storage(
106 initiator_kwargs=initiator_kwargs)
108 def _init_fs_resource_storage(self):
109 if not os.path.exists(self._resource_storage_dir):
110 os.makedirs(self._resource_storage_dir)
112 fs_kwargs = dict(directory=self._resource_storage_dir)
113 return application_resource_storage(
114 FileSystemResourceAPI,
115 api_kwargs=fs_kwargs)
117 def _init_plugin_manager(self):
118 if not os.path.exists(self._plugins_dir):
119 os.makedirs(self._plugins_dir)
121 return PluginManager(self.model_storage, self._plugins_dir)
124 env = _Environment(os.path.join(
125 os.environ.get('ARIA_WORKDIR', os.path.expanduser('~')), ARIA_DEFAULT_WORKDIR_NAME))
127 logger = env.logging.logger