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: 108f450857SSatish Balay import urlparse as urlparse_local # novermin 119ad79eecSSatish Balayexcept ImportError: 128f450857SSatish Balay from urllib import parse as urlparse_local 13179860b2SJed Brownimport config.base 14728600e6SSatish Balayimport socket 15728600e6SSatish Balay 16179860b2SJed Brown# Fix parsing for nonstandard schemes 178f450857SSatish Balayurlparse_local.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''' 288f450857SSatish Balay (scheme, location, path, parameters, query, fragment) = urlparse_local.urlparse(url) 29179860b2SJed Brown if not location: 308f450857SSatish Balay url = urlparse_local.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 388f450857SSatish Balay url = urlparse_local.urlunparse((scheme, location[index+1:], path, parameters, query, fragment)) 39179860b2SJed Brown wasAuth = 1 40179860b2SJed Brown else: 41179860b2SJed Brown login = location.split('.')[0] 428f450857SSatish Balay authUrl = urlparse_local.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') 508f450857SSatish Balay (scheme, location, path, parameters, query, fragment) = urlparse_local.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 69*3a911845SSatish Balay if url.startswith('link://'): 70*3a911845SSatish Balay import shutil 71*3a911845SSatish Balay dir = url[7:] 72*3a911845SSatish Balay if not os.path.isdir(dir): raise RuntimeError('Url begins with link:// but it is not pointing to a directory') 73*3a911845SSatish Balay 74*3a911845SSatish Balay if os.path.islink(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir))) 75*3a911845SSatish Balay if os.path.isfile(os.path.join(root,os.path.basename(dir))): os.unlink(os.path.join(root,os.path.basename(dir))) 76*3a911845SSatish Balay if os.path.isdir(os.path.join(root,os.path.basename(dir))): shutil.rmtree(os.path.join(root,os.path.basename(dir))) 77*3a911845SSatish Balay os.symlink(os.path.abspath(dir),os.path.join(root,os.path.basename(dir))) 78*3a911845SSatish Balay return 79*3a911845SSatish Balay 8052df3566SBarry Smith if url.startswith('git://'): 8152df3566SBarry Smith if not hasattr(self.sourceControl, 'git'): return 8252df3566SBarry Smith import shutil 8352df3566SBarry Smith dir = url[6:] 8452df3566SBarry Smith if os.path.isdir(dir): 8552df3566SBarry 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') 8652df3566SBarry Smith 871aefc9f4SSatish Balay newgitrepo = os.path.join(root,'git.'+package) 8852df3566SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 8952df3566SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 9052df3566SBarry Smith 91b93f8388SBarry Smith try: 921a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.git+' clone '+dir+' '+newgitrepo, log = self.log) 935b6bfdb9SJed Brown except RuntimeError as e: 94b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 95b93f8388SBarry Smith err = str(e) 96b93f8388SBarry Smith failureMessage = '''\ 97b93f8388SBarry SmithUnable to download package %s from: %s 98b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 99b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 100b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 1010aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 102b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 103b93f8388SBarry Smith and use the configure option: 104b93f8388SBarry Smith --download-%s=/yourselectedlocation 105b93f8388SBarry Smith''' % (package.upper(), url, package) 106b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 10752df3566SBarry Smith return 1085e208ef3SBarry Smith 1090c3d3c20SBarry Smith if url.startswith('hg://'): 1100c3d3c20SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 1110c3d3c20SBarry Smith 1120c3d3c20SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 1130c3d3c20SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 1140c3d3c20SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 115b93f8388SBarry Smith try: 1160c3d3c20SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url[5:]+' '+newgitrepo) 1175b6bfdb9SJed Brown except RuntimeError as e: 118b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 119b93f8388SBarry Smith err = str(e) 120b93f8388SBarry Smith failureMessage = '''\ 121b93f8388SBarry SmithUnable to download package %s from: %s 122b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 123b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 124b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 1250aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 126b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 127b93f8388SBarry Smith and use the configure option: 128b93f8388SBarry Smith --download-%s=/yourselectedlocation 129b93f8388SBarry Smith''' % (package.upper(), url, package) 130b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 1310c3d3c20SBarry Smith return 1320c3d3c20SBarry Smith 1335e208ef3SBarry Smith if url.startswith('ssh://hg@'): 1345e208ef3SBarry Smith if not hasattr(self.sourceControl, 'hg'): return 1355e208ef3SBarry Smith 1365e208ef3SBarry Smith newgitrepo = os.path.join(root,'hg.'+package) 1375e208ef3SBarry Smith if os.path.isdir(newgitrepo): shutil.rmtree(newgitrepo) 1385e208ef3SBarry Smith if os.path.isfile(newgitrepo): os.unlink(newgitrepo) 139b93f8388SBarry Smith try: 1405e208ef3SBarry Smith config.base.Configure.executeShellCommand(self.sourceControl.hg+' clone '+url+' '+newgitrepo) 1415b6bfdb9SJed Brown except RuntimeError as e: 142b93f8388SBarry Smith self.logPrint('ERROR: '+str(e)) 143b93f8388SBarry Smith err = str(e) 144b93f8388SBarry Smith failureMessage = '''\ 145b93f8388SBarry SmithUnable to download package %s from: %s 146b93f8388SBarry Smith* If URL specified manually - perhaps there is a typo? 147b93f8388SBarry Smith* If your network is disconnected - please reconnect and rerun ./configure 148b93f8388SBarry Smith* Or perhaps you have a firewall blocking the download 1490aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 150b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation 151b93f8388SBarry Smith and use the configure option: 152b93f8388SBarry Smith --download-%s=/yourselectedlocation 153b93f8388SBarry Smith''' % (package.upper(), url, package) 154b93f8388SBarry Smith raise RuntimeError('Unable to download '+package+'\n'+err+failureMessage) 1555e208ef3SBarry Smith return 1565e208ef3SBarry Smith 15715ac2963SJed Brown # get the tarball file name from the URL 1588f450857SSatish Balay filename = os.path.basename(urlparse_local.urlparse(url)[2]) 15915ac2963SJed Brown localFile = os.path.join(root,'_d_'+filename) 16015ac2963SJed Brown ext = os.path.splitext(localFile)[1] 16115ac2963SJed Brown if ext not in ['.bz2','.tbz','.gz','.tgz','.zip','.ZIP']: 162179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 163179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 164179860b2SJed Brown if os.path.exists(localFile): 16515ac2963SJed Brown os.unlink(localFile) 16615ac2963SJed Brown 167179860b2SJed Brown try: 168728600e6SSatish Balay sav_timeout = socket.getdefaulttimeout() 169728600e6SSatish Balay socket.setdefaulttimeout(30) 170e7c47bf1SJed Brown urlretrieve(url, localFile) 171728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 1725b6bfdb9SJed Brown except Exception as e: 173728600e6SSatish Balay socket.setdefaulttimeout(sav_timeout) 174179860b2SJed Brown failureMessage = '''\ 175179860b2SJed BrownUnable to download package %s from: %s 176179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 177179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 17815ac2963SJed Brown* Or perhaps you have a firewall blocking the download 1790aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 180b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s 181179860b2SJed Brown and use the configure option: 182179860b2SJed Brown --download-%s=/yourselectedlocation/%s 1831aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 184179860b2SJed Brown raise RuntimeError(failureMessage) 18515ac2963SJed Brown 18615ac2963SJed Brown self.logPrint('Extracting '+localFile) 18715ac2963SJed Brown if ext in ['.zip','.ZIP']: 18815ac2963SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+localFile, log = self.log) 18915ac2963SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+localFile+' | head -n 1', log = self.log) 190179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 19115ac2963SJed Brown else: 19215ac2963SJed Brown failureMessage = '''\ 19315ac2963SJed BrownDownloaded package %s from: %s is not a tarball. 19415ac2963SJed Brown[or installed python cannot process compressed files] 19515ac2963SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 19615ac2963SJed Brown For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 1970aa1f76dSSatish Balay* You can run with --with-packages-download-dir=/adirectory and ./configure will instruct you what packages to download manually 198b93f8388SBarry Smith* or you can download the above URL manually, to /yourselectedlocation/%s 19915ac2963SJed Brown and use the configure option: 20015ac2963SJed Brown --download-%s=/yourselectedlocation/%s 2011aefc9f4SSatish Balay''' % (package.upper(), url, filename, package, filename) 20215ac2963SJed Brown import tarfile 20315ac2963SJed Brown try: 20415ac2963SJed Brown tf = tarfile.open(os.path.join(root, localFile)) 2055b6bfdb9SJed Brown except tarfile.ReadError as e: 206b95f98c7SJed Brown raise RuntimeError(str(e)+'\n'+failureMessage) 20715ac2963SJed Brown if not tf: raise RuntimeError(failureMessage) 2082501eaf6SSatish Balay #git puts 'pax_global_header' as the first entry and some tar utils process this as a file 2092501eaf6SSatish Balay firstname = tf.getnames()[0] 2102501eaf6SSatish Balay if firstname == 'pax_global_header': 2112501eaf6SSatish Balay firstmember = tf.getmembers()[1] 21215ac2963SJed Brown else: 2132501eaf6SSatish Balay firstmember = tf.getmembers()[0] 2142501eaf6SSatish Balay # some tarfiles list packagename/ but some list packagename/filename in the first entry 2152501eaf6SSatish Balay if firstmember.isdir(): 2162501eaf6SSatish Balay dirname = firstmember.name 2172501eaf6SSatish Balay else: 2182501eaf6SSatish Balay dirname = os.path.dirname(firstmember.name) 21915ac2963SJed Brown tf.extractall(root) 22015ac2963SJed Brown tf.close() 22115ac2963SJed Brown 22215ac2963SJed Brown # fix file permissions for the untared tarballs. 22315ac2963SJed Brown try: 2242501eaf6SSatish Balay # check if 'dirname' is set' 2252501eaf6SSatish Balay if dirname: 226179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 2272501eaf6SSatish Balay else: 2282501eaf6SSatish Balay self.logPrintBox('WARNING: Could not determine dirname extracted by '+localFile+' to fix file permissions') 2295b6bfdb9SJed Brown except RuntimeError as e: 23015ac2963SJed Brown raise RuntimeError('Error changing permissions for '+dirname+' obtained from '+localFile+ ' : '+str(e)) 231179860b2SJed Brown os.unlink(localFile) 232179860b2SJed Brown return 233179860b2SJed Brown 234179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 235179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 236179860b2SJed Brown return self.genericRetrieve(url, root, name) 237179860b2SJed Brown 238179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 239179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 240179860b2SJed Brown return self.genericRetrieve(url, root, name) 241179860b2SJed Brown 242179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 243179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 244179860b2SJed Brown return self.genericRetrieve(url, root, name) 245179860b2SJed Brown 246179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 247179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 248179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 249179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 250179860b2SJed Brown try: 2511a117d15SMatthew G. Knepley config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name), log = self.log) 252179860b2SJed Brown except RuntimeError: 253179860b2SJed Brown pass 254179860b2SJed Brown 255179860b2SJed Brown 256