def run(self): """ Run command """ if not self.is_quiet(): pr.p('Loading packages list...') pr.p('========================') loaded_packages = [] for argument in self.arguments: arg_parts = argument.split('=') if len(arg_parts) == 1: # load last version as default pkg = Pkg.load_last(argument) elif len(arg_parts) == 2: # load specify version pkg = Pkg.load_version(arg_parts[0], arg_parts[1]) if pkg == 1: pkg = False elif pkg == 2: self.message('package "' + arg_parts[0] + '" has not version "' + arg_parts[1] + '"' + ansi.reset, before=ansi.red) continue else: # load specify version and specify arch pkg = Pkg.load_version(arg_parts[0], arg_parts[1], arg_parts[2]) if pkg == 1: pkg = False elif pkg == 2: self.message('package "' + arg_parts[0] + '" has not version or arch "' + arg_parts[1] + ':' + arg_parts[2] + '"' + ansi.reset, before=ansi.red) continue if pkg: loaded_packages.append(pkg) else: self.message('unknow package "' + argument + '"' + ansi.reset, before=ansi.red) if not loaded_packages: return 1 # download loaded packages is_any_success = False output_path = self.option_value('--output') for pkg in loaded_packages: if len(loaded_packages) > 1: tmp = self.download_once(pkg) else: tmp = self.download_once(pkg, output_path) if tmp: is_any_success = True if not is_any_success: return 1
def refresh_lists(self): """ Refresh packages list and sync them with depends and conflicts """ # sync versions i = 0 while i < len(self.to_remove): self.to_remove[i] = Pkg.load_version( self.to_remove[i].data['name'], self.to_remove[i].installed()) i += 1 i = 0 while i < len(self.to_install): wv = self.to_install[i].wanted_version im = self.to_install[i].is_manual self.to_install[i] = Pkg.load_version( self.to_install[i].data['name'], self.to_install[i].wanted_version) self.to_install[i].wanted_version = wv self.to_install[i].is_manual = im i += 1 # remove repeated lists in to_remove list a = 0 while a < len(self.to_remove): b = 0 while b < len(self.to_remove): if a != b: if self.to_remove[a].data['name'] == self.to_remove[ b].data['name']: self.to_remove.pop(b) a -= 1 b -= 1 b += 1 a += 1 # sync to_remove list new_to_remove = [] for item in self.to_remove: # load reverse dependnecy of current package and add them to list reverse_depends = item.get_reverse_depends() for rd in reverse_depends: if rd.installed(): if not rd.data['name'] in [ tmp.data['name'] for tmp in self.to_remove ]: new_to_remove.append(rd) if new_to_remove: self.remove(new_to_remove) self.handle_install_depends() self.handle_install_conflicts()
def handle_install_reverse_depends(self): """ Adds installable packages reverse depends to install list """ new_to_remove = [] i = 0 while i < len(self.to_install): reverse_depends = self.to_install[i].get_reverse_depends() for dep in reverse_depends: if dep.installed(): dep = Pkg.load_version(dep.data['name'], dep.installed()) for tmp_dep in dep.get_depends(): result = Pkg.check_state( tmp_dep, virtual={ 'install': [[ self.to_install[i].data['name'], self.to_install[i].data['version'] ]], }) if not result: a = 0 added = False while a < len(self.to_remove): if self.to_remove[a].data['name'] == dep.data[ 'name']: added = True a += 1 if not added: new_to_remove.append(dep) i += 1 if new_to_remove: self.remove(new_to_remove)
def find_unused_packages(self): """ finds unused packages """ all_packages = Pkg.all_list()['list'] for pkg in all_packages: try: if Pkg.is_installed(pkg.data['name']): if not Pkg.is_installed_manual(pkg.data['name']): has_any_reverse_depends = False reverse_depends = Pkg.load_version( pkg.data['name'], Pkg.installed_version( pkg.data['name'])).get_reverse_depends() for rdep in reverse_depends: if rdep.installed(): rdep_is_in_unused_packages = False for upkg in self.unused_packages: if upkg.data['name'] == rdep.data['name']: rdep_is_in_unused_packages = True if not rdep_is_in_unused_packages: has_any_reverse_depends = True if not has_any_reverse_depends: self.unused_packages.append(pkg) except: pass
def run(self): """ Run command """ pr.p('Loading packages list...') pr.p('========================') loaded_packages = [] for argument in self.arguments: arg_parts = argument.split('=') if len(arg_parts) == 1: # load last version as default pkg = Pkg.load_last(argument) else: # load specify version pkg = Pkg.load_version(arg_parts[0], arg_parts[1]) if pkg == 1: pkg = False elif pkg == 2: self.message('package "' + arg_parts[0] + '" has not version "' + arg_parts[1] + '"' + ansi.reset, before=ansi.red) continue if pkg: loaded_packages.append(pkg) else: self.message('unknow package "' + argument + '"' + ansi.reset, before=ansi.red) if not loaded_packages: return 1 # show loaded packages for pkg in loaded_packages: if self.has_option('--versions'): versions_list = pkg.get_versions_list() pr.p(pkg.data['name'] + ':') for ver in versions_list: pr.p(' ' + ver[0] + ':' + ver[1]) else: PackageShower.show(pkg.data) if len(loaded_packages) > 1: pr.p('---------------------')
def handle_install_depends(self): """ Adds installable packages depends to install list """ new_to_install = [] i = 0 while i < len(self.to_install): depends = self.to_install[i].get_depends() if self.with_recommends: depends = [*depends, *self.to_install[i].get_recommends()] for depend in depends: if not Pkg.check_state(depend) and depend.strip()[0] != '@': # find package depend try: self.to_install[i].depend_get_next[depend] except: try: self.to_install[i].depend_get_next[depend] = 0 except: self.to_install[i].depend_get_next = {} self.to_install[i].depend_get_next[depend] = 0 pkg = Pkg.check_state( depend, get_false_pkg=True, get_false_pkg_next=self.to_install[i]. depend_get_next[depend]) self.to_install[i].depend_get_next[depend] += 1 if len(pkg) == 1: a = 0 added = False while a < len(self.to_install): if self.to_install[a].data['name'] == pkg[0]: added = True a += 1 if not added: new_to_install.append(Pkg.load_last(pkg[0])) elif len(pkg) == 3: a = 0 added = False while a < len(self.to_install): if self.to_install[a].data['name'] == pkg[0]: wanted_version = pkg[2] installed_version = self.to_install[ a].wanted_version if pkg[1] == '=': if Pkg.compare_version( installed_version, wanted_version) == 0: added = True elif pkg[1] == '>=': if Pkg.compare_version( installed_version, wanted_version) >= 0: added = True elif pkg[1] == '<=': if Pkg.compare_version( installed_version, wanted_version) <= 0: added = True elif pkg[1] == '>': if Pkg.compare_version( installed_version, wanted_version) == 1: added = True elif pkg[1] == '<': if Pkg.compare_version( installed_version, wanted_version) == -1: added = True a += 1 if not added: pkg_obj = None if pkg[1] == '=': pkg_obj = Pkg.load_version(pkg[0], pkg[2]) elif pkg[1] == '>=' or pkg[1] == '>': pkg_obj = Pkg.load_last(pkg[0]) elif pkg[1] == '<=': pkg_obj = Pkg.load_version(pkg[0], pkg[2]) elif pkg[1] == '<': pkg_obj = Pkg.load_last(pkg[0]) versions = pkg_obj.get_versions_list() x = 0 while x < len(versions): if Pkg.compare_version( versions[x][0], pkg[0]) >= 0: versions.pop(x) x += 1 versions = [v[0] for v in versions] wanted_ver = pkg.get_last_version(versions) pkg_obj = Pkg.load_version(pkg[0], wanted_ver) new_to_install.append(pkg_obj) i += 1 if new_to_install: self.install(new_to_install) self.handle_install_reverse_depends()
def run(self): """ Run command """ RootRequired.require_root_permission() pr.p('Loading packages list...') pr.p('========================') loaded_packages = [] for argument in self.arguments: arg_parts = argument.split('=') if len(arg_parts) == 1: # load last version as default pkg = Pkg.load_last(argument) else: # load specify version pkg = Pkg.load_version(arg_parts[0], arg_parts[1]) if pkg == 1: pkg = False elif pkg == 2: self.message('package "' + arg_parts[0] + '" has not version "' + arg_parts[1] + '"' + ansi.reset, before=ansi.red) continue else: pkg.only_specify_version = True if pkg: try: pkg.only_specify_version except: pkg.only_specify_version = False if pkg.installed(): if not pkg.only_specify_version: self.message( 'package "' + argument + '" is installed. cannot forget installed packages' + ansi.reset, before=ansi.red) continue else: if pkg.installed() == pkg.data['version']: self.message( 'package ' + argument + ' (' + pkg.data['version'] + ') is installed. cannot forget installed packages' + ansi.reset, before=ansi.red) continue loaded_packages.append(pkg) else: self.message('unknow package "' + argument + '"' + ansi.reset, before=ansi.red) if not loaded_packages: return 1 # forget loaded packages for pkg in loaded_packages: if not pkg.only_specify_version: # forget all of package versions shutil.rmtree(Env.packages_lists('/' + pkg.data['name'])) pr.p('Package ' + pkg.data['name'] + ' was forgoten successfully') else: files = glob.glob( Env.packages_lists('/' + pkg.data['name'] + '/' + pkg.data['version'] + '-*')) for f in files: if not '-' in f[len( Env.packages_lists('/' + pkg.data['name'] + '/' + pkg.data['version'] + '-')):]: os.remove(f) pr.p('Version ' + pkg.data['version'] + ' of package ' + pkg.data['name'] + ' was forgoten successfully') try: if len(os.listdir( Env.packages_lists('/' + pkg.data['name']))) <= 1: shutil.rmtree(Env.packages_lists('/' + pkg.data['name'])) except: pass ListUpdater.update_indexes({ 'cannot_read_file': self.empty_method_for_event, 'invalid_json_data': self.empty_method_for_event, })
def run(self): """ Run command """ RootRequired.require_root_permission() pr.p('Loading packages list...') pr.p('========================') loaded_packages = [] for argument in self.arguments: arg_parts = argument.split('=') if len(arg_parts) == 1: # load last version as default pkg = Pkg.load_last(argument) else: # load specify version pkg = Pkg.load_version(arg_parts[0], arg_parts[1]) if pkg == 1: pkg = False elif pkg == 2: self.message('package "' + arg_parts[0] + '" has not version "' + arg_parts[1] + '"' + ansi.reset, before=ansi.red) continue if pkg: loaded_packages.append(pkg) else: self.message('unknow package "' + argument + '"' + ansi.reset, before=ansi.red) # remove local packages from list new_loaded_packages = [] for pkg in loaded_packages: try: file_path = pkg.data['file_path'] new_loaded_packages.append(pkg) except: self.message('package "' + pkg.data['name'] + '" is a local package', is_error=True) loaded_packages = new_loaded_packages if not loaded_packages: return 1 # calculate transactions pr.p('Calculating transactions...') calc = Calculator(with_recommends=self.has_option('--with-recommends')) i = 0 while i < len(loaded_packages): loaded_packages[i].is_manual = True i += 1 try: calc.install(list(reversed(loaded_packages))) except: pr.e(ansi.red + 'ERROR: There is some dependnecy problems.' + ansi.reset) return 1 # handle reinstallable packages i = 0 while i < len(calc.to_install): if calc.to_install[i].installed(): if calc.to_install[i].installed( ) == calc.to_install[i].wanted_version: if not self.has_option('--reinstall'): pr.p( 'Package ' + calc.to_install[i].data['name'] + '=' + calc.to_install[i].wanted_version + ' is currently installed. use --reinstall option to re-install it.' ) if calc.to_install[i].is_manual: try: pr.p( 'Setting it as manual installed package...' ) manual_f = open( Env.installed_lists('/' + pkg.data['name'] + '/manual'), 'w') manual_f.write('') manual_f.close() except: pass calc.to_install.pop(i) i += 1 # check transactions exists if not calc.has_any_thing(): pr.p('Nothing to do.') return 0 # show transaction TransactionShower.show(calc) if not self.has_option('-y') or self.has_option('--yes'): pr.p('Do you want to continue? [Y/n] ', end='') answer = input() if answer == 'y' or answer == 'Y' or answer == '': pass else: pr.p('Abort.') return 1 # start download packages pr.p('Downloading packages...') downloaed_paths = [] for pkg in calc.to_install: download_path = Env.cache_dir('/archives/' + pkg.data['name'] + '-' + pkg.wanted_version + '-' + pkg.data['arch']) if os.path.isfile(download_path): file_sha256 = calc_file_sha256(download_path) file_md5 = calc_file_md5(download_path) valid_sha256 = None valid_md5 = None try: valid_sha256 = pkg.data['file_sha256'] except: valid_sha256 = file_sha256 try: valid_md5 = pkg.data['file_md5'] except: valid_md5 = file_md5 if file_md5 != valid_md5 or file_sha256 != valid_sha256: # file is corrupt and should be download again os.remove(download_path) else: pr.p('Using Cache for ' + pkg.data['name'] + ':' + pkg.data['version'] + ':' + pkg.data['arch'] + '...') downloaed_paths.append(download_path) continue download_cmd = DownloadCommand() i = 0 res = 1 tmp = True while tmp: if i > 5: pr.e(ansi.red + 'Failed to download packages' + ansi.reset) return res pr.p('Downloading ' + pkg.data['name'] + ':' + pkg.data['version'] + ':' + pkg.data['arch'] + '...') res = download_cmd.handle( ArgParser.parse([ 'cati', 'download', '-q', pkg.data['name'] + '=' + pkg.wanted_version, '--output=' + download_path ])) if res == 1 or res == None: tmp = False i += 1 downloaed_paths.append(download_path) pr.p('Download completed.') # check --download-only option if self.has_option('--download-only'): return 0 cati_pkg_cmd_options = [] remove_cmd_options = [] if self.has_option('--keep-conffiles'): cati_pkg_cmd_options.append('--keep-conffiles') if self.has_option('--target'): cati_pkg_cmd_options.append('--target') if self.has_option('--without-scripts'): cati_pkg_cmd_options.append('--without-scripts') remove_cmd_options.append('--without-scripts') # remove packages if calc.to_remove: pr.p('Removing packages...') package_names = [pkg.data['name'] for pkg in calc.to_remove] remove_cmd = RemoveCommand() res = remove_cmd.handle( ArgParser.parse([ 'cati', 'remove', *package_names, '-y', *remove_cmd_options ])) if res != 0 and res != None: pr.e(ansi.red + 'Failed to remove packages' + ansi.reset) return res # install packages pr.p('Installing packages...') pkg_cmd = PkgCommand() res = pkg_cmd.handle( ArgParser.parse([ 'cati', 'pkg', 'install', *downloaed_paths, *cati_pkg_cmd_options ])) if res != 0 and res != None: self.set_manual_installs(calc.to_install) pr.e(ansi.red + 'Failed to install packages' + ansi.reset) return res self.set_manual_installs(calc.to_install) pr.p(ansi.green + 'Done.' + ansi.reset)
def run(self): """ Run command """ if self.has_option('--installed'): if not self.is_quiet(): pr.p('Loading files list...') pr.p('=====================') all_of_installed_files_list = Pkg.get_all_installed_files_list() for item in all_of_installed_files_list: if self.is_quiet(): pr.p(item[2]) else: message = '' if item[1] == 'd': message = ' (directory)' elif item[1] == 'cf': message = ' (config file)' elif item[1] == 'cd': message = ' (config directory)' pr.p(item[0] + ': ' + item[2] + message) return 0 if not self.arguments: self.message('argument package names is required', True) return 1 if not self.is_quiet(): pr.p('Loading packages list...') pr.p('========================') loaded_packages = [] for argument in self.arguments: arg_parts = argument.split('=') if len(arg_parts) == 1: # load last version as default pkg = Pkg.load_last(argument) else: # load specify version pkg = Pkg.load_version(arg_parts[0], arg_parts[1]) if pkg == 1: pkg = False elif pkg == 2: self.message('package "' + arg_parts[0] + '" has not version "' + arg_parts[1] + '"' + ansi.reset, before=ansi.red) continue if pkg: loaded_packages.append(pkg) else: self.message('unknow package "' + argument + '"' + ansi.reset, before=ansi.red) if not loaded_packages: return 1 # show files list of loaded packages for pkg in loaded_packages: try: files_list = pkg.data['files'] except: files_list = [] if not self.is_quiet(): pr.p(pkg.data['name'] + ':') if not files_list: pr.p(ansi.yellow + ' This package is empty' + ansi.reset) for item in files_list: pr.p(' ' + item) else: for item in files_list: pr.p(item) if len(loaded_packages) > 1: if not self.is_quiet(): pr.p('========================')