blob: 37ee3c0c64abb4b7285232ce71c7dd508384d813 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001# 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"""
5Helper to manage DEPS.
6"""
7
8import os
9import os.path as path
Yang Guo4fd355c2019-09-19 10:59:03 +020010import json
Yang Guo91178352019-10-31 08:50:19 +010011import shutil
12import subprocess
13import sys
14
15scripts_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16sys.path.append(scripts_path)
17
18import devtools_paths
Yang Guo4fd355c2019-09-19 10:59:03 +020019
20# List all DEPS here.
21DEPS = {
22 "@types/chai": "4.2.0",
23 "@types/mocha": "5.2.7",
24 "chai": "4.2.0",
Paul Lewis75090cf2019-10-25 14:13:11 +010025 "escodegen": "1.12.0",
Yang Guo4fd355c2019-09-19 10:59:03 +020026 "eslint": "6.0.1",
Paul Lewis75090cf2019-10-25 14:13:11 +010027 "esprima": "git+https://git@github.com/ChromeDevTools/esprima.git#4d0f0e18bd8d3731e5f931bf573af3394cbf7cbe",
28 "handlebars": "^4.3.1",
Yang Guo4fd355c2019-09-19 10:59:03 +020029 "karma": "4.2.0",
30 "karma-chai": "0.1.0",
31 "karma-chrome-launcher": "3.1.0",
Paul Lewisd9039092019-11-27 17:06:23 +000032 "karma-coverage-istanbul-instrumenter": "1.0.1",
33 "karma-coverage-istanbul-reporter": "2.1.0",
Yang Guo4fd355c2019-09-19 10:59:03 +020034 "karma-mocha": "1.3.0",
35 "karma-typescript": "4.1.1",
36 "mocha": "6.2.0",
Paul Lewisd9039092019-11-27 17:06:23 +000037 "puppeteer": "2.0.0",
Paul Lewis75090cf2019-10-25 14:13:11 +010038 "rollup": "^1.23.1",
Yang Guo4fd355c2019-09-19 10:59:03 +020039 "typescript": "3.5.3"
40}
41
42
43def popen(arguments, cwd=None):
44 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
45
46
47def 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
56def 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 Lewisd9039092019-11-27 17:06:23 +000088def 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 Lewis75090cf2019-10-25 14:13:11 +0100106def 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 Guo4fd355c2019-09-19 10:59:03 +0200126def install_deps():
127 clean_node_modules()
128
Paul Lewisd9039092019-11-27 17:06:23 +0000129 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 Guo4fd355c2019-09-19 10:59:03 +0200135
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 Lewis75090cf2019-10-25 14:13:11 +0100146 if errors_found:
147 return True
148
149 errors_found = remove_package_json_entries()
Yang Guo4fd355c2019-09-19 10:59:03 +0200150 return errors_found
151
152
153npm_errors_found = install_deps()
154
155if npm_errors_found:
156 print('npm installation failed')
157else:
158 print('npm installation successful')