OLD | NEW |
---|---|
1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
4 | 4 |
5 """CIPD-specific code is concentrated here.""" | 5 """CIPD-specific code is concentrated here.""" |
6 | 6 |
7 import re | 7 import re |
8 | 8 |
9 # Regular expressions below are copied from | 9 # Regular expressions below are copied from |
10 # https://chromium.googlesource.com/infra/infra/+/468bb43/appengine/chrome_infra _packages/cipd/impl.py | 10 # https://chromium.googlesource.com/infra/infra/+/468bb43/appengine/chrome_infra _packages/cipd/impl.py |
(...skipping 15 matching lines...) Expand all Loading... | |
26 # os can be "linux", "mac" or "windows" and arch can be "386", "amd64" or | 26 # os can be "linux", "mac" or "windows" and arch can be "386", "amd64" or |
27 # "armv6l". | 27 # "armv6l". |
28 PARAM_PLATFORM = '${platform}' | 28 PARAM_PLATFORM = '${platform}' |
29 # OS version parameter defines major and minor version of the OS distribution. | 29 # OS version parameter defines major and minor version of the OS distribution. |
30 # It is useful if package depends on .dll/.so libraries provided by the OS. | 30 # It is useful if package depends on .dll/.so libraries provided by the OS. |
31 # Example values: "ubuntu14_04", "mac10_9", "win6_1". | 31 # Example values: "ubuntu14_04", "mac10_9", "win6_1". |
32 PARAM_OS_VER = '${os_ver}' | 32 PARAM_OS_VER = '${os_ver}' |
33 ALL_PARAMS = (PARAM_PLATFORM, PARAM_OS_VER) | 33 ALL_PARAMS = (PARAM_PLATFORM, PARAM_OS_VER) |
34 | 34 |
35 | 35 |
36 class PinChecker(object): | |
37 PARAM_PLATFORM_ESC = re.escape(PARAM_PLATFORM) | |
38 PARAM_OS_VER_ESC = re.escape(PARAM_OS_VER) | |
39 | |
40 def __init__(self): | |
41 self.platform = None | |
42 self.os_ver = None | |
43 | |
44 def check(self, original, expanded): | |
M-A Ruel
2016/08/30 02:18:07
optional nit: I think I'd prefer if this function
iannucci
2016/08/30 09:13:29
Done.
edit: Actually I changed it to a contextman
| |
45 """Raises ValueError if expanded is not pluasibly a pinned version of | |
M-A Ruel
2016/08/30 02:18:07
plausibly
iannucci
2016/08/30 09:13:29
Done.
| |
46 original. | |
47 | |
48 Args: | |
49 original - a CipdPackage which may contain templates like ${platform} | |
50 expanded - a CipdPackage which is nominally an expansion of original. | |
51 | |
52 CipdPackage is duck-typed to have three string properties 'package_name', | |
53 'path' and 'version'. | |
54 | |
55 Side effects: | |
56 If this PinChecker has not encountered a platform or os_ver value yet, | |
57 the appropriate property will be set on this PinChecker. Subsequent | |
58 executions of check will replace ${platform} with the previously-scanned | |
59 value. | |
60 | |
61 Raises: | |
62 ValueError if expanded is not a valid derivation of original. | |
63 """ | |
64 if original.path != expanded.path: | |
65 raise ValueError('Mismatched path') | |
66 | |
67 def sub_param(regex, param_esc, param_re, param_const): | |
68 # The 1 allows each substitution to only show up in the pattern once. | |
69 # If it shows up more than once, then this is a very strange package name, | |
70 # and should be subject to further scrutiny. If it turns out that for some | |
71 # reason the substitutions SHOULD be allowed more than once per name, we | |
72 # will need to assert that the matched-values of them are also all the | |
73 # same (e.g. ${platform} cannot resolve to more than one value) | |
74 ret = False | |
75 if param_const is None: | |
76 ret = param_esc in regex | |
77 if ret: | |
78 regex = regex.replace(param_esc, param_re, 1) | |
79 else: | |
80 regex = regex.replace(param_esc, param_const, 1) | |
81 return regex, ret | |
82 | |
83 name_regex = re.escape(original.package_name) | |
84 name_regex, scan_plat = sub_param( | |
85 name_regex, self.PARAM_PLATFORM_ESC, '(?P<platform>\w+-[a-z0-9]+)', | |
M-A Ruel
2016/08/30 02:18:07
Use r'' otherwise your \ may not do what you think
iannucci
2016/08/30 09:13:29
Done
| |
86 self.platform) | |
87 name_regex, scan_os_ver = sub_param( | |
88 name_regex, self.PARAM_OS_VER_ESC, '(?P<os_ver>[_a-z0-9]+)', | |
M-A Ruel
2016/08/30 02:18:07
r'' as per habit.
iannucci
2016/08/30 09:13:29
Done
| |
89 self.os_ver) | |
90 | |
91 match = re.match(name_regex, expanded.package_name) | |
92 if not match: | |
93 raise ValueError('Mismatched package_name') | |
94 | |
95 if is_pinned_version(original.version): | |
96 if original.version != expanded.version: | |
97 raise ValueError('Mismatched pins') | |
98 else: | |
99 if not is_pinned_version(expanded.version): | |
100 raise ValueError('Pin value is not a pin') | |
101 | |
102 if scan_plat: | |
103 self.platform = re.escape(match.group('platform')) | |
104 if scan_os_ver: | |
105 self.os_ver = re.escape(match.group('os_ver')) | |
106 | |
107 | |
36 def is_valid_package_name(package_name): | 108 def is_valid_package_name(package_name): |
37 """Returns True if |package_name| is a valid CIPD package name.""" | 109 """Returns True if |package_name| is a valid CIPD package name.""" |
38 return bool(PACKAGE_NAME_RE.match(package_name)) | 110 return bool(PACKAGE_NAME_RE.match(package_name)) |
39 | 111 |
40 | 112 |
41 def is_valid_package_name_template(template): | 113 def is_valid_package_name_template(template): |
42 """Returns True if |package_name| is a valid CIPD package name template.""" | 114 """Returns True if |package_name| is a valid CIPD package name template.""" |
43 # Render known parameters first. | 115 # Render known parameters first. |
44 for p in ALL_PARAMS: | 116 for p in ALL_PARAMS: |
45 template = template.replace(p, 'x') | 117 template = template.replace(p, 'x') |
(...skipping 13 matching lines...) Expand all Loading... | |
59 """True if string looks like a valid package instance tag.""" | 131 """True if string looks like a valid package instance tag.""" |
60 if not tag or ':' not in tag or len(tag) > TAG_MAX_LEN: | 132 if not tag or ':' not in tag or len(tag) > TAG_MAX_LEN: |
61 return False | 133 return False |
62 # Care only about the key. Value can be anything (including empty string). | 134 # Care only about the key. Value can be anything (including empty string). |
63 return bool(TAG_KEY_RE.match(tag.split(':', 1)[0])) | 135 return bool(TAG_KEY_RE.match(tag.split(':', 1)[0])) |
64 | 136 |
65 | 137 |
66 def is_pinned_version(version): | 138 def is_pinned_version(version): |
67 """Returns True if |version| is pinned.""" | 139 """Returns True if |version| is pinned.""" |
68 return bool(INSTANCE_ID_RE.match(version)) or is_valid_tag(version) | 140 return bool(INSTANCE_ID_RE.match(version)) or is_valid_tag(version) |
OLD | NEW |