X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=rwlaunchpad%2Fplugins%2Frwlaunchpadtasklet%2Frift%2Fpackage%2Ficon.py;fp=rwlaunchpad%2Fplugins%2Frwlaunchpadtasklet%2Frift%2Fpackage%2Ficon.py;h=1c3d209bf1e2acc18cfc89dd9353c7f34cb9107b;hb=6f07e6f33f751ab4ffe624f6037f887b243bece2;hp=0000000000000000000000000000000000000000;hpb=72a563886272088feb7cb52e4aafbe6d2c580ff9;p=osm%2FSO.git diff --git a/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/icon.py b/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/icon.py new file mode 100644 index 00000000..1c3d209b --- /dev/null +++ b/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/icon.py @@ -0,0 +1,96 @@ + +# +# 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 IconExtractionError(Exception): + pass + + +class PackageIconExtractor(object): + """ This class extracts icons to a known location for the UI to access """ + + DEFAULT_INSTALL_DIR = os.path.join( + os.environ["RIFT_INSTALL"], + "usr/share/rw.ui/skyquake/plugins/composer/public/assets/logos" + ) + + ICON_REGEX = "{prefix}/?icons/(?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_type, descriptor_id, icon_name): + dest_path = os.path.join(self._install_dir, descriptor_type, descriptor_id, icon_name) + return dest_path + + @classmethod + def package_icon_files(cls, package): + icon_file_map = {} + + for file_name in package.files: + match = re.match( + cls.ICON_REGEX.format(prefix=package.prefix), + file_name, + ) + if match is None: + continue + + icon_name = match.group("icon_name") + + icon_file_map[icon_name] = file_name + + return icon_file_map + + def get_extracted_icon_path(self, descriptor_type, descriptor_id, icon_name): + return os.path.join( + self._get_rel_dest_path(descriptor_type, descriptor_id, icon_name), + ) + + def extract_icons(self, pkg): + """ Extract any icons in the package to the UI filesystem location + + Arguments: + pkg - A DescriptorPackage + """ + descriptor_id = pkg.descriptor_id + icon_files = PackageIconExtractor.package_icon_files(pkg) + + for icon_name, icon_file in icon_files.items(): + dest_rel_path = self._get_rel_dest_path(pkg.descriptor_type, descriptor_id, icon_name) + dest_path = os.path.join(self._install_dir, dest_rel_path) + + dest_dir = os.path.dirname(dest_path) + try: + os.makedirs(dest_dir, exist_ok=True) + except OSError as e: + self._log.error("Failed to create icon directory %s: %s", dest_dir, str(e)) + continue + + + self._log.debug("Extracting %s icon to %s", icon_name, dest_path) + try: + pkg.extract_file(icon_file, dest_path) + except package.ExtractError as e: + self._log.error("Failed to extact icon %s: %s", icon_name, str(e)) + continue +