def redownloadContentsFile(self, http): """ Downloads a new contents.xml file in case it has changed. Returns true if the file has indeed changed, false if it has not. """ assert self.hasContentsFile if self.appRunner and self.appRunner.verifyContents == self.appRunner.P3DVCNever: # Not allowed to. return False url = self.hostUrlPrefix + 'contents.xml' self.notify.info("Redownloading %s" % (url)) # Get the hash of the original file. assert self.hostDir hv1 = HashVal() if self.contentsSpec.hash: hv1.setFromHex(self.contentsSpec.hash) else: filename = Filename(self.hostDir, 'contents.xml') hv1.hashFile(filename) # Now download it again. self.hasContentsFile = False hv2 = HashVal() if not self.downloadContentsFile(http, redownload = True, hashVal = hv2): return False if hv2 == HashVal(): self.notify.info("%s didn't actually redownload." % (url)) return False elif hv1 != hv2: self.notify.info("%s has changed." % (url)) return True else: self.notify.info("%s has not changed." % (url)) return False
def stringHash(self): h = HashVal() h.hashString(repr(self)) return h.asHex()
def __determineHostDir(self, hostDirBasename, hostUrl): """ Hashes the host URL into a (mostly) unique directory string, which will be the root of the host's install tree. Returns the resulting path, as a Filename. This code is duplicated in C++, in P3DHost::determine_host_dir(). """ if hostDirBasename: # If the contents.xml specified a host_dir parameter, use # it. hostDir = str(self.rootDir) + '/hosts' for component in hostDirBasename.split('/'): if component: if component[0] == '.': # Forbid ".foo" or "..". component = 'x' + component hostDir += '/' hostDir += component return Filename(hostDir) hostDir = 'hosts/' # Look for a server name in the URL. Including this string in the # directory name makes it friendlier for people browsing the # directory. # We could use URLSpec, but we do it by hand instead, to make # it more likely that our hash code will exactly match the # similar logic in P3DHost. p = hostUrl.find('://') hostname = '' if p != -1: start = p + 3 end = hostUrl.find('/', start) # Now start .. end is something like "username@host:port". at = hostUrl.find('@', start) if at != -1 and at < end: start = at + 1 colon = hostUrl.find(':', start) if colon != -1 and colon < end: end = colon # Now start .. end is just the hostname. hostname = hostUrl[start : end] # Now build a hash string of the whole URL. We'll use MD5 to # get a pretty good hash, with a minimum chance of collision. # Even if there is a hash collision, though, it's not the end # of the world; it just means that both hosts will dump their # packages into the same directory, and they'll fight over the # toplevel contents.xml file. Assuming they use different # version numbers (which should be safe since they have the # same hostname), there will be minimal redownloading. hashSize = 16 keepHash = hashSize if hostname: hostDir += hostname + '_' # If we successfully got a hostname, we don't really need the # full hash. We'll keep half of it. keepHash = keepHash // 2 md = HashVal() md.hashString(hostUrl) hostDir += md.asHex()[:keepHash * 2] hostDir = Filename(self.rootDir, hostDir) return hostDir
def __determineHostDir(self, hostDirBasename, hostUrl): """ Hashes the host URL into a (mostly) unique directory string, which will be the root of the host's install tree. Returns the resulting path, as a Filename. This code is duplicated in C++, in P3DHost::determine_host_dir(). """ if hostDirBasename: # If the contents.xml specified a host_dir parameter, use # it. hostDir = str(self.rootDir) + '/hosts' for component in hostDirBasename.split('/'): if component: if component[0] == '.': # Forbid ".foo" or "..". component = 'x' + component hostDir += '/' hostDir += component return Filename(hostDir) hostDir = 'hosts/' # Look for a server name in the URL. Including this string in the # directory name makes it friendlier for people browsing the # directory. # We could use URLSpec, but we do it by hand instead, to make # it more likely that our hash code will exactly match the # similar logic in P3DHost. p = hostUrl.find('://') hostname = '' if p != -1: start = p + 3 end = hostUrl.find('/', start) # Now start .. end is something like "username@host:port". at = hostUrl.find('@', start) if at != -1 and at < end: start = at + 1 colon = hostUrl.find(':', start) if colon != -1 and colon < end: end = colon # Now start .. end is just the hostname. hostname = hostUrl[start:end] # Now build a hash string of the whole URL. We'll use MD5 to # get a pretty good hash, with a minimum chance of collision. # Even if there is a hash collision, though, it's not the end # of the world; it just means that both hosts will dump their # packages into the same directory, and they'll fight over the # toplevel contents.xml file. Assuming they use different # version numbers (which should be safe since they have the # same hostname), there will be minimal redownloading. hashSize = 16 keepHash = hashSize if hostname: hostDir += hostname + '_' # If we successfully got a hostname, we don't really need the # full hash. We'll keep half of it. keepHash = keepHash // 2 md = HashVal() md.hashString(hostUrl) hostDir += md.asHex()[:keepHash * 2] hostDir = Filename(self.rootDir, hostDir) return hostDir
def readHash(self, pathname): """ Reads the hash only from the indicated pathname. """ hv = HashVal() hv.hashFile(pathname) self.hash = hv.asHex()