def poll(self): filename = os.path.join(HOME, "timestamp") log.info("Polling") if os.path.exists(filename) and os.path.isfile(filename): with open(filename) as fp: timestamp = fp.read() try: updatedate = datetime.datetime.fromtimestamp(float(timestamp)) except: log.error("timestamp is corrupted!, initializing...") return self._initialize_datetime() if datetime.datetime.now() > updatedate: log.debug("updating timestamp") self.update() nexttimestamp = datetime.datetime.now() + datetime.timedelta( seconds=self.frequency) with open(filename, "wt") as fp: fp.write(nexttimestamp.strftime("%s")) return True else: return False else: log.info("No timestamp found! initializing...") return self._initialize_timestamp()
def update(self): assert (isinstance(self.retries, int)) assert (isinstance(self.fetcher, imgurfetcher)) assert (isinstance(self.target, str) or isinstance(self.target, unicode)) assert (isinstance(self.slack, int)) self.target = str(self.target) for i in range(self.retries): query = self.fetcher.query() if query is not None: break time.sleep(self.slack) if query is None: return None if (self.backup and not os.path.isdir(self.target) and os.path.exists(self.target)): digest = get_digest_for_file(self.target) name, ext = os.path.splitext(self.target) backup_target = "{}-{}{}".format(name, digest, ext) # we will only backup if it's not there yet if not os.path.exists(backup_target): try: shutil.copyfile(self.target, backup_target) except: log.error("couldn't create backup image!") pass try: self.fetcher.fetch(query, self.target) except Exception as e: log.error("Fetcher error, couldn't fetch image!") if self.backup and not os.path.isdir(self.target): try: shutil.copyfile(backup_target, self.target) shutil.rmfile(backup_taget) except: log.error("Couldn't load backup image!") pass else: raise # Run the update command, the environment variables are overwritten # in case the daemon is not in the same namespace (happens with chron) if self.update_hook: os.environ.update(self.env) subprocess.call(shlex.split(self.update_hook))
def update(self): assert(isinstance(self.retries, int)) assert(isinstance(self.fetcher, imgurfetcher)) assert(isinstance(self.target, str) or isinstance(self.target, unicode)) assert(isinstance(self.slack, int)) self.target = str(self.target) for i in range(self.retries): query = self.fetcher.query() if query is not None: break time.sleep(self.slack) if query is None: return None if (self.backup and not os.path.isdir(self.target) and os.path.exists(self.target)): digest = get_digest_for_file(self.target) name, ext = os.path.splitext(self.target) backup_target = "{}-{}{}".format(name, digest, ext) # we will only backup if it's not there yet if not os.path.exists(backup_target): try: shutil.copyfile(self.target, backup_target) except: log.error("couldn't create backup image!") pass try: self.fetcher.fetch(query, self.target) except Exception as e: log.error("Fetcher error, couldn't fetch image!") if self.backup and not os.path.isdir(self.target): try: shutil.copyfile(backup_target, self.target) shutil.rmfile(backup_taget) except: log.error("Couldn't load backup image!") pass else: raise # Run the update command, the environment variables are overwritten # in case the daemon is not in the same namespace (happens with chron) if self.update_hook: os.environ.update(self.env) subprocess.call(shlex.split(self.update_hook))