Example #1
0
    def wrapper(*args, **kwargs):
        level = 'INFO'
        msg = 'Not set'
        exc = None
        msgs = func(*args, **kwargs)

        # maybe there is a function that prints stuff to the terminal in
        # case of failures, BUT return data structures like dicts etc as well.
        # In this case, we need a way to distinguish between
        # both possibilities.
        # So if you have a function with the @log decorator,
        # but in some occasions it has to return data, then wrap this data
        # in a dict like so:
        # return {'payload': my_data}
        # 'payload' is the key to return whatever you want.

        try:
            return msgs['payload']
        except:
            pass

        if not msgs:
            # If a decorated function returns None than do nothing.
            # (That might be expected behavior)
            return

        for msg in msgs:

            # we distinguish between tuples with an 3rd argument
            # (sys.exit() exitcode)
            # and no tuples without an exitcode
            show_lvl = 'INFO'
            if 2 == len(msg):
                level, msg = msg
            elif 3 == len(msg):
                level, msg, exc = msg
                show_lvl = 'WARN'
            else:
                raise ValueError('tuple must contain 2 or 3 elements!')

            if level == 'INFO':
                sys.stdout.write(u'[ {0} ] {1}'.format(show_lvl, color(level, msg)) + '\n')
            else:
                sys.stderr.write(u'[ {0} ] {1}'.format(show_lvl, color(level, msg)) + '\n')

            if exc:
                try:
                    exc = int(exc)
                except:
                    raise(ValueError('Exit code must be an integer!'))

                sys.exit(exc)
Example #2
0
File: git.py Project: rmatulat/nacl
def compare_remote():
    """
    Try to find differences between local and remote repositories

    Compare the existence of remote and local git repositories and show
    missing local repositories.
    """
    local_repo_urls = get_local_url_list()
    remote_url_dict = nacl.gitapi.get_remote_url_dict()

    print(color("FAIL", "WARNING: This list might be inaccurate!\n It will list remote git repositories, that are not in one of our salt environments!\n That might be ok!\n"))

    for url, desc in remote_url_dict.iteritems():
        if url not in local_repo_urls:
                url = color("GREEN", url)
                desc = color("WARNING", desc)
                print("%-59s" % (url))
                print("%-59s\n" % (desc))
Example #3
0
    def __call__(self, *args, **kwargs):
        __ret = self._fn(*args, **kwargs)

        if not __ret:
            return

        if __ret['status'] == 'Clean':
            st_level = 'UNDERLINE'
        else:
            st_level = 'GREEN'

        # colorize merge_status
        if __ret['merge_status'] == '(merged)':
            m_s_level = 'INFO'
        else:
            m_s_level = 'FAIL'

        # colorize pull_push
        if __ret['pull_push'] == "Up-to-date":
            p_p_level = 'GREEN'
        else:
            p_p_level = 'FAIL'

        # sys.stdout.write(
        s = u'{0}{1}{2}{3}{4}{5}\n'.format(
            self.t.move_x(0) + color('WARNING', __ret['dir_name']),
            self.t.move_x(51) + color('GREEN', __ret['branch']),
            self.t.move_x(57) + color(m_s_level, __ret['merge_status']),
            self.t.move_x(67) + color(st_level, __ret['status']),
            self.t.move_x(83) + color(p_p_level, __ret['pull_push']),
            self.t.move_x(99) + color('DARKCYAN', __ret['all_branches'])).encode()

        sys.stdout.write(s.decode('utf-8'))