vcs-check.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. import re
  3. import os
  4. import os.path
  5. import sys
  6. import shlex
  7. import pandas
  8. from subprocess import check_output, run, PIPE, DEVNULL
  9. from io import StringIO
  10. def check_output_lines(*args, **kwargs):
  11. return check_output(*args, **kwargs).rstrip('\n').split('\n')
  12. git_tracked_files = set(check_output_lines(
  13. ["git", "ls-tree", "-r", "HEAD", "--name-only"],
  14. text = True))
  15. snakemake_untracked_files = set(run(
  16. ["snakemake", "--list-untracked"],
  17. capture_output = True, text = True, check = True
  18. ).stderr.rstrip('\n').split('\n'))
  19. all_files = set()
  20. for curdir, subdirs, files in os.walk('.'):
  21. subdirs[:] = [d for d in subdirs if not d.startswith(".")]
  22. for f in files:
  23. if f.startswith('.'):
  24. continue
  25. all_files.add(os.path.normpath(os.path.join(curdir, f)))
  26. snakemake_summary_output = check_output(['snakemake', '--summary'], text = True, stderr=DEVNULL)
  27. snakemake_summary_table = pandas.read_csv(StringIO(snakemake_summary_output), sep='\t')
  28. snakemake_generated_files = {os.path.normpath(f) for f in snakemake_summary_table.output_file}
  29. input_files = all_files - (snakemake_untracked_files | snakemake_generated_files)
  30. untracked_input_files = input_files - git_tracked_files
  31. if untracked_input_files:
  32. print("Untracked input files detected. Run the following command to add them to git:\ngit add " + " ".join(shlex.quote(f) for f in untracked_input_files),
  33. file=sys.stderr)
  34. sys.exit(1)
  35. else:
  36. print("All input files known to be used in the workflow are tracked in git.")
  37. sys.exit(0)