-
Notifications
You must be signed in to change notification settings - Fork 326
/
backcompat.py
182 lines (152 loc) · 6.57 KB
/
backcompat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
Utility code for supporting earlier Rez data in later Rez releases.
"""
from rez.config import config
from rez.utils.logging_ import print_debug
import re
import os
import os.path
import textwrap
variant_key_conversions = {
"name": "name",
"version": "version",
"index": "index",
"search_path": "location"
}
def convert_old_variant_handle(handle_dict):
"""Convert a variant handle from serialize_version < 4.0."""
old_variables = handle_dict.get("variables", {})
variables = dict(repository_type="filesystem")
for old_key, key in variant_key_conversions.items():
value = old_variables.get(old_key)
#if value is not None:
variables[key] = value
path = handle_dict["path"]
filename = os.path.basename(path)
if os.path.splitext(filename)[0] == "package":
key = "filesystem.variant"
else:
key = "filesystem.variant.combined"
return dict(key=key, variables=variables)
def convert_old_command_expansions(command):
"""Convert expansions from !OLD! style to {new}."""
command = command.replace("!VERSION!", "{version}")
command = command.replace("!MAJOR_VERSION!", "{version.major}")
command = command.replace("!MINOR_VERSION!", "{version.minor}")
command = command.replace("!BASE!", "{base}")
command = command.replace("!ROOT!", "{root}")
command = command.replace("!USER!", "{system.user}")
return command
within_unescaped_quotes_regex = re.compile('(?<!\\\\)"(.*?)(?<!\\\\)"')
def convert_old_commands(commands, annotate=True):
"""Converts old-style package commands into equivalent Rex code."""
from rez.config import config
from rez.utils.logging_ import print_debug
def _repl(s):
return s.replace('\\"', '"')
def _encode(s):
# this replaces all occurrances of '\"' with '"', *except* for those
# occurrances of '\"' that are within double quotes, which themselves
# are not escaped. In other words, the string:
# 'hey "there \"you\" " \"dude\" '
# ..will convert to:
# 'hey "there \"you\" " "dude" '
s_new = ''
prev_i = 0
for m in within_unescaped_quotes_regex.finditer(s):
s_ = s[prev_i:m.start()]
s_new += _repl(s_)
s_new += s[m.start():m.end()]
prev_i = m.end()
s_ = s[prev_i:]
s_new += _repl(s_)
return repr(s_new)
loc = []
for cmd in commands:
if annotate:
txt = "OLD COMMAND: %s" % cmd
line = "comment(%s)" % _encode(txt)
loc.append(line)
cmd = convert_old_command_expansions(cmd)
toks = cmd.strip().split()
try:
if toks[0] == "export":
var, value = cmd.split(' ', 1)[1].split('=', 1)
for bookend in ('"', "'"):
if value.startswith(bookend) and value.endswith(bookend):
value = value[1:-1]
break
# As the only old-style commands were Linux/Bash based,
# we assume using the default separator ":" is ok - we don't
# need to use os.pathsep as we don't expected to see a
# Windows path here.
separator = config.env_var_separators.get(var, ":")
# This is a special case. We don't want to include "';'" in
# our env var separators map as it's not really the correct
# behaviour/something we want to promote. It's included here for
# backwards compatibility only, and to not propogate elsewhere.
if var == "CMAKE_MODULE_PATH":
value = value.replace("'%s'" % separator, separator)
value = value.replace('"%s"' % separator, separator)
value = value.replace(":", separator)
parts = value.split(separator)
parts = [x for x in parts if x]
if len(parts) > 1:
idx = None
var1 = "$%s" % var
var2 = "${%s}" % var
if var1 in parts:
idx = parts.index(var1)
elif var2 in parts:
idx = parts.index(var2)
if idx in (0, len(parts) - 1):
func = "appendenv" if idx == 0 else "prependenv"
parts = parts[1:] if idx == 0 else parts[:-1]
val = separator.join(parts)
loc.append("%s('%s', %s)" % (func, var, _encode(val)))
continue
loc.append("setenv('%s', %s)" % (var, _encode(value)))
elif toks[0].startswith('#'):
loc.append("comment(%s)" % _encode(' '.join(toks[1:])))
elif toks[0] == "alias":
match = re.search("alias (?P<key>.*?)=(?P<value>.*)", cmd)
key = match.groupdict()['key'].strip()
value = match.groupdict()['value'].strip()
if (value.startswith('"') and value.endswith('"')) or \
(value.startswith("'") and value.endswith("'")):
value = value[1:-1]
loc.append("alias('%s', %s)" % (key, _encode(value)))
else:
# assume we can execute this as a straight command
loc.append("command(%s)" % _encode(cmd))
except:
# if anything goes wrong, just fall back to bash command
loc.append("command(%s)" % _encode(cmd))
rex_code = '\n'.join(loc)
if config.debug("old_commands"):
br = '-' * 80
msg = textwrap.dedent(
"""
%s
OLD COMMANDS:
%s
NEW COMMANDS:
%s
%s
""") % (br, '\n'.join(commands), rex_code, br)
print_debug(msg)
return rex_code
# Copyright 2013-2016 Allan Johns.
#
# This library is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.