Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: tools/go/upload.py

Issue 1065333003: tool: add script that uploads go tool binaries (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/go/VERSION ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """
7 This script downloads GO_VERSION linux binaries from golang.org, build android
8 binaries with NDK tool chain configured with NDK_PLATFORM and NDK_TOOLCHAIN
9 parameters, zips the stuff and uploads it to Google Cloud Storage at
10 gs://mojo/go/tool. It also produces VERSION file with sha1 code of the uploaded
11 archive.
12
13 In order to use it, you need:
14 - depot_tools in your path
15 - installed android build deps
16 - WRITE access to GCS
17
18 To update go tool binaries you need to
19 1) run 'gsutil.py config' to update initialize gsutil's credentials
20 2) run this script
21 3) push new version of file 'VERSION'
22
23 This script doesn't check if current version is already up to date, as produced
viettrungluu 2015/04/08 20:42:29 "as produced" -> "as the produced"
rogulenko 2015/04/08 21:31:34 Done.
24 tar.gz archive is slightly different every time, as archive includes time stamp.
viettrungluu 2015/04/08 20:42:29 ", as archive includes time stamp" -> "since it in
rogulenko 2015/04/08 21:31:34 Done.
25 """
26
27 import hashlib
28 import os
29 import shutil
30 import subprocess
31 import sys
32 import tarfile
33 import tempfile
34
35 GO_VERSION = 'go1.4.2'
36 NDK_PLATFORM = 'android-19'
viettrungluu 2015/04/08 20:42:29 Where do these NDK_... values come from? Don't the
rogulenko 2015/04/08 21:31:34 Changed android-19 -> android-14. Actually I didn
37 NDK_TOOLCHAIN = 'arm-linux-androideabi-4.9'
38
39 # Path constants. (All of these should be absolute paths.)
40 THIS_DIR = os.path.abspath(os.path.dirname(__file__))
41 MOJO_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..'))
42
43
44 def RunCommand(command):
45 """Run command and return success (True) or failure."""
46
47 print 'Running %s' % (str(command))
48 if subprocess.call(command, shell=False) == 0:
49 return True
50 print 'Failed.'
51 return False
52
53 def DownloadGoLinux(tmpdir):
54 """Downloads and go linux_amd64 go binaries from golang.org and extracts them
55 to provided tmpdir. The extracted files appear under
56 tmpdir/tool/linux_amd64."""
57
58 archive_name = '%s.linux-amd64.tar.gz' % GO_VERSION
59 tool_dir = os.path.join(tmpdir, 'tool')
60 os.mkdir(tool_dir)
61 archive_path = os.path.join(tool_dir, archive_name)
62 # Download the Linux x64 Go binaries from golang.org.
63 # '-C -': Resume transfer if possible.
64 # '--location': Follow Location: redirects.
65 # '-o': Output file.
66 curl_command = ['curl',
67 '-C', '-',
68 '--location',
69 '-o', archive_path,
70 'http://golang.org/dl/%s' % archive_name]
71 if not RunCommand(curl_command):
72 print "Failed to linux go binaries from server."
73 sys.exit(1)
74 with tarfile.open(archive_path) as arch:
75 arch.extractall(tool_dir)
76 os.remove(archive_path)
77 os.rename(os.path.join(tool_dir, 'go'),
78 os.path.join(tool_dir, 'linux_amd64'))
79
80 def ConfigureAndroidNDK(tmpdir):
81 """Creates a standalone tool chain under tmpdir. Returns path to ndk cc
82 compiler."""
83
84 ndk_root = os.path.join(tmpdir, 'ndk-toolchain')
85 os.chdir(os.path.join(MOJO_DIR, 'third_party', 'android_tools', 'ndk'))
86 script = os.path.join('build', 'tools', 'make-standalone-toolchain.sh')
87 make_command = ['bash', script,
88 '--platform=%s' % NDK_PLATFORM,
89 '--toolchain=%s' % NDK_TOOLCHAIN,
90 '--install-dir=%s' % ndk_root]
91 if not RunCommand(make_command):
92 print "Failed to generate NDK tool chain."
93 sys.exit(1)
94 return os.path.join(ndk_root, 'bin', 'arm-linux-androideabi-gcc')
95
96 def BuildGoAndroid(tmpdir):
97 ndk_cc = ConfigureAndroidNDK(tmpdir)
98 go_linux = os.path.join(tmpdir, 'tool', 'linux_amd64')
99 go_android = os.path.join(tmpdir, 'tool', 'android_arm')
100 # Copy go sources and remove binaries to keep only that we generate.
101 shutil.copytree(go_linux, go_android)
102 shutil.rmtree(os.path.join(go_android, 'bin'))
103 shutil.rmtree(os.path.join(go_android, 'pkg'))
104 # Configure environment variables.
105 os.environ["GOPATH"] = go_linux
viettrungluu 2015/04/08 20:42:29 Rather than modifying your own environment (which
rogulenko 2015/04/08 21:31:34 Replaced with os.environ.copy().
106 os.environ["GOROOT"] = go_linux
107 os.environ["CC_FOR_TARGET"] = ndk_cc
108 os.environ["GOOS"] = 'android'
109 os.environ["GOARCH"] = 'arm'
110 os.environ["GOARM"] = '7'
111 # Build go tool.
112 os.chdir(os.path.join(go_android, 'src'))
113 make_command = ['bash', 'make.bash']
114 if not RunCommand(make_command):
115 print "Failed to make go tool for android."
116 sys.exit(1)
117
118 def Compress(tmpdir):
119 """Compresses the go tool into tar.gz and generates sha1 code, renames the
120 archive to sha1.tar.gz and returns the sha1 code."""
121
122 print "Compressing go tool, this may take several minutes."
123 os.chdir(os.path.join(tmpdir, 'tool'))
124 with tarfile.open(os.path.join('..', 'a.tar.gz'), 'w|gz') as arch:
125 arch.add('android_arm')
126 arch.add('linux_amd64')
127
128 sha1 = ''
129 with open(os.path.join(tmpdir, 'a.tar.gz')) as f:
130 sha1 = hashlib.sha1(f.read()).hexdigest()
131 os.rename(os.path.join(tmpdir, 'a.tar.gz'),
132 os.path.join(tmpdir, '%s.tar.gz' % sha1))
133 return sha1
134
135 def Upload(tmpdir, sha1):
136 """Uploads tmpdir/sha1.tar.gz to Google Cloud Storage under gs://mojo/go/tool
137 and writes sha1 to THID_DIR/VERSION."""
viettrungluu 2015/04/08 20:42:29 "THID" -> "THIS"
rogulenko 2015/04/08 21:31:34 Done.
138
139 stamp_file = os.path.join(THIS_DIR, 'VERSION')
140 with open(stamp_file, 'w+') as stamp:
141 stamp.write('%s\n' % sha1)
142 file_name = '%s.tar.gz' % sha1
143 upload_cmd = ['gsutil.py', 'cp',
144 '-n', # Do not upload if the file already exists.
145 os.path.join(tmpdir, file_name),
146 'gs://mojo/go/tool/%s' % file_name]
147
148 print "Uploading go tool to GCS."
149 if not RunCommand(upload_cmd):
150 print "Failed to upload go tool to GCS."
151 sys.exit(1)
152
153 def main():
154 tmpdir = tempfile.mkdtemp(prefix='mojo')
155 print "Created temp dir %s" % tmpdir
156 DownloadGoLinux(tmpdir)
157 BuildGoAndroid(tmpdir)
158 sha1 = Compress(tmpdir)
viettrungluu 2015/04/08 20:42:29 Would it be useful to print out the sha1 somewhere
rogulenko 2015/04/08 21:31:34 I think writing the sha1 code to VERSION is suffic
159 Upload(tmpdir, sha1)
160 shutil.rmtree(tmpdir)
161 print "Done."
162
163 if __name__ == '__main__':
164 sys.exit(main())
OLDNEW
« no previous file with comments | « tools/go/VERSION ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698