def add_to_unremoved_conffiles(pkg: Pkg, filepath: str): """ Adds filepath to list of unremoved conffiles """ f = open(Env.unremoved_conffiles(), 'r') filelist = f.read().strip().split('\n') f.close() # add item to list if not filepath in filelist: filelist.append(filepath) # generate new content of unremoved_conffiles file new_content = '' for item in filelist: new_content += item + '\n' # write new content to file f = open(Env.unremoved_conffiles(), 'w') f.write(new_content) f.close()
def copy_files(self, pkg: BaseArchive, directory_not_empty_event, target_path='') -> list: """ Copy package files on system Args: pkg (BaseArchive): the package archive object directory_not_empty_event (callable): the function will be run when we want to delete old direcotry of package and that is not empty. target_path (str): target path prefix of copied files location. default is `/` means copies files on the root directory. you can change it. Returns: list[str]: list of copied files """ # load package old files list old_files = [] if os.path.isfile( Env.installed_lists('/' + pkg.data['name'] + '/files')): try: f = open( Env.installed_lists('/' + pkg.data['name'] + '/files'), 'r') for line in f.read().strip().split('\n'): if line != '': old_files.append(line.strip()) except: pass old_files = list(reversed(old_files)) # load unremoved conffiles list unremoved_conffiles_f = open(Env.unremoved_conffiles(), 'r') unremoved_conffiles = unremoved_conffiles_f.read().strip().split('\n') unremoved_conffiles_f.close() temp_dir = self.extracted_package_dir # load files list from `files` directory of package self.loaded_files = [] self.load_files(temp_dir + '/files', temp_dir + '/files') self.loaded_files = [[target_path + f[0], f[1]] for f in self.loaded_files] # check file conflicts all_installed_files = Pkg.get_all_installed_files_list() for lf in self.loaded_files: if not os.path.isdir(lf[1]): for insf in all_installed_files: if insf[0].split(':', 1)[0] != pkg.data['name']: if lf[0] == insf[2]: insf_pkg = Pkg.load_last(insf[0]) if insf_pkg: insf[0] = insf[0] + ':' + insf_pkg.installed() # check package is in `replaces` list do_raise_error = True replaces_list = pkg.get_replaces() for rep in replaces_list: if Pkg.check_state(rep): tmp_parts = rep.split(' ') tmp_parts = tmp_parts[0].split('>') tmp_parts = tmp_parts[0].split('<') tmp_parts = tmp_parts[0].split('=') tmp_parts = tmp_parts[0] if tmp_parts == insf[0].split(':', 1)[0]: do_raise_error = False if do_raise_error: raise FileConflictError('package ' + pkg.data['name'] + ':' + pkg.data['version'] + ' and ' + insf[0] + ' both has file "' + lf[0] + '"') # copy loaded files self.copied_files = [] for f in self.loaded_files: if os.path.exists(Env.base_path(f[0])): if os.path.isfile(Env.base_path(f[0])): if ('f:' + f[0]) in old_files or ('cf:' + f[0]) in old_files: self.copy_once_file(f) try: old_files.pop(old_files.index(('f:' + f[0]))) except: pass else: if f[0] in unremoved_conffiles: self.copy_once_file(f) unremoved_conffiles.pop( unremoved_conffiles.index(f[0])) else: self.copy_once_file(f) else: if ('d:' + f[0]) in old_files or ('cd:' + f[0]) in old_files: if ('cd:' + f[0]) in old_files: self.copied_files.append('cd:' + f[0]) old_files.pop(old_files.index(('cd:' + f[0]))) else: self.copied_files.append('d:' + f[0]) old_files.pop(old_files.index(('d:' + f[0]))) else: if f[0] in unremoved_conffiles: self.copied_files.append('d:' + f[0]) unremoved_conffiles.pop( unremoved_conffiles.index(f[0])) else: self.copy_once_file(f) # delete not wanted old files for item in old_files: parts = item.strip().split(':', 1) if parts[0] == 'cf' or parts[0] == 'cd': pass else: if os.path.isfile(parts[1]): os.remove(parts[1]) else: try: os.rmdir(parts[1]) except: # directory is not emptyr directory_not_empty_event(pkg, parts[1]) # write new unremoved conffiles list unremoved_conffiles_f = open(Env.unremoved_conffiles(), 'w') new_content = '' for item in unremoved_conffiles: new_content += item + '\n' unremoved_conffiles_f.write(new_content) unremoved_conffiles_f.close() return self.copied_files