| 282 | |
| 283 | |
| 284 | class QueueFile: |
| 285 | |
| 286 | fileName = '' |
| 287 | configCache = "/var/cache/cloud" |
| 288 | keep = True |
| 289 | data = {} |
| 290 | |
| 291 | def load(self, data): |
| 292 | if data is not None: |
| 293 | self.data = data |
| 294 | self.type = self.data["type"] |
| 295 | updateDataBag(self) |
| 296 | return |
| 297 | filename = '{cache_location}/{json_file}'.format(cache_location=self.configCache, json_file=self.fileName) |
| 298 | try: |
| 299 | handle = open(filename) |
| 300 | except IOError as exception: |
| 301 | error_message = ("Exception occurred with the following exception error '{error}'. Could not open '{filename}'. " |
| 302 | "It seems that the file has already been moved.".format(error=exception, filename=filename)) |
| 303 | logging.error(error_message) |
| 304 | else: |
| 305 | logging.info("Continuing with the processing of file '{filename}'".format(filename=filename)) |
| 306 | |
| 307 | self.data = json.load(handle) |
| 308 | self.type = self.data["type"] |
| 309 | handle.close() |
| 310 | if self.keep: |
| 311 | self.__moveFile(filename, self.configCache + "/processed") |
| 312 | else: |
| 313 | logging.debug("Processed file deleted: %s and not kept in /processed", filename) |
| 314 | os.remove(filename) |
| 315 | updateDataBag(self) |
| 316 | |
| 317 | def setFile(self, name): |
| 318 | self.fileName = name |
| 319 | |
| 320 | def getType(self): |
| 321 | return self.type |
| 322 | |
| 323 | def getData(self): |
| 324 | return self.data |
| 325 | |
| 326 | def setPath(self, path): |
| 327 | self.configCache = path |
| 328 | |
| 329 | def __moveFile(self, origPath, path): |
| 330 | if not os.path.exists(path): |
| 331 | os.makedirs(path) |
| 332 | originalName = os.path.basename(origPath) |
| 333 | if originalName.count(".") == 1: |
| 334 | originalName += "." + str(uuid.uuid4()) |
| 335 | zipped_file_name = path + "/" + originalName + ".gz" |
| 336 | with open(origPath, 'rb') as f_in, gzip.open(zipped_file_name, 'wb') as f_out: |
| 337 | shutil.copyfileobj(f_in, f_out) |
| 338 | os.remove(origPath) |
| 339 | |
| 340 | logging.debug("Processed file written to %s", zipped_file_name) |
no outgoing calls
no test coverage detected