generic_gather.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import argparse
  2. import logging
  3. import os
  4. import sys
  5. from .. import io
  6. LOG = logging.getLogger()
  7. def run(gathered_fn, scattered_fn):
  8. thatdir = os.path.dirname(scattered_fn)
  9. thisdir = os.path.dirname(gathered_fn)
  10. scattered = io.deserialize(scattered_fn)
  11. gathered = dict()
  12. for job in scattered:
  13. job_output = dict()
  14. #job_output['wildcards'] = dict()
  15. fn_dict = dict(job['output'])
  16. for key in list(fn_dict.keys()):
  17. # Fix path to be relative to gathered_fn.
  18. fn = fn_dict[key]
  19. if not os.path.isabs(fn):
  20. thatfn = os.path.join(thatdir, fn)
  21. else:
  22. thatfn = fn
  23. thisfn = os.path.relpath(thatfn, thisdir)
  24. fn_dict[key] = thisfn
  25. job_output['fns'] = fn_dict
  26. wildcards = job['wildcards']
  27. wildkey = ','.join('{}={}'.format(k,v) for k,v in sorted(wildcards.items()))
  28. gathered[wildkey] = job_output
  29. io.serialize(gathered_fn, gathered)
  30. class HelpF(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
  31. pass
  32. def parse_args(argv):
  33. description = 'Gather generic filenames into ... something. For now, just serialize.'
  34. epilog = 'We expect the scattered file to have a specific format.'
  35. parser = argparse.ArgumentParser(
  36. description=description,
  37. epilog=epilog,
  38. formatter_class=HelpF,
  39. )
  40. parser.add_argument(
  41. '--scattered-fn',
  42. help='Input: result of scattering',
  43. )
  44. parser.add_argument(
  45. '--gathered-fn',
  46. help='Output: serialized something-or-other',
  47. )
  48. args = parser.parse_args(argv[1:])
  49. return args
  50. def main(argv=sys.argv):
  51. args = parse_args(argv)
  52. logging.basicConfig(level=logging.INFO)
  53. run(**vars(args))
  54. if __name__ == '__main__': # pragma: no cover
  55. main()