Skip to content

Instantly share code, notes, and snippets.

@lembergerth
Last active January 11, 2019 19:03
Show Gist options
  • Save lembergerth/47569b7579089df49ab3be8d78cd6932 to your computer and use it in GitHub Desktop.
Save lembergerth/47569b7579089df49ab3be8d78cd6932 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
## Usage: Run from directory `sv-benchmarks/c`.
## The script will scan all subdirectories for yml-Files and rename them if they contain any
## property or are duplicates.
import glob
import os
NAME_TO_PROP_AND_SUBPROP = {
'unreach-call': ('properties/unreach-call.prp', None),
'termination': ('properties/termination.prp', None),
'no-overflow': ('properties/no-overflow.prp', None),
'valid-memcleanup': ('properties/valid-memcleanup.prp', None),
'valid-memsafety': ('properties/valid-memsafety.prp', None),
'valid-deref': ('properties/valid-memsafety.prp', 'valid-deref'),
'valid-free': ('properties/valid-memsafety.prp', 'valid-free'),
'valid-memtrack': ('properties/valid-memsafety.prp', 'valid-memtrack'),
'def-behavior': ('properties/def-behavior.prp', None)
}
all_tasks = glob.glob('**/*.yml', recursive=True)
tasks_to_new_names_and_yml = dict()
for task_file in all_tasks:
properties = list()
name_pcs_dot = task_file.split('.')
new_name_pcs_dot = list()
for pd in name_pcs_dot:
name_pcs = pd.split('_')
new_name_pcs = list()
for p in name_pcs:
offset = 0
for name, prop in NAME_TO_PROP_AND_SUBPROP.items():
if name not in p:
continue # with next name_pc p
if p.startswith('true'):
expected = 'true'
offset += len('true-')
elif p.startswith('false'):
expected = 'false'
offset += len('false-')
elif p.startswith('unknown-'):
expected = None
offset += len('unknown-')
else:
continue # with next name_pc p
properties.append((prop, expected))
offset += len(name)
break # for-loop over properties once one matches, because they are distinct
new_p = p[offset:]
if new_p:
new_name_pcs.append(new_p)
new_name_pcs_dot.append("_".join(new_name_pcs))
yml_info = (task_file, properties)
new_task_file = '.'.join(new_name_pcs_dot)
if new_task_file.endswith('.c.yml'):
suffix_offset = 5
elif new_task_file.endswith('.c.i.yml'):
suffix_offset = 7
else:
suffix_offset = 3
new_task_file = new_task_file[:-suffix_offset] + "yml"
tasks_to_new_names_and_yml[task_file] = (new_task_file, yml_info)
for old_name, (curr_task, yml_info) in tasks_to_new_names_and_yml.items():
task_basename = os.path.basename(curr_task)
collisions = [k
for k, v in tasks_to_new_names_and_yml.items()
if os.path.basename(v[0]).lower() == task_basename.lower()
and k != old_name]
if collisions:
curr_task = curr_task[:-4] + '-1' + curr_task[-4:]
for idx, other in enumerate(collisions, 2):
new_name, content = tasks_to_new_names_and_yml.pop(other)
new_name = new_name[:-4] + "-" + str(idx) + new_name[-4:]
tasks_to_new_names_and_yml[other] = (new_name, content)
task_basename = os.path.basename(curr_task)
task_dir = os.path.dirname(curr_task)
if old_name != curr_task:
os.rename(old_name, curr_task)
if os.path.exists(old_name[:-3] + 'i'):
old_c = old_name[:-3] + 'i'
curr_task_name = curr_task.split('/')[-1]
new_c_basename = curr_task_name[:-3] + 'i'
new_c_name = os.path.dirname(old_c) + '/' + new_c_basename
os.rename(old_c, new_c_name)
with open(curr_task) as inp:
content = inp.readlines()
new_content = list()
old_c_basename = os.path.basename(old_c)
for line in content:
if 'input_file' in line:
line = line.replace(old_c_basename, new_c_basename)
new_content.append(line)
with open(curr_task, 'w+') as outp:
outp.writelines(new_content)
if os.path.exists(old_name[:-3] + 'c'):
old_c = old_name[:-3] + 'c'
elif os.path.exists(old_name[:-4]):
old_c = old_name[:-4]
# ldv-memsafety/memleaks*.i -> ldv-memsafety/memleaks-notpreprocessed/memleaks*.c
elif old_name.startswith('ldv-memsafety/memleaks'):
old_c = 'ldv-memsafety/memleaks-notpreprocessed/'\
+ old_name.split('/')[-1][:-3] + 'c'
else:
old_c = None
if old_c:
assert old_c not in all_tasks
curr_task_name = curr_task.split('/')[-1]
new_c_basename = curr_task_name[:-3] + 'c'
new_c_name = os.path.dirname(old_c) + '/' + new_c_basename
with open(curr_task) as inp:
content = inp.readlines()
new_content = list()
old_c_basename = os.path.basename(old_c)
for line in content:
if 'input_file' in line:
line = line.replace(old_c_basename, new_c_basename)
new_content.append(line)
with open(curr_task, 'w+') as outp:
outp.writelines(new_content)
os.rename(old_c, new_c_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment