Skip to content

Commit

Permalink
Quick fix on pipe symble and export cmd
Browse files Browse the repository at this point in the history
Pipe symbol '|' should not be seperated.
Command 'export' can be treated as variable assignment.

Works towards #682 and #706.

Signed-off-by: WangJL <hazard15020@gmail.com>
  • Loading branch information
ForgetMe17 committed Jun 18, 2020
1 parent c090e49 commit 5be24e5
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tern/utils/general.py
Expand Up @@ -198,7 +198,7 @@ def split_shell_script(shell_script):
# pattern for skipping single and double quote
skip_pattern = r"\".*?\"(*SKIP)(*F)|'.*?'(*SKIP)(*F)"
# pattern for split a concatenated command
match_pattern = r':;|&&|;|\|\||\|'
match_pattern = r':;|&&|;|\|\|'
pattern = skip_pattern + '|' + match_pattern
concatenated_commands = regex.split(pattern, shell_script)
# use keywords to match loop, branch.
Expand Down Expand Up @@ -248,12 +248,20 @@ def parse_shell_variables_and_command(concatenated_command):
'''given a concatenated command, classify the variable and command type,
and then parse it '''
# pattern for matching variable, looking for '='
variable_pattern = r'^([A-Za-z_][A-Za-z0-9_]*)=(.*)'
assignment_pattern = r'^([A-Za-z_][A-Za-z0-9_]*)=(.*)'
export_pattern = r'^export ([A-Za-z_][A-Za-z0-9_]*)=(.*)'
variable_pattern = assignment_pattern + r'|' + export_pattern
match_res = re.match(variable_pattern, concatenated_command)
statement = {}
if match_res:
statement['variable'] = {'name': match_res.group(1),
'value': match_res.group(2)}
if match_res.group(1):
# assignment_pattern matched
statement['variable'] = {'name': match_res.group(1),
'value': match_res.group(2)}
else:
# export_pattern matched
statement['variable'] = {'name': match_res.group(3),
'value': match_res.group(4)}
statement['content'] = concatenated_command
else:
# use clean_command() to clean tab and line indentations
Expand Down

0 comments on commit 5be24e5

Please sign in to comment.