1import hashlib,os,subprocess,sys 2 3# try to auto-detect the maximum number of available CPUs 4def get_cpus(): 5 try: 6 import multiprocessing 7 n_cpus = multiprocessing.cpu_count() 8 except: 9 n_cpus = 1 10 return n_cpus 11 12# expand to full path name 13# process leading '~' or relative path 14 15def fullpath(path): 16 return os.path.abspath(os.path.expanduser(path)) 17 18def which(program): 19 def is_exe(fpath): 20 return os.path.isfile(fpath) and os.access(fpath, os.X_OK) 21 22 fpath, fname = os.path.split(program) 23 if fpath: 24 if is_exe(program): 25 return program 26 else: 27 for path in os.environ["PATH"].split(os.pathsep): 28 path = path.strip('"') 29 exe_file = os.path.join(path, program) 30 if is_exe(exe_file): 31 return exe_file 32 33 return None 34 35def geturl(url,fname): 36 success = False 37 38 if which('curl') != None: 39 cmd = 'curl -L -o "%s" %s' % (fname,url) 40 try: 41 subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True) 42 success = True 43 except subprocess.CalledProcessError as e: 44 print("Calling curl failed with: %s" % e.output.decode('UTF-8')) 45 46 if not success and which('wget') != None: 47 cmd = 'wget -O "%s" %s' % (fname,url) 48 try: 49 subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True) 50 success = True 51 except subprocess.CalledProcessError as e: 52 print("Calling wget failed with: %s" % e.output.decode('UTF-8')) 53 54 if not success: 55 error("Failed to download source code with 'curl' or 'wget'") 56 return 57 58def checkmd5sum(md5sum,fname): 59 with open(fname,'rb') as fh: 60 m = hashlib.md5() 61 while True: 62 data = fh.read(81920) 63 if not data: 64 break 65 m.update(data) 66 fh.close() 67 return m.hexdigest() == md5sum 68 69