Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2019 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | |
| 14 | INFRA_GO = 'go.skia.org/infra' |
| 15 | WHICH = 'where' if sys.platform == 'win32' else 'which' |
| 16 | |
| 17 | |
| 18 | def check(): |
| 19 | '''Verify that golang is properly installed. If not, exit with an error.''' |
| 20 | def _fail(msg): |
| 21 | print >> sys.stderr, msg |
| 22 | sys.exit(1) |
| 23 | |
| 24 | try: |
| 25 | go_exe = subprocess.check_output([WHICH, 'go']) |
| 26 | except (subprocess.CalledProcessError, OSError): |
Eric Boren | 06a7257 | 2019-04-18 08:54:33 -0400 | [diff] [blame] | 27 | go_exe = None |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 28 | if not go_exe: |
| 29 | _fail('Unable to find Golang installation; see ' |
| 30 | 'https://golang.org/doc/install') |
| 31 | if not os.environ.get('GOPATH'): |
| 32 | _fail('GOPATH environment variable is not set; is Golang properly ' |
| 33 | 'installed?') |
| 34 | go_bin = os.path.join(os.environ['GOPATH'], 'bin') |
| 35 | for entry in os.environ.get('PATH', '').split(os.pathsep): |
| 36 | if entry == go_bin: |
| 37 | break |
| 38 | else: |
| 39 | _fail('%s not in PATH; is Golang properly installed?' % go_bin) |
| 40 | |
| 41 | |
Eric Boren | 46e2d8d | 2019-12-05 14:29:38 -0500 | [diff] [blame^] | 42 | def get(pkg): |
| 43 | '''Obtain/update the given package/module via "go get".''' |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 44 | check() |
Eric Boren | 46e2d8d | 2019-12-05 14:29:38 -0500 | [diff] [blame^] | 45 | subprocess.check_call(['go', 'get', '-u', pkg]) |
Eric Boren | 522efc4 | 2019-04-16 15:15:21 -0400 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def update_infra(): |
| 49 | '''Update the local checkout of the Skia infra codebase.''' |
| 50 | get(INFRA_GO + '/...') |
Eric Boren | 46e2d8d | 2019-12-05 14:29:38 -0500 | [diff] [blame^] | 51 | |
| 52 | |
| 53 | def mod_download(*pkgs): |
| 54 | '''Run "go mod download" to obtain the given package(s).''' |
| 55 | check() |
| 56 | subprocess.check_call(['go', 'mod', 'download']+list(pkgs)) |
| 57 | |
| 58 | |
| 59 | def install(pkg): |
| 60 | '''"go install" the given package.''' |
| 61 | check() |
| 62 | subprocess.check_call(['go', 'install', pkg]) |