addon-sdk/source/bin/integration-scripts/integration-check

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 #!/usr/bin/env python
michael@0 2 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 5
michael@0 6 import os
michael@0 7 import signal
michael@0 8 import threading
michael@0 9 import urllib2, urllib
michael@0 10 import zipfile
michael@0 11 import tarfile
michael@0 12 import subprocess
michael@0 13 import optparse
michael@0 14 import sys, re
michael@0 15 #import win32api
michael@0 16
michael@0 17
michael@0 18 class SDK:
michael@0 19 def __init__(self):
michael@0 20 try:
michael@0 21 # Take the current working directory
michael@0 22 self.default_path = os.getcwd()
michael@0 23 if sys.platform == "win32":
michael@0 24 self.mswindows = True
michael@0 25 else:
michael@0 26 self.mswindows = False
michael@0 27 # Take the default home path of the user.
michael@0 28 home = os.path.expanduser('~')
michael@0 29
michael@0 30 # The following are the parameters that can be used to pass a dynamic URL, a specific path or a binry. The binary is not used yet. It will be used in version 2.0
michael@0 31 # If a dynamic path is to be mentioned, it should start with a '/'. For eg. "/Desktop"
michael@0 32 parser = optparse.OptionParser()
michael@0 33 parser.add_option('-u', '--url', dest = 'url', default = 'https://ftp.mozilla.org/pub/mozilla.org/labs/jetpack/addon-sdk-latest.zip')
michael@0 34 parser.add_option('-p', '--path', dest = 'path', default = self.default_path)
michael@0 35 parser.add_option('-b', '--binary', dest = 'binary')#, default='/Applications/Firefox.app')
michael@0 36 (options, args) = parser.parse_args()
michael@0 37
michael@0 38 # Get the URL from the parameter
michael@0 39 self.link = options.url
michael@0 40 # Set the base path for the user. If the user supplies the path, use the home variable as well. Else, take the default path of this script as the installation directory.
michael@0 41 if options.path!=self.default_path:
michael@0 42 if self.mswindows:
michael@0 43 self.base_path = home + str(options.path).strip() + '\\'
michael@0 44 else:
michael@0 45 self.base_path = home + str(options.path).strip() + '/'
michael@0 46 else:
michael@0 47 if self.mswindows:
michael@0 48 self.base_path = str(options.path).strip() + '\\'
michael@0 49 else:
michael@0 50 self.base_path = str(options.path).strip() + '/'
michael@0 51 assert ' ' not in self.base_path, "You cannot have a space in your home path. Please remove the space before you continue."
michael@0 52 print('Your Base path is =' + self.base_path)
michael@0 53
michael@0 54 # This assignment is not used in this program. It will be used in version 2 of this script.
michael@0 55 self.bin = options.binary
michael@0 56 # if app or bin is empty, dont pass anything
michael@0 57
michael@0 58 # Search for the .zip file or tarball file in the URL.
michael@0 59 i = self.link.rfind('/')
michael@0 60
michael@0 61 self.fname = self.link[i+1:]
michael@0 62 z = re.search('zip',self.fname,re.I)
michael@0 63 g = re.search('gz',self.fname,re.I)
michael@0 64 if z:
michael@0 65 print 'zip file present in the URL.'
michael@0 66 self.zip = True
michael@0 67 self.gz = False
michael@0 68 elif g:
michael@0 69 print 'gz file present in the URL'
michael@0 70 self.gz = True
michael@0 71 self.zip = False
michael@0 72 else:
michael@0 73 print 'zip/gz file not present. Check the URL.'
michael@0 74 return
michael@0 75 print("File name is =" + self.fname)
michael@0 76
michael@0 77 # Join the base path and the zip/tar file name to crate a complete Local file path.
michael@0 78 self.fpath = self.base_path + self.fname
michael@0 79 print('Your local file path will be=' + self.fpath)
michael@0 80 except AssertionError, e:
michael@0 81 print e.args[0]
michael@0 82 sys.exit(1)
michael@0 83
michael@0 84 # Download function - to download the SDK from the URL to the local machine.
michael@0 85 def download(self,url,fpath,fname):
michael@0 86 try:
michael@0 87 # Start the download
michael@0 88 print("Downloading...Please be patient!")
michael@0 89 urllib.urlretrieve(url,filename = fname)
michael@0 90 print('Download was successful.')
michael@0 91 except ValueError: # Handles broken URL errors.
michael@0 92 print 'The URL is ether broken or the file does not exist. Please enter the correct URL.'
michael@0 93 raise
michael@0 94 except urllib2.URLError: # Handles URL errors
michael@0 95 print '\nURL not correct. Check again!'
michael@0 96 raise
michael@0 97
michael@0 98 # Function to extract the downloaded zipfile.
michael@0 99 def extract(self, zipfilepath, extfile):
michael@0 100 try:
michael@0 101 # Timeout is set to 30 seconds.
michael@0 102 timeout = 30
michael@0 103 # Change the directory to the location of the zip file.
michael@0 104 try:
michael@0 105 os.chdir(zipfilepath)
michael@0 106 except OSError:
michael@0 107 # Will reach here if zip file doesnt exist
michael@0 108 print 'O/S Error:' + zipfilepath + 'does not exist'
michael@0 109 raise
michael@0 110
michael@0 111 # Get the folder name of Jetpack to get the exact version number.
michael@0 112 if self.zip:
michael@0 113 try:
michael@0 114 f = zipfile.ZipFile(extfile, "r")
michael@0 115 except IOError as (errno, strerror): # Handles file errors
michael@0 116 print "I/O error - Cannot perform extract operation: {1}".format(errno, strerror)
michael@0 117 raise
michael@0 118 list = f.namelist()[0]
michael@0 119 temp_name = list.split('/')
michael@0 120 print('Folder Name= ' +temp_name[0])
michael@0 121 self.folder_name = temp_name[0]
michael@0 122 elif self.gz:
michael@0 123 try:
michael@0 124 f = tarfile.open(extfile,'r')
michael@0 125 except IOError as (errno, strerror): # Handles file errors
michael@0 126 print "I/O error - Cannot perform extract operation: {1}".format(errno, strerror)
michael@0 127 raise
michael@0 128 list = f.getnames()[0]
michael@0 129 temp_name = list.split('/')
michael@0 130 print('Folder Name= ' +temp_name[0])
michael@0 131 self.folder_name = temp_name[0]
michael@0 132
michael@0 133 print ('Starting to Extract...')
michael@0 134
michael@0 135 # Timeout code. The subprocess.popen exeutes the command and the thread waits for a timeout. If the process does not finish within the mentioned-
michael@0 136 # timeout, the process is killed.
michael@0 137 kill_check = threading.Event()
michael@0 138
michael@0 139 if self.zip:
michael@0 140 # Call the command to unzip the file.
michael@0 141 if self.mswindows:
michael@0 142 zipfile.ZipFile.extractall(f)
michael@0 143 else:
michael@0 144 p = subprocess.Popen('unzip '+extfile, stdout=subprocess.PIPE, shell=True)
michael@0 145 pid = p.pid
michael@0 146 elif self.gz:
michael@0 147 # Call the command to untar the file.
michael@0 148 if self.mswindows:
michael@0 149 tarfile.TarFile.extractall(f)
michael@0 150 else:
michael@0 151 p = subprocess.Popen('tar -xf '+extfile, stdout=subprocess.PIPE, shell=True)
michael@0 152 pid = p.pid
michael@0 153
michael@0 154 #No need to handle for windows because windows automatically replaces old files with new files. It does not ask the user(as it does in Mac/Unix)
michael@0 155 if self.mswindows==False:
michael@0 156 watch = threading.Timer(timeout, kill_process, args=(pid, kill_check, self.mswindows ))
michael@0 157 watch.start()
michael@0 158 (stdout, stderr) = p.communicate()
michael@0 159 watch.cancel() # if it's still waiting to run
michael@0 160 success = not kill_check.isSet()
michael@0 161
michael@0 162 # Abort process if process fails.
michael@0 163 if not success:
michael@0 164 raise RuntimeError
michael@0 165 kill_check.clear()
michael@0 166 print('Extraction Successful.')
michael@0 167 except RuntimeError:
michael@0 168 print "Ending the program"
michael@0 169 sys.exit(1)
michael@0 170 except:
michael@0 171 print "Error during file extraction: ", sys.exc_info()[0]
michael@0 172 raise
michael@0 173
michael@0 174 # Function to run the cfx testall comands and to make sure the SDK is not broken.
michael@0 175 def run_testall(self, home_path, folder_name):
michael@0 176 try:
michael@0 177 timeout = 500
michael@0 178
michael@0 179 self.new_dir = home_path + folder_name
michael@0 180 try:
michael@0 181 os.chdir(self.new_dir)
michael@0 182 except OSError:
michael@0 183 # Will reach here if the jetpack 0.X directory doesnt exist
michael@0 184 print 'O/S Error: Jetpack directory does not exist at ' + self.new_dir
michael@0 185 raise
michael@0 186 print '\nStarting tests...'
michael@0 187 # Timeout code. The subprocess.popen exeutes the command and the thread waits for a timeout. If the process does not finish within the mentioned-
michael@0 188 # timeout, the process is killed.
michael@0 189 kill_check = threading.Event()
michael@0 190
michael@0 191 # Set the path for the logs. They will be in the parent directory of the Jetpack SDK.
michael@0 192 log_path = home_path + 'tests.log'
michael@0 193
michael@0 194 # Subprocess call to set up the jetpack environment and to start the tests. Also sends the output to a log file.
michael@0 195 if self.bin != None:
michael@0 196 if self.mswindows:
michael@0 197 p = subprocess.Popen("bin\\activate && cfx testall -a firefox -b \"" + self.bin + "\"" , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 198 proc_handle = p._handle
michael@0 199 (stdout,stderr) = p.communicate()
michael@0 200 else:
michael@0 201 p = subprocess.Popen('. bin/activate; cfx testall -a firefox -b ' + self.bin , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 202 pid = p.pid
michael@0 203 (stdout,stderr) = p.communicate()
michael@0 204 elif self.bin == None:
michael@0 205 if self.mswindows:
michael@0 206 p=subprocess.Popen('bin\\activate && cfx testall -a firefox > '+log_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 207 proc_handle = p._handle
michael@0 208 (stdout,stderr) = p.communicate()
michael@0 209 else:
michael@0 210 p = subprocess.Popen('. bin/activate; cfx testall -a firefox > '+log_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 211 pid = p.pid
michael@0 212 (stdout,stderr) = p.communicate()
michael@0 213
michael@0 214 #Write the output to log file
michael@0 215 f=open(log_path,"w")
michael@0 216 f.write(stdout+stderr)
michael@0 217 f.close()
michael@0 218
michael@0 219 #Watchdog for timeout process
michael@0 220 if self.mswindows:
michael@0 221 watch = threading.Timer(timeout, kill_process, args=(proc_handle, kill_check, self.mswindows))
michael@0 222 else:
michael@0 223 watch = threading.Timer(timeout, kill_process, args=(pid, kill_check, self.mswindows))
michael@0 224 watch.start()
michael@0 225 watch.cancel() # if it's still waiting to run
michael@0 226 success = not kill_check.isSet()
michael@0 227 if not success:
michael@0 228 raise RuntimeError
michael@0 229 kill_check.clear()
michael@0 230
michael@0 231 if p.returncode!=0:
michael@0 232 print('\nAll tests were not successful. Check the test-logs in the jetpack directory.')
michael@0 233 result_sdk(home_path)
michael@0 234 #sys.exit(1)
michael@0 235 raise RuntimeError
michael@0 236 else:
michael@0 237 ret_code=result_sdk(home_path)
michael@0 238 if ret_code==0:
michael@0 239 print('\nAll tests were successful. Yay \o/ . Running a sample package test now...')
michael@0 240 else:
michael@0 241 print ('\nThere were errors during the tests.Take a look at logs')
michael@0 242 raise RuntimeError
michael@0 243 except RuntimeError:
michael@0 244 print "Ending the program"
michael@0 245 sys.exit(1)
michael@0 246 except:
michael@0 247 print "Error during the testall command execution:", sys.exc_info()[0]
michael@0 248 raise
michael@0 249
michael@0 250 def package(self, example_dir):
michael@0 251 try:
michael@0 252 timeout = 30
michael@0 253
michael@0 254 print '\nNow Running packaging tests...'
michael@0 255
michael@0 256 kill_check = threading.Event()
michael@0 257
michael@0 258 # Set the path for the example logs. They will be in the parent directory of the Jetpack SDK.
michael@0 259 exlog_path = example_dir + 'test-example.log'
michael@0 260 # Subprocess call to test the sample example for packaging.
michael@0 261 if self.bin!=None:
michael@0 262 if self.mswindows:
michael@0 263 p = subprocess.Popen('bin\\activate && cfx run --pkgdir examples\\reading-data --static-args="{\\"quitWhenDone\\":true}" -b \"" + self.bin + "\"' , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 264 proc_handle = p._handle
michael@0 265 (stdout, stderr) = p.communicate()
michael@0 266 else:
michael@0 267 p = subprocess.Popen('. bin/activate; cfx run --pkgdir examples/reading-data --static-args=\'{\"quitWhenDone\":true}\' -b ' + self.bin , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 268 pid = p.pid
michael@0 269 (stdout, stderr) = p.communicate()
michael@0 270 elif self.bin==None:
michael@0 271 if self.mswindows:
michael@0 272 p = subprocess.Popen('bin\\activate && cfx run --pkgdir examples\\reading-data --static-args="{\\"quitWhenDone\\":true}"', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 273 proc_handle = p._handle
michael@0 274 (stdout, stderr) = p.communicate()
michael@0 275 else:
michael@0 276 p = subprocess.Popen('. bin/activate; cfx run --pkgdir examples/reading-data --static-args=\'{\"quitWhenDone\":true}\' ', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
michael@0 277 pid = p.pid
michael@0 278 (stdout, stderr) = p.communicate()
michael@0 279
michael@0 280 #Write the output to log file
michael@0 281 f=open(exlog_path,"w")
michael@0 282 f.write(stdout+stderr)
michael@0 283 f.close()
michael@0 284
michael@0 285 #Watch dog for timeout process
michael@0 286 if self.mswindows:
michael@0 287 watch = threading.Timer(timeout, kill_process, args=(proc_handle, kill_check, self.mswindows))
michael@0 288 else:
michael@0 289 watch = threading.Timer(timeout, kill_process, args=(pid, kill_check, self.mswindows))
michael@0 290 watch.start()
michael@0 291 watch.cancel() # if it's still waiting to run
michael@0 292 success = not kill_check.isSet()
michael@0 293 if not success:
michael@0 294 raise RuntimeError
michael@0 295 kill_check.clear()
michael@0 296
michael@0 297 if p.returncode != 0:
michael@0 298 print('\nSample tests were not executed correctly. Check the test-example log in jetpack diretory.')
michael@0 299 result_example(example_dir)
michael@0 300 raise RuntimeError
michael@0 301 else:
michael@0 302 ret_code=result_example(example_dir)
michael@0 303 if ret_code==0:
michael@0 304 print('\nAll tests pass. The SDK is working! Yay \o/')
michael@0 305 else:
michael@0 306 print ('\nTests passed with warning.Take a look at logs')
michael@0 307 sys.exit(1)
michael@0 308
michael@0 309 except RuntimeError:
michael@0 310 print "Ending program"
michael@0 311 sys.exit(1)
michael@0 312 except:
michael@0 313 print "Error during running sample tests:", sys.exc_info()[0]
michael@0 314 raise
michael@0 315
michael@0 316 def result_sdk(sdk_dir):
michael@0 317 log_path = sdk_dir + 'tests.log'
michael@0 318 print 'Results are logged at:' + log_path
michael@0 319 try:
michael@0 320 f = open(log_path,'r')
michael@0 321 # Handles file errors
michael@0 322 except IOError :
michael@0 323 print 'I/O error - Cannot open test log at ' + log_path
michael@0 324 raise
michael@0 325
michael@0 326 for line in reversed(open(log_path).readlines()):
michael@0 327 if line.strip()=='FAIL':
michael@0 328 print ('\nOverall result - FAIL. Look at the test log at '+log_path)
michael@0 329 return 1
michael@0 330 return 0
michael@0 331
michael@0 332
michael@0 333 def result_example(sdk_dir):
michael@0 334 exlog_path = sdk_dir + 'test-example.log'
michael@0 335 print 'Sample test results are logged at:' + exlog_path
michael@0 336 try:
michael@0 337 f = open(exlog_path,'r')
michael@0 338 # Handles file errors
michael@0 339 except IOError :
michael@0 340 print 'I/O error - Cannot open sample test log at ' + exlog_path
michael@0 341 raise
michael@0 342
michael@0 343 #Read the file in reverse and check for the keyword 'FAIL'.
michael@0 344 for line in reversed(open(exlog_path).readlines()):
michael@0 345 if line.strip()=='FAIL':
michael@0 346 print ('\nOverall result for Sample tests - FAIL. Look at the test log at '+exlog_path)
michael@0 347 return 1
michael@0 348 return 0
michael@0 349
michael@0 350 def kill_process(process, kill_check, mswindows):
michael@0 351 print '\nProcess Timedout. Killing the process. Please Rerun this script.'
michael@0 352 if mswindows:
michael@0 353 win32api.TerminateProcess(process, -1)
michael@0 354 else:
michael@0 355 os.kill(process, signal.SIGKILL)
michael@0 356 kill_check.set()# tell the main routine to kill. Used SIGKILL to hard kill the process.
michael@0 357 return
michael@0 358
michael@0 359 if __name__ == "__main__":
michael@0 360 obj = SDK()
michael@0 361 obj.download(obj.link,obj.fpath,obj.fname)
michael@0 362 obj.extract(obj.base_path,obj.fname)
michael@0 363 obj.run_testall(obj.base_path,obj.folder_name)
michael@0 364 obj.package(obj.base_path)

mercurial