def check_corrupt(self): # Quick check for corruption in config file. # Check that is has at least some expected data if not os.path.exists(self.path): return True try: lines = read_file_contents_as_lines(self.path) except: return True must_contain = set(['dtparam']) found = set() for l in lines: for m in must_contain: if m in l: found.add(m) if must_contain == found: return False logger.warn( 'Parameters {} not found in config.txt, assuming corrupt' .format(must_contain) ) return True
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 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 get_value(self, name, config_filter=Filter.ALL, fallback=True): lines = read_file_contents_as_lines(self.path) if not lines: return 0 config = BootConfigParser(lines) return config.get(name, config_filter=config_filter, fallback=fallback)
def check_corrupt(self): # Quick check for corruption in config file. # Check that is has at least some expected data if not os.path.exists(self.path): return True try: lines = read_file_contents_as_lines(self.path) except: return True must_contain = set(['dtparam']) found = set() for l in lines: for m in must_contain: if m in l: found.add(m) if must_contain == found: return False logger.warn( 'Parameters {} not found in config.txt, assuming corrupt' .format(must_contain) ) return True
def get_comment(self, name, value): lines = read_file_contents_as_lines(self.path) if not lines: return False comment_str_full = '### {}: {}'.format(name, value) return comment_str_full in lines
def has_comment(self, name): lines = read_file_contents_as_lines(self.path) if not lines: return False comment_start = '### {}:'.format(name) for l in lines: if l.startswith(comment_start): return True return False
def get_comment(self, name, value): ''' Query a custom Kano comment key from the config file in the form: ### my_comment_name: value ''' lines = read_file_contents_as_lines(self.path) if not lines: return False comment_str_full = '### {}: {}'.format(name, value) return comment_str_full in lines
def has_comment(self, name): lines = read_file_contents_as_lines(self.path) if not lines: return False comment_start = '### {}:'.format(name) for l in lines: if l.startswith(comment_start): return True return False
def get_comment(self, name, value): ''' Query a custom Kano comment key from the config file in the form: ### my_comment_name: value ''' lines = read_file_contents_as_lines(self.path) if not lines: return False comment_str_full = '### {}: {}'.format(name, value) return comment_str_full in lines
def get_cpu_id(): ''' Returns the RaspberryPI Serial number from /proc/cpuinfo ''' lines = read_file_contents_as_lines(CPUINFO_FILE) if not lines: return for l in lines: parts = [p.strip() for p in l.split(':')] if parts[0] == 'Serial': return parts[1].upper()
def get_cpu_id(): ''' Returns the RaspberryPI Serial number from /proc/cpuinfo ''' lines = read_file_contents_as_lines(CPUINFO_FILE) if not lines: return for l in lines: parts = [p.strip() for p in l.split(':')] if parts[0] == 'Serial': return parts[1].upper()
def get_value(self, name, config_filter=Filter.ALL, fallback=True, ignore_comments=False): lines = read_file_contents_as_lines(self.path) if not lines: return 0 config = BootConfigParser(lines) return config.get( name, config_filter=config_filter, fallback=fallback, ignore_comments=ignore_comments )
def check_corrupt(self): # Quick check for corrpution in config file. # Check that is has at least some expected data try: lines = read_file_contents_as_lines(self.path) except: return True must_contain = set(['overscan_bottom', 'over_voltage']) found = set() for l in lines: for m in must_contain: if m in l: found.add(m) return must_contain != found
def check_corrupt(self): # Quick check for corrpution in config file. # Check that is has at least some expected data try: lines = read_file_contents_as_lines(self.path) except: return True must_contain = set(['overscan_bottom', 'over_voltage']) found = set() for l in lines: for m in must_contain: if m in l: found.add(m) return must_contain != found
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 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 _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 get_board_revision(use_cached=True): """Get the Raspberry Pi board revision. Args: use_cached (bool): Read the revision from a cached value or read it from the ``/proc/cpuinfo`` file directly. Returns: str: Hexadecimal value for the Raspberry Pi board revision; emptry string if the value could not be read. """ global _g_revision if use_cached and _g_revision: return _g_revision for entry in reversed(read_file_contents_as_lines(CPUINFO_FILE)): if entry.startswith('Revision'): _g_revision = entry.split(':')[1].strip() return _g_revision return ''
def get_board_revision(use_cached=True): """Get the Raspberry Pi board revision. Args: use_cached (bool): Read the revision from a cached value or read it from the ``/proc/cpuinfo`` file directly. Returns: str: Hexadecimal value for the Raspberry Pi board revision; emptry string if the value could not be read. """ global _g_revision if use_cached and _g_revision: return _g_revision for entry in reversed(read_file_contents_as_lines(CPUINFO_FILE)): if entry.startswith('Revision'): _g_revision = entry.split(':')[1].strip() return _g_revision return ''
def set_hostname(new_hostname): if os.environ['LOGNAME'] != 'root': logger.error("Error: Settings must be executed with root privileges") # Check username chars new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower() if new_hostname == '': logger.error("no letters left in username after removing illegal ones") return if new_hostname == 'kano': logger.info( " not tryng to set hostname as it is the same as the default") return # check for missing hosts file if not os.path.exists(hosts_file): create_empty_hosts() # check if already done curr_hosts = read_file_contents_as_lines(hosts_file) if hosts_mod_comment in curr_hosts: logger.warn("/etc/hosts already modified, not changing") return # actually edit the hosts file edit_hosts_file(hosts_file, new_hostname) # edit the backup file. if os.path.exists(hosts_file_backup): edit_hosts_file(hosts_file_backup, new_hostname) try: write_file_contents('/etc/hostname', new_hostname + '\n') except: logger.error("exception while changing change /etc/hostname")
def _get_kano_os_version_stamp(): """Read the version of the OS when it was built""" return read_file_contents_as_lines(KANUX_STAMP_PATH)[-1].strip().split()[-1]
def _noobs_defaults_present(self): lines = read_file_contents_as_lines(self.path) return noobs_line in lines
def read_listed_sites(): return (read_file_contents_as_lines(blacklist_file), read_file_contents_as_lines(whitelist_file))
def _noobs_defaults_present(self): lines = read_file_contents_as_lines(self.path) return noobs_line in lines
def _get_config_txt(): """Read the RPi config.txt file""" return read_file_contents_as_lines(CONFIG_TXT_PATH)