generic_unsplit.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import argparse
  2. import logging
  3. import os
  4. import sys
  5. from .. import io
  6. LOG = logging.getLogger()
  7. def run(result_fn_list_fn, gathered_fn):
  8. thatdir = os.path.dirname(result_fn_list_fn)
  9. thisdir = os.path.dirname(gathered_fn)
  10. result_fn_list = io.deserialize(result_fn_list_fn)
  11. io.serialize(gathered_fn, result_fn_list)
  12. gathered_dn = os.path.dirname(gathered_fn)
  13. gathered = list()
  14. for result_fn in result_fn_list:
  15. some_results = io.deserialize(result_fn)
  16. d = os.path.abspath(os.path.dirname(result_fn))
  17. def abspath(v):
  18. if v.startswith('.'):
  19. return os.path.normpath(os.path.relpath(os.path.join(d, v), gathered_dn))
  20. else:
  21. return v # apparently not a path
  22. # By construction, this is a list of dicts of k:output,
  23. # where outputs are relative to the location of result_fn.
  24. some_abs_results = list()
  25. for one in some_results:
  26. for v in one.values():
  27. assert not v.startswith('/'), '{!r} was expected to be relative'.format(v)
  28. abs_one = {k: abspath(v) for k,v in list(one.items())}
  29. some_abs_results.append(abs_one)
  30. gathered.extend(some_abs_results)
  31. io.serialize(gathered_fn, gathered)
  32. class HelpF(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
  33. pass
  34. def parse_args(argv):
  35. description = 'Gather the contents of contents of result-lists into a single gathered-list.'
  36. epilog = 'results-list is known already, so that is a pseudo output. Its filenames point to the actual, unknown results.'
  37. # Question: Do we need to know the wildcards for each result?
  38. parser = argparse.ArgumentParser(
  39. description=description,
  40. epilog=epilog,
  41. formatter_class=HelpF,
  42. )
  43. parser.add_argument(
  44. '--result-fn-list-fn',
  45. help='Input: Combined list of filenames of results (pseudo output, expected to exist already in our run-dir)')
  46. parser.add_argument(
  47. '--gathered-fn',
  48. help='Output: serialized something-or-other')
  49. args = parser.parse_args(argv[1:])
  50. return args
  51. def main(argv=sys.argv):
  52. args = parse_args(argv)
  53. logging.basicConfig(level=logging.INFO)
  54. run(**vars(args))
  55. if __name__ == '__main__': # pragma: no cover
  56. main()