Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | # Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """ |
| 5 | Helper to manage DEPS. |
| 6 | """ |
| 7 | |
| 8 | import os |
| 9 | import os.path as path |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 10 | import json |
Yang Guo | 9117835 | 2019-10-31 08:50:19 +0100 | [diff] [blame] | 11 | import shutil |
| 12 | import subprocess |
| 13 | import sys |
| 14 | |
| 15 | scripts_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 16 | sys.path.append(scripts_path) |
| 17 | |
| 18 | import devtools_paths |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 19 | |
| 20 | # List all DEPS here. |
| 21 | DEPS = { |
| 22 | "@types/chai": "4.2.0", |
| 23 | "@types/mocha": "5.2.7", |
| 24 | "chai": "4.2.0", |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 25 | "escodegen": "1.12.0", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 26 | "eslint": "6.0.1", |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 27 | "esprima": "git+https://git@github.com/ChromeDevTools/esprima.git#4d0f0e18bd8d3731e5f931bf573af3394cbf7cbe", |
| 28 | "handlebars": "^4.3.1", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 29 | "karma": "4.2.0", |
| 30 | "karma-chai": "0.1.0", |
| 31 | "karma-chrome-launcher": "3.1.0", |
Paul Lewis | d903909 | 2019-11-27 17:06:23 +0000 | [diff] [blame^] | 32 | "karma-coverage-istanbul-instrumenter": "1.0.1", |
| 33 | "karma-coverage-istanbul-reporter": "2.1.0", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 34 | "karma-mocha": "1.3.0", |
| 35 | "karma-typescript": "4.1.1", |
| 36 | "mocha": "6.2.0", |
Paul Lewis | d903909 | 2019-11-27 17:06:23 +0000 | [diff] [blame^] | 37 | "puppeteer": "2.0.0", |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 38 | "rollup": "^1.23.1", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 39 | "typescript": "3.5.3" |
| 40 | } |
| 41 | |
| 42 | |
| 43 | def popen(arguments, cwd=None): |
| 44 | return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 45 | |
| 46 | |
| 47 | def clean_node_modules(): |
| 48 | # Clean the node_modules folder first. That way only the packages listed above |
| 49 | # (and their deps) are installed. |
| 50 | try: |
| 51 | shutil.rmtree(path.realpath(devtools_paths.node_modules_path())) |
| 52 | except OSError as err: |
| 53 | print('Error removing node_modules: %s, %s' % (err.filename, err.strerror)) |
| 54 | |
| 55 | |
| 56 | def strip_private_fields(): |
| 57 | # npm adds private fields which need to be stripped. |
| 58 | pattern = path.join(devtools_paths.node_modules_path(), 'package.json') |
| 59 | packages = [] |
| 60 | for root, dirnames, filenames in os.walk(devtools_paths.node_modules_path()): |
| 61 | for filename in filter(lambda f: f == 'package.json', filenames): |
| 62 | packages.append(path.join(root, filename)) |
| 63 | |
| 64 | for pkg in packages: |
| 65 | with open(pkg, 'r+') as pkg_file: |
| 66 | prop_removal_count = 0 |
| 67 | try: |
| 68 | pkg_data = json.load(pkg_file) |
| 69 | |
| 70 | # Remove anything that begins with an underscore, as these are |
| 71 | # the private fields in a package.json |
| 72 | for key in pkg_data.keys(): |
| 73 | if key.find(u'_') == 0: |
| 74 | pkg_data.pop(key) |
| 75 | prop_removal_count = prop_removal_count + 1 |
| 76 | |
| 77 | pkg_file.truncate(0) |
| 78 | pkg_file.seek(0) |
| 79 | json.dump(pkg_data, pkg_file, indent=2, sort_keys=True) |
| 80 | print("(%s): %s" % (prop_removal_count, pkg)) |
| 81 | except: |
| 82 | print('Unable to fix: %s' % pkg) |
| 83 | return True |
| 84 | |
| 85 | return False |
| 86 | |
| 87 | |
Paul Lewis | d903909 | 2019-11-27 17:06:23 +0000 | [diff] [blame^] | 88 | def append_package_json_entries(): |
| 89 | with open(devtools_paths.package_json_path(), 'r+') as pkg_file: |
| 90 | try: |
| 91 | pkg_data = json.load(pkg_file) |
| 92 | |
| 93 | # Replace the dev deps. |
| 94 | pkg_data[u'devDependencies'] = DEPS |
| 95 | |
| 96 | pkg_file.truncate(0) |
| 97 | pkg_file.seek(0) |
| 98 | json.dump(pkg_data, pkg_file, indent=2, sort_keys=True) |
| 99 | |
| 100 | except: |
| 101 | print('Unable to fix: %s' % sys.exc_info()[0]) |
| 102 | return True |
| 103 | return False |
| 104 | |
| 105 | |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 106 | def remove_package_json_entries(): |
| 107 | with open(devtools_paths.package_json_path(), 'r+') as pkg_file: |
| 108 | try: |
| 109 | pkg_data = json.load(pkg_file) |
| 110 | |
| 111 | # Remove the dependencies and devDependencies from the root package.json |
| 112 | # so that they can't be used to overwrite the node_modules managed by this file. |
| 113 | for key in pkg_data.keys(): |
| 114 | if key.find(u'dependencies') == 0 or key.find(u'devDependencies') == 0: |
| 115 | pkg_data.pop(key) |
| 116 | |
| 117 | pkg_file.truncate(0) |
| 118 | pkg_file.seek(0) |
| 119 | json.dump(pkg_data, pkg_file, indent=2, sort_keys=True) |
| 120 | except: |
| 121 | print('Unable to fix: %s' % pkg) |
| 122 | return True |
| 123 | return False |
| 124 | |
| 125 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 126 | def install_deps(): |
| 127 | clean_node_modules() |
| 128 | |
Paul Lewis | d903909 | 2019-11-27 17:06:23 +0000 | [diff] [blame^] | 129 | errors_found = append_package_json_entries() |
| 130 | if errors_found: |
| 131 | return True |
| 132 | |
| 133 | # Run the CI version of npm, which prevents updates to the versions of modules. |
| 134 | exec_command = ['npm', 'ci'] |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 135 | |
| 136 | errors_found = False |
| 137 | npm_proc_result = subprocess.check_call(exec_command, cwd=devtools_paths.root_path()) |
| 138 | if npm_proc_result != 0: |
| 139 | errors_found = True |
| 140 | |
| 141 | # If npm fails, bail here, otherwise attempt to strip private fields. |
| 142 | if errors_found: |
| 143 | return True |
| 144 | |
| 145 | errors_found = strip_private_fields() |
Paul Lewis | 75090cf | 2019-10-25 14:13:11 +0100 | [diff] [blame] | 146 | if errors_found: |
| 147 | return True |
| 148 | |
| 149 | errors_found = remove_package_json_entries() |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 150 | return errors_found |
| 151 | |
| 152 | |
| 153 | npm_errors_found = install_deps() |
| 154 | |
| 155 | if npm_errors_found: |
| 156 | print('npm installation failed') |
| 157 | else: |
| 158 | print('npm installation successful') |