Tim van der Lippe | 8e12f92 | 2020-12-03 16:33:50 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
Jan Scheffler | a93c437 | 2020-10-28 15:10:59 | [diff] [blame] | 2 | # |
| 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 | |
| 7 | import urllib |
| 8 | import tarfile |
| 9 | import os |
| 10 | import re |
| 11 | import sys |
| 12 | import subprocess |
| 13 | import json |
| 14 | import shutil |
| 15 | |
| 16 | scripts_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 17 | sys.path.append(scripts_path) |
| 18 | |
| 19 | import devtools_paths |
| 20 | |
| 21 | package_info = json.load( |
| 22 | urllib.urlopen('https://registry.npmjs.org/puppeteer/latest')) |
| 23 | url = package_info['dist']['tarball'] |
| 24 | |
| 25 | file = urllib.urlretrieve(url, filename=None)[0] |
| 26 | tar = tarfile.open(file, 'r:gz') |
| 27 | members = [ |
| 28 | member for member in tar.getmembers() |
| 29 | if not member.name.startswith('package/lib/cjs/') |
| 30 | ] |
| 31 | path = devtools_paths.root_path() + '/front_end/third_party/puppeteer' |
| 32 | shutil.rmtree(path + '/package') |
| 33 | tar.extractall(path=path, members=members) |
| 34 | |
| 35 | members = [m.name for m in members] |
| 36 | |
| 37 | |
| 38 | def 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 | |
| 59 | def 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 | |
| 67 | def 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 | |
| 81 | updateGNVariable('./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 Lippe | 732be58 | 2021-04-09 15:58:55 | [diff] [blame] | 89 | prev = readGNVariable('./config/gni/devtools_grd_files.gni', |
| 90 | 'grd_files_debug_sources') |
Jan Scheffler | a93c437 | 2020-10-28 15:10:59 | [diff] [blame] | 91 | tmp = [ |
| 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 Lippe | 732be58 | 2021-04-09 15:58:55 | [diff] [blame] | 99 | updateGNVariable('./config/gni/devtools_grd_files.gni', |
| 100 | 'grd_files_debug_sources', tmp) |
Jan Scheffler | a93c437 | 2020-10-28 15:10:59 | [diff] [blame] | 101 | |
Jan Scheffler | a93c437 | 2020-10-28 15:10:59 | [diff] [blame] | 102 | tar.close() |
| 103 | |
| 104 | subprocess.check_call(['git', 'cl', 'format', '--js'], |
| 105 | cwd=devtools_paths.root_path()) |