1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Common utilities for all buildbot scripts that specifically don't rely
6 on having a full chromium checkout.
13 from build_paths
import SDK_SRC_DIR
, NACL_DIR
15 sys
.path
.append(os
.path
.join(SDK_SRC_DIR
, 'tools'))
20 """Returns True if this script is running on an SDK builder.
22 False means it is either running on a trybot, or a user's machine.
25 (win|mac|linux)_nacl_sdk
27 Build-only Trybot names:
28 (win|mac|linux)_nacl_sdk_build
31 (windows|mac|linux)-sdk-multi(bionic)(rel)?"""
32 bot
= os
.getenv('BUILDBOT_BUILDERNAME', '')
33 return '-sdk-multi' in bot
or '-sdk-bionic-multi' in bot
37 """Returns True if this script is running on an SDK trybot.
39 False means it is either running on an SDK builder, or a user's machine.
41 See IsSDKBuilder above for trybot/buildbot names."""
42 return '_nacl_sdk' in os
.getenv('BUILDBOT_BUILDERNAME', '')
46 """Write and error to stderr, then exit with 1 signaling failure."""
47 sys
.stderr
.write(str(msg
) + '\n')
51 def GetWindowsEnvironment():
52 sys
.path
.append(os
.path
.join(NACL_DIR
, 'buildbot'))
53 import buildbot_standard
55 # buildbot_standard.SetupWindowsEnvironment expects a "context" object. We'll
56 # fake enough of that here to work.
57 class FakeContext(object):
61 def GetEnv(self
, key
):
64 def __getitem__(self
, key
):
67 def SetEnv(self
, key
, value
):
70 def __setitem__(self
, key
, value
):
73 context
= FakeContext()
74 buildbot_standard
.SetupWindowsEnvironment(context
)
76 # buildbot_standard.SetupWindowsEnvironment adds the directory which contains
77 # vcvarsall.bat to the path, but not the directory which contains cl.exe,
79 # Running vcvarsall.bat adds the correct directories to the path, which we
81 process
= subprocess
.Popen('vcvarsall.bat x86 > NUL && set',
82 stdout
=subprocess
.PIPE
, env
=context
.env
, shell
=True)
83 stdout
, _
= process
.communicate()
85 # Parse environment from "set" command above.
90 return dict(line
.split('=') for line
in stdout
.split('\r\n')[:-1])
94 """Annotate a buildbot build step."""
96 print '\n@@@BUILD_STEP %s@@@' % name
100 def Run(args
, cwd
=None, env
=None, shell
=False):
101 """Start a process with the provided arguments.
103 Starts a process in the provided directory given the provided arguments. If
104 shell is not False, the process is launched via the shell to provide shell
105 interpretation of the arguments. Shell behavior can differ between platforms
106 so this should be avoided when not using platform dependent shell scripts."""
108 # We need to modify the environment to build host on Windows.
109 if not env
and getos
.GetPlatform() == 'win':
110 env
= GetWindowsEnvironment()
112 print 'Running: ' + ' '.join(args
)
116 subprocess
.check_call(args
, cwd
=cwd
, env
=env
, shell
=shell
)
117 except subprocess
.CalledProcessError
as e
:
120 ErrorExit('buildbot_common: %s' % e
)
126 def CopyDir(src
, dst
, excludes
=('.svn', '*/.svn')):
127 """Recursively copy a directory using."""
128 args
= ['-r', src
, dst
]
130 args
.append('--exclude=' + exc
)
131 print 'cp -r %s %s' % (src
, dst
)
132 if os
.path
.abspath(src
) == os
.path
.abspath(dst
):
133 ErrorExit('ERROR: Copying directory onto itself: ' + src
)
137 def CopyFile(src
, dst
):
138 print 'cp %s %s' % (src
, dst
)
139 if os
.path
.abspath(src
) == os
.path
.abspath(dst
):
140 ErrorExit('ERROR: Copying file onto itself: ' + src
)
146 """Remove the provided path."""
147 print 'rm -fr ' + dst
148 oshelpers
.Remove(['-fr', dst
])
152 """Create the path including all parent directories as needed."""
153 print 'mkdir -p ' + dst
154 oshelpers
.Mkdir(['-p', dst
])
158 """Move the path src to dst."""
159 print 'mv -f %s %s' % (src
, dst
)
160 oshelpers
.Move(['-f', src
, dst
])
164 """Remove the provided file."""
166 oshelpers
.Remove(['-f', dst
])
169 BOT_GSUTIL
= '/b/build/scripts/slave/gsutil'
170 # On Windows, the current working directory may be on a different drive than
172 WIN_BOT_GSUTIL
= 'E:' + BOT_GSUTIL
173 LOCAL_GSUTIL
= 'gsutil'
177 if os
.environ
.get('BUILDBOT_BUILDERNAME') \
178 and not os
.environ
.get('BUILDBOT_FAKE'):
179 if getos
.GetPlatform() == 'win':
180 return WIN_BOT_GSUTIL
186 def Archive(filename
, bucket_path
, cwd
=None, step_link
=True):
187 """Upload the given filename to Google Store."""
188 full_dst
= 'gs://%s/%s' % (bucket_path
, filename
)
190 # Since GetGsutil() might just return 'gsutil' and expect it to be looked
191 # up in the PATH, we must pass shell=True on windows.
192 # Without shell=True the windows implementation of subprocess.call will not
193 # search the PATH for the executable: http://bugs.python.org/issue8557
194 shell
= getos
.GetPlatform() == 'win'
196 cmd
= [GetGsutil(), 'cp', '-a', 'public-read', filename
, full_dst
]
197 Run(cmd
, shell
=shell
, cwd
=cwd
)
198 url
= 'https://storage.googleapis.com/%s/%s' % (bucket_path
, filename
)
200 print '@@@STEP_LINK@download@%s@@@' % url