copy_fofn.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import argparse
  2. import logging
  3. import os
  4. import sys
  5. from .. import io
  6. LOG = logging.getLogger()
  7. def run(abs, in_fn, out_fn):
  8. out_dir = os.path.normpath(os.path.dirname(out_fn))
  9. io.mkdirs(out_dir)
  10. def identity(fn): return fn
  11. def relative(fn): return os.path.relpath(fn, out_dir)
  12. adjusted_fn = identity if abs else relative
  13. with open(out_fn, 'w') as stream:
  14. for abs_fn in io.yield_abspath_from_fofn(in_fn):
  15. fn = adjusted_fn(abs_fn)
  16. stream.write('{}\n'.format(fn))
  17. class HelpF(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
  18. pass
  19. def parse_args(argv):
  20. description = 'Copy FOFN. If directory changes, then relative paths must change too.'
  21. epilog = ''
  22. parser = argparse.ArgumentParser(
  23. description=description,
  24. epilog=epilog,
  25. formatter_class=HelpF,
  26. )
  27. parser.add_argument(
  28. '--in-fn',
  29. help='Input. FOFN of paths relative to its own directory.'
  30. )
  31. parser.add_argument(
  32. '--abs', action='store_true',
  33. help='Store absolute paths. (Otherwise, paths will be relative to directory of output FOFN.)'
  34. )
  35. parser.add_argument(
  36. '--out-fn',
  37. help='Output. FOFN of paths relative to its own directory.'
  38. )
  39. args = parser.parse_args(argv[1:])
  40. return args
  41. def main(argv=sys.argv):
  42. args = parse_args(argv)
  43. logging.basicConfig(level=logging.INFO)
  44. run(**vars(args))
  45. if __name__ == '__main__': # pragma: no cover
  46. main()