def convert_old_commands(commands, annotate=True):
from rez.config import config<EOL>from rez.utils.logging_ import print_debug<EOL>def _repl(s):<EOL><INDENT>return s.replace('<STR_LIT>', '<STR_LIT:">')<EOL><DEDENT>def _encode(s):<EOL><INDENT>s_new = '<STR_LIT>'<EOL>prev_i = <NUM_LIT:0><EOL>for m in within_unescaped_quotes_regex.finditer(s):<EOL><INDENT>s_ = s[prev_i:m.start()]<EOL>s_new += _repl(s_)<EOL>s_new += s[m.start():m.end()]<EOL>prev_i = m.end()<EOL><DEDENT>s_ = s[prev_i:]<EOL>s_new += _repl(s_)<EOL>return repr(s_new)<EOL><DEDENT>loc = []<EOL>for cmd in commands:<EOL><INDENT>if annotate:<EOL><INDENT>txt = "<STR_LIT>" % cmd<EOL>line = "<STR_LIT>" % _encode(txt)<EOL>loc.append(line)<EOL><DEDENT>cmd = convert_old_command_expansions(cmd)<EOL>toks = cmd.strip().split()<EOL>try:<EOL><INDENT>if toks[<NUM_LIT:0>] == "<STR_LIT>":<EOL><INDENT>var, value = cmd.split('<STR_LIT:U+0020>', <NUM_LIT:1>)[<NUM_LIT:1>].split('<STR_LIT:=>', <NUM_LIT:1>)<EOL>for bookend in ('<STR_LIT:">', "<STR_LIT:'>"):<EOL><INDENT>if value.startswith(bookend) and value.endswith(bookend):<EOL><INDENT>value = value[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>break<EOL><DEDENT><DEDENT>separator = config.env_var_separators.get(var, "<STR_LIT::>")<EOL>if var == "<STR_LIT>":<EOL><INDENT>value = value.replace("<STR_LIT>" % separator, separator)<EOL>value = value.replace('<STR_LIT>' % separator, separator)<EOL>value = value.replace("<STR_LIT::>", separator)<EOL><DEDENT>parts = value.split(separator)<EOL>parts = [x for x in parts if x]<EOL>if len(parts) > <NUM_LIT:1>:<EOL><INDENT>idx = None<EOL>var1 = "<STR_LIT>" % var<EOL>var2 = "<STR_LIT>" % var<EOL>if var1 in parts:<EOL><INDENT>idx = parts.index(var1)<EOL><DEDENT>elif var2 in parts:<EOL><INDENT>idx = parts.index(var2)<EOL><DEDENT>if idx in (<NUM_LIT:0>, len(parts) - <NUM_LIT:1>):<EOL><INDENT>func = "<STR_LIT>" if idx == <NUM_LIT:0> else "<STR_LIT>"<EOL>parts = parts[<NUM_LIT:1>:] if idx == <NUM_LIT:0> else parts[:-<NUM_LIT:1>]<EOL>val = separator.join(parts)<EOL>loc.append("<STR_LIT>" % (func, var, _encode(val)))<EOL>continue<EOL><DEDENT><DEDENT>loc.append("<STR_LIT>" % (var, _encode(value)))<EOL><DEDENT>elif toks[<NUM_LIT:0>].startswith('<STR_LIT:#>'):<EOL><INDENT>loc.append("<STR_LIT>" % _encode('<STR_LIT:U+0020>'.join(toks[<NUM_LIT:1>:])))<EOL><DEDENT>elif toks[<NUM_LIT:0>] == "<STR_LIT>":<EOL><INDENT>match = re.search("<STR_LIT>", cmd)<EOL>key = match.groupdict()['<STR_LIT:key>'].strip()<EOL>value = match.groupdict()['<STR_LIT:value>'].strip()<EOL>if (value.startswith('<STR_LIT:">') and value.endswith('<STR_LIT:">')) or(value.startswith("<STR_LIT:'>") and value.endswith("<STR_LIT:'>")):<EOL><INDENT>value = value[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL><DEDENT>loc.append("<STR_LIT>" % (key, _encode(value)))<EOL><DEDENT>else:<EOL><INDENT>loc.append("<STR_LIT>" % _encode(cmd))<EOL><DEDENT><DEDENT>except:<EOL><INDENT>loc.append("<STR_LIT>" % _encode(cmd))<EOL><DEDENT><DEDENT>rex_code = '<STR_LIT:\n>'.join(loc)<EOL>if config.debug("<STR_LIT>"):<EOL><INDENT>br = '<STR_LIT:->' * <NUM_LIT><EOL>msg = textwrap.dedent(<EOL>"""<STR_LIT>""") % (br, '<STR_LIT:\n>'.join(commands), rex_code, br)<EOL>print_debug(msg)<EOL><DEDENT>return rex_code<EOL>
Converts old-style package commands into equivalent Rex code.