1179860b2SJed Brownimport logger 2179860b2SJed Brown 3179860b2SJed Brownimport os 4179860b2SJed Brownimport urllib 5179860b2SJed Brownimport urlparse 6179860b2SJed Brownimport config.base 7728600e6SSatish Balayimport socket 8728600e6SSatish Balay 9179860b2SJed Brown# Fix parsing for nonstandard schemes 10179860b2SJed Brownurlparse.uses_netloc.extend(['bk', 'ssh', 'svn']) 11179860b2SJed Brown 12179860b2SJed Brownclass Retriever(logger.Logger): 13179860b2SJed Brown def __init__(self, sourceControl, clArgs = None, argDB = None): 14179860b2SJed Brown logger.Logger.__init__(self, clArgs, argDB) 15179860b2SJed Brown self.sourceControl = sourceControl 16179860b2SJed Brown self.stamp = None 17179860b2SJed Brown return 18179860b2SJed Brown 19179860b2SJed Brown def getAuthorizedUrl(self, url): 20179860b2SJed Brown '''This returns a tuple of the unauthorized and authorized URLs for the given URL, and a flag indicating which was input''' 21179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 22179860b2SJed Brown if not location: 23179860b2SJed Brown url = urlparse.urlunparse(('', '', path, parameters, query, fragment)) 24179860b2SJed Brown authUrl = None 25179860b2SJed Brown wasAuth = 0 26179860b2SJed Brown else: 27179860b2SJed Brown index = location.find('@') 28179860b2SJed Brown if index >= 0: 29179860b2SJed Brown login = location[0:index] 30179860b2SJed Brown authUrl = url 31179860b2SJed Brown url = urlparse.urlunparse((scheme, location[index+1:], path, parameters, query, fragment)) 32179860b2SJed Brown wasAuth = 1 33179860b2SJed Brown else: 34179860b2SJed Brown login = location.split('.')[0] 35179860b2SJed Brown authUrl = urlparse.urlunparse((scheme, login+'@'+location, path, parameters, query, fragment)) 36179860b2SJed Brown wasAuth = 0 37179860b2SJed Brown return (url, authUrl, wasAuth) 38179860b2SJed Brown 39179860b2SJed Brown def testAuthorizedUrl(self, authUrl): 40179860b2SJed Brown '''Raise an exception if the URL cannot receive an SSH login without a password''' 41179860b2SJed Brown if not authUrl: 42179860b2SJed Brown raise RuntimeError('Url is empty') 43179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(authUrl) 44*1a117d15SMatthew G. Knepley return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location, log = self.log) 45179860b2SJed Brown 461aefc9f4SSatish Balay def genericRetrieve(self, url, root, package): 47179860b2SJed Brown '''Fetch the gzipped tarfile indicated by url and expand it into root 48179860b2SJed Brown - All the logic for removing old versions, updating etc. must move''' 49179860b2SJed Brown 5052df3566SBarry Smith # copy a directory 5152df3566SBarry Smith if url.startswith('dir://'): 5252df3566SBarry Smith import shutil 5352df3566SBarry Smith dir = url[6:] 5452df3566SBarry Smith if not os.path.isdir(dir): raise RuntimeError('Url begins with dir:// but is not a directory') 5552df3566SBarry Smith 5652df3566SBarry Smith if os.path.isdir(os.path.join(root,os.path.basename(dir))): shutil.rmtree(os.path.join(root,os.path.basename(dir))) 5752df3566SBarry Smith if os.path.isfile(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir))) 5852df3566SBarry Smith 5952df3566SBarry Smith shutil.copytree(dir,os.path.join(root,os.path.basename(dir))) 6052df3566SBarry Smith return 6152df3566SBarry Smith 6252df3566SBarry Smith if url.startswith('git://'): 6352df3566SBarry Smith if not hasattr(self.sourceControl, 'git'): return 6452df3566SBarry Smith import shutil 6552df3566SBarry Smith dir = url[6:] 6652df3566SBarry Smith if os.path.isdir(dir): 6752df3566SBarry Smith if not os.path.isdir(os.path.join(dir,'.git')): raise RuntimeError('Url begins with git:// and is a directory but but does not have a .git subdirectory') 6852df3566SBarry Smith 691aefc9f4SSatish Balay newgitrepo = os.path.join(root,'git.'+package) 7052df3566SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 7152df3566SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 7252df3566SBarry Smith 73*1a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo, log = self.log) 7452df3566SBarry Smith return 7515ac2963SJed Brown # get the tarball file name from the URL 7615ac2963SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 7715ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 7815ac2963SJed Brown ext = os.path.splitext(localFile)[1] 7915ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 80179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 81179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 82179860b2SJed Brown if os.path.exists(localFile): 8315ac2963SJed Brown os.unlink(localFile) 8415ac2963SJed Brown 85179860b2SJed Brown try: 86728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 87728600e6SSatish Balay socket.setdefaulttimeout(30) 88179860b2SJed Brown urllib.urlretrieve(url, localFile) 89728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 90179860b2SJed Brown except Exception, e: 91728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 92179860b2SJed Brown failureMessage = '''\ 93179860b2SJed BrownUnable to download package %s from: %s 94179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 95179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 9615ac2963SJed Brown* Or perhaps you have a firewall blocking the download 97179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 98179860b2SJed Brown and use the configure option: 99179860b2SJed Brown --download-%s=/yourselectedlocation/%s 1001aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 101179860b2SJed Brown raise RuntimeError(failureMessage) 10215ac2963SJed Brown 10315ac2963SJed Brown self.logPrint('Extracting '+localFile) 10415ac2963SJed Brown if ext in ['.zip','.ZIP']: 10515ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 10615ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 107179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 10815ac2963SJed Brown else: 10915ac2963SJed Brown failureMessage = '''\ 11015ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 11115ac2963SJed Brown[or installed python cannot process compressed files] 11215ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 11315ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 11415ac2963SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 11515ac2963SJed Brown and use the configure option: 11615ac2963SJed Brown --download-%s=/yourselectedlocation/%s 1171aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 11815ac2963SJed Brown import tarfile 11915ac2963SJed Brown try: 12015ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 121b95f98c7SJed Brown except tarfile.ReadError, e: 122b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 12315ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 1242501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 1252501eaf6SSatish Balay firstname = tf.getnames()[0] 1262501eaf6SSatish Balay if firstname == 'pax_global_header': 1272501eaf6SSatish Balay firstmember = tf.getmembers()[1] 12815ac2963SJed Brown else: 1292501eaf6SSatish Balay firstmember = tf.getmembers()[0] 1302501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 1312501eaf6SSatish Balay if firstmember.isdir(): 1322501eaf6SSatish Balay dirname = firstmember.name 1332501eaf6SSatish Balay else: 1342501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 13515ac2963SJed Brown if hasattr(tf,'extractall'): #python 2.5+ 13615ac2963SJed Brown tf.extractall(root) 13715ac2963SJed Brown else: 13815ac2963SJed Brown for tfile in tf.getmembers(): 13915ac2963SJed Brown tf.extract(tfile,root) 14015ac2963SJed Brown tf.close() 14115ac2963SJed Brown 14215ac2963SJed Brown # fix file permissions for the untared tarballs. 14315ac2963SJed Brown try: 1442501eaf6SSatish Balay # check if 'dirname' is set' 1452501eaf6SSatish Balay if dirname: 146179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 1472501eaf6SSatish Balay else: 1482501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 149179860b2SJed Brown except RuntimeError, e: 15015ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 151179860b2SJed Brown os.unlink(localFile) 152179860b2SJed Brown return 153179860b2SJed Brown 154179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 155179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 156179860b2SJed Brown return self.genericRetrieve(url, root, name) 157179860b2SJed Brown 158179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 159179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 160179860b2SJed Brown return self.genericRetrieve(url, root, name) 161179860b2SJed Brown 162179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 163179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 164179860b2SJed Brown return self.genericRetrieve(url, root, name) 165179860b2SJed Brown 166179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 167179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 168179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 169179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 170179860b2SJed Brown try: 171*1a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name), log = self.log) 172179860b2SJed Brown except RuntimeError: 173179860b2SJed Brown pass 174179860b2SJed Brown 175179860b2SJed Brown 176