|
1 | 1 | import os |
2 | | -import importlib |
3 | | -import json |
4 | | -import subprocess |
5 | | -import sys |
6 | 2 | from pathlib import Path |
7 | | - |
8 | 3 | import uvicorn |
9 | | -import yaml |
10 | 4 | from fastapi import FastAPI, Header, HTTPException, Request |
11 | 5 | from fastapi.openapi.docs import get_swagger_ui_html |
12 | | - |
13 | 6 | from python_activator.loader import load_packages |
14 | | - |
15 | | - |
16 | | -class knowledge_object: |
17 | | - def __init__(self, name, status, function, id, url, entry): |
18 | | - self.id = id |
19 | | - self.name = name |
20 | | - self.status = status |
21 | | - self.function = function |
22 | | - self.url = url |
23 | | - self.entry = entry |
24 | | - |
25 | | - async def execute(self, request): |
26 | | - data = ( |
27 | | - await request.json() |
28 | | - ) # if content_type == 'application/json': data = await request.json() else: data="test" |
29 | | - try: |
30 | | - return self.function(data) |
31 | | - except TypeError as e: |
32 | | - raise HTTPException( |
33 | | - status_code=422, |
34 | | - detail={"status": self.status, "cause": e.__cause__} |
35 | | - ) |
| 7 | +from python_activator.installer import install_packages |
36 | 8 |
|
37 | 9 |
|
38 | 10 | app = FastAPI() |
@@ -94,128 +66,19 @@ async def execute_endpoint( |
94 | 66 | ) |
95 | 67 |
|
96 | 68 |
|
97 | | -# Install requirements using the requirements.txt file for each package |
98 | | -def install_requirements(modulepath): |
99 | | - dependency_requirements = Path(modulepath).joinpath("requirements.txt") |
100 | | - |
101 | | - # To Do: using pip install bellow explore installing requirements in a folder specific to the ko |
102 | | - # you may need to add that folder to the sys.path |
103 | | - |
104 | | - # uses 'pip install -r requirements.txt' to install requirements |
105 | | - if os.path.exists(dependency_requirements): |
106 | | - subprocess.check_call( |
107 | | - [sys.executable, "-m", "pip", "install", "-r", dependency_requirements] |
108 | | - ) |
109 | | - |
110 | | - |
111 | | -# look into the main directory that has all the packages and have the python ones installed |
112 | | -def install_packages(directory, manifest: dict): |
113 | | - for ko in manifest: |
114 | | - install_module(directory, manifest[ko]) |
115 | | - list_installed_packages() |
116 | | - |
117 | | - |
118 | | -def install_module(directory, ko): |
119 | | - Knowledge_Objects[ko.name] = knowledge_object(ko.name, ko.status, None, "", "", "") |
120 | | - |
121 | | - try: |
122 | | - modulepath = os.path.join(Path(directory).joinpath(ko.name), "") |
123 | | - #########delete me: temporarily ignoring execute package |
124 | | - if ko.name == "python-executive-v1.0": |
125 | | - return |
126 | | - |
127 | | - if ko.status != "Ready for install": |
128 | | - return |
129 | | - |
130 | | - Knowledge_Objects[ko.name].status = "Activating" |
131 | | - |
132 | | - # get metadata and deployment files |
133 | | - with open(Path(modulepath).joinpath("deployment.yaml"), "r") as file: |
134 | | - deployment_data = yaml.safe_load(file) |
135 | | - with open(Path(modulepath).joinpath("metadata.json"), "r") as file: |
136 | | - metadata = json.load(file) |
137 | | - first_key = next(iter(deployment_data)) |
138 | | - second_key = next(iter(deployment_data[first_key])) |
139 | | - |
140 | | - # do not install non python packages |
141 | | - if deployment_data[first_key][second_key]["engine"] != "python": |
142 | | - del Knowledge_Objects[ko.name] |
143 | | - Knowledge_Objects[metadata["@id"]] = knowledge_object( |
144 | | - ko.name, |
145 | | - "Knowledge object is not activated. It is not a python object.", |
146 | | - None, |
147 | | - metadata["@id"], |
148 | | - "", |
149 | | - "", |
150 | | - ) |
151 | | - return |
152 | | - |
153 | | - # install requirements |
154 | | - install_requirements(modulepath) |
155 | | - |
156 | | - # import module, get the function and add it to the dictionary |
157 | | - spec = importlib.util.spec_from_file_location( |
158 | | - ko.name, Path(modulepath).joinpath("src").joinpath("__init__.py") |
159 | | - ) |
160 | | - pac_a = importlib.util.module_from_spec(spec) |
161 | | - sys.modules[pac_a.__name__] = pac_a |
162 | | - module = importlib.import_module( |
163 | | - str.replace( |
164 | | - deployment_data[first_key][second_key]["entry"], "src/", "." |
165 | | - ).replace(".py", ""), |
166 | | - pac_a.__name__, |
167 | | - ) |
168 | | - mymethod = getattr(module, deployment_data[first_key][second_key]["function"]) |
169 | | - del Knowledge_Objects[ko.name] |
170 | | - Knowledge_Objects[metadata["@id"]] = knowledge_object( |
171 | | - ko.name, |
172 | | - "Activated", |
173 | | - mymethod, |
174 | | - metadata["@id"], |
175 | | - "", |
176 | | - Path(modulepath).joinpath(deployment_data[first_key][second_key]["entry"]), |
177 | | - ) |
178 | | - except Exception as e: |
179 | | - Knowledge_Objects[ko.name].status = "Faield activating with error: " + repr(e) |
180 | | - |
181 | | - |
182 | | -def list_installed_packages(): |
183 | | - print("-------------------\nPackages installed:") |
184 | | - print("{:<4}. {:<30} {:<30} {:<30}".format("", "ID", "NAME", "STATUS")) |
185 | | - for i, item in enumerate(Knowledge_Objects): |
186 | | - print( |
187 | | - "{:<4}. {:<30} {:<30} {:<30}".format( |
188 | | - str(i), |
189 | | - Knowledge_Objects[item].id, |
190 | | - Knowledge_Objects[item].name[:30], |
191 | | - Knowledge_Objects[item].status[:50], |
192 | | - ) |
193 | | - ) |
194 | | - print("-------------------") |
195 | | - |
196 | | - |
197 | 69 | # run install if the app is starated using poetry run uvicorn python_activator.api:app --reload |
198 | 70 | @app.on_event("startup") |
199 | 71 | async def startup_event(): |
200 | 72 | print(">>>>>> running startup event") |
201 | | - if os.environ.get("COLLECTION_PATH"): |
202 | | - object_directory = os.path.join(Path(os.environ["COLLECTION_PATH"]), "") |
203 | | - else: |
204 | | - object_directory = os.path.join(Path(os.getcwd()).joinpath("pyshelf"), "") |
205 | | - |
206 | | - manifest = load_packages(object_directory) |
207 | | - install_packages(object_directory, manifest) |
208 | | - try: |
209 | | - del os.environ["COLLECTION_PATH"] |
210 | | - del os.environ["MANIFEST_PATH"] |
211 | | - except Exception as e: |
212 | | - print("error deleting env variables") |
213 | | - |
| 73 | + global Knowledge_Objects |
| 74 | + Knowledge_Objects, object_directory = load_packages() |
| 75 | + Knowledge_Objects = install_packages(object_directory, Knowledge_Objects) |
| 76 | + |
214 | 77 |
|
215 | 78 | # run virtual server when running this .py file directly for debugging. It will look for objects at {code folder}/pyshelf |
216 | 79 | if __name__ == "__main__": |
217 | 80 | print(">>>>>running with debug<<<<<") |
218 | | - # os.environ["MANIFEST_PATH"]="/home/faridsei/dev/test/package/manifest.json" |
| 81 | + os.environ["MANIFEST_PATH"]="/home/faridsei/dev/test/manifest/manifest.json" |
219 | 82 | # os.environ["MANIFEST_PATH"] = "https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/manifest.json" |
220 | 83 | os.environ["COLLECTION_PATH"] = "/home/faridsei/dev/test/pyshelf/" |
221 | 84 | uvicorn.run(app, host="127.0.0.1", port=8000) |
0 commit comments