def put(self, result): if not result.jobHash: return path = os.path.join(self.path, result.jobHash) result = freeze('ResolveResult', result) fobj = util.AtomicFile(path, chmod=0644) fobj.write(xmlrpclib.dumps((result,))) fobj.commit()
def store(cls, root, archive): """ Copy a manifest from inside a chroot to adjacent to a file archive """ f_in = open(os.path.join(root, cls.FILENAME)) with util.AtomicFile(archive + cls.AR_SUFFIX) as f_out: util.copyfileobj(f_in, f_out) f_in.close()
def writeCert(options, rsa, x509): """ Write a new certificate C{x509} and private key C{rsa} as prescribed by the command-line C{options}. """ if options.output: certOut = conary_util.AtomicFile(options.output, chmod=0600) else: certOut = sys.stdout if options.output_key and options.output_key != options.output: keyOut = conary_util.AtomicFile(options.output_key, chmod=0600) else: keyOut = certOut keyOut.write(rsa.as_pem(None)) certOut.write(x509.as_pem()) if hasattr(certOut, 'commit'): certOut.commit() if keyOut is not certOut and hasattr(keyOut, 'commit'): keyOut.commit() return 0
def writeSurveytoStore(self, xml, store, uuid=None, uid=None, gid=None): """ Write the Survey to the store, using the supplied uid and gid """ if not uuid: uuid = self.surveyScanner.uuid self.surveyPath = os.path.join(store, 'survey-%s.xml' % uuid ) if self.surveyPath is None: # Already written return None logger.info("Writing survey as %s" % self.surveyPath) util.mkdirChain(os.path.dirname(self.surveyPath)) f = util.AtomicFile(self.surveyPath, chmod=0600) f.write(xml) f.commit() if uid or gid: os.chown(self.surveyPath, uid, gid) return self.surveyPath
def writeCertificateToStore(self, crt, store, uid=None, gid=None): """ Write the certifcate to the store, using the supplied uid and gid """ certHash = crt.hash x509Pem = crt.x509.as_pem() certPath = self._getPathInCertificateStore(store, certHash, x509Pem) if certPath is None: # Already written return None logger.info("Writing certificate as %s" % certPath) util.mkdirChain(os.path.dirname(certPath)) f = util.AtomicFile(certPath, chmod=0600) f.write(x509Pem) f.commit() if uid or gid: os.chown(certPath, uid, gid) return certPath
def cacheFilePath(self, cachePrefix, url): cachePath = self.getCachePath(cachePrefix, url) util.mkdirChain(os.path.dirname(cachePath)) if url.filePath() in self.cacheMap: # don't check sha1 twice return self.cacheMap[url.filePath()] (troveName, troveVersion, pathId, troveFile, fileId, troveFileVersion, sha1, mode) = self.nameMap[url.filePath()] sha1Cached = None cachedMode = None if os.path.exists(cachePath): sha1Cached = sha1helper.sha1FileBin(cachePath) if sha1Cached != sha1: if sha1Cached: log.info('%s sha1 %s != %s; fetching new...', url.filePath(), sha1helper.sha1ToString(sha1), sha1helper.sha1ToString(sha1Cached)) else: log.info('%s not yet cached, fetching...', url.filePath()) if self.quiet: csCallback = None else: csCallback = ChangesetCallback() f = self.repos.getFileContents([(fileId, troveFileVersion)], callback=csCallback)[0].get() outF = util.AtomicFile(cachePath, chmod=0644) util.copyfileobj(f, outF) outF.commit() fileObj = self.repos.getFileVersion(pathId, fileId, troveFileVersion) fileObj.chmod(cachePath) cachedMode = os.stat(cachePath).st_mode & 0777 if mode != cachedMode: os.chmod(cachePath, mode) self.cacheMap[url.filePath()] = cachePath return cachePath
def addFileToCache(self, cachePrefix, url, infile, contentLength): # cache needs to be hierarchical to avoid collisions, thus we # use cachePrefix so that files with the same name and different # contents in different packages do not collide cachedname = self.getCachePath(cachePrefix, url) util.mkdirChain(os.path.dirname(cachedname)) f = util.AtomicFile(cachedname, chmod=0644) try: BLOCKSIZE = 1024 * 4 if self.quiet: callback = callbacks.FetchCallback() else: callback = FetchCallback() wrapper = callbacks.CallbackRateWrapper(callback, callback.fetch, contentLength) util.copyfileobj(infile, f, bufSize=BLOCKSIZE, rateLimit=self.downloadRateLimit, callback=wrapper.callback) f.commit() infile.close() except: f.close() raise # work around FTP bug (msw had a better way?) if url.scheme == 'ftp': if os.stat(cachedname).st_size == 0: os.unlink(cachedname) self.createNegativeCacheEntry(cachePrefix, url) return None return cachedname
def write(self, root): with util.AtomicFile(os.path.join(root, self.FILENAME)) as fobj: cPickle.dump(self, fobj)
def _setValue(self, value): ConfigManager.logger.log_info("Setting value for %s" % self.path) util.mkdirChain(os.path.dirname(self.path)) f = util.AtomicFile(self.path, chmod=0600) f.write(value) f.commit()