Commit 0fdf7116 authored by Tim van der Lippe's avatar Tim van der Lippe Committed by Commit Bot

Allow node.py to run any node script

In DevTools, we want to write Node scripts as part of our Ninja action
scripts. That's because we are more familiar with Node than with Python.
However, since Ninja only allows Python scripts to be a script of an
`action`, we were previously forced to write Python.

This change allows the `third_party/node/node.py` script to be used as
if it was a regular `node <script args>` invocation. As a result, we can
use this script as part of a Ninja action as follows:

```
action(target_name) {
  script = "//third_party/node/node.py"

  args = [
    "path/to/my/node/script.js",
    "--any-arg",
    anyValueForArg,
  ]
}
```

We were previously already using this in DevTools itself, but that
requires a Node checkout in
`third_party/devtools-frontend/src/third_party/node`. To make sure that
a build in Chromium is possible as well, we need to reference the
absolute path to `//third_party/node/node.py` and thus requires the same
file to exist in Chromium itself.

Additionally, the script would now fail with the exitcode as defined by
the invoked Node script.

Lastly, this CL reformats the script according to the Python formatting
standard.

Bug: 1096473

R=jacktfranklin@chromium.org,dpapad@chromium.org
CC=​devtools-reviews+blink@chromium.org

Change-Id: Ie11150475d4d32c9dc9e8ec18228d490239ff7c6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2248568Reviewed-by: default avatardpapad <dpapad@chromium.org>
Reviewed-by: default avatarDirk Pranke <dpranke@google.com>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#780723}
parent e81bb07e
...@@ -105,7 +105,7 @@ for (redirect_url, file_path) in [ ...@@ -105,7 +105,7 @@ for (redirect_url, file_path) in [
_VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map(
lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) lambda m: ['--redirect', '%s|%s' % (m[0], m[1])], _URL_MAPPINGS)))
def _undo_mapping(mappings, url): def _undo_mapping(mappings, url):
...@@ -191,7 +191,7 @@ def _generate_manifest_file(tmp_out_dir, in_path, manifest_out_path): ...@@ -191,7 +191,7 @@ def _generate_manifest_file(tmp_out_dir, in_path, manifest_out_path):
replaced_sources = [] replaced_sources = []
for source in sources: for source in sources:
replaced_sources.append( replaced_sources.append(
source.replace('../' + os.path.basename(in_path) + "/", "")) source.replace('../' + os.path.basename(in_path) + '/', ''))
filename = sourcemap_file[:-len('.map')] filename = sourcemap_file[:-len('.map')]
manifest[os.path.basename(filename)] = replaced_sources manifest[os.path.basename(filename)] = replaced_sources
output_filenames.append(filename) output_filenames.append(filename)
...@@ -271,7 +271,7 @@ def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args, ...@@ -271,7 +271,7 @@ def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args,
[ [
'--manifest-out', manifest_out_path, '--manifest-out', manifest_out_path,
'--root', in_path, '--root', in_path,
'--redirect', '"chrome://%s/|%s"' % (args.host, in_path + '/'), '--redirect', 'chrome://%s/|%s' % (args.host, in_path + '/'),
'--out-dir', os.path.relpath(tmp_out_dir, _CWD).replace('\\', '/'), '--out-dir', os.path.relpath(tmp_out_dir, _CWD).replace('\\', '/'),
'--shell', args.html_in_files[0], '--shell', args.html_in_files[0],
] + in_html_args) ] + in_html_args)
...@@ -349,7 +349,7 @@ def _optimize(in_folder, args): ...@@ -349,7 +349,7 @@ def _optimize(in_folder, args):
for index, js_out_file in enumerate(args.js_out_files): for index, js_out_file in enumerate(args.js_out_files):
node.RunNode([node_modules.PathToUglify(), node.RunNode([node_modules.PathToUglify(),
os.path.join(tmp_out_dir, js_out_file), os.path.join(tmp_out_dir, js_out_file),
'--comments', '"/Copyright|license|LICENSE|\<\/?if/"', '--comments', '/Copyright|license|LICENSE|\<\/?if/',
'--output', os.path.join(out_path, js_out_file)]) '--output', os.path.join(out_path, js_out_file)])
finally: finally:
shutil.rmtree(tmp_out_dir) shutil.rmtree(tmp_out_dir)
......
...@@ -7,23 +7,34 @@ from os import path as os_path ...@@ -7,23 +7,34 @@ from os import path as os_path
import platform import platform
import subprocess import subprocess
import sys import sys
import os
def GetBinaryPath(): def GetBinaryPath():
return os_path.join(os_path.dirname(__file__), *{ return os_path.join(
'Darwin': ('mac', 'node-darwin-x64', 'bin', 'node'), os_path.dirname(__file__), *{
'Linux': ('linux', 'node-linux-x64', 'bin', 'node'), 'Darwin': ('mac', 'node-darwin-x64', 'bin', 'node'),
'Windows': ('win', 'node.exe'), 'Linux': ('linux', 'node-linux-x64', 'bin', 'node'),
}[platform.system()]) 'Windows': ('win', 'node.exe'),
}[platform.system()])
def RunNode(cmd_parts, stdout=None): def RunNode(cmd_parts, stdout=None):
cmd = " ".join([GetBinaryPath()] + cmd_parts) cmd = [GetBinaryPath()] + cmd_parts
process = subprocess.Popen( # Pipe the output, because in Ninja actions we don't want
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) # to print any output, unless the action failed.
stdout, stderr = process.communicate() process = subprocess.Popen(cmd,
cwd=os.getcwd(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if stderr: if process.returncode != 0:
raise RuntimeError('%s failed: %s' % (cmd, stderr)) print('%s failed:\n%s' % (cmd, stdout + stderr))
exit(process.returncode)
return stdout return stdout
if __name__ == '__main__':
RunNode(sys.argv[1:])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment