Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / build_tools / buildbot_common.py
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.
4
5 """Common utilities for all buildbot scripts that specifically don't rely
6 on having a full chromium checkout.
7 """
8
9 import os
10 import subprocess
11 import sys
12
13 from build_paths import SDK_SRC_DIR, NACL_DIR
14
15 sys.path.append(os.path.join(SDK_SRC_DIR, 'tools'))
16 import oshelpers
17 import getos
18
19 def IsSDKBuilder():
20   """Returns True if this script is running on an SDK builder.
21
22   False means it is either running on a trybot, or a user's machine.
23
24   Trybot names:
25     (win|mac|linux)_nacl_sdk
26
27   Build-only Trybot names:
28     (win|mac|linux)_nacl_sdk_build
29
30   Builder names:
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
34
35
36 def IsSDKTrybot():
37   """Returns True if this script is running on an SDK trybot.
38
39   False means it is either running on an SDK builder, or a user's machine.
40
41   See IsSDKBuilder above for trybot/buildbot names."""
42   return '_nacl_sdk' in os.getenv('BUILDBOT_BUILDERNAME', '')
43
44
45 def ErrorExit(msg):
46   """Write and error to stderr, then exit with 1 signaling failure."""
47   sys.stderr.write(str(msg) + '\n')
48   sys.exit(1)
49
50
51 def GetWindowsEnvironment():
52   sys.path.append(os.path.join(NACL_DIR, 'buildbot'))
53   import buildbot_standard
54
55   # buildbot_standard.SetupWindowsEnvironment expects a "context" object. We'll
56   # fake enough of that here to work.
57   class FakeContext(object):
58     def __init__(self):
59       self.env = os.environ
60
61     def GetEnv(self, key):
62       return self.env[key]
63
64     def __getitem__(self, key):
65       return self.env[key]
66
67     def SetEnv(self, key, value):
68       self.env[key] = value
69
70     def __setitem__(self, key, value):
71       self.env[key] = value
72
73   context = FakeContext()
74   buildbot_standard.SetupWindowsEnvironment(context)
75
76   # buildbot_standard.SetupWindowsEnvironment adds the directory which contains
77   # vcvarsall.bat to the path, but not the directory which contains cl.exe,
78   # link.exe, etc.
79   # Running vcvarsall.bat adds the correct directories to the path, which we
80   # extract below.
81   process = subprocess.Popen('vcvarsall.bat x86 > NUL && set',
82       stdout=subprocess.PIPE, env=context.env, shell=True)
83   stdout, _ = process.communicate()
84
85   # Parse environment from "set" command above.
86   # It looks like this:
87   # KEY1=VALUE1\r\n
88   # KEY2=VALUE2\r\n
89   # ...
90   return dict(line.split('=') for line in stdout.split('\r\n')[:-1])
91
92
93 def BuildStep(name):
94   """Annotate a buildbot build step."""
95   sys.stdout.flush()
96   print '\n@@@BUILD_STEP %s@@@' % name
97   sys.stdout.flush()
98
99
100 def Run(args, cwd=None, env=None, shell=False):
101   """Start a process with the provided arguments.
102
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."""
107
108   # We need to modify the environment to build host on Windows.
109   if not env and getos.GetPlatform() == 'win':
110     env = GetWindowsEnvironment()
111
112   print 'Running: ' + ' '.join(args)
113   sys.stdout.flush()
114   sys.stderr.flush()
115   try:
116     subprocess.check_call(args, cwd=cwd, env=env, shell=shell)
117   except subprocess.CalledProcessError as e:
118     sys.stdout.flush()
119     sys.stderr.flush()
120     ErrorExit('buildbot_common: %s' % e)
121
122   sys.stdout.flush()
123   sys.stderr.flush()
124
125
126 def CopyDir(src, dst, excludes=('.svn', '*/.svn')):
127   """Recursively copy a directory using."""
128   args = ['-r', src, dst]
129   for exc in excludes:
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)
134   oshelpers.Copy(args)
135
136
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)
141   args = [src, dst]
142   oshelpers.Copy(args)
143
144
145 def RemoveDir(dst):
146   """Remove the provided path."""
147   print 'rm -fr ' + dst
148   oshelpers.Remove(['-fr', dst])
149
150
151 def MakeDir(dst):
152   """Create the path including all parent directories as needed."""
153   print 'mkdir -p ' + dst
154   oshelpers.Mkdir(['-p', dst])
155
156
157 def Move(src, dst):
158   """Move the path src to dst."""
159   print 'mv -f %s %s' % (src, dst)
160   oshelpers.Move(['-f', src, dst])
161
162
163 def RemoveFile(dst):
164   """Remove the provided file."""
165   print 'rm ' + dst
166   oshelpers.Remove(['-f', dst])
167
168
169 BOT_GSUTIL = '/b/build/scripts/slave/gsutil'
170 # On Windows, the current working directory may be on a different drive than
171 # gsutil.
172 WIN_BOT_GSUTIL = 'E:' + BOT_GSUTIL
173 LOCAL_GSUTIL = 'gsutil'
174
175
176 def GetGsutil():
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
181     return BOT_GSUTIL
182   else:
183     return LOCAL_GSUTIL
184
185
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)
189
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'
195
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)
199   if step_link:
200     print '@@@STEP_LINK@download@%s@@@' % url
201     sys.stdout.flush()