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.
24 class IconExtractionError(Exception):
28 class PackageIconExtractor(object):
29 """ This class extracts icons to a known location for the UI to access """
31 DEFAULT_INSTALL_DIR
= os
.path
.join(
32 os
.environ
["RIFT_INSTALL"],
33 "usr/share/rw.ui/skyquake/plugins/composer/public/assets/logos"
36 ICON_REGEX
= "{prefix}/?icons/(?P<icon_name>[^/]+)$"
38 def __init__(self
, log
, install_dir
=DEFAULT_INSTALL_DIR
):
40 self
._install
_dir
= install_dir
42 def _get_rel_dest_path(self
, descriptor_type
, descriptor_id
, icon_name
):
43 dest_path
= os
.path
.join(self
._install
_dir
, descriptor_type
, descriptor_id
, icon_name
)
47 def package_icon_files(cls
, package
):
50 for file_name
in package
.files
:
52 cls
.ICON_REGEX
.format(prefix
=package
.prefix
),
58 icon_name
= match
.group("icon_name")
60 icon_file_map
[icon_name
] = file_name
64 def get_extracted_icon_path(self
, descriptor_type
, descriptor_id
, icon_name
):
66 self
._get
_rel
_dest
_path
(descriptor_type
, descriptor_id
, icon_name
),
69 def extract_icons(self
, pkg
):
70 """ Extract any icons in the package to the UI filesystem location
73 pkg - A DescriptorPackage
75 descriptor_id
= pkg
.descriptor_id
76 icon_files
= PackageIconExtractor
.package_icon_files(pkg
)
78 for icon_name
, icon_file
in icon_files
.items():
79 dest_rel_path
= self
._get
_rel
_dest
_path
(pkg
.descriptor_type
, descriptor_id
, icon_name
)
80 dest_path
= os
.path
.join(self
._install
_dir
, dest_rel_path
)
82 dest_dir
= os
.path
.dirname(dest_path
)
84 os
.makedirs(dest_dir
, exist_ok
=True)
86 self
._log
.error("Failed to create icon directory %s: %s", dest_dir
, str(e
))
90 self
._log
.debug("Extracting %s icon to %s", icon_name
, dest_path
)
92 pkg
.extract_file(icon_file
, dest_path
)
93 except package
.ExtractError
as e
:
94 self
._log
.error("Failed to extact icon %s: %s", icon_name
, str(e
))