Skip to content

Commit

Permalink
Add -b BRANCH argument to the compare and restore commands
Browse files Browse the repository at this point in the history
Adding an optional -b BRANCH argument to build_examples.py to enable
comparing to and restoring from any branch or commit. That will help
when e.g. reversing an unfortunate commit of generated files.

This solves part 2 of issue #167.
  • Loading branch information
kvid authored and formatc1702 committed Oct 11, 2020
1 parent df90d83 commit b14f5cb
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/wireviz/build_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
from wv_helper import open_file_write, open_file_read, open_file_append


dir = script_path.parent.parent.parent
readme = 'readme.md'
groups = {
'examples': {
'path': Path(script_path).parent.parent.parent / 'examples',
'path': dir / 'examples',
'prefix': 'ex',
readme: [], # Include no files
'title': 'Example Gallery',
},
'tutorial' : {
'path': Path(script_path).parent.parent.parent / 'tutorial',
'path': dir / 'tutorial',
'prefix': 'tutorial',
readme: ['md', 'yml'], # Include .md and .yml files
'title': 'WireViz Tutorial',
},
'demos' : {
'path': Path(script_path).parent.parent.parent / 'examples',
'path': dir / 'examples',
'prefix': 'demo',
},
}
Expand Down Expand Up @@ -96,17 +97,21 @@ def clean_generated(groupkeys):
os.remove(filename)


def compare_generated(groupkeys, include_graphviz_output = False):
def compare_generated(groupkeys, branch = '', include_graphviz_output = False):
if branch:
branch = f' {branch.strip()}'
compare_extensions = generated_extensions if include_graphviz_output else extensions_not_containing_graphviz_output
for key in groupkeys:
# collect and compare files
for filename in collect_filenames('Comparing', key, compare_extensions):
cmd = f'git --no-pager diff "{filename}"'
cmd = f'git --no-pager diff{branch} -- "{filename}"'
print(f' {cmd}')
os.system(cmd)


def restore_generated(groupkeys):
def restore_generated(groupkeys, branch = ''):
if branch:
branch = f' {branch.strip()}'
for key in groupkeys:
# collect input YAML files
filename_list = collect_filenames('Restoring', key, input_extensions)
Expand All @@ -116,7 +121,7 @@ def restore_generated(groupkeys):
filename_list.append(groups[key]['path'] / readme)
# restore files
for filename in filename_list:
cmd = f'git checkout -- "{filename}"'
cmd = f'git checkout{branch} -- "{filename}"'
print(f' {cmd}')
os.system(cmd)

Expand All @@ -128,6 +133,8 @@ def parse_args():
help='what to do with the generated files (default: build)')
parser.add_argument('-c', '--compare-graphviz-output', action='store_true',
help='the Graphviz output is also compared (default: False)')
parser.add_argument('-b', '--branch', action='store', default='',
help='branch or commit to compare with or restore from')
parser.add_argument('-g', '--groups', nargs='+',
choices=groups.keys(), default=groups.keys(),
help='the groups of generated files (default: all)')
Expand All @@ -141,9 +148,9 @@ def main():
elif args.action == 'clean':
clean_generated(args.groups)
elif args.action == 'compare':
compare_generated(args.groups, args.compare_graphviz_output)
compare_generated(args.groups, args.branch, args.compare_graphviz_output)
elif args.action == 'restore':
restore_generated(args.groups)
restore_generated(args.groups, args.branch)


if __name__ == '__main__':
Expand Down

0 comments on commit b14f5cb

Please sign in to comment.