3 # Copyright 2016 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
25 class ScriptExtractionError(Exception):
29 class PackageScriptExtractor(object):
30 """ This class is reponsible for extracting scripts to the correct directory
32 In order to remain compatible with the existing config manager, we extract the scripts
33 to a known location (RIFT-13282)
35 DEFAULT_INSTALL_DIR
= os
.path
.join(
36 os
.environ
["RIFT_INSTALL"],
40 SCRIPT_REGEX
= "{prefix}/?scripts/(?P<script_name>[^/]+)$"
42 def __init__(self
, log
, install_dir
=DEFAULT_INSTALL_DIR
):
44 self
._install
_dir
= install_dir
46 def _get_rel_dest_path(self
, descriptor_id
, script_name
):
47 dest_path
= os
.path
.join(self
._install
_dir
, script_name
)
51 def package_script_files(cls
, package
):
54 for file_name
in package
.files
:
56 cls
.SCRIPT_REGEX
.format(prefix
=package
.prefix
),
62 script_name
= match
.group("script_name")
64 script_file_map
[script_name
] = file_name
66 return script_file_map
68 def get_extracted_script_path(self
, package_id
, script_name
):
70 self
._get
_rel
_dest
_path
(package_id
, script_name
),
73 def extract_scripts(self
, pkg
):
74 descriptor_id
= pkg
.descriptor_id
75 script_files
= PackageScriptExtractor
.package_script_files(pkg
)
77 for script_name
, script_file
in script_files
.items():
78 dest_path
= self
._get
_rel
_dest
_path
(descriptor_id
, script_name
)
80 self
._log
.debug("Extracting %s script to %s", script_name
, dest_path
)
82 pkg
.extract_file(script_file
, dest_path
)
83 except package
.ExtractError
as e
:
84 raise ScriptExtractionError("Failed to extract script %s" % script_name
) from e
86 def read_script(self
, pkg
, filename
):
87 script_files
= PackageScriptExtractor
.package_script_files(pkg
)
89 for script_name
, script_file
in script_files
.items():
90 if script_name
== filename
:
91 self
._log
.debug("Found %s script file in package at %s", filename
, script_file
)
94 with pkg
.open(script_file
) as f
:
96 self
._log
.info("Custom script read from file %s", userdata
)
97 # File contents are read in binary string, decode to regular string and return
98 return userdata
.decode()
99 except package
.ExtractError
as e
:
100 raise ScriptExtractionError("Failed to extract script %s" % script_name
) from e
102 # If we've reached this point but not found a matching script,
103 # raise an Exception, since we got here only because there was supposed
104 # to be a script in the VDU
105 errmsg
= "No script file found in the descriptor package"
106 self
._log
.error(errmsg
)
107 raise ScriptExtractionError(errmsg
)