RIFT OSM R1 Initial Submission
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / convert_pkg.py
1 ############################################################################
2 # Copyright 2016 RIFT.IO Inc #
3 # #
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 #
7 # #
8 # http://www.apache.org/licenses/LICENSE-2.0 #
9 # #
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 ############################################################################
16
17
18 import os
19 import shutil
20 import tempfile
21
22 from .tosca import ImportTosca
23
24
25 class ConvertPackageError(Exception):
26 pass
27
28
29 class ConvertPackage(object):
30 """Convert a package to our YANG model
31
32 Currently only TOSCA to our model is supported
33 """
34
35 def __init__(self, log, filename, pkgfile):
36 self._log = log
37 self._filename = filename
38 self._pkgfile = pkgfile
39 self._tempdir = None
40
41 def convert(self, delete=False):
42 """Convert package to our YANG model
43
44 Arguments:
45 delete: If the pkgfile is to be deleted after converting
46
47 Returns:
48 List of descriptor packages. If the package is not a
49 suported format, None is returned
50
51 Note:
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.
55 """
56
57 # Create a temporary directory to store the converted packages
58 tempdir = tempfile.mkdtemp()
59
60 out_files = []
61 converted = False
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))
66 try:
67 tosca = ImportTosca(self._log, self._pkgfile, out_dir=tempdir)
68 out_files = tosca.translate()
69 converted = True
70
71 except Exception as e:
72 self._log.error("Exception converting package from TOSCA {}: {}".
73 format(self._filename, e))
74
75 # Remove the tempdir
76 try:
77 shutil.rmtree(tempdir)
78 except OSError as e:
79 self._log.warning("Unable to remove temporary directory {}: {}".
80 format(tempdir, e))
81
82 raise
83
84 # Delete the input file, if converted
85 if converted:
86 self._tempdir = tempdir
87 try:
88 os.remove(self._pkgfile)
89 except OSError as e:
90 self._log.warning("Failed to remove package file: %s", str(e))
91 else:
92 # Remove the temp dir
93 shutil.rmtree(tempdir, ignore_errors=True)
94
95 #Return the input file
96 out_files.append(self._pkgfile)
97
98
99 # Return the converted files
100 self._log.debug("Converted package files: {}".format(out_files))
101 return out_files
102