X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=rwlaunchpad%2Fplugins%2Frwlaunchpadtasklet%2Frift%2Fpackage%2Fscript.py;fp=rwlaunchpad%2Fplugins%2Frwlaunchpadtasklet%2Frift%2Fpackage%2Fscript.py;h=01f66b09f8a6abe60a8ecdd57fd986752ab3d382;hb=6f07e6f33f751ab4ffe624f6037f887b243bece2;hp=0000000000000000000000000000000000000000;hpb=72a563886272088feb7cb52e4aafbe6d2c580ff9;p=osm%2FSO.git diff --git a/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/script.py b/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/script.py new file mode 100644 index 00000000..01f66b09 --- /dev/null +++ b/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/script.py @@ -0,0 +1,84 @@ + +# +# Copyright 2016 RIFT.IO Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import re +import os.path + +from . import package + + +class ScriptExtractionError(Exception): + pass + + +class PackageScriptExtractor(object): + """ This class is reponsible for extracting scripts to the correct directory + + In order to remain compatible with the existing config manager, we extract the scripts + to a known location (RIFT-13282) + """ + DEFAULT_INSTALL_DIR = os.path.join( + os.environ["RIFT_INSTALL"], + "usr/bin" + ) + + SCRIPT_REGEX = "{prefix}/?scripts/(?P[^/]+)$" + + def __init__(self, log, install_dir=DEFAULT_INSTALL_DIR): + self._log = log + self._install_dir = install_dir + + def _get_rel_dest_path(self, descriptor_id, script_name): + dest_path = os.path.join(self._install_dir, script_name) + return dest_path + + @classmethod + def package_script_files(cls, package): + script_file_map = {} + + for file_name in package.files: + match = re.match( + cls.SCRIPT_REGEX.format(prefix=package.prefix), + file_name, + ) + if match is None: + continue + + script_name = match.group("script_name") + + script_file_map[script_name] = file_name + + return script_file_map + + def get_extracted_script_path(self, package_id, script_name): + return os.path.join( + self._get_rel_dest_path(package_id, script_name), + ) + + def extract_scripts(self, pkg): + descriptor_id = pkg.descriptor_id + script_files = PackageScriptExtractor.package_script_files(pkg) + + for script_name, script_file in script_files.items(): + dest_path = self._get_rel_dest_path(descriptor_id, script_name) + + self._log.debug("Extracting %s script to %s", script_name, dest_path) + try: + pkg.extract_file(script_file, dest_path) + except package.ExtractError as e: + raise ScriptExtractionError("Failed to extract script %s" % script_name) from e