def set_comment(self, name, value, config_filter=Filter.ALL): ''' Adds a custom Kano comment key to the config file in the form: ### my_comment_name: value ''' lines = read_file_contents_as_lines(self.path) if not lines: return logger.info("writing comment to {} {} {}".format( self.path, name, value)) comment_str_full = '### {}: {}'.format(name, value) comment_str_name = '### {}'.format(name) with open_locked(self.path, 'w') as boot_config_file: boot_config_file.write(comment_str_full + '\n') for line in lines: if comment_str_name in line: continue boot_config_file.write(line + '\n') # make sure changes go to disk boot_config_file.flush() os.fsync(boot_config_file.fileno())
def save(self): data = {'stage': self._stage, 'username': self._username} with open_locked(self._status_file, 'w', timeout=1.0) as status_file: json.dump(data, status_file) status_file.flush() os.fsync(status_file.fileno())
def set_comment(self, name, value, config_filter=Filter.ALL): ''' Adds a custom Kano comment key to the config file in the form: ### my_comment_name: value ''' lines = read_file_contents_as_lines(self.path) if not lines: return logger.info("writing comment to {} {} {}".format(self.path, name, value)) comment_str_full = '### {}: {}'.format(name, value) comment_str_name = '### {}'.format(name) with open_locked(self.path, 'w') as boot_config_file: boot_config_file.write(comment_str_full + '\n') for line in lines: if comment_str_name in line: continue boot_config_file.write(line + '\n') # make sure changes go to disk boot_config_file.flush() os.fsync(boot_config_file.fileno())
def ensure_exists(self): if not self.exists(): f = open_locked(self.path, 'w') print >> f, "#" # otherwise set_value thinks the file should not be written to # make sure changes go to disk f.flush() os.fsync(f.fileno()) f.close() # make file, even if empty
def ensure_exists(self): if not self.exists(): f = open_locked(self.path, 'w') print >>f, "#" # otherwise set_value thinks the file should not be written to # make sure changes go to disk f.flush() os.fsync(f.fileno()) f.close() # make file, even if empty
def load(self): with open_locked(self._status_file, 'r', timeout=1.0) as status_file: try: data = json.load(status_file) self._stage = data['stage'] self._username = data['username'] except: # Initialise the file again if it is corrupted logger.warn("The status file was corrupted.") self.save() return
def is_running(): try: with open_locked(PID_FILE, 'a+', nonblock=True) as pid_file: pd = pidData(pid_file) old_pid = pd.read() if old_pid and pid_exists(old_pid): return True pd.write() except IOError: # file was locked by another process return True return False
def set_value(self, name, value=None, config_filter=Filter.ALL): # if the value argument is None, the option will be commented out lines = read_file_contents_as_lines(self.path) if not lines: # this is true if the file is empty, not sure that was intended. return logger.info('writing value to {} {} {}'.format(self.path, name, value)) config = BootConfigParser(lines) config.set(name, value, config_filter=config_filter) with open_locked(self.path, "w") as boot_config_file: boot_config_file.write(config.dump()) # flush changes to disk boot_config_file.flush() os.fsync(boot_config_file.fileno())
def _remove_noobs_defaults(self): """ Remove the config entries added by Noobs, by removing all the lines after and including noobs' sentinel """ lines = read_file_contents_as_lines(self.path) with open_locked(self.path, 'w') as boot_config_file: for line in lines: if line == noobs_line: break boot_config_file.write(line + "\n") # flush changes to disk boot_config_file.flush() os.fsync(boot_config_file.fileno())
def raise_state_to_locked(self): if self.state == 0: self.state = 1 self.lock = open_locked(self.lockpath, 'w', timeout=lock_timeout)