def main()

in tools/js/run_npm_binary.py [0:0]


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
    # working directory
    sha1 = hashlib.sha1(open(path, 'rb').read()).hexdigest()
    outdir = '%s-%s' % (path[:-len(suffix)], sha1)
    rel_bin = os.path.join('package', 'bin', name)
    rel_lib_bin = os.path.join('package', 'lib', 'bin', name + '.js')
    bin = os.path.join(outdir, rel_bin)
    libbin = os.path.join(outdir, rel_lib_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.
        if not os.path.isfile(bin) and os.path.isfile(libbin):
            subprocess.check_call([nodejs, libbin] + args[1:])
        else:
            subprocess.check_call([nodejs, bin] + args[1:])
    elif not os.path.isfile(bin) and os.path.isfile(libbin):
        subprocess.check_call([libbin] + args[1:])
    else:
        subprocess.check_call([bin] + args[1:])