def xmlizeFiles(self, directory, fileType): # stat table contains triples of (time, file, stat) statTable = [] fields = self.request().fields() for f in findSysFiles(directory): s = os.stat(os.path.join(basicPathName, directory, f)) md5sum = self.readMd5Sum(directory, f) statTable.append((s[stat.ST_MTIME], f, s, md5sum)) # sort first by reverse time, then by name statTable.sort(lambda a,b: cmp(b[0], a[0]) or FormUtils.alphanumericCompare(a[1], b[1])) result = self.doc.createElement(directory) for sysMtime, eachFile, fileStat, md5Sum in statTable: sysBytes = fileStat[stat.ST_SIZE] sysTimeStr = time.strftime('%Y/%m/%d %H:%M', time.localtime(sysMtime)) fileEl = self.doc.createElement('file') fileEl.setAttribute('name', eachFile) fileEl.setAttribute( 'href', '/mgmt/download?f=%s&type=%s' % (eachFile, fileType)) # bytes are for column sorting fileEl.setAttribute('bytes', '%d' % sysBytes) fileEl.setAttribute('sizeStr', GraphUtils.scale( sysBytes, GraphUtils.SCALER_HUNDREDS_OF_BYTES)) # timestamp is for column sorting fileEl.setAttribute('timestamp', '%d' % sysMtime) fileEl.setAttribute('timestring', sysTimeStr) fileEl.setAttribute('md5sum', '%s' % md5Sum) result.appendChild(fileEl) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def logFiles(self): fields = self.request().fields() baseName = fields.get('logPrefix', '') + 'messages' msgFileSeq = logDownload.findMessageFiles(baseName) result = self.doc.createElement('logfiles') if baseName in msgFileSeq: plainSize = os.stat('/var/log/' + baseName)[stat.ST_SIZE] plainSize = '(%s)' % \ GraphUtils.scale(plainSize, GraphUtils.SCALER_TENTHS_OF_BYTES, precision=1) fileEl = self.doc.createElement('file') fileEl.setAttribute('logName', 'Current Log') fileEl.setAttribute('uncompressedTitle', 'Download the current log as plain text') fileEl.setAttribute( 'uncompressedHref', '/mgmt/download?f=%s&type=plainlog' % baseName) fileEl.setAttribute('compressedTitle', '') fileEl.setAttribute('compressedHref', '') fileEl.setAttribute('plainSize', plainSize) result.appendChild(fileEl) msgFileSeq.remove(baseName) downloads = [] for num in [mf.split('.')[1] for mf in msgFileSeq]: if num not in downloads: downloads.append(num) downloads.sort(FormUtils.alphanumericCompare) downloads = [(num, '%s.%s' % (baseName, num), '%s.%s.gz' % (baseName, num)) for num in downloads] downloads = [(num, plain in msgFileSeq and plain or compressed, compressed in msgFileSeq and compressed or '') for num, plain, compressed in downloads] for num, plain, compressed in downloads: fileEl = self.doc.createElement('file') fileEl.setAttribute('logName', 'Archived log # ' + num) fileEl.setAttribute('uncompressedTitle', 'Download archived log # %s as plain text' % num) plainSize = '' if plain.endswith('.gz'): fileEl.setAttribute('uncompressedHref', '/mgmt/download?f=%s&type=gunzippedlog' % plain) else: fileEl.setAttribute('uncompressedHref', '/mgmt/download?f=%s&type=plainlog' % plain) plainSize = os.stat('/var/log/' + plain)[stat.ST_SIZE] plainSize = '(%s)' % \ GraphUtils.scale(plainSize, GraphUtils.SCALER_TENTHS_OF_BYTES, precision=1) compressedSize = '' if compressed: fileEl.setAttribute('compressedTitle', 'Download archived log # %s in gzip format' % num) fileEl.setAttribute('compressedHref', '/mgmt/download?f=%s&type=gzippedlog' % compressed) compressedSize = os.stat('/var/log/' + compressed)[stat.ST_SIZE] compressedSize = '(%s)' % \ GraphUtils.scale(compressedSize, GraphUtils.SCALER_TENTHS_OF_BYTES, precision=1) else: fileEl.setAttribute('compressedTitle', '') fileEl.setAttribute('compressedHref', '') fileEl.setAttribute('plainSize', plainSize) fileEl.setAttribute('compressedSize', compressedSize) result.appendChild(fileEl) self.doc.documentElement.appendChild(result) self.writeXmlDoc()