blob: b4762f52ef17c32dfd20a99ba7724fa84e3f1bca [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
10import subprocess
11import devtools_paths
12import shutil
13import json
14
15# List all DEPS here.
16DEPS = {
17 "@types/chai": "4.2.0",
18 "@types/mocha": "5.2.7",
19 "chai": "4.2.0",
20 "eslint": "6.0.1",
21 "karma": "4.2.0",
22 "karma-chai": "0.1.0",
23 "karma-chrome-launcher": "3.1.0",
24 "karma-mocha": "1.3.0",
25 "karma-typescript": "4.1.1",
26 "mocha": "6.2.0",
27 "escodegen": "1.12.0",
28 "esprima": "git+https://git@github.com/jquery/esprima.git#fe13460e646a0adc3c434ca8c478264ca2e78cec",
29 "typescript": "3.5.3"
30}
31
32
33def popen(arguments, cwd=None):
34 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
35
36
37def clean_node_modules():
38 # Clean the node_modules folder first. That way only the packages listed above
39 # (and their deps) are installed.
40 try:
41 shutil.rmtree(path.realpath(devtools_paths.node_modules_path()))
42 except OSError as err:
43 print('Error removing node_modules: %s, %s' % (err.filename, err.strerror))
44
45
46def strip_private_fields():
47 # npm adds private fields which need to be stripped.
48 pattern = path.join(devtools_paths.node_modules_path(), 'package.json')
49 packages = []
50 for root, dirnames, filenames in os.walk(devtools_paths.node_modules_path()):
51 for filename in filter(lambda f: f == 'package.json', filenames):
52 packages.append(path.join(root, filename))
53
54 for pkg in packages:
55 with open(pkg, 'r+') as pkg_file:
56 prop_removal_count = 0
57 try:
58 pkg_data = json.load(pkg_file)
59
60 # Remove anything that begins with an underscore, as these are
61 # the private fields in a package.json
62 for key in pkg_data.keys():
63 if key.find(u'_') == 0:
64 pkg_data.pop(key)
65 prop_removal_count = prop_removal_count + 1
66
67 pkg_file.truncate(0)
68 pkg_file.seek(0)
69 json.dump(pkg_data, pkg_file, indent=2, sort_keys=True)
70 print("(%s): %s" % (prop_removal_count, pkg))
71 except:
72 print('Unable to fix: %s' % pkg)
73 return True
74
75 return False
76
77
78def install_deps():
79 clean_node_modules()
80
81 exec_command = ['npm', 'install', '--no-save']
82 for pkg, version in DEPS.items():
83 exec_command.append('%s@%s' % (pkg, version))
84
85 errors_found = False
86 npm_proc_result = subprocess.check_call(exec_command, cwd=devtools_paths.root_path())
87 if npm_proc_result != 0:
88 errors_found = True
89
90 # If npm fails, bail here, otherwise attempt to strip private fields.
91 if errors_found:
92 return True
93
94 errors_found = strip_private_fields()
95 return errors_found
96
97
98npm_errors_found = install_deps()
99
100if npm_errors_found:
101 print('npm installation failed')
102else:
103 print('npm installation successful')