def pop_state(): """ remove first item from state """ f = open(Env.state_file(), 'r') content = f.read() f.close() content = content.strip() lines = content.split('\n') if lines: lines.pop(0) new_content = '' for line in lines: new_content += line + '\n' f = open(Env.state_file(), 'w') f.write(new_content) f.close()
def state_list(): """ returns list of undoned transactions from state file """ f = open(Env.state_file(), 'r') content = f.read() f.close() content = content.strip().split('\n') content = [line.strip() for line in content] result = [] for item in content: if item != '': tmp = {} parts = item.split('%') tmp['action'] = parts[0] tmp['pkg'] = parts[1] try: tmp['version'] = parts[2] except: tmp['version'] = None try: tmp['arch'] = parts[3] except: tmp['arch'] = None try: tmp['file'] = parts[4] except: tmp['file'] = None result.append(tmp) return result
def run(self): """ Run test """ self.assert_equals( self.run_command( 'pkg', ['install', 'repository/test-repository/testpkgc-2.0.cati']), 0) state_f = open(Env.state_file(), 'w') state_f.write(f'install%testpackage1%1.0%amd64\nremove%anotherpackage') state_f.close() self.assert_equals(self.run_command('remove', [ 'testpkgc', '-y', ]), 1) self.assert_equals( self.run_command( 'pkg', ['install', 'repository/test-repository/testpkgc-2.0.cati']), 1) # tests for cli `state` command self.assert_equals(self.run_command('state'), 1) self.assert_equals(self.run_command('state', ['--abort', '-y']), 0) self.assert_equals(self.run_command('state'), 0) self.refresh_env() self.assert_equals( self.run_command( 'pkg', ['install', 'repository/test-repository/testpkgc-2.0.cati']), 0) self.assert_true(Pkg.is_installed('testpkgc')) state_f = open(Env.state_file(), 'w') state_f.write(f'remove%testpkgc') state_f.close() self.assert_equals(self.run_command('state'), 1) self.assert_equals(self.run_command('state', ['--complete']), 0) self.assert_equals(self.run_command('state'), 0) self.assert_true(not Pkg.is_installed('testpkgc'))
def add_to_state(calc: Calculator): """ add new item to state """ content = '' for item in calc.get_sorted_list(): content += item['action'] + '%' + item['pkg'].data[ 'name'] + '%' + item['pkg'].data['version'] + '%' + item[ 'pkg'].data['arch'] + '\n' f = open(Env.state_file(), 'w') f.write(content) f.close()
def finish_all_state(): """ clear all of states """ f = open(Env.state_file(), 'w') f.write('') f.close()
def sub_install(self): """ install sub command (cati pkg install) """ if len(self.arguments) <= 1: self.message('argument package file(s) required') return 1 require_root_permission() # check transactions state before run new transactions pr.p('Checking transactions state...') state_list = BaseTransaction.state_list( ) # get list of undoned transactions if state_list: # the list is not empty StateContentShower.show(state_list) return 1 packages_to_install = [] i = 1 while i < len(self.arguments): try: pkg = archive_factory(self.arguments[i], 'r') pkg.read() pkg.package_file_path = os.path.abspath(self.arguments[i]) packages_to_install.append(pkg) except FileNotFoundError as ex: self.message('file "' + self.arguments[i] + '" not found' + ansi.reset, before=ansi.red) return 1 except: self.message('cannot open "' + self.arguments[i] + '": file is corrupt' + ansi.reset, before=ansi.red) return 1 i += 1 # add packages to state state_f = open(Env.state_file(), 'w') tmp = '' for pkg in packages_to_install: tmp += ('install%' + pkg.data['name'] + '%' + pkg.data['version'] + '%' + pkg.data['arch'] + '%' + pkg.package_file_path + '\n') state_f.write(tmp) state_f.close() packages_to_install_names_and_versions = [ pkg.data['name'] + '@' + pkg.data['version'] for pkg in packages_to_install ] i = 0 while i < len(packages_to_install): try: pkg = packages_to_install[i] tmp = self.install_once(pkg) if type(tmp) == int and tmp != 0: if not self.has_option('--dont-ignore-state'): BaseTransaction.finish_all_state() return tmp pkg.close() except: if not self.has_option('--dont-ignore-state'): BaseTransaction.finish_all_state() self.message('cannot install "' + packages_to_install[i].data['name'] + '"' + ansi.reset, before=ansi.red) return 1 i += 1 BaseTransaction.run_any_scripts( ['install', packages_to_install_names_and_versions], events={ 'start_run_script': self.start_run_any_script_event, }) BaseTransaction.finish_all_state()