1 ############################################################################
2 # Copyright 2016 RIFT.IO Inc #
4 # Licensed under the Apache License, Version 2.0 (the "License"); #
5 # you may not use this file except in compliance with the License. #
6 # 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. #
15 ############################################################################
22 from .tosca
import ImportTosca
25 class ConvertPackageError(Exception):
29 class ConvertPackage(object):
30 """Convert a package to our YANG model
32 Currently only TOSCA to our model is supported
35 def __init__(self
, log
, filename
, pkgfile
):
37 self
._filename
= filename
38 self
._pkgfile
= pkgfile
41 def convert(self
, delete
=False):
42 """Convert package to our YANG model
45 delete: If the pkgfile is to be deleted after converting
48 List of descriptor packages. If the package is not a
49 suported format, None is returned
52 This will create a temporary directory and the converted
53 files will be in that. The converted files and directory
54 need to be deleted after use.
57 # Create a temporary directory to store the converted packages
58 tempdir
= tempfile
.mkdtemp()
62 # Check if this is a tosca archive
63 if ImportTosca
.is_tosca_package(self
._pkgfile
):
64 self
._log
.debug("Uploaded file {} is a TOSCA archive".
65 format(self
._filename
))
67 tosca
= ImportTosca(self
._log
, self
._pkgfile
, out_dir
=tempdir
)
68 out_files
= tosca
.translate()
71 except Exception as e
:
72 self
._log
.error("Exception converting package from TOSCA {}: {}".
73 format(self
._filename
, e
))
77 shutil
.rmtree(tempdir
)
79 self
._log
.warning("Unable to remove temporary directory {}: {}".
84 # Delete the input file, if converted
86 self
._tempdir
= tempdir
88 os
.remove(self
._pkgfile
)
90 self
._log
.warning("Failed to remove package file: %s", str(e
))
93 shutil
.rmtree(tempdir
, ignore_errors
=True)
95 #Return the input file
96 out_files
.append(self
._pkgfile
)
99 # Return the converted files
100 self
._log
.debug("Converted package files: {}".format(out_files
))