4b1a13c00d38fae001635c410615006fdfa863f7
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 CharmExtractionError(Exception):
28 class PackageCharmExtractor(object):
29 """ This class is reponsible for extracting charms to the correct directory
31 In order to remain compatible with the existing Jujuclient, we extract the charms
32 to a known location (RIFT-13282)
34 DEFAULT_INSTALL_DIR
= os
.path
.join(
35 os
.environ
["RIFT_VAR_ROOT"],
39 CHARM_REGEX
= "{prefix}charms/(trusty/)?(?P<charm_name>[^/]+)$"
41 def __init__(self
, log
, install_dir
=DEFAULT_INSTALL_DIR
):
43 self
._install
_dir
= install_dir
45 def _get_rel_dest_path(self
, descriptor_id
, charm_name
):
46 dest_rel_path
= "libs/{}/charms/trusty/{}".format(descriptor_id
, charm_name
)
47 dest_path
= os
.path
.join(self
._install
_dir
, dest_rel_path
)
51 def charm_dir_map(cls
, package
):
53 regex
= cls
.CHARM_REGEX
.format(prefix
=package
.prefix
)
55 for dir_name
in package
.dirs
:
57 cls
.CHARM_REGEX
.format(prefix
=package
.prefix
), dir_name
,
62 charm_name
= match
.group("charm_name")
63 if charm_name
== "trusty":
66 charm_map
[charm_name
] = dir_name
70 def get_extracted_charm_dir(self
, package_id
, charm_name
):
72 self
._get
_rel
_dest
_path
(package_id
, charm_name
),
75 def extract_charms(self
, pkg
):
76 """ Extract charms contained within the DescriptorPackage
77 to the known charm directory.
80 pkg - The descriptor package that MAY contain charm directories
83 CharmExtractionError - Charms in the package failed to get extracted
85 descriptor_id
= pkg
.descriptor_id
86 charm_dir_map
= PackageCharmExtractor
.charm_dir_map(pkg
)
88 for charm_name
, charm_dir
in charm_dir_map
.items():
89 dest_rel_path
= self
._get
_rel
_dest
_path
(descriptor_id
, charm_name
)
90 dest_path
= os
.path
.join(self
._install
_dir
, dest_rel_path
)
92 self
._log
.debug("Extracting %s charm to %s", charm_name
, dest_path
)
94 pkg
.extract_dir(charm_dir
, dest_path
)
95 except package
.ExtractError
as e
:
96 raise CharmExtractionError("Failed to extract charm %s" % charm_name
) from e