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 46*52df3566SBarry Smith def genericRetrieve(self, url, root, name, gitcommit = None): 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 50*52df3566SBarry Smith # copy a directory 51*52df3566SBarry Smith if url.startswith('dir://'): 52*52df3566SBarry Smith import shutil 53*52df3566SBarry Smith dir = url[6:] 54*52df3566SBarry Smith if not os.path.isdir(dir): raise RuntimeError('Url begins with dir:// but is not a directory') 55*52df3566SBarry Smith 56*52df3566SBarry Smith if os.path.isdir(os.path.join(root,os.path.basename(dir))): shutil.rmtree(os.path.join(root,os.path.basename(dir))) 57*52df3566SBarry Smith if os.path.isfile(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir))) 58*52df3566SBarry Smith 59*52df3566SBarry Smith shutil.copytree(dir,os.path.join(root,os.path.basename(dir))) 60*52df3566SBarry Smith return 61*52df3566SBarry Smith 62*52df3566SBarry Smith if url.startswith('git://'): 63*52df3566SBarry Smith if not hasattr(self.sourceControl, 'git'): return 64*52df3566SBarry Smith import shutil 65*52df3566SBarry Smith dir = url[6:] 66*52df3566SBarry Smith if os.path.isdir(dir): 67*52df3566SBarry 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') 68*52df3566SBarry Smith 69*52df3566SBarry Smith newgitrepo = os.path.join(root,name) 70*52df3566SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 71*52df3566SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 72*52df3566SBarry Smith 73*52df3566SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo) 74*52df3566SBarry Smith if gitcommit: 75*52df3566SBarry Smith try: 76*52df3566SBarry Smith config.base.Configure.executeShellCommand([self.sourceControl.git, 'checkout', '-f', gitcommit], cwd=newgitrepo, log = self.log) 77*52df3566SBarry Smith except: 78*52df3566SBarry Smith raise RuntimeError('Unable to checkout commit id '+gitcommit+' in repository '+newgitrepo) 79*52df3566SBarry Smith return 80*52df3566SBarry Smith 8115ac2963SJed Brown # get the tarball file name from the URL 8215ac2963SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 8315ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 8415ac2963SJed Brown ext = os.path.splitext(localFile)[1] 8515ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 86179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 87179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 88179860b2SJed Brown if os.path.exists(localFile): 8915ac2963SJed Brown os.unlink(localFile) 9015ac2963SJed Brown 91179860b2SJed Brown try: 92728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 93728600e6SSatish Balay socket.setdefaulttimeout(30) 94179860b2SJed Brown urllib.urlretrieve(url, localFile) 95728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 96179860b2SJed Brown except Exception, e: 97728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 98179860b2SJed Brown failureMessage = '''\ 99179860b2SJed BrownUnable to download package %s from: %s 100179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 101179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 10215ac2963SJed Brown* Or perhaps you have a firewall blocking the download 103179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 104179860b2SJed Brown and use the configure option: 105179860b2SJed Brown --download-%s=/yourselectedlocation/%s 106179860b2SJed Brown''' % (name, url, filename, name.lower(), filename) 107179860b2SJed Brown raise RuntimeError(failureMessage) 10815ac2963SJed Brown 10915ac2963SJed Brown self.logPrint('Extracting '+localFile) 11015ac2963SJed Brown if ext in ['.zip','.ZIP']: 11115ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 11215ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 113179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 11415ac2963SJed Brown else: 11515ac2963SJed Brown failureMessage = '''\ 11615ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 11715ac2963SJed Brown[or installed python cannot process compressed files] 11815ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 11915ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 12015ac2963SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 12115ac2963SJed Brown and use the configure option: 12215ac2963SJed Brown --download-%s=/yourselectedlocation/%s 12315ac2963SJed Brown''' % (name, url, filename, name.lower(), filename) 12415ac2963SJed Brown import tarfile 12515ac2963SJed Brown try: 12615ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 127b95f98c7SJed Brown except tarfile.ReadError, e: 128b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 12915ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 1302501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 1312501eaf6SSatish Balay firstname = tf.getnames()[0] 1322501eaf6SSatish Balay if firstname == 'pax_global_header': 1332501eaf6SSatish Balay firstmember = tf.getmembers()[1] 13415ac2963SJed Brown else: 1352501eaf6SSatish Balay firstmember = tf.getmembers()[0] 1362501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 1372501eaf6SSatish Balay if firstmember.isdir(): 1382501eaf6SSatish Balay dirname = firstmember.name 1392501eaf6SSatish Balay else: 1402501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 14115ac2963SJed Brown if hasattr(tf,'extractall'): #python 2.5+ 14215ac2963SJed Brown tf.extractall(root) 14315ac2963SJed Brown else: 14415ac2963SJed Brown for tfile in tf.getmembers(): 14515ac2963SJed Brown tf.extract(tfile,root) 14615ac2963SJed Brown tf.close() 14715ac2963SJed Brown 14815ac2963SJed Brown # fix file permissions for the untared tarballs. 14915ac2963SJed Brown try: 1502501eaf6SSatish Balay # check if 'dirname' is set' 1512501eaf6SSatish Balay if dirname: 152179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 1532501eaf6SSatish Balay else: 1542501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 155179860b2SJed Brown except RuntimeError, e: 15615ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 157179860b2SJed Brown os.unlink(localFile) 158179860b2SJed Brown return 159179860b2SJed Brown 160179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 161179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 162179860b2SJed Brown return self.genericRetrieve(url, root, name) 163179860b2SJed Brown 164179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 165179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 166179860b2SJed Brown return self.genericRetrieve(url, root, name) 167179860b2SJed Brown 168179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 169179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 170179860b2SJed Brown return self.genericRetrieve(url, root, name) 171179860b2SJed Brown 172179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 173179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 174179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 175179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 176179860b2SJed Brown try: 177179860b2SJed Brown config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name)) 178179860b2SJed Brown except RuntimeError: 179179860b2SJed Brown pass 180179860b2SJed Brown 181179860b2SJed Brown 182