15b6bfdb9SJed Brownfrom __future__ import absolute_import 2179860b2SJed Brownimport logger 3179860b2SJed Brown 4179860b2SJed Brownimport os 5e7c47bf1SJed Browntry: 6e7c47bf1SJed Brown from urllib import urlretrieve 7e7c47bf1SJed Brownexcept ImportError: 8e7c47bf1SJed Brown from urllib.request import urlretrieve 9e7c47bf1SJed Browntry: 10179860b2SJed Brown import urlparse 11*9ad79eecSSatish Balayexcept ImportError: 12e7c47bf1SJed Brown from urllib import parse as urlparse 13179860b2SJed Brownimport config.base 14728600e6SSatish Balayimport socket 15728600e6SSatish Balay 16179860b2SJed Brown# Fix parsing for nonstandard schemes 17179860b2SJed Brownurlparse.uses_netloc.extend(['bk', 'ssh', 'svn']) 18179860b2SJed Brown 19179860b2SJed Brownclass Retriever(logger.Logger): 20179860b2SJed Brown def __init__(self, sourceControl, clArgs = None, argDB = None): 21179860b2SJed Brown logger.Logger.__init__(self, clArgs, argDB) 22179860b2SJed Brown self.sourceControl = sourceControl 23179860b2SJed Brown self.stamp = None 24179860b2SJed Brown return 25179860b2SJed Brown 26179860b2SJed Brown def getAuthorizedUrl(self, url): 27179860b2SJed Brown '''This returns a tuple of the unauthorized and authorized URLs for the given URL, and a flag indicating which was input''' 28179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 29179860b2SJed Brown if not location: 30179860b2SJed Brown url = urlparse.urlunparse(('', '', path, parameters, query, fragment)) 31179860b2SJed Brown authUrl = None 32179860b2SJed Brown wasAuth = 0 33179860b2SJed Brown else: 34179860b2SJed Brown index = location.find('@') 35179860b2SJed Brown if index >= 0: 36179860b2SJed Brown login = location[0:index] 37179860b2SJed Brown authUrl = url 38179860b2SJed Brown url = urlparse.urlunparse((scheme, location[index+1:], path, parameters, query, fragment)) 39179860b2SJed Brown wasAuth = 1 40179860b2SJed Brown else: 41179860b2SJed Brown login = location.split('.')[0] 42179860b2SJed Brown authUrl = urlparse.urlunparse((scheme, login+'@'+location, path, parameters, query, fragment)) 43179860b2SJed Brown wasAuth = 0 44179860b2SJed Brown return (url, authUrl, wasAuth) 45179860b2SJed Brown 46179860b2SJed Brown def testAuthorizedUrl(self, authUrl): 47179860b2SJed Brown '''Raise an exception if the URL cannot receive an SSH login without a password''' 48179860b2SJed Brown if not authUrl: 49179860b2SJed Brown raise RuntimeError('Url is empty') 50179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(authUrl) 511a117d15SMatthew G. Knepley return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location, log = self.log) 52179860b2SJed Brown 531aefc9f4SSatish Balay def genericRetrieve(self, url, root, package): 54179860b2SJed Brown '''Fetch the gzipped tarfile indicated by url and expand it into root 55179860b2SJed Brown - All the logic for removing old versions, updating etc. must move''' 56179860b2SJed Brown 5752df3566SBarry Smith # copy a directory 5852df3566SBarry Smith if url.startswith('dir://'): 5952df3566SBarry Smith import shutil 6052df3566SBarry Smith dir = url[6:] 6152df3566SBarry Smith if not os.path.isdir(dir): raise RuntimeError('Url begins with dir:// but is not a directory') 6252df3566SBarry Smith 6352df3566SBarry Smith if os.path.isdir(os.path.join(root,os.path.basename(dir))): shutil.rmtree(os.path.join(root,os.path.basename(dir))) 6452df3566SBarry Smith if os.path.isfile(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir))) 6552df3566SBarry Smith 6652df3566SBarry Smith shutil.copytree(dir,os.path.join(root,os.path.basename(dir))) 6752df3566SBarry Smith return 6852df3566SBarry Smith 6952df3566SBarry Smith if url.startswith('git://'): 7052df3566SBarry Smith if not hasattr(self.sourceControl, 'git'): return 7152df3566SBarry Smith import shutil 7252df3566SBarry Smith dir = url[6:] 7352df3566SBarry Smith if os.path.isdir(dir): 7452df3566SBarry 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') 7552df3566SBarry Smith 761aefc9f4SSatish Balay newgitrepo = os.path.join(root,'git.'+package) 7752df3566SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 7852df3566SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 7952df3566SBarry Smith 80b93f8388SBarry Smith try: 811a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo, log = self.log) 825b6bfdb9SJed Brown except RuntimeError as e: 83b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 84b93f8388SBarry Smith err = str(e) 85b93f8388SBarry Smith failureMessage = '''\ 86b93f8388SBarry SmithUnable to download package %s from: %s 87b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 88b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 89b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 900aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 91b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 92b93f8388SBarry Smith and use the configure option: 93b93f8388SBarry Smith --download-%s=/yourselectedlocation 94b93f8388SBarry Smith''' % (package.upper(), url, package) 95b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 9652df3566SBarry Smith return 975e208ef3SBarry Smith 980c3d3c20SBarry Smith if url.startswith('hg://'): 990c3d3c20SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 1000c3d3c20SBarry Smith 1010c3d3c20SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 1020c3d3c20SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 1030c3d3c20SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 104b93f8388SBarry Smith try: 1050c3d3c20SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url[5:]+' '+newgitrepo) 1065b6bfdb9SJed Brown except RuntimeError as e: 107b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 108b93f8388SBarry Smith err = str(e) 109b93f8388SBarry Smith failureMessage = '''\ 110b93f8388SBarry SmithUnable to download package %s from: %s 111b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 112b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 113b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 1140aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 115b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 116b93f8388SBarry Smith and use the configure option: 117b93f8388SBarry Smith --download-%s=/yourselectedlocation 118b93f8388SBarry Smith''' % (package.upper(), url, package) 119b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 1200c3d3c20SBarry Smith return 1210c3d3c20SBarry Smith 1225e208ef3SBarry Smith if url.startswith('ssh://hg@'): 1235e208ef3SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 1245e208ef3SBarry Smith 1255e208ef3SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 1265e208ef3SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 1275e208ef3SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 128b93f8388SBarry Smith try: 1295e208ef3SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url+' '+newgitrepo) 1305b6bfdb9SJed Brown except RuntimeError as e: 131b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 132b93f8388SBarry Smith err = str(e) 133b93f8388SBarry Smith failureMessage = '''\ 134b93f8388SBarry SmithUnable to download package %s from: %s 135b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 136b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 137b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 1380aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 139b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 140b93f8388SBarry Smith and use the configure option: 141b93f8388SBarry Smith --download-%s=/yourselectedlocation 142b93f8388SBarry Smith''' % (package.upper(), url, package) 143b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 1445e208ef3SBarry Smith return 1455e208ef3SBarry Smith 14615ac2963SJed Brown # get the tarball file name from the URL 14715ac2963SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 14815ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 14915ac2963SJed Brown ext = os.path.splitext(localFile)[1] 15015ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 151179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 152179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 153179860b2SJed Brown if os.path.exists(localFile): 15415ac2963SJed Brown os.unlink(localFile) 15515ac2963SJed Brown 156179860b2SJed Brown try: 157728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 158728600e6SSatish Balay socket.setdefaulttimeout(30) 159e7c47bf1SJed Brown urlretrieve(url, localFile) 160728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 1615b6bfdb9SJed Brown except Exception as e: 162728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 163179860b2SJed Brown failureMessage = '''\ 164179860b2SJed BrownUnable to download package %s from: %s 165179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 166179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 16715ac2963SJed Brown* Or perhaps you have a firewall blocking the download 1680aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 169b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s 170179860b2SJed Brown and use the configure option: 171179860b2SJed Brown --download-%s=/yourselectedlocation/%s 1721aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 173179860b2SJed Brown raise RuntimeError(failureMessage) 17415ac2963SJed Brown 17515ac2963SJed Brown self.logPrint('Extracting '+localFile) 17615ac2963SJed Brown if ext in ['.zip','.ZIP']: 17715ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 17815ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 179179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 18015ac2963SJed Brown else: 18115ac2963SJed Brown failureMessage = '''\ 18215ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 18315ac2963SJed Brown[or installed python cannot process compressed files] 18415ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 18515ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 1860aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 187b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s 18815ac2963SJed Brown and use the configure option: 18915ac2963SJed Brown --download-%s=/yourselectedlocation/%s 1901aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 19115ac2963SJed Brown import tarfile 19215ac2963SJed Brown try: 19315ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 1945b6bfdb9SJed Brown except tarfile.ReadError as e: 195b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 19615ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 1972501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 1982501eaf6SSatish Balay firstname = tf.getnames()[0] 1992501eaf6SSatish Balay if firstname == 'pax_global_header': 2002501eaf6SSatish Balay firstmember = tf.getmembers()[1] 20115ac2963SJed Brown else: 2022501eaf6SSatish Balay firstmember = tf.getmembers()[0] 2032501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 2042501eaf6SSatish Balay if firstmember.isdir(): 2052501eaf6SSatish Balay dirname = firstmember.name 2062501eaf6SSatish Balay else: 2072501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 20815ac2963SJed Brown tf.extractall(root) 20915ac2963SJed Brown tf.close() 21015ac2963SJed Brown 21115ac2963SJed Brown # fix file permissions for the untared tarballs. 21215ac2963SJed Brown try: 2132501eaf6SSatish Balay # check if 'dirname' is set' 2142501eaf6SSatish Balay if dirname: 215179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 2162501eaf6SSatish Balay else: 2172501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 2185b6bfdb9SJed Brown except RuntimeError as e: 21915ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 220179860b2SJed Brown os.unlink(localFile) 221179860b2SJed Brown return 222179860b2SJed Brown 223179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 224179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 225179860b2SJed Brown return self.genericRetrieve(url, root, name) 226179860b2SJed Brown 227179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 228179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 229179860b2SJed Brown return self.genericRetrieve(url, root, name) 230179860b2SJed Brown 231179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 232179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 233179860b2SJed Brown return self.genericRetrieve(url, root, name) 234179860b2SJed Brown 235179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 236179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 237179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 238179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 239179860b2SJed Brown try: 2401a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name), log = self.log) 241179860b2SJed Brown except RuntimeError: 242179860b2SJed Brown pass 243179860b2SJed Brown 244179860b2SJed Brown 245