File: get.py

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (62 lines) | stat: -rwxr-xr-x 2,013 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
"""Downloads a prebuilt gn binary to a place where gn.py can find it."""

import io
import os
import sys
import urllib.request
import zipfile


def download_and_unpack(url, output_dir, gn):
    """Download an archive from url and extract gn from it into output_dir."""
    print('downloading %s ...' % url, end='')
    sys.stdout.flush()
    data = urllib.request.urlopen(url).read()
    print(' done')
    zipfile.ZipFile(io.BytesIO(data)).extract(gn, path=output_dir)


def set_executable_bit(path):
    mode = os.stat(path).st_mode
    mode |= (mode & 0o444) >> 2 # Copy R bits to X.
    os.chmod(path, mode) # No-op on Windows.


def get_platform():
    import platform
    if sys.platform == 'darwin':
        return 'mac-amd64' if platform.machine() != 'arm64' else 'mac-arm64'
    if platform.machine() not in ('AMD64', 'x86_64'):
        return None
    if sys.platform.startswith('linux'):
        return 'linux-amd64'
    if sys.platform == 'win32':
        return 'windows-amd64'


def main():
    platform = get_platform()
    if not platform:
        print('no prebuilt binary for', sys.platform)
        print('build it yourself with:')
        print('  rm -rf /tmp/gn &&')
        print('  pushd /tmp && git clone https://gn.googlesource.com/gn &&')
        print('  cd gn && build/gen.py && ninja -C out gn && popd &&')
        print('  cp /tmp/gn/out/gn somewhere/on/PATH')
        return 1
    dirname = os.path.join(os.path.dirname(__file__), 'bin', platform)
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/%s/+/latest'
    gn = 'gn' + ('.exe' if sys.platform == 'win32' else '')
    if platform == 'mac-arm64': # For https://openradar.appspot.com/FB8914243
        try: os.remove(os.path.join(dirname, gn))
        except OSError: pass
    download_and_unpack(url % platform, dirname, gn)
    set_executable_bit(os.path.join(dirname, gn))


if __name__ == '__main__':
    sys.exit(main())