large_files.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import json
  2. import os
  3. import ymake
  4. from _common import strip_roots, resolve_common_const
  5. PLACEHOLDER_EXT = "external"
  6. def onlarge_files(unit, *args):
  7. """
  8. @usage LARGE_FILES([AUTOUPDATED] Files...)
  9. Use large file ether from working copy or from remote storage via placeholder <File>.external
  10. If <File> is present locally (and not a symlink!) it will be copied to build directory.
  11. Otherwise macro will try to locate <File>.external, parse it retrieve ot during build phase.
  12. """
  13. args = list(args)
  14. if args and args[0] == 'AUTOUPDATED':
  15. args = args[1:]
  16. for arg in args:
  17. if arg == 'AUTOUPDATED':
  18. unit.message(["warn", "Please set AUTOUPDATED argument before other file names"])
  19. continue
  20. src = unit.resolve_arc_path(arg)
  21. if src.startswith("$S"):
  22. msg = "Used local large file {}. Don't forget to run 'ya upload --update-external' and commit {}.{}".format(
  23. src, src, PLACEHOLDER_EXT
  24. )
  25. unit.message(["warn", msg])
  26. unit.oncopy_file([arg, arg])
  27. else:
  28. external = "{}.{}".format(arg, PLACEHOLDER_EXT)
  29. rel_placeholder = resolve_common_const(unit.resolve_arc_path(external))
  30. if not rel_placeholder.startswith("$S"):
  31. ymake.report_configure_error('LARGE_FILES: neither actual data nor placeholder is found for "{}"'.format(arg))
  32. return
  33. try:
  34. abs_placeholder = unit.resolve(rel_placeholder)
  35. with open(abs_placeholder, "r") as f:
  36. res_desc = json.load(f)
  37. storage = res_desc["storage"]
  38. res_id = res_desc["resource_id"]
  39. except e:
  40. ymake.report_configure_error('LARGE_FILES: error processing placeholder file "{}.": {}'.format(external, e))
  41. return
  42. from_cmd = ['FILE', '{}'.format(res_id), 'OUT_NOAUTO', arg, 'EXTERNAL_FILE', external]
  43. if os.path.dirname(arg):
  44. from_cmd.extend(("RENAME", os.path.basename(arg)))
  45. method = getattr(unit, 'onfrom_{}'.format(storage.lower()), None)
  46. if method:
  47. method(from_cmd)
  48. else:
  49. ymake.report_configure_error('LARGE_FILES: error processing placeholder file "{}.": unknown storage kind "{}"'.format(external, storage))