Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
| 16 | from xosconfig import Config |
| 17 | from multistructlog import create_logger |
| 18 | |
| 19 | log = create_logger(Config().get("logging")) |
| 20 | |
| 21 | |
| 22 | class ModelLoadClient(object): |
| 23 | def __init__(self, api): |
| 24 | self.api = api |
| 25 | |
| 26 | def upload_models(self, name, dir, version="unknown"): |
| 27 | request = self.api.dynamicload_pb2.LoadModelsRequest(name=name, version=version) |
| 28 | |
| 29 | for fn in os.listdir(dir): |
| 30 | if fn.endswith(".xproto"): |
| 31 | item = request.xprotos.add() |
| 32 | item.filename = fn |
| 33 | item.contents = open(os.path.join(dir, fn)).read() |
| 34 | |
| 35 | models_fn = os.path.join(dir, "models.py") |
| 36 | if os.path.exists(models_fn): |
| 37 | item = request.decls.add() |
| 38 | item.filename = "models.py" |
| 39 | item.contents = open(models_fn).read() |
| 40 | |
| 41 | attic_dir = os.path.join(dir, "attic") |
| 42 | if os.path.exists(attic_dir): |
| 43 | log.warn( |
| 44 | "Attics are deprecated, please use the legacy=True option in xProto" |
| 45 | ) |
| 46 | for fn in os.listdir(attic_dir): |
| 47 | if fn.endswith(".py"): |
| 48 | item = request.attics.add() |
| 49 | item.filename = fn |
| 50 | item.contents = open(os.path.join(attic_dir, fn)).read() |
| 51 | |
| 52 | api_convenience_dir = os.path.join(dir, "convenience") |
| 53 | if os.path.exists(api_convenience_dir): |
| 54 | for fn in os.listdir(api_convenience_dir): |
| 55 | if fn.endswith(".py") and "test" not in fn: |
| 56 | item = request.convenience_methods.add() |
| 57 | item.filename = fn |
| 58 | item.contents = open(os.path.join(api_convenience_dir, fn)).read() |
| 59 | |
Scott Baker | 8cc464d | 2019-02-06 16:51:42 -0800 | [diff] [blame] | 60 | # migrations directory is a sibling to the models directory |
| 61 | migrations_dir = os.path.join(dir, "..", "migrations") |
| 62 | if os.path.exists(migrations_dir): |
| 63 | for fn in os.listdir(migrations_dir): |
| 64 | if fn.endswith(".py") and "test" not in fn: |
| 65 | item = request.migrations.add() |
| 66 | item.filename = fn |
| 67 | item.contents = open(os.path.join(migrations_dir, fn)).read() |
| 68 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 69 | result = self.api.dynamicload.LoadModels(request) |