large_files.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import json
  2. import os
  3. import ymake
  4. from _common import 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(
  32. 'LARGE_FILES: neither actual data nor placeholder is found for "{}"'.format(arg)
  33. )
  34. return
  35. try:
  36. abs_placeholder = unit.resolve(rel_placeholder)
  37. with open(abs_placeholder, "r") as f:
  38. res_desc = json.load(f)
  39. storage = res_desc["storage"]
  40. res_id = res_desc["resource_id"]
  41. except Exception as e:
  42. ymake.report_configure_error(
  43. 'LARGE_FILES: error processing placeholder file "{}.": {}'.format(external, e)
  44. )
  45. return
  46. from_cmd = ['FILE', '{}'.format(res_id), 'OUT_NOAUTO', arg, 'EXTERNAL_FILE', external]
  47. if os.path.dirname(arg):
  48. from_cmd.extend(("RENAME", os.path.basename(arg)))
  49. method = getattr(unit, 'onfrom_{}'.format(storage.lower()), None)
  50. if method:
  51. method(from_cmd)
  52. else:
  53. ymake.report_configure_error(
  54. 'LARGE_FILES: error processing placeholder file "{}.": unknown storage kind "{}"'.format(
  55. external, storage
  56. )
  57. )