michael@0: #!/usr/bin/env python michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import os michael@0: import signal michael@0: import threading michael@0: import urllib2, urllib michael@0: import zipfile michael@0: import tarfile michael@0: import subprocess michael@0: import optparse michael@0: import sys, re michael@0: #import win32api michael@0: michael@0: michael@0: class SDK: michael@0: def __init__(self): michael@0: try: michael@0: # Take the current working directory michael@0: self.default_path = os.getcwd() michael@0: if sys.platform == "win32": michael@0: self.mswindows = True michael@0: else: michael@0: self.mswindows = False michael@0: # Take the default home path of the user. michael@0: home = os.path.expanduser('~') michael@0: michael@0: # 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: # If a dynamic path is to be mentioned, it should start with a '/'. For eg. "/Desktop" michael@0: parser = optparse.OptionParser() michael@0: parser.add_option('-u', '--url', dest = 'url', default = 'https://ftp.mozilla.org/pub/mozilla.org/labs/jetpack/addon-sdk-latest.zip') michael@0: parser.add_option('-p', '--path', dest = 'path', default = self.default_path) michael@0: parser.add_option('-b', '--binary', dest = 'binary')#, default='/Applications/Firefox.app') michael@0: (options, args) = parser.parse_args() michael@0: michael@0: # Get the URL from the parameter michael@0: self.link = options.url michael@0: # 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: if options.path!=self.default_path: michael@0: if self.mswindows: michael@0: self.base_path = home + str(options.path).strip() + '\\' michael@0: else: michael@0: self.base_path = home + str(options.path).strip() + '/' michael@0: else: michael@0: if self.mswindows: michael@0: self.base_path = str(options.path).strip() + '\\' michael@0: else: michael@0: self.base_path = str(options.path).strip() + '/' michael@0: assert ' ' not in self.base_path, "You cannot have a space in your home path. Please remove the space before you continue." michael@0: print('Your Base path is =' + self.base_path) michael@0: michael@0: # This assignment is not used in this program. It will be used in version 2 of this script. michael@0: self.bin = options.binary michael@0: # if app or bin is empty, dont pass anything michael@0: michael@0: # Search for the .zip file or tarball file in the URL. michael@0: i = self.link.rfind('/') michael@0: michael@0: self.fname = self.link[i+1:] michael@0: z = re.search('zip',self.fname,re.I) michael@0: g = re.search('gz',self.fname,re.I) michael@0: if z: michael@0: print 'zip file present in the URL.' michael@0: self.zip = True michael@0: self.gz = False michael@0: elif g: michael@0: print 'gz file present in the URL' michael@0: self.gz = True michael@0: self.zip = False michael@0: else: michael@0: print 'zip/gz file not present. Check the URL.' michael@0: return michael@0: print("File name is =" + self.fname) michael@0: michael@0: # Join the base path and the zip/tar file name to crate a complete Local file path. michael@0: self.fpath = self.base_path + self.fname michael@0: print('Your local file path will be=' + self.fpath) michael@0: except AssertionError, e: michael@0: print e.args[0] michael@0: sys.exit(1) michael@0: michael@0: # Download function - to download the SDK from the URL to the local machine. michael@0: def download(self,url,fpath,fname): michael@0: try: michael@0: # Start the download michael@0: print("Downloading...Please be patient!") michael@0: urllib.urlretrieve(url,filename = fname) michael@0: print('Download was successful.') michael@0: except ValueError: # Handles broken URL errors. michael@0: print 'The URL is ether broken or the file does not exist. Please enter the correct URL.' michael@0: raise michael@0: except urllib2.URLError: # Handles URL errors michael@0: print '\nURL not correct. Check again!' michael@0: raise michael@0: michael@0: # Function to extract the downloaded zipfile. michael@0: def extract(self, zipfilepath, extfile): michael@0: try: michael@0: # Timeout is set to 30 seconds. michael@0: timeout = 30 michael@0: # Change the directory to the location of the zip file. michael@0: try: michael@0: os.chdir(zipfilepath) michael@0: except OSError: michael@0: # Will reach here if zip file doesnt exist michael@0: print 'O/S Error:' + zipfilepath + 'does not exist' michael@0: raise michael@0: michael@0: # Get the folder name of Jetpack to get the exact version number. michael@0: if self.zip: michael@0: try: michael@0: f = zipfile.ZipFile(extfile, "r") michael@0: except IOError as (errno, strerror): # Handles file errors michael@0: print "I/O error - Cannot perform extract operation: {1}".format(errno, strerror) michael@0: raise michael@0: list = f.namelist()[0] michael@0: temp_name = list.split('/') michael@0: print('Folder Name= ' +temp_name[0]) michael@0: self.folder_name = temp_name[0] michael@0: elif self.gz: michael@0: try: michael@0: f = tarfile.open(extfile,'r') michael@0: except IOError as (errno, strerror): # Handles file errors michael@0: print "I/O error - Cannot perform extract operation: {1}".format(errno, strerror) michael@0: raise michael@0: list = f.getnames()[0] michael@0: temp_name = list.split('/') michael@0: print('Folder Name= ' +temp_name[0]) michael@0: self.folder_name = temp_name[0] michael@0: michael@0: print ('Starting to Extract...') michael@0: michael@0: # 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: # timeout, the process is killed. michael@0: kill_check = threading.Event() michael@0: michael@0: if self.zip: michael@0: # Call the command to unzip the file. michael@0: if self.mswindows: michael@0: zipfile.ZipFile.extractall(f) michael@0: else: michael@0: p = subprocess.Popen('unzip '+extfile, stdout=subprocess.PIPE, shell=True) michael@0: pid = p.pid michael@0: elif self.gz: michael@0: # Call the command to untar the file. michael@0: if self.mswindows: michael@0: tarfile.TarFile.extractall(f) michael@0: else: michael@0: p = subprocess.Popen('tar -xf '+extfile, stdout=subprocess.PIPE, shell=True) michael@0: pid = p.pid michael@0: michael@0: #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: if self.mswindows==False: michael@0: watch = threading.Timer(timeout, kill_process, args=(pid, kill_check, self.mswindows )) michael@0: watch.start() michael@0: (stdout, stderr) = p.communicate() michael@0: watch.cancel() # if it's still waiting to run michael@0: success = not kill_check.isSet() michael@0: michael@0: # Abort process if process fails. michael@0: if not success: michael@0: raise RuntimeError michael@0: kill_check.clear() michael@0: print('Extraction Successful.') michael@0: except RuntimeError: michael@0: print "Ending the program" michael@0: sys.exit(1) michael@0: except: michael@0: print "Error during file extraction: ", sys.exc_info()[0] michael@0: raise michael@0: michael@0: # Function to run the cfx testall comands and to make sure the SDK is not broken. michael@0: def run_testall(self, home_path, folder_name): michael@0: try: michael@0: timeout = 500 michael@0: michael@0: self.new_dir = home_path + folder_name michael@0: try: michael@0: os.chdir(self.new_dir) michael@0: except OSError: michael@0: # Will reach here if the jetpack 0.X directory doesnt exist michael@0: print 'O/S Error: Jetpack directory does not exist at ' + self.new_dir michael@0: raise michael@0: print '\nStarting tests...' michael@0: # 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: # timeout, the process is killed. michael@0: kill_check = threading.Event() michael@0: michael@0: # Set the path for the logs. They will be in the parent directory of the Jetpack SDK. michael@0: log_path = home_path + 'tests.log' michael@0: michael@0: # Subprocess call to set up the jetpack environment and to start the tests. Also sends the output to a log file. michael@0: if self.bin != None: michael@0: if self.mswindows: michael@0: p = subprocess.Popen("bin\\activate && cfx testall -a firefox -b \"" + self.bin + "\"" , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) michael@0: proc_handle = p._handle michael@0: (stdout,stderr) = p.communicate() michael@0: else: michael@0: p = subprocess.Popen('. bin/activate; cfx testall -a firefox -b ' + self.bin , stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) michael@0: pid = p.pid michael@0: (stdout,stderr) = p.communicate() michael@0: elif self.bin == None: michael@0: if self.mswindows: michael@0: p=subprocess.Popen('bin\\activate && cfx testall -a firefox > '+log_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) michael@0: proc_handle = p._handle michael@0: (stdout,stderr) = p.communicate() michael@0: else: michael@0: p = subprocess.Popen('. bin/activate; cfx testall -a firefox > '+log_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) michael@0: pid = p.pid michael@0: (stdout,stderr) = p.communicate() michael@0: michael@0: #Write the output to log file michael@0: f=open(log_path,"w") michael@0: f.write(stdout+stderr) michael@0: f.close() michael@0: michael@0: #Watchdog for timeout process michael@0: if self.mswindows: michael@0: watch = threading.Timer(timeout, kill_process, args=(proc_handle, kill_check, self.mswindows)) michael@0: else: michael@0: watch = threading.Timer(timeout, kill_process, args=(pid, kill_check, self.mswindows)) michael@0: watch.start() michael@0: watch.cancel() # if it's still waiting to run michael@0: success = not kill_check.isSet() michael@0: if not success: michael@0: raise RuntimeError michael@0: kill_check.clear() michael@0: michael@0: if p.returncode!=0: michael@0: print('\nAll tests were not successful. Check the test-logs in the jetpack directory.') michael@0: result_sdk(home_path) michael@0: #sys.exit(1) michael@0: raise RuntimeError michael@0: else: michael@0: ret_code=result_sdk(home_path) michael@0: if ret_code==0: michael@0: print('\nAll tests were successful. Yay \o/ . Running a sample package test now...') michael@0: else: michael@0: print ('\nThere were errors during the tests.Take a look at logs') michael@0: raise RuntimeError michael@0: except RuntimeError: michael@0: print "Ending the program" michael@0: sys.exit(1) michael@0: except: michael@0: print "Error during the testall command execution:", sys.exc_info()[0] michael@0: raise michael@0: michael@0: def package(self, example_dir): michael@0: try: michael@0: timeout = 30 michael@0: michael@0: print '\nNow Running packaging tests...' michael@0: michael@0: kill_check = threading.Event() michael@0: michael@0: # Set the path for the example logs. They will be in the parent directory of the Jetpack SDK. michael@0: exlog_path = example_dir + 'test-example.log' michael@0: # Subprocess call to test the sample example for packaging. michael@0: if self.bin!=None: michael@0: if self.mswindows: michael@0: 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: proc_handle = p._handle michael@0: (stdout, stderr) = p.communicate() michael@0: else: michael@0: 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: pid = p.pid michael@0: (stdout, stderr) = p.communicate() michael@0: elif self.bin==None: michael@0: if self.mswindows: michael@0: 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: proc_handle = p._handle michael@0: (stdout, stderr) = p.communicate() michael@0: else: michael@0: 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: pid = p.pid michael@0: (stdout, stderr) = p.communicate() michael@0: michael@0: #Write the output to log file michael@0: f=open(exlog_path,"w") michael@0: f.write(stdout+stderr) michael@0: f.close() michael@0: michael@0: #Watch dog for timeout process michael@0: if self.mswindows: michael@0: watch = threading.Timer(timeout, kill_process, args=(proc_handle, kill_check, self.mswindows)) michael@0: else: michael@0: watch = threading.Timer(timeout, kill_process, args=(pid, kill_check, self.mswindows)) michael@0: watch.start() michael@0: watch.cancel() # if it's still waiting to run michael@0: success = not kill_check.isSet() michael@0: if not success: michael@0: raise RuntimeError michael@0: kill_check.clear() michael@0: michael@0: if p.returncode != 0: michael@0: print('\nSample tests were not executed correctly. Check the test-example log in jetpack diretory.') michael@0: result_example(example_dir) michael@0: raise RuntimeError michael@0: else: michael@0: ret_code=result_example(example_dir) michael@0: if ret_code==0: michael@0: print('\nAll tests pass. The SDK is working! Yay \o/') michael@0: else: michael@0: print ('\nTests passed with warning.Take a look at logs') michael@0: sys.exit(1) michael@0: michael@0: except RuntimeError: michael@0: print "Ending program" michael@0: sys.exit(1) michael@0: except: michael@0: print "Error during running sample tests:", sys.exc_info()[0] michael@0: raise michael@0: michael@0: def result_sdk(sdk_dir): michael@0: log_path = sdk_dir + 'tests.log' michael@0: print 'Results are logged at:' + log_path michael@0: try: michael@0: f = open(log_path,'r') michael@0: # Handles file errors michael@0: except IOError : michael@0: print 'I/O error - Cannot open test log at ' + log_path michael@0: raise michael@0: michael@0: for line in reversed(open(log_path).readlines()): michael@0: if line.strip()=='FAIL': michael@0: print ('\nOverall result - FAIL. Look at the test log at '+log_path) michael@0: return 1 michael@0: return 0 michael@0: michael@0: michael@0: def result_example(sdk_dir): michael@0: exlog_path = sdk_dir + 'test-example.log' michael@0: print 'Sample test results are logged at:' + exlog_path michael@0: try: michael@0: f = open(exlog_path,'r') michael@0: # Handles file errors michael@0: except IOError : michael@0: print 'I/O error - Cannot open sample test log at ' + exlog_path michael@0: raise michael@0: michael@0: #Read the file in reverse and check for the keyword 'FAIL'. michael@0: for line in reversed(open(exlog_path).readlines()): michael@0: if line.strip()=='FAIL': michael@0: print ('\nOverall result for Sample tests - FAIL. Look at the test log at '+exlog_path) michael@0: return 1 michael@0: return 0 michael@0: michael@0: def kill_process(process, kill_check, mswindows): michael@0: print '\nProcess Timedout. Killing the process. Please Rerun this script.' michael@0: if mswindows: michael@0: win32api.TerminateProcess(process, -1) michael@0: else: michael@0: os.kill(process, signal.SIGKILL) michael@0: kill_check.set()# tell the main routine to kill. Used SIGKILL to hard kill the process. michael@0: return michael@0: michael@0: if __name__ == "__main__": michael@0: obj = SDK() michael@0: obj.download(obj.link,obj.fpath,obj.fname) michael@0: obj.extract(obj.base_path,obj.fname) michael@0: obj.run_testall(obj.base_path,obj.folder_name) michael@0: obj.package(obj.base_path)