xref: /third_party/node/deps/zlib/GN-scraper.py (revision 1cb0ef41)
1# Copyright (c) 2019 Refael Ackeramnn<refack@gmail.com>. All rights reserved.
2# Use of this source code is governed by an MIT-style license.
3import re
4import os
5import sys
6
7PLAIN_SOURCE_RE = re.compile('\s*"([^/$].+)"\s*')
8def DoMain(args):
9  gn_filename, pattern = args
10  src_root = os.path.dirname(gn_filename)
11  with open(gn_filename, 'rb') as gn_file:
12    gn_content = gn_file.read().decode('utf-8')
13
14  scraper_re = re.compile(pattern + r'\[([^\]]+)', re.DOTALL)
15  matches = scraper_re.search(gn_content)
16  match = matches.group(1)
17  files = []
18  for l in match.splitlines():
19    m2 = PLAIN_SOURCE_RE.match(l)
20    if not m2:
21      continue
22    files.append(m2.group(1))
23  # always use `/` since GYP will process paths further downstream
24  rel_files = ['"%s/%s"' % (src_root, f) for f in files]
25  return ' '.join(rel_files)
26
27if __name__ == '__main__':
28  print(DoMain(sys.argv[1:]))
29