def testChecksumFile(self): filenames = [] filenames.append(self._foo_tempfile()) filenames.append(self._foo_tempfile('.gz')) filenames.append(self._foo_tempfile('.bz2')) # checksums of 'foo' foo_size = 3 checksums_ref = {'md5' : 'acbd18db4cc2f85cedef654fccc4a4d8', 'sha1': '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33',} for filename in filenames: try: size, sums = Compressor.checksum_file(filename, ['sha1']) self.assertEqual(size, foo_size) self.assertEqual(sums, {'sha1' : '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'}) size, sums = Compressor.checksum_file(filename, ['md5', 'sha1']) self.assertEqual(size, foo_size) for sum, val in sums.items(): self.assertEqual(checksums_ref[sum], val) self.failUnlessRaises(Exception, Compressor.checksum_file, filename, ['bar']) finally: os.unlink(filename)
def testGetCompressionFormat(self): for suffix in Compressor.compressionFormats: fname = "dummy.%s" % suffix self.assertEqual(Compressor.getCompressionFormat(fname), suffix) fname = "dummy.%s" % suffix.upper() self.assertEqual(Compressor.getCompressionFormat(fname), suffix) self.assertEqual(Compressor.getCompressionFormat("dummy"), "") self.assertEqual(Compressor.getCompressionFormat(""), "")
def _inflateImage(self, imageFilename): extension = self._extractCompressionExtension(imageFilename) inflatedFilename = imageFilename if extension: self._printDetail('Inflating image %s' % imageFilename) Compressor.inflate(imageFilename) inflatedFilename = imageFilename[:-len(extension)] return inflatedFilename
def _foo_tempfile(self, suffix=''): fd, filename = tempfile.mkstemp(suffix=suffix) os.close(fd) f = Compressor.openCompressedFile(filename, options='wb') try: f.write('foo') finally: f.close() return filename
def _compressFile(self, filename, fmt): if fmt.lower() == 'none': return filename if Compressor.getCompressionFormat(filename) != '': Util.printWarning('skipping compression; file appears to already be compressed') return filename compressionCmd = Compressor._getCompressionCommandByFormat(fmt) compressedFilename = '%s.%s' % (filename, fmt) if os.path.isfile(compressedFilename): Util.printWarning('Compressed file %s already exists, skipping' % compressedFilename) return compressedFilename if not os.path.exists(filename): Util.printError('Missing file: ' + filename, exit=True) ret = self._execute([compressionCmd, filename]) if ret != 0: Util.printError('Error compressing file: %s' % compressedFilename, exit=True) return compressedFilename
def checkOptions(self): if len(self.args) != 1: self.parser.error('Please specify an image') self.image = self.args[0] if not os.path.isfile(self.image): self.parser.error('Image does not exist: ' + self.image) if not self.options.locations: self.options.__dict__.update({'locations': []}) Util.printWarning("Image physical location (URL) was not provided. " "You'll have to set it manually in the resulting manifest.") else: self.options.__dict__.update({'locations': [self.options.__dict__['locations']]}) if not self.options.compression: # Guess compression from file name if not given explicitly. compression = Compressor.getCompressionFormat(self.image) self.options.__dict__.update({'compression': compression}) if self.options.disksbus not in VmManager.DISKS_BUS_AVAILABLE: self.parser.error("Unknown disks bus type %s. Available types: %s" % (self.options.disksbus, ', '.join(VmManager.DISKS_BUS_AVAILABLE)))
def checksumImage(filename, checksums=ManifestInfo.MANDATORY_CHECKSUMS): """Return dictionary of checksums.""" return Compressor.checksum_file(filename, checksums)
def _getDownloadedImageChecksum(self, hash_fun): size_b, sums = Compressor.checksum_file( self.downloadedLocalImageLocation, [hash_fun]) return size_b, sums[self._CHECKSUM]