1*179860b2SJed Brownimport logger 2*179860b2SJed Brown 3*179860b2SJed Brownimport os 4*179860b2SJed Brownimport urllib 5*179860b2SJed Brownimport urlparse 6*179860b2SJed Brownimport config.base 7*179860b2SJed Brown# Fix parsing for nonstandard schemes 8*179860b2SJed Brownurlparse.uses_netloc.extend(['bk', 'ssh', 'svn']) 9*179860b2SJed Brown 10*179860b2SJed Brownclass Retriever(logger.Logger): 11*179860b2SJed Brown def __init__(self, sourceControl, clArgs = None, argDB = None): 12*179860b2SJed Brown logger.Logger.__init__(self, clArgs, argDB) 13*179860b2SJed Brown self.sourceControl = sourceControl 14*179860b2SJed Brown self.stamp = None 15*179860b2SJed Brown return 16*179860b2SJed Brown 17*179860b2SJed Brown def getAuthorizedUrl(self, url): 18*179860b2SJed Brown '''This returns a tuple of the unauthorized and authorized URLs for the given URL, and a flag indicating which was input''' 19*179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 20*179860b2SJed Brown if not location: 21*179860b2SJed Brown url = urlparse.urlunparse(('', '', path, parameters, query, fragment)) 22*179860b2SJed Brown authUrl = None 23*179860b2SJed Brown wasAuth = 0 24*179860b2SJed Brown else: 25*179860b2SJed Brown index = location.find('@') 26*179860b2SJed Brown if index >= 0: 27*179860b2SJed Brown login = location[0:index] 28*179860b2SJed Brown authUrl = url 29*179860b2SJed Brown url = urlparse.urlunparse((scheme, location[index+1:], path, parameters, query, fragment)) 30*179860b2SJed Brown wasAuth = 1 31*179860b2SJed Brown else: 32*179860b2SJed Brown login = location.split('.')[0] 33*179860b2SJed Brown authUrl = urlparse.urlunparse((scheme, login+'@'+location, path, parameters, query, fragment)) 34*179860b2SJed Brown wasAuth = 0 35*179860b2SJed Brown return (url, authUrl, wasAuth) 36*179860b2SJed Brown 37*179860b2SJed Brown def testAuthorizedUrl(self, authUrl): 38*179860b2SJed Brown '''Raise an exception if the URL cannot receive an SSH login without a password''' 39*179860b2SJed Brown if not authUrl: 40*179860b2SJed Brown raise RuntimeError('Url is empty') 41*179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(authUrl) 42*179860b2SJed Brown return self.executeShellCommand('echo "quit" | ssh -oBatchMode=yes '+location) 43*179860b2SJed Brown 44*179860b2SJed Brown def genericRetrieve(self, url, root, name): 45*179860b2SJed Brown '''Fetch the gzipped tarfile indicated by url and expand it into root 46*179860b2SJed Brown - All the logic for removing old versions, updating etc. must move''' 47*179860b2SJed Brown 48*179860b2SJed Brown archive = '_d_'+name 49*179860b2SJed Brown if url.endswith(".bz2") or url.endswith(".tbz"): 50*179860b2SJed Brown archive += '.tar' 51*179860b2SJed Brown archiveZip = archive+'.bz2' 52*179860b2SJed Brown elif url.endswith('.tgz') or url.endswith('.tar.gz'): 53*179860b2SJed Brown archive += '.tar' 54*179860b2SJed Brown archiveZip = archive+'.gz' 55*179860b2SJed Brown elif url.endswith(".zip") or url.endswith('.ZIP'): 56*179860b2SJed Brown archiveZip = archive+'.zip' 57*179860b2SJed Brown else: 58*179860b2SJed Brown raise RuntimeError('Unknown compression type in URL: '+ url) 59*179860b2SJed Brown localFile = os.path.join(root, archiveZip) 60*179860b2SJed Brown 61*179860b2SJed Brown self.logPrint('Downloading '+url+' to '+localFile) 62*179860b2SJed Brown 63*179860b2SJed Brown if os.path.exists(localFile): 64*179860b2SJed Brown os.remove(localFile) 65*179860b2SJed Brown httpfail=-1 66*179860b2SJed Brown ftpfail=-1 67*179860b2SJed Brown try: 68*179860b2SJed Brown urllib.urlretrieve(url, localFile) 69*179860b2SJed Brown httpfail=0 70*179860b2SJed Brown except Exception, e: 71*179860b2SJed Brown httpfail=1 72*179860b2SJed Brown if httpfail and (url.find('http://ftp.mcs.anl.gov') >=0): 73*179860b2SJed Brown furl = url.replace('http://','ftp://') 74*179860b2SJed Brown self.logPrintBox('Warning failed download: '+url+'\nReattempting with: '+furl) 75*179860b2SJed Brown try: 76*179860b2SJed Brown urllib.urlretrieve(furl, localFile) 77*179860b2SJed Brown ftpfail=0 78*179860b2SJed Brown except Exception, e: 79*179860b2SJed Brown self.logPrintBox('Failed download with alternate: '+furl) 80*179860b2SJed Brown ftpfail=1 81*179860b2SJed Brown if ((ftpfail == 1) or ((ftpfail == -1) and httpfail)): 82*179860b2SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 83*179860b2SJed Brown failureMessage = '''\ 84*179860b2SJed BrownUnable to download package %s from: %s 85*179860b2SJed Brown* If URL specified manually - perhaps there is a typo? 86*179860b2SJed Brown* If your network is disconnected - please reconnect and rerun ./configure 87*179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 88*179860b2SJed Brown and use the configure option: 89*179860b2SJed Brown --download-%s=/yourselectedlocation/%s 90*179860b2SJed Brown''' % (name, url, filename, name.lower(), filename) 91*179860b2SJed Brown raise RuntimeError(failureMessage) 92*179860b2SJed Brown self.logPrint('Uncompressing '+localFile) 93*179860b2SJed Brown if not archiveZip.endswith(".zip"): 94*179860b2SJed Brown localFile = os.path.join(root, archive) 95*179860b2SJed Brown # just in case old local .tar file is still hanging around get rid of it 96*179860b2SJed Brown if os.path.exists(localFile): 97*179860b2SJed Brown os.remove(localFile) 98*179860b2SJed Brown try: 99*179860b2SJed Brown if archiveZip.endswith(".bz2"): 100*179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; bunzip2 '+archiveZip, log = self.log) 101*179860b2SJed Brown elif archiveZip.endswith(".zip"): 102*179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; unzip '+archiveZip, log = self.log) 103*179860b2SJed Brown else: 104*179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; gunzip '+archiveZip, log = self.log) 105*179860b2SJed Brown except RuntimeError, e: 106*179860b2SJed Brown filename = os.path.basename(urlparse.urlparse(url)[2]) 107*179860b2SJed Brown if str(e).find("not in gzip format") > -1: 108*179860b2SJed Brown failureMessage = '''\ 109*179860b2SJed BrownUnable to unzip downloaded package %s from: %s 110*179860b2SJed Brown* If you are behind a firewall - please fix your proxy and rerun ./configure 111*179860b2SJed Brown* For example at LANL you may need to set the environmental variable http_proxy (or HTTP_PROXY?) to http://proxyout.lanl.gov 112*179860b2SJed Brown* Alternatively, you can download the above URL manually, to /yourselectedlocation/%s 113*179860b2SJed Brown and use the configure option: 114*179860b2SJed Brown --download-%s=/yourselectedlocation/%s 115*179860b2SJed Brown''' % (name, url, filename, name.lower(), filename) 116*179860b2SJed Brown raise RuntimeError(failureMessage) 117*179860b2SJed Brown else: 118*179860b2SJed Brown raise RuntimeError('Error unzipping '+archiveZip+': '+str(e)) 119*179860b2SJed Brown self.logPrint('Expanding '+localFile) 120*179860b2SJed Brown try: 121*179860b2SJed Brown if not archiveZip.endswith(".zip"): 122*179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; tar -xf '+archive, log = self.log) 123*179860b2SJed Brown except RuntimeError, e: 124*179860b2SJed Brown raise RuntimeError('Error doing tar -xf '+archive+': '+str(e)) 125*179860b2SJed Brown # now find the dirname - and do a chmod 126*179860b2SJed Brown try: 127*179860b2SJed Brown if archiveZip.endswith(".zip"): 128*179860b2SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; zipinfo -1 '+archive+' | head -n 1', log = self.log) 129*179860b2SJed Brown else: 130*179860b2SJed Brown output = config.base.Configure.executeShellCommand('cd '+root+'; tar -tf '+archive+' | head -n 1', log = self.log) 131*179860b2SJed Brown dirname = os.path.normpath(output[0].strip()) 132*179860b2SJed Brown # some tarfiles list packagename/ but some list packagename/filename in the first entry - so handle both cases 133*179860b2SJed Brown apath,bpath=os.path.split(dirname) 134*179860b2SJed Brown if (apath != ''): dirname = apath 135*179860b2SJed Brown config.base.Configure.executeShellCommand('cd '+root+'; chmod -R a+r '+dirname+';find '+dirname + ' -type d -name "*" -exec chmod a+rx {} \;', log = self.log) 136*179860b2SJed Brown except RuntimeError, e: 137*179860b2SJed Brown raise RuntimeError('Error changing permissions for '+archive+': '+str(e)) 138*179860b2SJed Brown os.unlink(localFile) 139*179860b2SJed Brown return 140*179860b2SJed Brown 141*179860b2SJed Brown def ftpRetrieve(self, url, root, name,force): 142*179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via ftp', 3, 'install') 143*179860b2SJed Brown return self.genericRetrieve(url, root, name) 144*179860b2SJed Brown 145*179860b2SJed Brown def httpRetrieve(self, url, root, name,force): 146*179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via http', 3, 'install') 147*179860b2SJed Brown return self.genericRetrieve(url, root, name) 148*179860b2SJed Brown 149*179860b2SJed Brown def fileRetrieve(self, url, root, name,force): 150*179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via cp', 3, 'install') 151*179860b2SJed Brown return self.genericRetrieve(url, root, name) 152*179860b2SJed Brown 153*179860b2SJed Brown def svnRetrieve(self, url, root, name,force): 154*179860b2SJed Brown if not hasattr(self.sourceControl, 'svn'): 155*179860b2SJed Brown raise RuntimeError('Cannot retrieve a SVN repository since svn was not found') 156*179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via svn', 3, 'install') 157*179860b2SJed Brown try: 158*179860b2SJed Brown config.base.Configure.executeShellCommand(self.sourceControl.svn+' checkout http'+url[3:]+' '+os.path.join(root, name)) 159*179860b2SJed Brown except RuntimeError: 160*179860b2SJed Brown pass 161*179860b2SJed Brown 162*179860b2SJed Brown 163*179860b2SJed Brown # This is the old code for updating a BK repository 164*179860b2SJed Brown # Stamp used to be stored with a url 165*179860b2SJed Brown def bkUpdate(self): 166*179860b2SJed Brown if not self.stamp is None and url in self.stamp: 167*179860b2SJed Brown if not self.stamp[url] == self.bkHeadRevision(root): 168*179860b2SJed Brown raise RuntimeError('Existing stamp for '+url+' does not match revision of repository in '+root) 169*179860b2SJed Brown (url, authUrl, wasAuth) = self.getAuthorizedUrl(self.getBKParentURL(root)) 170*179860b2SJed Brown if not wasAuth: 171*179860b2SJed Brown self.debugPrint('Changing parent from '+url+' --> '+authUrl, 1, 'install') 172*179860b2SJed Brown output = self.executeShellCommand('cd '+root+'; bk parent '+authUrl) 173*179860b2SJed Brown try: 174*179860b2SJed Brown self.testAuthorizedUrl(authUrl) 175*179860b2SJed Brown output = self.executeShellCommand('cd '+root+'; bk pull') 176*179860b2SJed Brown except RuntimeError, e: 177*179860b2SJed Brown (url, authUrl, wasAuth) = self.getAuthorizedUrl(self.getBKParentURL(root)) 178*179860b2SJed Brown if wasAuth: 179*179860b2SJed Brown self.debugPrint('Changing parent from '+authUrl+' --> '+url, 1, 'install') 180*179860b2SJed Brown output = self.executeShellCommand('cd '+root+'; bk parent '+url) 181*179860b2SJed Brown output = self.executeShellCommand('cd '+root+'; bk pull') 182*179860b2SJed Brown else: 183*179860b2SJed Brown raise e 184*179860b2SJed Brown return 185*179860b2SJed Brown 186*179860b2SJed Brown def bkClone(self, url, root, name): 187*179860b2SJed Brown '''Clone a Bitkeeper repository located at url into root/name 188*179860b2SJed Brown - If self.stamp exists, clone only up to that revision''' 189*179860b2SJed Brown failureMessage = '''\ 190*179860b2SJed BrownUnable to bk clone %s 191*179860b2SJed BrownYou may be off the network. Connect to the internet and run ./configure again 192*179860b2SJed Brownor from the directory %s try: 193*179860b2SJed Brown bk clone %s 194*179860b2SJed Brownand if that succeeds then rerun ./configure 195*179860b2SJed Brown''' % (name, root, url, name) 196*179860b2SJed Brown try: 197*179860b2SJed Brown if not self.stamp is None and url in self.stamp: 198*179860b2SJed Brown (output, error, status) = self.executeShellCommand('bk clone -r'+self.stamp[url]+' '+url+' '+os.path.join(root, name)) 199*179860b2SJed Brown else: 200*179860b2SJed Brown (output, error, status) = self.executeShellCommand('bk clone '+url+' '+os.path.join(root, name)) 201*179860b2SJed Brown except RuntimeError, e: 202*179860b2SJed Brown status = 1 203*179860b2SJed Brown output = str(e) 204*179860b2SJed Brown error = '' 205*179860b2SJed Brown if status: 206*179860b2SJed Brown if output.find('ommand not found') >= 0: 207*179860b2SJed Brown failureMessage = 'Unable to locate bk (Bitkeeper) to download repository; make sure bk is in your path' 208*179860b2SJed Brown elif output.find('Cannot resolve host') >= 0: 209*179860b2SJed Brown failureMessage = output+'\n'+error+'\n'+failureMessage 210*179860b2SJed Brown else: 211*179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 212*179860b2SJed Brown try: 213*179860b2SJed Brown self.bkClone(urlparse.urlunparse(('http', location, path, parameters, query, fragment)), root, name) 214*179860b2SJed Brown except RuntimeError, e: 215*179860b2SJed Brown failureMessage += '\n'+str(e) 216*179860b2SJed Brown else: 217*179860b2SJed Brown return 218*179860b2SJed Brown raise RuntimeError(failureMessage) 219*179860b2SJed Brown return 220*179860b2SJed Brown 221*179860b2SJed Brown def bkRetrieve(self, url, root, name): 222*179860b2SJed Brown if not hasattr(self.sourceControl, 'bk'): 223*179860b2SJed Brown raise RuntimeError('Cannot retrieve a BitKeeper repository since BK was not found') 224*179860b2SJed Brown self.logPrint('Retrieving '+url+' --> '+os.path.join(root, name)+' via bk', 3, 'install') 225*179860b2SJed Brown (url, authUrl, wasAuth) = self.getAuthorizedUrl(url) 226*179860b2SJed Brown try: 227*179860b2SJed Brown self.testAuthorizedUrl(authUrl) 228*179860b2SJed Brown self.bkClone(authUrl, root, name) 229*179860b2SJed Brown except RuntimeError: 230*179860b2SJed Brown pass 231*179860b2SJed Brown else: 232*179860b2SJed Brown return 233*179860b2SJed Brown return self.bkClone(url, root, name) 234*179860b2SJed Brown 235*179860b2SJed Brown def retrieve(self, url, root = None, canExist = 0, force = 0): 236*179860b2SJed Brown '''Retrieve the project corresponding to url 237*179860b2SJed Brown - If root is None, the local root directory is automatically determined. If the project 238*179860b2SJed Brown was already installed, this root is used. Otherwise a guess is made based upon the url. 239*179860b2SJed Brown - If canExist is True and the root exists, an update is done instead of a full download. 240*179860b2SJed Brown The canExist is automatically true if the project has been installed. The retrievalCanExist 241*179860b2SJed Brown flag can also be used to set this. 242*179860b2SJed Brown - If force is True, a full download is mandated. 243*179860b2SJed Brown Providing the root is an easy way to make a copy, for instance when making tarballs. 244*179860b2SJed Brown ''' 245*179860b2SJed Brown if root is None: 246*179860b2SJed Brown root = self.getInstallRoot(url) 247*179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 248*179860b2SJed Brown if hasattr(self,scheme+'Retrieve'): 249*179860b2SJed Brown getattr(self, scheme+'Retrieve')(url, os.path.abspath(root), canExist, force) 250*179860b2SJed Brown else: 251*179860b2SJed Brown raise RuntimeError('Invalid transport for retrieval: '+scheme) 252*179860b2SJed Brown return 253*179860b2SJed Brown 254*179860b2SJed Brown ############################################## 255*179860b2SJed Brown # This is the old shit 256*179860b2SJed Brown ############################################## 257*179860b2SJed Brown def removeRoot(self, root, canExist, force = 0): 258*179860b2SJed Brown '''Returns 1 if removes root''' 259*179860b2SJed Brown if os.path.exists(root): 260*179860b2SJed Brown if canExist: 261*179860b2SJed Brown if force: 262*179860b2SJed Brown import shutil 263*179860b2SJed Brown shutil.rmtree(root) 264*179860b2SJed Brown return 1 265*179860b2SJed Brown else: 266*179860b2SJed Brown return 0 267*179860b2SJed Brown else: 268*179860b2SJed Brown raise RuntimeError('Root directory '+root+' already exists') 269*179860b2SJed Brown return 1 270*179860b2SJed Brown 271*179860b2SJed Brown def getBKParentURL(self, root): 272*179860b2SJed Brown '''Return the parent URL for the BK repository at "root"''' 273*179860b2SJed Brown return self.executeShellCommand('cd '+root+'; bk parent')[21:] 274*179860b2SJed Brown 275*179860b2SJed Brown def bkHeadRevision(self, root): 276*179860b2SJed Brown '''Return the last change set revision in the repository''' 277*179860b2SJed Brown return self.executeShellCommand('cd '+root+'; bk changes -and:REV: | head -1') 278*179860b2SJed Brown 279*179860b2SJed Brown def bkfileRetrieve(self, url, root, canExist = 0, force = 0): 280*179860b2SJed Brown self.debugPrint('Retrieving '+url+' --> '+root+' via local bk', 3, 'install') 281*179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 282*179860b2SJed Brown return self.bkRetrieve(urlparse.urlunparse(('file', location, path, parameters, query, fragment)), root, canExist, force) 283*179860b2SJed Brown 284*179860b2SJed Brown def sshRetrieve(self, url, root, canExist = 0, force = 0): 285*179860b2SJed Brown command = 'hg clone '+url+' '+os.path.join(root,os.path.basename(url)) 286*179860b2SJed Brown output = config.base.Configure.executeShellCommand(command) 287*179860b2SJed Brown return root 288*179860b2SJed Brown 289*179860b2SJed Brown def oldRetrieve(self, url, root = None, canExist = 0, force = 0): 290*179860b2SJed Brown '''Retrieve the project corresponding to url 291*179860b2SJed Brown - If root is None, the local root directory is automatically determined. If the project 292*179860b2SJed Brown was already installed, this root is used. Otherwise a guess is made based upon the url. 293*179860b2SJed Brown - If canExist is True and the root exists, an update is done instead of a full download. 294*179860b2SJed Brown The canExist is automatically true if the project has been installed. The retrievalCanExist 295*179860b2SJed Brown flag can also be used to set this. 296*179860b2SJed Brown - If force is True, a full download is mandated. 297*179860b2SJed Brown Providing the root is an easy way to make a copy, for instance when making tarballs. 298*179860b2SJed Brown ''' 299*179860b2SJed Brown origUrl = url 300*179860b2SJed Brown url = self.getMappedUrl(origUrl) 301*179860b2SJed Brown project = self.getInstalledProject(url) 302*179860b2SJed Brown if not project is None and root is None: 303*179860b2SJed Brown root = project.getRoot() 304*179860b2SJed Brown canExist = 1 305*179860b2SJed Brown if root is None: 306*179860b2SJed Brown root = self.getInstallRoot(origUrl) 307*179860b2SJed Brown (scheme, location, path, parameters, query, fragment) = urlparse.urlparse(url) 308*179860b2SJed Brown try: 309*179860b2SJed Brown if self.argDB['retrievalCanExist']: 310*179860b2SJed Brown canExist = 1 311*179860b2SJed Brown return getattr(self, scheme+'Retrieve')(url, os.path.abspath(root), canExist, force) 312*179860b2SJed Brown except AttributeError: 313*179860b2SJed Brown raise RuntimeError('Invalid transport for retrieval: '+scheme) 314