def do_import_patch(cls, patch_filepath): ok_to_import, msg = cls.is_ready_for_import() if not ok_to_import: return CmdResult.error(stderr=msg) epatch = patchlib.Patch.parse_text_file(patch_filepath) description = epatch.get_description() if not description: return CmdResult.error(stderr="Empty description") result = runext.run_cmd(["git", "apply", patch_filepath]) if not result.is_less_than_error: return result result = runext.run_cmd(["git", "add"] + epatch.get_file_paths(1)) if not result.is_less_than_error: return result return runext.run_cmd(["git", "commit", "-q", "-m", description])
def git_do_copy_file_to_index(self, file_path): # TODO: move nuts and bolts down to scm_ifce_git # TODO: use os_utils function for most of this PROMPT = _('Enter target path for copy of "{0}"'.format(file_path)) as_file_path = self.ask_file_path(PROMPT, existing=False, suggestion=file_path) if as_file_path is None or os.path.relpath(as_file_path) == file_path: return while os.path.exists(as_file_path): from gi.repository import Gtk from aipoed import CmdResult from aipoed.gui import dialogue result = CmdResult.error(stderr="{0}: already exists".format(as_file_path)) | CmdResult.Suggest.OVERWRITE_OR_RENAME resp = self.ask_rename_overwrite_or_cancel(result) if resp == Gtk.ResponseType.CANCEL: return elif resp == dialogue.Response.OVERWRITE: break elif resp == dialogue.Response.RENAME: as_file_path = self.ask_file_path(PROMPT, existing=False, suggestion=as_file_path) if as_file_path is None: return import shutil from aipoed.gui import console console.LOG.start_cmd('cp -p {0} {1}'.format(file_path, as_file_path)) try: shutil.copy2(file_path, as_file_path) except IOError as edata: console.LOG.append_stderr(str(edata)) console.LOG.end_cmd() self.report_exception_as_error(edata) return console.LOG.end_cmd() self.git_do_add_fsis_to_index([as_file_path])
def os_move_or_copy_fs_item(fsi_path, destn, opsym, overwrite=False, force=False, verbose=False): assert opsym in (Relation.MOVED_TO, Relation.COPIED_TO), _("Invalid operation requested") new_path = os.path.join(destn, os.path.basename(fsi_path)) if destn.endswith(os.sep) else destn omsg = "{0} {1} {2}.".format(fsi_path, opsym, new_path) if verbose else "" _CONSOLE_LOG.start_cmd("{0} {1} {2}\n".format(fsi_path, opsym, new_path)) if os.path.exists(new_path): if not overwrite: emsg = _("{0} \"{1}\" already exists.").format(_("Directory") if os.path.isdir(new_path) else _("File"), new_path) result = CmdResult.error(omsg, emsg) | Suggestion.OVERWRITE_OR_RENAME _CONSOLE_LOG.end_cmd(result) return result try: if os.path.isdir(new_path) and not os.path.islink(new_path): if force: shutil.rmtree(new_path) else: os.removedirs(new_path) else: os.remove(new_path) except OSError as edata: errorcode = CmdResult.ERROR | Suggestion.FORCE if edata.errno == errno.ENOTEMPTY else CmdResult.ERROR errmsg = _("Error: {}: \"{}\" {} \"{}\"\n").format(edata.strerror, fsi_path, opsym, new_path) _CONSOLE_LOG.append_stderr(errmsg) return CmdResult(errorcode, "", errmsg) except shutil.Error as edata: serr = _("Error: \"{0}\" {1} \"{2}\" failed.\n").format(fsi_path, opsym, new_path) for src_path, dest_path, reason in edata.args: serr += _("Error: \"{0}\" {1} \"{2}\": {3}.\n").format(src_path, opsym, dest_path, reason) return CmdResult.error(omsg, serr) try: if opsym is Relation.MOVED_TO: os.rename(fsi_path, new_path) elif os.path.isdir(fsi_path): shutil.copytree(fsi_path, new_path) else: shutil.copy2(fsi_path, new_path) result = CmdResult.ok(omsg) except OSError as edata: result = CmdResult.error(omsg, _("Error: \"{0}\" {1} \"{2}\" failed. {3}.\n").format(fsi_path, opsym, new_path, edata.strerror)) except shutil.Error as edata: serr = _("Error: \"{0}\" {1} \"{2}\" failed.\n").format(fsi_path, opsym, new_path) for src_path, dest_path, reason in edata.args: serr += _("Error: \"{0}\" {1} \"{2}\": {3}.\n").format(src_path, opsym, dest_path, reason) result = CmdResult.error(omsg, serr) _CONSOLE_LOG.end_cmd(result) enotify.notify_events(E_FILE_MOVED) return result
def load_pgnd_options(): global PGND_OPTIONS PGND_OPTIONS = configparser.SafeConfigParser() try: PGND_OPTIONS.read(_PGND_CFG_FILE_PATH) except configparser.ParsingError as edata: return CmdResult.error(stderr=_("Error reading playground options: {0}\n").format(str(edata))) return CmdResult.ok()
def load_global_options(): global GLOBAL_OPTIONS GLOBAL_OPTIONS = configparser.SafeConfigParser() try: GLOBAL_OPTIONS.read(_GLOBAL_CFG_FILE_PATH) except configparser.ParsingError as edata: return CmdResult.error(stderr=_("Error reading global options: {0}\n").format(str(edata))) return CmdResult.ok()
def set_patch_file_description(patch_file_path, description, overwrite=False): from aipoed.patch_diff import patchlib from aipoed import utils if os.path.isfile(patch_file_path): try: patch_obj = patchlib.Patch.parse_text(utils.get_file_contents(patch_file_path)) except IOError as edata: return CmdResult.error(stderr=str(edata)) except patchlib.ParseError: if overwrite: patch_obj = patchlib.Patch() else: return CmdResult.error(stderr=_("{0}: exists but is not a valid patch file".format(patch_file_path))) | CmdResult.Suggest.OVERWRITE else: patch_obj = patchlib.Patch() patch_obj.set_description(description) result = utils.set_file_contents(patch_file_path, str(patch_obj), compress=True) return result
def os_create_dir(dir_path): _CONSOLE_LOG.start_cmd("mkdir -p " + dir_path) try: os.makedirs(dir_path) result = CmdResult.ok() except OSError as edata: result = CmdResult.error(str(edata)) _CONSOLE_LOG.end_cmd(result) return result
def check_for_overwrites(destn_file_paths): from aipoed import utils overwritten = [file_path for file_path in destn_file_paths if os.path.exists(file_path)] if overwritten: stderr = _("File(s):\n") for file_path in overwritten: stderr += "\t{0}\n".format(utils.quote_if_needed(file_path)) return CmdResult.error(stderr=stderr + _("will be overwritten!\n")) | Suggestion.OVERWRITE_OR_RENAME return CmdResult.ok()
def launch_external_diff(file_a, file_b): extdiff = options.get("diff", "extdiff") if not extdiff: return CmdResult.warning(_("No external diff viewer is defined.\n")) try: runext.run_cmd_in_bgnd([extdiff, file_a, file_b]) except OSError as edata: return CmdResult.error( stderr=_("Error launching external viewer \"{0}\": {1}\n").format( extdiff, edata.strerror)) return CmdResult.ok()
def do_export_patch_as(cls, patch_name, export_file_name=None, force=False, overwrite=False): if not force: result = cls._check_patch_export_status(patch_name) if result: return result if not export_file_name: export_file_name = utils.convert_patchname_to_filename(patch_name) if not overwrite and os.path.exists(export_file_name): emsg = _("{0}: file already exists.\n").format(export_file_name) return CmdResult.error(stderr=emsg) + CmdResult.Suggest.OVERWRITE_OR_RENAME # NB we don't use shutil.copyfile() here as names may dictate (de)compression return utils.set_file_contents(export_file_name, cls.get_patch_text(patch_name))
def _save_to_file(self): if not self._save_file: return try: fobj = open(self._save_file, 'w') except IOError as edata: dialogue.main_window.report_any_problems( CmdResult.error(stderr=edata[1])) self.check_set_save_sensitive() return text = self._get_text_to_save() fobj.write(text) fobj.close() self.check_set_save_sensitive()
def _save_to_file(self): if not self._save_file: return try: fobj = open(self._save_file, "w") except IOError as edata: strerror = edata[1] dialogue.main_window.report_any_problems( CmdResult.error(stderr=strerror)) self.check_set_save_sensitive() return text = self.bfr.get_text(self.bfr.get_start_iter(), self.bfr.get_end_iter()) fobj.write(text) fobj.close() self.check_set_save_sensitive()
def os_create_file(file_path): """Attempt to create a file with the given file_path and report the outcome as a CmdResult tuple. 1. If console is not None print report of successful creation on it. 2. If a file with same file_path already exists fail and report a warning. 3. If file creation fails for other reasons report an error. """ _CONSOLE_LOG.start_cmd("create \"{0}\"".format(file_path)) if not os.path.exists(file_path): try: open(file_path, 'w').close() enotify.notify_events(E_FILE_ADDED) result = CmdResult.ok() except (IOError, OSError) as msg: result = CmdResult.error(stderr="\"{0}\": {1}".format(file_path, msg)) else: result = CmdResult.warning(stderr=_("\"{0}\": file already exists").format(file_path)) _CONSOLE_LOG.end_cmd(result) return result
def chdir(newdir): from aipoed import CmdResult events = 0 try: os.chdir(newdir) retval = CmdResult.ok() except OSError as err: import errno ecode = errno.errorcode[err.errno] emsg = err.strerror retval = CmdResult.error( stderr="{0}: \"{1}\" : {2}".format(ecode, newdir, emsg)) newdir = os.getcwd() # NB regardless of success of os.chdir() we need to check the interfaces from aipoed import enotify from aipoed import options from aipoed.gui.console import LOG from aipoed.scm.gui import ifce as scm_ifce scm_ifce.get_ifce() if scm_ifce.SCM.in_valid_pgnd: # move down to the root dir newdir = scm_ifce.SCM.get_playground_root() os.chdir(newdir) from aipoed.gui import recollect WorkspacePathView.append_saved_path(newdir) recollect.set("workspace", "last_used", newdir) from aipoed.pm.gui import ifce as pm_ifce pm_ifce.get_ifce() options.reload_pgnd_options() CURDIR = os.getcwd() LOG.start_cmd(_("New Working Directory: {0}\n").format(CURDIR)) LOG.append_stdout(retval.stdout) LOG.append_stderr(retval.stderr) if scm_ifce.SCM.in_valid_pgnd: LOG.append_stdout('In valid repository\n') else: LOG.append_stderr('NOT in valid repository\n') LOG.end_cmd() enotify.notify_events(enotify.E_CHANGE_WD, new_wd=CURDIR) return retval
def os_move_or_copy_fs_items(fsi_paths, destn, opsym, overwrite=False, force=False, verbose=False): assert opsym in (Relation.MOVED_TO, Relation.COPIED_TO), _("Invalid operation requested") assert len(fsi_paths) > 1 from aipoed import utils def _overwrite_msg(overwrites): if len(overwrites) == 0: return "" elif len(overwrites) > 1: return _("Files:\n\t{0}\nalready exist.").format("\n\t".join(["\"" + fp + "\"" for fp in overwrites])) else: return _("File \"{0}\" already exists.").format(overwrites[0]) _CONSOLE_LOG.start_cmd("{0} {1} {2}\n".format(utils.quoted_join(fsi_paths), opsym, destn)) if not os.path.isdir(destn): result = CmdResult.error(stderr=_('"{0}": Destination must be a directory for multifile move/copy.').format(destn)) _CONSOLE_LOG.end_cmd(result) return result opn_paths_list = [(fsi_path, os.path.join(destn, os.path.basename(fsi_path))) for fsi_path in fsi_paths] omsg = "\n".join(["{0} {1} {2}.".format(src, opsym, destn) for (src, destn) in opn_paths_list]) if verbose else "" if not overwrite: overwrites = [destn for (src, destn) in opn_paths_list if os.path.exists(destn)] if len(overwrites) > 0: emsg = _overwrite_msg(overwrites) result = CmdResult.error(omsg, emsg) | Suggestion.OVERWRITE_OR_RENAME _CONSOLE_LOG.end_cmd(result) return result failed_opns_str = "" rescode = CmdResult.OK for (src, tgt) in opn_paths_list: if verbose: _CONSOLE_LOG.append_stdout("{0} {1} {2}.".format(src, opsym, tgt)) if os.path.exists(tgt): try: if os.path.isdir(tgt) and not os.path.islink(tgt): if force: shutil.rmtree(tgt) else: os.removedirs(tgt) else: os.remove(tgt) except OSError as edata: rescode |= CmdResult.ERROR | Suggestion.FORCE if edata.errno == errno.ENOTEMPTY else CmdResult.ERROR serr = _("Error: {}: \"{}\" {} \"{}\"\n").format(edata.strerror, src, opsym, tgt) _CONSOLE_LOG.append_stderr(serr) failed_opns_str += serr continue except shutil.Error as edata: rescode |= CmdResult.ERROR serr = _("Error: \"{0}\" {1} \"{2}\" failed.\n").format(src, opsym, tgt) for src_path, tgt_path, reason in edata.args: serr += _("Error: \"{0}\" {1} \"{2}\": {3}.\n").format(src_path, opsym, tgt_path, reason) _CONSOLE_LOG.append_stderr(serr) failed_opns_str += serr continue try: if opsym is Relation.MOVED_TO: os.rename(src, tgt) elif os.path.isdir(src): shutil.copytree(src, tgt) else: shutil.copy2(src, tgt) except OSError as edata: rescode |= CmdResult.ERROR serr = _("Error: \"{0}\" {1} \"{2}\" failed. {3}.\n").format(src, opsym, tgt, edata.strerror) _CONSOLE_LOG.append_stderr(serr) failed_opns_str += serr continue except shutil.Error as edata: rescode |= CmdResult.ERROR serr = _("Error: \"{0}\" {1} \"{2}\" failed.\n").format(src, opsym, tgt) for src_path, tgt_path, reason in edata.args: serr += _("Error: \"{0}\" {1} \"{2}\": {3}.\n").format(src_path, opsym, tgt_path, reason) _CONSOLE_LOG.append_stderr(serr) failed_opns_str += serr continue _CONSOLE_LOG.end_cmd() enotify.notify_events(E_FILE_MOVED) return CmdResult(rescode, omsg, failed_opns_str)