cromwell_undot.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import argparse
  2. import glob
  3. import logging
  4. import os
  5. import sys
  6. from .. import io
  7. LOG = logging.getLogger()
  8. def rename(fn, prefix):
  9. dn, bn = os.path.split(fn)
  10. nfn = os.path.join(dn, prefix + bn)
  11. cmd = 'mv -f {} {}'.format(fn, nfn)
  12. LOG.info('cmd ={!r}'.format(cmd))
  13. io.syscall(cmd)
  14. def run(pattern, prefix):
  15. for fn in glob.iglob(pattern):
  16. rename(fn, prefix)
  17. class HelpF(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
  18. pass
  19. def parse_args(argv):
  20. description = 'Find all files matching "pattern" under CWD, and prefix with "prefix".'
  21. epilog = 'We do this because Cromwell does not properly glob dot-files.'
  22. parser = argparse.ArgumentParser(
  23. description=description,
  24. epilog=epilog,
  25. formatter_class=HelpF,
  26. )
  27. parser.add_argument(
  28. '--pattern',
  29. help='Find all files matching this, including in subdirs.')
  30. parser.add_argument(
  31. '--prefix', default='dot',
  32. help='Rename the matching files to have this prefix. E.g. ".foo" becomes "PREFIX.foo".')
  33. args = parser.parse_args(argv[1:])
  34. return args
  35. def main(argv=sys.argv):
  36. args = parse_args(argv)
  37. logging.basicConfig(level=logging.INFO)
  38. run(**vars(args))
  39. if __name__ == '__main__': # pragma: no cover
  40. main()