def run(self): """ Run command """ if self.has_option('--edit') or self.has_option('-e'): return os.system('vim "' + Env.repos_config() + '"') if self.has_option('--add') or self.has_option('-a'): RootRequired.require_root_permission() repo_string = '' for arg in self.arguments: repo_string += arg + ' ' repo_string = repo_string.strip() tmp_repo = Repo(repo_string) tmp_repo.loaded_from_file = 'argument' tmp_repo.line_number = 0 if not tmp_repo.successful_loaded: ReposListErrorShower.show([tmp_repo]) return 1 # write repo path = Env.repos_config_dir('/' + tmp_repo.name + '-' + tmp_repo.get_pkg_str() + '-' + tmp_repo.get_arch_str()) tmp = '' tmp_i = 1 while os.path.isfile(path + tmp): tmp = '-' + str(tmp_i) f = open(path, 'w') f.write('# added manually\n' + repo_string) f.close() return 0 if self.has_option('--scan'): for arg in self.arguments: if os.path.isdir(arg): Scanner.scan(arg) pr.p(ansi.green + 'directory ' + arg + ' scanned successfully' + ansi.reset) else: self.message('directory' + arg + ' not found', is_error=True) return 0 # show list of repos if not self.is_quiet(): pr.p('Loading repositories list...') repos = Repo.get_list() if not self.is_quiet(): pr.p('============================') ReposListErrorShower.show(repos) for repo in repos: if repo.successful_loaded: pr.p(repo.name + ': ' + repo.url + ' pkg=' + repo.get_pkg_str() + ' arch=' + repo.get_arch_str() + ' channel=' + repo.get_channel_str())
def get_list() -> list: """ returns list of repositories Returns: list[Repo]: list of loaded repositories """ repos = [] files = [Env.repos_config()] for item in os.listdir(Env.repos_config_dir()): if os.path.isfile(Env.repos_config_dir('/' + item)): files.append(Env.repos_config_dir('/' + item)) for fl in files: f = open(fl, 'r') repos_content = f.read() f.close() lines = repos_content.split('\n') line_counter = 1 for line in lines: line = line.split('#')[0].strip() line = line.strip() if line != '': repo = Repo(line) repo.line_number = line_counter repo.loaded_from_file = fl repos.append(repo) line_counter += 1 # sort by priority sorted_repos = [] while len(repos): i = 0 while i < len(repos): is_less_than_all = True j = 0 while j < len(repos): if int(repos[i].priority) > int(repos[j].priority): is_less_than_all = False j += 1 if is_less_than_all: sorted_repos.append(repos[i]) repos.pop(i) i += 1 return sorted_repos
def require_root_permission(is_cli=True, die_action=None): """ checks root premission. Args: is_cli (bool): if is True, when user have not root permission, error will print in terminal. but if is False, the `die_action` will run as a function. (will be disable in testing environment) die_action (callable): the function will be run when `is_cli` is False """ # if program is in testing mode don't check permission if is_testing: return if os.getuid() == 0: return # check write and read access for needed files files_to_check = [ Env.packages_lists(), Env.installed_lists(), Env.state_file(), Env.unremoved_conffiles(), Env.security_blacklist(), Env.any_scripts(), Env.repos_config(), Env.repos_config_dir(), Env.cache_dir(), Env.allowed_archs(), ] for f in files_to_check: if not os.access(f, os.W_OK) or not os.access(f, os.R_OK): if is_cli: pr.e(ansi.red + sys.argv[0] + ': permission is denied' + ansi.reset) pr.exit(1) return else: die_action() return