def set_unmanaged_by_network_manager(cls, interface): Logger.debug( "Adding interface \"%s-%s\" as an unmanaged interface to network manager", interface, cls.get_mac(interface)) with open(constants.PATH_CONF_NETWORK_MANAGER, "r") as conf_read: conf = conf_read.read().splitlines() added = False entry = cls.get_device_unmanaged_entry(interface) # Add Entry for line in range(0, len(conf)): # Add keyfile plugin if it's not enabled if conf[line].startswith( "plugins=") and "keyfile" not in conf[line]: conf[line] += ",keyfile" # Add unmanaged device if conf[line].startswith( "unmanaged-devices=") and entry not in conf[line]: conf[line] += ";" + entry added = True # Add the initial unmanaged entry if it was not present if not added: conf.append("[keyfile]") conf.append("unmanaged-devices=" + entry) # Write with open(constants.PATH_CONF_NETWORK_MANAGER, "w") as conf_write: for line in conf: conf_write.write(line + "\n") # Deprecating init.d service calls and using Systemd ProcessUtil.call(["systemctl", "stop", "NetworkManager"]) ProcessUtil.call(["systemctl", "restart", "wpa_supplicant"]) ProcessUtil.call(["systemctl", "start", "NetworkManager"])
def is_interface_wiiu_compatible(cls, interface): if not OsUtil.is_linux(): Logger.extra("Ignoring interface compatibility check for %s", interface) return False frequency_info = ProcessUtil.get_output( ["iwlist", interface, "frequency"]) return "5." in frequency_info
def get_output(cls, command, silent=False): """ Wraps a subprocess.check_output call. Checks for process errors and command not found errors. :param silent: If silent is true the command will not be logged :param command: array of strings - same as check_output :return: string of output, error, or None if the command is not found """ try: if not silent: Logger.extra("Attempting to execute command %s", command) output = subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True) except OSError as e: output = "" if e.errno == errno.ENOENT: Logger.warn("\"%s\" may not be installed", command[0]) else: Logger.exception(e) cls.log_failed_command(command) except subprocess.CalledProcessError as e: output = e.output cls.log_failed_command(command, output.strip()) Logger.verbose("Command \"%s\" output %s", command, output) return output
def is_managed_by_network_manager(cls, interface): if not os.path.exists(constants.PATH_CONF_NETWORK_MANAGER): Logger.debug("Network manager config not found.") return False conf = open(constants.PATH_CONF_NETWORK_MANAGER) conf_data = conf.readlines() conf.close() managed = False for line in conf_data: if line.startswith("unmanaged-devices=") and "mac:" + cls.get_mac( interface) not in line: managed = True # Ensure configs with duplicates raise an unmanaged prompt if "unmanaged-devices=" not in " ".join(conf_data): managed = True Logger.debug("Interface \"%s\" managed by network manager: %s", interface, managed) return managed
def log_failed_command(cls, command, output=None): Logger.extra("Failed to execute command \"%s\" and got output \"%s\"", command, output)
def __init__(self, name=None): Logger.__init__(self, name) LoggerCli.logger, LoggerCli.console_handler, LoggerCli.file_handler = self.create_logger( name)
def __init__(self, in_path): pre = "resources/" Logger.debug("Loading resource \"%s\"", join(pre, in_path)) current_dir = os.path.dirname(__file__).split(os.sep) # Check local files first file_path = "/" if len(current_dir) >= 3: for path in range(0, len(current_dir) - 3): file_path = join(file_path, current_dir[path]) file_path = join(file_path, pre, in_path) if os.path.exists(file_path): try: with open(file_path) as f: self.resource = f.read() except UnicodeDecodeError: Logger.debug("Opening resource as binary.") with open(file_path, "rb") as f: self.resource = f.read() Logger.extra("Found resource in local resource directory.") return # Check /usr/local - pip installs to here file_path = "/usr/local/resources/" + in_path if os.path.exists(file_path): try: with open(file_path) as f: self.resource = f.read() except UnicodeDecodeError: Logger.debug("Opening resource as binary.") with open(file_path, "rb") as f: self.resource = f.read() Logger.extra( "Found resource in /usr/local/ resource directory.") return # Attempt to get from package - setup.py installs here try: self.resource = pkg_resources.resource_string( pkg_resources.Requirement.parse("drcsim"), join(pre, in_path)) Logger.extra("Found resource in package.") except FileNotFoundError: Logger.throw("Could not find resource: %s" % join(pre, in_path)) sys.exit()