Esempio n. 1
0
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()
Esempio n. 2
0
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()
Esempio n. 3
0
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
Esempio n. 4
0
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()
Esempio n. 5
0
 def run_cmd_in_console(console,
                        cmd,
                        input_text=None,
                        sanitize_stderr=None):
     from aipoed.utils import quote_if_needed
     if isinstance(cmd, str):
         cmd = shlex.split(cmd)
     if IS_POSIX:
         savedsh = signal.getsignal(signal.SIGPIPE)
         signal.signal(signal.SIGPIPE, signal.SIG_DFL)
     console.start_cmd(" ".join((quote_if_needed(s) for s in cmd)) + "\n")
     try:
         # we need to catch OSError if command is unknown
         sub = subprocess.Popen(cmd,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                close_fds=IS_POSIX,
                                bufsize=-1)
         if input_text is not None:
             sub.stdin.write(input_text)
             console.append_stdin(input_text)
         sub.stdin.close()
         stdout_eof = stderr_eof = False
         outd = errd = ""
         while True:
             to_check_in = [sub.stdout] * (not stdout_eof) + \
                           [sub.stderr] * (not stderr_eof)
             ready = select.select(to_check_in, [], [], 0)
             if sub.stdout in ready[0]:
                 ochunk = sub.stdout.readline().decode()
                 if ochunk == "":
                     stdout_eof = True
                 else:
                     console.append_stdout(ochunk)
                     outd += ochunk
             if sub.stderr in ready[0]:
                 echunk = sub.stderr.readline().decode()
                 if echunk == "":
                     stderr_eof = True
                 else:
                     console.append_stderr(echunk)
                     errd += echunk
             if stdout_eof and stderr_eof:
                 break
         sub.wait()
         result = CmdResult(ecode=sub.returncode, stdout=outd, stderr=errd)
     except OSError as edata:
         emsg = "{0}: [Error {1}] {2}\n".format(cmd[0], edata.errno,
                                                edata.strerror)
         console.append_stderr(emsg)
         result = CmdResult(ecode=edata.errno, stdout="", stderr=emsg)
     console.end_cmd()
     if IS_POSIX:
         signal.signal(signal.SIGPIPE, savedsh)
     return result.mapped_for_warning(sanitize_stderr=sanitize_stderr)
Esempio n. 6
0
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()
Esempio n. 7
0
 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])
Esempio n. 8
0
 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])
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
 def _move_or_copy_fs_item(self, do_copy, fsi_path):
     get_target = lambda suggestion: self.ask_file_path(
         _("New Path"), suggestion=suggestion)
     target = get_target(fsi_path)
     if target:
         op = os_utils.os_copy_fs_item if do_copy else os_utils.os_move_fs_item
         do_op = lambda destn, overwrite=False, force=False: op(
             fsi_path, destn, overwrite=overwrite, force=force)
         return self.do_op_rename_overwrite_force_or_cancel(
             target, do_op, get_target)
     return CmdResult.ok()
Esempio n. 12
0
 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))
Esempio n. 13
0
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
Esempio n. 14
0
 def _move_or_copy_fs_items(self, do_copy, fsi_paths):
     if len(fsi_paths) == 1:
         return self._move_or_copy_fs_item(do_copy, fsi_paths[0])
     get_target = lambda suggestion=None: self.ask_dir_path(
         _("Target Directory Path"), suggestion=suggestion)
     target = get_target()
     if target:
         op = os_utils.os_copy_fs_items if do_copy else os_utils.os_move_fs_items
         do_op = lambda destn, overwrite=False, force=False: op(
             fsi_paths, destn, overwrite=overwrite, force=force)
         return self.do_op_rename_overwrite_force_or_cancel(
             target, do_op, get_target)
     return CmdResult.ok()
Esempio n. 15
0
 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()
Esempio n. 16
0
def os_delete_fs_items(fsi_paths, events=E_FILE_DELETED, force=False):
    from aipoed import utils
    _CONSOLE_LOG.start_cmd(_('delete {0}').format(utils.quoted_join(fsi_paths)))
    serr = ""
    errorcode = CmdResult.ERROR
    for fsi_path in fsi_paths:
        try:
            if os.path.isdir(fsi_path) and not os.path.islink(fsi_path):
                if force:
                    shutil.rmtree(fsi_path)
                else:
                    os.removedirs(fsi_path)
            else:
                os.remove(fsi_path)
            _CONSOLE_LOG.append_stdout(_('Deleted: {0}\n').format(fsi_path))
        except OSError as edata:
            if edata.errno == errno.ENOTEMPTY:
                errorcode = CmdResult.ERROR | Suggestion.FORCE
            errmsg = _("Error: {}: \"{}\"\n").format(edata.strerror, fsi_path)
            serr += errmsg
            _CONSOLE_LOG.append_stderr(errmsg)
    _CONSOLE_LOG.end_cmd()
    enotify.notify_events(events)
    return CmdResult(errorcode, "", serr)  if serr else CmdResult.ok()
Esempio n. 17
0
 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()
Esempio n. 18
0
 def run_cmd(cmd,
             input_text=None,
             sanitize_stderr=None,
             decode_stdout=True):
     if isinstance(cmd, str):
         cmd = shlex.split(cmd)
     sub = subprocess.Popen(cmd,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            close_fds=IS_POSIX,
                            bufsize=-1,
                            startupinfo=startupinfo)
     outd, errd = sub.communicate(input_text)
     return CmdResult(ecode=sub.returncode,
                      stdout=outd.decode() if decode_stdout else outd,
                      stderr=errd.decode()).mapped_for_warning(
                          sanitize_stderr=sanitize_stderr)
Esempio n. 19
0
def do_action_cmd(cmd, success_emask, fail_emask, eflag_modifiers):
    from aipoed.gui import console
    # TODO: improve do_action_cmd() and move to runext
    result = runext.run_cmd_in_console(console.LOG, cmd)
    # Because git uses stderr to report progress etc we should consider
    # a warning a success
    if result.is_less_than_error:
        if success_emask:
            enotify.notify_events(success_emask)
        return result
    else:
        if fail_emask:
            enotify.notify_events(fail_emask)
        eflags = CmdResult.ERROR
        for tgt_string, suggestion in eflag_modifiers:
            if result.stderr.find(tgt_string) != -1:
                eflags |= suggestion
        return CmdResult(eflags, result.stdout, result.stderr)
Esempio n. 20
0
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
Esempio n. 21
0
 def run_cmd(cmd,
             input_text=None,
             sanitize_stderr=None,
             decode_stdout=True):
     if isinstance(cmd, str):
         cmd = shlex.split(cmd)
     if IS_POSIX:
         savedsh = signal.getsignal(signal.SIGPIPE)
         signal.signal(signal.SIGPIPE, signal.SIG_DFL)
     sub = subprocess.Popen(cmd,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            close_fds=IS_POSIX,
                            bufsize=-1)
     outd, errd = sub.communicate(input_text)
     if IS_POSIX:
         signal.signal(signal.SIGPIPE, savedsh)
     return CmdResult(ecode=sub.returncode,
                      stdout=outd.decode() if decode_stdout else outd,
                      stderr=errd.decode()).mapped_for_warning(
                          sanitize_stderr=sanitize_stderr)
Esempio n. 22
0
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)
Esempio n. 23
0
 def _add_extra_patch_file_paths(cls, file_paths):
     patch_file_paths = cls.get_patch_files()
     ep_file_paths_set = {fp for fp in file_paths if fp not in patch_file_paths}
     if ep_file_paths_set:
         return cls.do_add_files(ep_file_paths_set)
     return CmdResult.ok()