res.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import json
  2. import os
  3. import six
  4. from _common import rootrel_arc_src
  5. import ymake
  6. def remove_prefix(text, prefix):
  7. if text.startswith(prefix):
  8. return text[len(prefix) :]
  9. return text
  10. def onresource_files(unit, *args):
  11. """
  12. @usage: RESOURCE_FILES([PREFIX {prefix}] [STRIP prefix_to_strip] {path})
  13. This macro expands into
  14. RESOURCE(DONT_PARSE {path} resfs/file/{prefix}{path}
  15. - resfs/src/resfs/file/{prefix}{remove_prefix(path, prefix_to_strip)}={rootrel_arc_src(path)}
  16. )
  17. resfs/src/{key} stores a source root (or build root) relative path of the
  18. source of the value of the {key} resource.
  19. resfs/file/{key} stores any value whose source was a file on a filesystem.
  20. resfs/src/resfs/file/{key} must store its path.
  21. DONT_PARSE disables parsing for source code files (determined by extension)
  22. Please don't abuse: use separate DONT_PARSE macro call only for files subject to parsing
  23. This form is for use from other plugins:
  24. RESOURCE_FILES([DEST {dest}] {path}) expands into RESOURCE({path} resfs/file/{dest})
  25. @see: https://wiki.yandex-team.ru/devtools/commandsandvars/resourcefiles/
  26. """
  27. prefix = ''
  28. prefix_to_strip = None
  29. dest = None
  30. res = []
  31. if args and not unit.enabled('_GO_MODULE'):
  32. # GO_RESOURCE currently doesn't support DONT_PARSE
  33. res.append('DONT_PARSE')
  34. args = iter(args)
  35. for arg in args:
  36. if arg == 'PREFIX':
  37. prefix, dest = next(args), None
  38. elif arg == 'DEST':
  39. dest, prefix = next(args), None
  40. elif arg == 'STRIP':
  41. prefix_to_strip = next(args)
  42. else:
  43. path = arg
  44. key = 'resfs/file/' + (
  45. dest or (prefix + (path if not prefix_to_strip else remove_prefix(path, prefix_to_strip)))
  46. )
  47. if key in res:
  48. unit.message(
  49. ['warn', "Dublicated resource file {} in RESOURCE_FILES() macro. Skipped it.".format(path)]
  50. )
  51. continue
  52. src = 'resfs/src/{}={}'.format(key, rootrel_arc_src(path, unit))
  53. res += ['-', src, path, key]
  54. if unit.enabled('_GO_MODULE'):
  55. unit.on_go_resource(res)
  56. else:
  57. unit.onresource(res)
  58. def on_all_resource_files(unit, macro, *args):
  59. # This is only validation, actual work is done in ymake.core.conf implementation
  60. for arg in args:
  61. if '*' in arg or '?' in arg:
  62. ymake.report_configure_error('Wildcards in [[imp]]{}[[rst]] are not allowed'.format(macro))
  63. def onall_resource_files(unit, *args):
  64. on_all_resource_files(unit, 'ALL_RESOURCE_FILES', args)
  65. def onall_resource_files_from_dirs(unit, *args):
  66. on_all_resource_files(unit, 'ALL_RESOURCE_FILES_FROM_DIRS', args)
  67. def on_ya_conf_json(unit, conf_file):
  68. conf_abs_path = unit.resolve('$S/' + conf_file)
  69. if not os.path.exists(conf_abs_path):
  70. ymake.report_configure_error('File "{}" not found'.format(conf_abs_path))
  71. return
  72. # conf_file should be passed to the RESOURCE_FILES macro without path.
  73. # To resolve it later by name only we must add it's path to SRCDIR().
  74. conf_dir = os.path.dirname(conf_file)
  75. if conf_dir:
  76. unit.onsrcdir(conf_dir)
  77. unit.onresource_files(os.path.basename(conf_file))
  78. valid_dirs = (
  79. "build",
  80. conf_dir,
  81. )
  82. with open(conf_abs_path) as f:
  83. conf = json.load(f)
  84. formulas = set()
  85. for bottle_name, bottle in conf['bottles'].items():
  86. formula = bottle['formula']
  87. if isinstance(formula, six.string_types):
  88. if formula.startswith(valid_dirs):
  89. abs_path = unit.resolve('$S/' + formula)
  90. if os.path.exists(abs_path):
  91. formulas.add(formula)
  92. else:
  93. ymake.report_configure_error(
  94. 'File "{}" (referenced from bottle "{}" in "{}") is not found'.format(
  95. abs_path, bottle_name, conf_abs_path
  96. )
  97. )
  98. else:
  99. ymake.report_configure_error(
  100. 'File "{}" (referenced from bottle "{}" in "{}") must be located in "{}" file tree'.format(
  101. formula, bottle_name, conf_file, '" or "'.join(valid_dirs)
  102. )
  103. )
  104. for formula in sorted(formulas):
  105. unit.onresource_files(formula)