blob: 43b429e67f8326508ef6d1ea20ef3eff9f22fc72 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# 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
Zack Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
16
Scott Bakerbba67b62019-01-28 17:38:21 -080017import os
Zack Williams5c2ea232019-01-30 15:23:01 -070018
Scott Bakerbba67b62019-01-28 17:38:21 -080019from multistructlog import create_logger
Zack Williams5c2ea232019-01-30 15:23:01 -070020from xosconfig import Config
Scott Bakerbba67b62019-01-28 17:38:21 -080021
22log = create_logger(Config().get("logging"))
23
24
25class ModelLoadClient(object):
Scott Baker7ff8ad92019-02-15 17:02:41 -080026 REQUIRE_CLEAN = 0
27 AUTOMATICALLY_CLEAN = 1
28 PURGE = 2
29
Scott Bakerbba67b62019-01-28 17:38:21 -080030 def __init__(self, api):
31 self.api = api
32
33 def upload_models(self, name, dir, version="unknown"):
34 request = self.api.dynamicload_pb2.LoadModelsRequest(name=name, version=version)
35
36 for fn in os.listdir(dir):
37 if fn.endswith(".xproto"):
38 item = request.xprotos.add()
39 item.filename = fn
40 item.contents = open(os.path.join(dir, fn)).read()
41
42 models_fn = os.path.join(dir, "models.py")
43 if os.path.exists(models_fn):
44 item = request.decls.add()
45 item.filename = "models.py"
46 item.contents = open(models_fn).read()
47
48 attic_dir = os.path.join(dir, "attic")
49 if os.path.exists(attic_dir):
50 log.warn(
51 "Attics are deprecated, please use the legacy=True option in xProto"
52 )
53 for fn in os.listdir(attic_dir):
54 if fn.endswith(".py"):
55 item = request.attics.add()
56 item.filename = fn
57 item.contents = open(os.path.join(attic_dir, fn)).read()
58
59 api_convenience_dir = os.path.join(dir, "convenience")
60 if os.path.exists(api_convenience_dir):
61 for fn in os.listdir(api_convenience_dir):
62 if fn.endswith(".py") and "test" not in fn:
63 item = request.convenience_methods.add()
64 item.filename = fn
65 item.contents = open(os.path.join(api_convenience_dir, fn)).read()
66
Scott Baker8cc464d2019-02-06 16:51:42 -080067 # migrations directory is a sibling to the models directory
68 migrations_dir = os.path.join(dir, "..", "migrations")
69 if os.path.exists(migrations_dir):
70 for fn in os.listdir(migrations_dir):
71 if fn.endswith(".py") and "test" not in fn:
72 item = request.migrations.add()
73 item.filename = fn
74 item.contents = open(os.path.join(migrations_dir, fn)).read()
75
Matteo Scandolo12651d72019-02-21 15:15:29 -080076 # loading core requested version from synchronizer config
77 core_version = Config.get("core_version")
78 if core_version is None:
79 log.warn("Core version is not set in the config file")
80
81 request.core_version = core_version
82
Scott Bakerbba67b62019-01-28 17:38:21 -080083 result = self.api.dynamicload.LoadModels(request)
Scott Baker7ff8ad92019-02-15 17:02:41 -080084
85 return result
86
87 def unload_models(self, name, version="unknown", cleanup_behavior=REQUIRE_CLEAN):
88 request = self.api.dynamicload_pb2.UnloadModelsRequest(
89 name=name,
90 version=version,
91 cleanup_behavior=cleanup_behavior)
92 result = self.api.dynamicload.UnloadModels(request)
93
94 return result