Skip to content

Instantly share code, notes, and snippets.

@OCHyams
Created April 23, 2021 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OCHyams/8255efe7757cac266440ed2ba55f1442 to your computer and use it in GitHub Desktop.
Save OCHyams/8255efe7757cac266440ed2ba55f1442 to your computer and use it in GitHub Desktop.
[dexter] Replace old style line label references (='label') with the new style (=ref('label')).
# Replace old style line label references (='label') with the new style (=ref('label')).
import os
import re
import sys
def help_and_quit():
print('usage: python3 make_label_refs.py <directory|file>')
exit(1)
try:
path = sys.argv[1]
except:
help_and_quit()
# DexExpectWatchValue and DexExpectWatchType line number fields:
# on_line='label'
# to_line='label'
# from_line='label'
# ^--------^^-----^
# <arg> <label>
watch_patt = re.compile(r'(?P<arg>(to|from|on)_line\s*=\s*)(?P<label>(\'|")(.+?)(\'|"))', re.M)
# DexExpectProgramState line number fields:
# 'lineno': 'label'
# ^--------^^-----^
# <arg> <label>
state_patt = re.compile(r'(?P<arg>(\'|")\s*lineno\s*(\'|")\s*:\s*)(?P<label>(\'|")(.+?)(\'|"))', re.M)
def replace_in_text(text):
text, changes1 = re.subn(watch_patt, r'\g<arg>ref(\g<label>)', text)
text, changes2 = re.subn(state_patt, r'\g<arg>ref(\g<label>)', text)
return (text, changes1 + changes2)
def update_in_file(file):
with open(file, 'r+') as f:
try:
text = f.read()
except:
print(f'Can\'t read {file}')
return
text, changes = replace_in_text(text)
if changes != 0:
print(f'Modifying {file}')
with open(file, 'w') as f:
f.write(text)
if os.path.isdir(path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
update_in_file(os.path.join(dirpath, filename))
elif os.path.isfile(path):
update_in_file(path)
else:
help_and_quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment