def extract()

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


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.
        if bin in tar.getnames():
            extract_one(tar.getmember(bin))