blob: e16071d7e01c5a08b9e72241ee5275afffccca08 [file] [log] [blame]
Tim van der Lippe8e12f922020-12-03 16:33:501#!/usr/bin/env vpython
Jan Schefflera93c4372020-10-28 15:10:592#
3# Copyright 2020 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import urllib
8import tarfile
9import os
10import re
11import sys
12import subprocess
13import json
14import shutil
15
16scripts_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17sys.path.append(scripts_path)
18
19import devtools_paths
20
21package_info = json.load(
22 urllib.urlopen('https://registry.npmjs.org/puppeteer/latest'))
23url = package_info['dist']['tarball']
24
25file = urllib.urlretrieve(url, filename=None)[0]
26tar = tarfile.open(file, 'r:gz')
27members = [
28 member for member in tar.getmembers()
29 if not member.name.startswith('package/lib/cjs/')
30]
31path = devtools_paths.root_path() + '/front_end/third_party/puppeteer'
32shutil.rmtree(path + '/package')
33tar.extractall(path=path, members=members)
34
35members = [m.name for m in members]
36
37
38def getStartAndEndIndexForGNVariable(content, variable):
39 startIndex = None
40 endIndex = None
41 startPattern = re.compile(r'\s*' + variable + '\s*=\s*\[\s*')
42 endPattern = re.compile(r'\s*\]\s*')
43 for i, line in enumerate(content):
44 if startPattern.match(line):
45 startIndex = i + 1
46 break
47
48 if startIndex is None:
49 raise BaseException(variable + ' not found')
50
51 for i, line in enumerate(content[startIndex:], start=startIndex):
52 if endPattern.match(line):
53 endIndex = i
54 break
55
56 return startIndex, endIndex
57
58
59def readGNVariable(filename, variable):
60 with open(filename) as f:
61 content = f.readlines()
62 startIndex, endIndex = getStartAndEndIndexForGNVariable(
63 content, variable)
64 return [x.strip(' \t",\'\n') for x in content[startIndex:endIndex]]
65
66
67def updateGNVariable(file, variable, files):
68 content = None
69 with open(file) as f:
70 content = f.readlines()
71
72 files.sort()
73 startIndex, endIndex = getStartAndEndIndexForGNVariable(content, variable)
74 newContent = content[:startIndex] + [' "' + x + '",\n'
75 for x in files] + content[endIndex:]
76
77 with open(file, 'w') as f:
78 f.write(''.join(newContent))
79
80
81updateGNVariable('./front_end/third_party/puppeteer/BUILD.gn', 'sources', [
82 'puppeteer-tsconfig.json',
83] + [
84 name for name in members
85 if name.startswith('package/lib/esm/') and (name.endswith(
86 '.js') or name.endswith('.d.ts') or name.endswith('.d.ts.map'))
87])
88
Tim van der Lippe732be582021-04-09 15:58:5589prev = readGNVariable('./config/gni/devtools_grd_files.gni',
90 'grd_files_debug_sources')
Jan Schefflera93c4372020-10-28 15:10:5991tmp = [
92 'front_end/third_party/puppeteer/' + name for name in members
93 if name.startswith('package/lib/esm/') and name.endswith('.js')
94] + [
95 name
96 for name in prev if not name.startswith('front_end/third_party/puppeteer')
97]
98
Tim van der Lippe732be582021-04-09 15:58:5599updateGNVariable('./config/gni/devtools_grd_files.gni',
100 'grd_files_debug_sources', tmp)
Jan Schefflera93c4372020-10-28 15:10:59101
Jan Schefflera93c4372020-10-28 15:10:59102tar.close()
103
104subprocess.check_call(['git', 'cl', 'format', '--js'],
105 cwd=devtools_paths.root_path())