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 75*5e208ef3SBarry Smith 76*5e208ef3SBarry Smith if url.startswith('ssh://hg@'): 77*5e208ef3SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 78*5e208ef3SBarry Smith 79*5e208ef3SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 80*5e208ef3SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 81*5e208ef3SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 82*5e208ef3SBarry Smith 83*5e208ef3SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url+' '+newgitrepo) 84*5e208ef3SBarry Smith return 85*5e208ef3SBarry Smith 8615ac2963SJed Brown # get the tarball file name from the URL 8715ac2963SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 8815ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 8915ac2963SJed Brown ext = os.path.splitext(localFile)[1] 9015ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 91179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 92179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 93179860b2SJed Brown if os.path.exists(localFile): 9415ac2963SJed Brown os.unlink(localFile) 9515ac2963SJed Brown 96179860b2SJed Brown try: 97728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 98728600e6SSatish Balay socket.setdefaulttimeout(30) 99179860b2SJed Brown urllib.urlretrieve(url, localFile) 100728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 101179860b2SJed Brown except Exception, e: 102728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 103179860b2SJed Brown failureMessage = '''\ 104179860b2SJed BrownUnable to download package %s from: %s 105179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 106179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 10715ac2963SJed Brown* Or perhaps you have a firewall blocking the download 108179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 109179860b2SJed Brown and use the configure option: 110179860b2SJed Brown --download-%s=/yourselectedlocation/%s 1111aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 112179860b2SJed Brown raise RuntimeError(failureMessage) 11315ac2963SJed Brown 11415ac2963SJed Brown self.logPrint('Extracting '+localFile) 11515ac2963SJed Brown if ext in ['.zip','.ZIP']: 11615ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 11715ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 118179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 11915ac2963SJed Brown else: 12015ac2963SJed Brown failureMessage = '''\ 12115ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 12215ac2963SJed Brown[or installed python cannot process compressed files] 12315ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 12415ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 12515ac2963SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 12615ac2963SJed Brown and use the configure option: 12715ac2963SJed Brown --download-%s=/yourselectedlocation/%s 1281aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 12915ac2963SJed Brown import tarfile 13015ac2963SJed Brown try: 13115ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 132b95f98c7SJed Brown except tarfile.ReadError, e: 133b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 13415ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 1352501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 1362501eaf6SSatish Balay firstname = tf.getnames()[0] 1372501eaf6SSatish Balay if firstname == 'pax_global_header': 1382501eaf6SSatish Balay firstmember = tf.getmembers()[1] 13915ac2963SJed Brown else: 1402501eaf6SSatish Balay firstmember = tf.getmembers()[0] 1412501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 1422501eaf6SSatish Balay if firstmember.isdir(): 1432501eaf6SSatish Balay dirname = firstmember.name 1442501eaf6SSatish Balay else: 1452501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 14615ac2963SJed Brown if hasattr(tf,'extractall'): #python 2.5+ 14715ac2963SJed Brown tf.extractall(root) 14815ac2963SJed Brown else: 14915ac2963SJed Brown for tfile in tf.getmembers(): 15015ac2963SJed Brown tf.extract(tfile,root) 15115ac2963SJed Brown tf.close() 15215ac2963SJed Brown 15315ac2963SJed Brown # fix file permissions for the untared tarballs. 15415ac2963SJed Brown try: 1552501eaf6SSatish Balay # check if 'dirname' is set' 1562501eaf6SSatish Balay if dirname: 157179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 1582501eaf6SSatish Balay else: 1592501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 160179860b2SJed Brown except RuntimeError, e: 16115ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 162179860b2SJed Brown os.unlink(localFile) 163179860b2SJed Brown return 164179860b2SJed Brown 165179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 166179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 167179860b2SJed Brown return self.genericRetrieve(url, root, name) 168179860b2SJed Brown 169179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 170179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 171179860b2SJed Brown return self.genericRetrieve(url, root, name) 172179860b2SJed Brown 173179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 174179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 175179860b2SJed Brown return self.genericRetrieve(url, root, name) 176179860b2SJed Brown 177179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 178179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 179179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 180179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 181179860b2SJed Brown try: 182179860b2SJed Brown config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name)) 183179860b2SJed Brown except RuntimeError: 184179860b2SJed Brown pass 185179860b2SJed Brown 186179860b2SJed Brown 187