|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import re |
|
17
|
|
|
|
|
18
|
|
|
from st2common.constants.pack import PACK_REF_WHITELIST_REGEX |
|
19
|
|
|
|
|
20
|
|
|
__all__ = [ |
|
21
|
|
|
'get_pack_ref_from_metadata' |
|
22
|
|
|
] |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def get_pack_ref_from_metadata(metadata, pack_directory_name=None): |
|
26
|
|
|
""" |
|
27
|
|
|
Utility function which retrieves pack "ref" attribute from the pack metadata file. |
|
28
|
|
|
|
|
29
|
|
|
If this attribute is not provided, an attempt is made to infer "ref" from the "name" attribute. |
|
30
|
|
|
|
|
31
|
|
|
:rtype: ``str`` |
|
32
|
|
|
""" |
|
33
|
|
|
pack_ref = None |
|
34
|
|
|
|
|
35
|
|
|
# The rules for the pack ref are as follows: |
|
36
|
|
|
# 1. If ref attribute is available, we used that |
|
37
|
|
|
# 2. If pack_directory_name is available we use that (this only applies to packs |
|
38
|
|
|
# which are in sub-directories) |
|
39
|
|
|
# 2. If attribute is not available, but pack name is and pack name meets the valid name |
|
40
|
|
|
# criteria, we use that |
|
41
|
|
|
if metadata.get('ref', None): |
|
42
|
|
|
pack_ref = metadata['ref'] |
|
43
|
|
|
elif pack_directory_name and re.match(PACK_REF_WHITELIST_REGEX, pack_directory_name): |
|
44
|
|
|
pack_ref = pack_directory_name |
|
45
|
|
|
else: |
|
46
|
|
|
if re.match(PACK_REF_WHITELIST_REGEX, metadata['name']): |
|
47
|
|
|
pack_ref = metadata['name'] |
|
48
|
|
|
else: |
|
49
|
|
|
raise ValueError('Pack name "%s" contains invalid characters and "ref" ' |
|
50
|
|
|
'attribute is not available' % (metadata['name'])) |
|
51
|
|
|
|
|
52
|
|
|
return pack_ref |
|
53
|
|
|
|