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) 44179860b2SJed Brown return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location) 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 7352df3566SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo) 7452df3566SBarry Smith return 755e208ef3SBarry Smith 76*0c3d3c20SBarry Smith if url.startswith('hg://'): 77*0c3d3c20SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 78*0c3d3c20SBarry Smith 79*0c3d3c20SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 80*0c3d3c20SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 81*0c3d3c20SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 82*0c3d3c20SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url[5:]+' '+newgitrepo) 83*0c3d3c20SBarry Smith return 84*0c3d3c20SBarry Smith 855e208ef3SBarry Smith if url.startswith('ssh://hg@'): 865e208ef3SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 875e208ef3SBarry Smith 885e208ef3SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 895e208ef3SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 905e208ef3SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 915e208ef3SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url+' '+newgitrepo) 925e208ef3SBarry Smith return 935e208ef3SBarry Smith 9415ac2963SJed Brown # get the tarball file name from the URL 9515ac2963SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 9615ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 9715ac2963SJed Brown ext = os.path.splitext(localFile)[1] 9815ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 99179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 100179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 101179860b2SJed Brown if os.path.exists(localFile): 10215ac2963SJed Brown os.unlink(localFile) 10315ac2963SJed Brown 104179860b2SJed Brown try: 105728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 106728600e6SSatish Balay socket.setdefaulttimeout(30) 107179860b2SJed Brown urllib.urlretrieve(url, localFile) 108728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 109179860b2SJed Brown except Exception, e: 110728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 111179860b2SJed Brown failureMessage = '''\ 112179860b2SJed BrownUnable to download package %s from: %s 113179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 114179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 11515ac2963SJed Brown* Or perhaps you have a firewall blocking the download 116179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 117179860b2SJed Brown and use the configure option: 118179860b2SJed Brown --download-%s=/yourselectedlocation/%s 1191aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 120179860b2SJed Brown raise RuntimeError(failureMessage) 12115ac2963SJed Brown 12215ac2963SJed Brown self.logPrint('Extracting '+localFile) 12315ac2963SJed Brown if ext in ['.zip','.ZIP']: 12415ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 12515ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 126179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 12715ac2963SJed Brown else: 12815ac2963SJed Brown failureMessage = '''\ 12915ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 13015ac2963SJed Brown[or installed python cannot process compressed files] 13115ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 13215ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 13315ac2963SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 13415ac2963SJed Brown and use the configure option: 13515ac2963SJed Brown --download-%s=/yourselectedlocation/%s 1361aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 13715ac2963SJed Brown import tarfile 13815ac2963SJed Brown try: 13915ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 140b95f98c7SJed Brown except tarfile.ReadError, e: 141b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 14215ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 1432501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 1442501eaf6SSatish Balay firstname = tf.getnames()[0] 1452501eaf6SSatish Balay if firstname == 'pax_global_header': 1462501eaf6SSatish Balay firstmember = tf.getmembers()[1] 14715ac2963SJed Brown else: 1482501eaf6SSatish Balay firstmember = tf.getmembers()[0] 1492501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 1502501eaf6SSatish Balay if firstmember.isdir(): 1512501eaf6SSatish Balay dirname = firstmember.name 1522501eaf6SSatish Balay else: 1532501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 15415ac2963SJed Brown if hasattr(tf,'extractall'): #python 2.5+ 15515ac2963SJed Brown tf.extractall(root) 15615ac2963SJed Brown else: 15715ac2963SJed Brown for tfile in tf.getmembers(): 15815ac2963SJed Brown tf.extract(tfile,root) 15915ac2963SJed Brown tf.close() 16015ac2963SJed Brown 16115ac2963SJed Brown # fix file permissions for the untared tarballs. 16215ac2963SJed Brown try: 1632501eaf6SSatish Balay # check if 'dirname' is set' 1642501eaf6SSatish Balay if dirname: 165179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 1662501eaf6SSatish Balay else: 1672501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 168179860b2SJed Brown except RuntimeError, e: 16915ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 170179860b2SJed Brown os.unlink(localFile) 171179860b2SJed Brown return 172179860b2SJed Brown 173179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 174179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 175179860b2SJed Brown return self.genericRetrieve(url, root, name) 176179860b2SJed Brown 177179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 178179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 179179860b2SJed Brown return self.genericRetrieve(url, root, name) 180179860b2SJed Brown 181179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 182179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 183179860b2SJed Brown return self.genericRetrieve(url, root, name) 184179860b2SJed Brown 185179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 186179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 187179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 188179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 189179860b2SJed Brown try: 190179860b2SJed Brown config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name)) 191179860b2SJed Brown except RuntimeError: 192179860b2SJed Brown pass 193179860b2SJed Brown 194179860b2SJed Brown 195