1*5b6bfdb9SJed Brownfrom __future__ import absolute_import 2179860b2SJed Brownimport logger 3179860b2SJed Brown 4179860b2SJed Brownimport os 5179860b2SJed Brownimport urllib 6179860b2SJed Brownimport urlparse 7179860b2SJed Brownimport config.base 8728600e6SSatish Balayimport socket 9728600e6SSatish Balay 10179860b2SJed Brown# Fix parsing for nonstandard schemes 11179860b2SJed Brownurlparse.uses_netloc.extend(['bk', 'ssh', 'svn']) 12179860b2SJed Brown 13179860b2SJed Brownclass Retriever(logger.Logger): 14179860b2SJed Brown def __init__(self, sourceControl, clArgs = None, argDB = None): 15179860b2SJed Brown logger.Logger.__init__(self, clArgs, argDB) 16179860b2SJed Brown self.sourceControl = sourceControl 17179860b2SJed Brown self.stamp = None 18179860b2SJed Brown return 19179860b2SJed Brown 20179860b2SJed Brown def getAuthorizedUrl(self, url): 21179860b2SJed Brown '''This returns a tuple of the unauthorized and authorized URLs for the given URL, and a flag indicating which was input''' 22179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 23179860b2SJed Brown if not location: 24179860b2SJed Brown url = urlparse.urlunparse(('', '', path, parameters, query, fragment)) 25179860b2SJed Brown authUrl = None 26179860b2SJed Brown wasAuth = 0 27179860b2SJed Brown else: 28179860b2SJed Brown index = location.find('@') 29179860b2SJed Brown if index >= 0: 30179860b2SJed Brown login = location[0:index] 31179860b2SJed Brown authUrl = url 32179860b2SJed Brown url = urlparse.urlunparse((scheme, location[index+1:], path, parameters, query, fragment)) 33179860b2SJed Brown wasAuth = 1 34179860b2SJed Brown else: 35179860b2SJed Brown login = location.split('.')[0] 36179860b2SJed Brown authUrl = urlparse.urlunparse((scheme, login+'@'+location, path, parameters, query, fragment)) 37179860b2SJed Brown wasAuth = 0 38179860b2SJed Brown return (url, authUrl, wasAuth) 39179860b2SJed Brown 40179860b2SJed Brown def testAuthorizedUrl(self, authUrl): 41179860b2SJed Brown '''Raise an exception if the URL cannot receive an SSH login without a password''' 42179860b2SJed Brown if not authUrl: 43179860b2SJed Brown raise RuntimeError('Url is empty') 44179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(authUrl) 451a117d15SMatthew G. Knepley return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location, log = self.log) 46179860b2SJed Brown 471aefc9f4SSatish Balay def genericRetrieve(self, url, root, package): 48179860b2SJed Brown '''Fetch the gzipped tarfile indicated by url and expand it into root 49179860b2SJed Brown - All the logic for removing old versions, updating etc. must move''' 50179860b2SJed Brown 5152df3566SBarry Smith # copy a directory 5252df3566SBarry Smith if url.startswith('dir://'): 5352df3566SBarry Smith import shutil 5452df3566SBarry Smith dir = url[6:] 5552df3566SBarry Smith if not os.path.isdir(dir): raise RuntimeError('Url begins with dir:// but is not a directory') 5652df3566SBarry Smith 5752df3566SBarry Smith if os.path.isdir(os.path.join(root,os.path.basename(dir))): shutil.rmtree(os.path.join(root,os.path.basename(dir))) 5852df3566SBarry Smith if os.path.isfile(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir))) 5952df3566SBarry Smith 6052df3566SBarry Smith shutil.copytree(dir,os.path.join(root,os.path.basename(dir))) 6152df3566SBarry Smith return 6252df3566SBarry Smith 6352df3566SBarry Smith if url.startswith('git://'): 6452df3566SBarry Smith if not hasattr(self.sourceControl, 'git'): return 6552df3566SBarry Smith import shutil 6652df3566SBarry Smith dir = url[6:] 6752df3566SBarry Smith if os.path.isdir(dir): 6852df3566SBarry 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') 6952df3566SBarry Smith 701aefc9f4SSatish Balay newgitrepo = os.path.join(root,'git.'+package) 7152df3566SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 7252df3566SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 7352df3566SBarry Smith 74b93f8388SBarry Smith try: 751a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo, log = self.log) 76*5b6bfdb9SJed Brown except RuntimeError as e: 77b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 78b93f8388SBarry Smith err = str(e) 79b93f8388SBarry Smith failureMessage = '''\ 80b93f8388SBarry SmithUnable to download package %s from: %s 81b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 82b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 83b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 84b93f8388SBarry Smith* You can run with --with-packages-dir=/adirectory and ./configure will instruct you what packages to download manually 85b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 86b93f8388SBarry Smith and use the configure option: 87b93f8388SBarry Smith --download-%s=/yourselectedlocation 88b93f8388SBarry Smith''' % (package.upper(), url, package) 89b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 9052df3566SBarry Smith return 915e208ef3SBarry Smith 920c3d3c20SBarry Smith if url.startswith('hg://'): 930c3d3c20SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 940c3d3c20SBarry Smith 950c3d3c20SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 960c3d3c20SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 970c3d3c20SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 98b93f8388SBarry Smith try: 990c3d3c20SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url[5:]+' '+newgitrepo) 100*5b6bfdb9SJed Brown except RuntimeError as e: 101b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 102b93f8388SBarry Smith err = str(e) 103b93f8388SBarry Smith failureMessage = '''\ 104b93f8388SBarry SmithUnable to download package %s from: %s 105b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 106b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 107b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 108b93f8388SBarry Smith* You can run with --with-packages-dir=/adirectory and ./configure will instruct you what packages to download manually 109b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 110b93f8388SBarry Smith and use the configure option: 111b93f8388SBarry Smith --download-%s=/yourselectedlocation 112b93f8388SBarry Smith''' % (package.upper(), url, package) 113b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 1140c3d3c20SBarry Smith return 1150c3d3c20SBarry Smith 1165e208ef3SBarry Smith if url.startswith('ssh://hg@'): 1175e208ef3SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 1185e208ef3SBarry Smith 1195e208ef3SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 1205e208ef3SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 1215e208ef3SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 122b93f8388SBarry Smith try: 1235e208ef3SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url+' '+newgitrepo) 124*5b6bfdb9SJed Brown except RuntimeError as e: 125b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 126b93f8388SBarry Smith err = str(e) 127b93f8388SBarry Smith failureMessage = '''\ 128b93f8388SBarry SmithUnable to download package %s from: %s 129b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 130b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 131b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 132b93f8388SBarry Smith* You can run with --with-packages-dir=/adirectory and ./configure will instruct you what packages to download manually 133b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 134b93f8388SBarry Smith and use the configure option: 135b93f8388SBarry Smith --download-%s=/yourselectedlocation 136b93f8388SBarry Smith''' % (package.upper(), url, package) 137b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 1385e208ef3SBarry Smith return 1395e208ef3SBarry Smith 14015ac2963SJed Brown # get the tarball file name from the URL 14115ac2963SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 14215ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 14315ac2963SJed Brown ext = os.path.splitext(localFile)[1] 14415ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 145179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 146179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 147179860b2SJed Brown if os.path.exists(localFile): 14815ac2963SJed Brown os.unlink(localFile) 14915ac2963SJed Brown 150179860b2SJed Brown try: 151728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 152728600e6SSatish Balay socket.setdefaulttimeout(30) 153179860b2SJed Brown urllib.urlretrieve(url, localFile) 154728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 155*5b6bfdb9SJed Brown except Exception as e: 156728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 157179860b2SJed Brown failureMessage = '''\ 158179860b2SJed BrownUnable to download package %s from: %s 159179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 160179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 16115ac2963SJed Brown* Or perhaps you have a firewall blocking the download 162b93f8388SBarry Smith* You can run with --with-packages-dir=/adirectory and ./configure will instruct you what packages to download manually 163b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s 164179860b2SJed Brown and use the configure option: 165179860b2SJed Brown --download-%s=/yourselectedlocation/%s 1661aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 167179860b2SJed Brown raise RuntimeError(failureMessage) 16815ac2963SJed Brown 16915ac2963SJed Brown self.logPrint('Extracting '+localFile) 17015ac2963SJed Brown if ext in ['.zip','.ZIP']: 17115ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 17215ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 173179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 17415ac2963SJed Brown else: 17515ac2963SJed Brown failureMessage = '''\ 17615ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 17715ac2963SJed Brown[or installed python cannot process compressed files] 17815ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 17915ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 180b93f8388SBarry Smith* You can run with --with-packages-dir=/adirectory and ./configure will instruct you what packages to download manually 181b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s 18215ac2963SJed Brown and use the configure option: 18315ac2963SJed Brown --download-%s=/yourselectedlocation/%s 1841aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 18515ac2963SJed Brown import tarfile 18615ac2963SJed Brown try: 18715ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 188*5b6bfdb9SJed Brown except tarfile.ReadError as e: 189b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 19015ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 1912501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 1922501eaf6SSatish Balay firstname = tf.getnames()[0] 1932501eaf6SSatish Balay if firstname == 'pax_global_header': 1942501eaf6SSatish Balay firstmember = tf.getmembers()[1] 19515ac2963SJed Brown else: 1962501eaf6SSatish Balay firstmember = tf.getmembers()[0] 1972501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 1982501eaf6SSatish Balay if firstmember.isdir(): 1992501eaf6SSatish Balay dirname = firstmember.name 2002501eaf6SSatish Balay else: 2012501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 20215ac2963SJed Brown if hasattr(tf,'extractall'): #python 2.5+ 20315ac2963SJed Brown tf.extractall(root) 20415ac2963SJed Brown else: 20515ac2963SJed Brown for tfile in tf.getmembers(): 20615ac2963SJed Brown tf.extract(tfile,root) 20715ac2963SJed Brown tf.close() 20815ac2963SJed Brown 20915ac2963SJed Brown # fix file permissions for the untared tarballs. 21015ac2963SJed Brown try: 2112501eaf6SSatish Balay # check if 'dirname' is set' 2122501eaf6SSatish Balay if dirname: 213179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 2142501eaf6SSatish Balay else: 2152501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 216*5b6bfdb9SJed Brown except RuntimeError as e: 21715ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 218179860b2SJed Brown os.unlink(localFile) 219179860b2SJed Brown return 220179860b2SJed Brown 221179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 222179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 223179860b2SJed Brown return self.genericRetrieve(url, root, name) 224179860b2SJed Brown 225179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 226179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 227179860b2SJed Brown return self.genericRetrieve(url, root, name) 228179860b2SJed Brown 229179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 230179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 231179860b2SJed Brown return self.genericRetrieve(url, root, name) 232179860b2SJed Brown 233179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 234179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 235179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 236179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 237179860b2SJed Brown try: 2381a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name), log = self.log) 239179860b2SJed Brown except RuntimeError: 240179860b2SJed Brown pass 241179860b2SJed Brown 242179860b2SJed Brown 243