Ejemplo n.º 1
0
def init():
    import os
    from aipoed import options
    from aipoed import enotify
    orig_dir = os.getcwd()
    options.load_global_options()
    get_ifce()
    if SCM.in_valid_pgnd:
        root = SCM.get_playground_root()
        os.chdir(root)
        from aipoed.scm.gui import wspce
        from aipoed.gui import recollect
        wspce.add_workspace_path(root)
        recollect.set("workspace", "last_used", root)
    from aipoed.pm.gui import ifce as pm_ifce
    pm_ifce.get_ifce()
    curr_dir = os.getcwd()
    options.reload_pgnd_options()
    from aipoed.gui.console import LOG
    LOG.start_cmd("Working Directory: {0}\n".format(curr_dir))
    if SCM.in_valid_pgnd:
        LOG.append_stdout('In valid repository\n')
    else:
        LOG.append_stderr('NOT in valid repository\n')
    LOG.end_cmd()
    # NB: need to send either enotify.E_CHANGE_WD or E_NEW_SCM|E_NEW_PM to ensure action sates get set
    if not os.path.samefile(orig_dir, curr_dir):
        enotify.notify_events(enotify.E_CHANGE_WD, new_wd=curr_dir)
    else:
        from aipoed.scm.events import E_NEW_SCM
        from aipoed.pm.events import E_NEW_PM
        enotify.notify_events(E_NEW_SCM | E_NEW_PM)
    from aipoed.gui import auto_update
    auto_update.set_initialize_event_flags(check_interfaces)
Ejemplo n.º 2
0
def _auto_update_cb():
    DEBUG = False  # set to True to investigate unexpected activity
    invalid_cbs = []
    event_args = {}
    # do any necessary initialization of flags and arguments
    event_flags = initialize_event_flags(event_args)
    if DEBUG: print("AA START:", event_flags)
    for callback in _REGISTERED_CBS:
        try:
            # pass event_flags in to give the client a chance to skip
            # any checks if existing flags would cause them to update anyway
            if DEBUG:
                cb_flags = callback(event_flags, event_args)
                if cb_flags:
                    print("AA FIRE:", cb_flags, callback)
                event_flags |= cb_flags
            else:
                event_flags |= callback(event_flags, event_args)
        except Exception as edata:
            # TODO: try to be more explicit in naming exception type to catch here
            # this is done to catch the race between a caller has going away and deleting its registers
            if True:  # NB: for debug assistance e.g . locating exceptions not due to caller going away
                print("AUTO UPDATE:", edata, callback, event_flags, event_args)
                raise edata
            invalid_cbs.append(callback)
    if DEBUG: print("AA END:", event_flags)
    if event_flags:
        enotify.notify_events(event_flags, **event_args)
    for cb in invalid_cbs:
        deregister_cb(cb)
Ejemplo n.º 3
0
def init_current_dir(backend):
    import os
    from aipoed import enotify
    result = create_new_playground(os.getcwd(), backend)
    events = 0
    curr_scm = SCM
    get_ifce()
    if curr_scm != SCM:
        from aipoed.scm.events import E_NEW_SCM
        events |= E_NEW_SCM
    from aipoed.pm.gui import ifce as pm_ifce
    curr_pm = pm_ifce.PM
    pm_ifce.get_ifce()
    if curr_pm != pm_ifce.PM:
        from aipoed.pm.events import E_NEW_PM
        events |= E_NEW_PM
    if SCM.in_valid_pgnd:
        from aipoed.scm.gui import wspce
        from aipoed.gui import recollect
        curr_dir = os.getcwd()
        wspce.add_workspace_path(curr_dir)
        recollect.set("workspace", "last_used", curr_dir)
    if events:
        enotify.notify_events(events)
    return result
Ejemplo n.º 4
0
 def _change_wd_acb(self, _action=None):
     new_dir_path = self.ask_dir_path("New Directory Path",
                                      suggestion=None,
                                      existing=True)
     if new_dir_path and os.path.isdir(new_dir_path):
         with self.showing_busy():
             result = os.chdir(new_dir_path)
             enotify.notify_events(enotify.E_CHANGE_WD)
             recollect.set(APP_NAME, "last_wd", os.getcwd())
Ejemplo n.º 5
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
Ejemplo n.º 6
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)
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
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()
Ejemplo n.º 10
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)