summaryrefslogtreecommitdiffstats
path: root/tools/js/run_npm_binary.py
blob: d769b98c3d189df7513196eee3045b57151163e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import atexit
from distutils import spawn
import hashlib
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile


def extract(path, outdir, bin):
  if os.path.exists(os.path.join(outdir, bin)):
    return # Another process finished extracting, ignore.

  # Use a temp directory adjacent to outdir so shutil.move can use the same
  # device atomically.
  tmpdir = tempfile.mkdtemp(dir=os.path.dirname(outdir))
  def cleanup():
    try:
      shutil.rmtree(tmpdir)
    except OSError:
      pass # Too late now
  atexit.register(cleanup)

  def extract_one(mem):
    dest = os.path.join(outdir, mem.name)
    tar.extract(mem, path=tmpdir)
    try:
      os.makedirs(os.path.dirname(dest))
    except OSError:
      pass # Either exists, or will fail on the next line.
    shutil.move(os.path.join(tmpdir, mem.name), dest)

  with tarfile.open(path, 'r:gz') as tar:
    for mem in tar.getmembers():
      if mem.name != bin:
        extract_one(mem)
    # Extract bin last so other processes only short circuit when extraction is
    # finished.
    extract_one(tar.getmember(bin))

def main(args):
  path = args[0]
  suffix = '.npm_binary.tgz'
  tgz = os.path.basename(path)

  parts = tgz[:-len(suffix)].split('@')

  if not tgz.endswith(suffix) or len(parts) != 2:
    print('usage: %s <path/to/npm_binary>' % sys.argv[0], file=sys.stderr)
    return 1

  name, _ = parts

  # Avoid importing from gerrit because we don't want to depend on the right CWD.
  sha1 = hashlib.sha1(open(path, 'rb').read()).hexdigest()
  outdir = '%s-%s' % (path[:-len(suffix)], sha1)
  rel_bin = os.path.join('package', 'bin', name)
  bin = os.path.join(outdir, rel_bin)
  if not os.path.isfile(bin):
    extract(path, outdir, rel_bin)

  nodejs = spawn.find_executable('nodejs')
  if nodejs:
    # Debian installs Node.js as 'nodejs', due to a conflict with another
    # package.
    subprocess.check_call([nodejs, bin] + args[1:])
  else:
    subprocess.check_call([bin] + args[1:])


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))