Beispiel #1
0
    def run(self):
        log_debug(2)
        r = self.repository
        files = r.list_files()

        if not files:
            die(1, "No managed files.")

        label = "Config Channel"
        maxlen = max(map(lambda s: len(s[0]), files))
        maxlen = max(maxlen, len(label)) + 2
        print "%-10s %8s %-8s %10s %+3s    %*s    %s" % ('Mode', 'Owner', 'Group', 'Size', 'Rev', maxlen, label, "File")

        for file in files:

            # Get the file info
            finfo = r.get_file_info(file[1])[1]
            # Get the file length
            if finfo['encoding'] == 'base64':
                fsize = len(base64.decodestring(finfo['file_contents']))
            else:
                # * indicates raw 'unencoded' size
                fsize = '*' + str(len(finfo['file_contents']))

            if finfo['filetype'] == 'symlink':
                permstr = ostr_to_sym('777', finfo['filetype'])
                dest = "%s -> %s" % (file[1], finfo['symlink'])
                fsize = str(len(finfo['symlink']))
                finfo['username'] = '******'
                finfo['groupname'] = 'root'
            else:
                permstr = ostr_to_sym(finfo['filemode'], finfo['filetype']) or ''
                dest = file[1]
            print "%10s %8s %-8s %10s %+3s    %*s    %s" % (permstr, finfo['username'], finfo['groupname'], fsize, finfo['revision'], maxlen, file[0], dest)
Beispiel #2
0
    def run(self):
        log_debug(2)
        r = self.repository
        files = r.list_files()

        if not files:
            die(1, "No managed files.")

        label = "Config Channel"
        maxlen = max(map(lambda s: len(s[0]), files))
        maxlen = max(maxlen, len(label)) + 2
        print "%-10s %8s %-8s %10s %+3s    %*s    %s" % (
            'Mode', 'Owner', 'Group', 'Size', 'Rev', maxlen, label, "File")

        arg_files = []
        if len(sys.argv) > 2:
            arg_files = sys.argv[2:len(sys.argv)]

        for file in files:

            if len(arg_files) and not file[1] in arg_files:
                continue

            # Get the file info
            finfo = r.get_file_info(file[1])[1]
            # Get the file length
            if finfo['encoding'] == 'base64':
                fsize = len(base64.decodestring(finfo['file_contents']))
            else:
                # * indicates raw 'unencoded' size
                fsize = '*' + str(len(finfo['file_contents']))

            if finfo['filetype'] == 'symlink':
                permstr = ostr_to_sym('777', finfo['filetype'])
                dest = "%s -> %s" % (file[1], finfo['symlink'])
                fsize = str(len(finfo['symlink']))
                finfo['username'] = '******'
                finfo['groupname'] = 'root'
            else:
                permstr = ostr_to_sym(finfo['filemode'],
                                      finfo['filetype']) or ''
                dest = file[1]
            print "%10s %8s %-8s %10s %+3s    %*s    %s" % (
                permstr, finfo['username'], finfo['groupname'], fsize,
                finfo['revision'], maxlen, file[0], dest)
Beispiel #3
0
 def diff_file(self, channel, path, local_file, revision):
     r = self.repository
     try:
         info = r.get_raw_file_info(channel, path, revision)
         if info.has_key('encoding') and info['file_contents']:
             if info['encoding'] == 'base64':
                 info['file_contents'] = base64.decodestring(info['file_contents'])
             else:
                 die(9, 'Error: unknown encoding %s' % info['encoding'])
     except cfg_exceptions.RepositoryFileMissingError:
         die(2, "Error: no such file %s (revision %s) in config channel %s"
             % (path, revision, channel))
     if os.path.islink(local_file) and info['filetype'] != 'symlink' :
         die(8, "Cannot diff %s; the file on the system is a symbolic link while the file in the channel is not. " % local_file)
     if  info['filetype'] == 'symlink' and not os.path.islink(local_file) :
         die(8, "Cannot diff %s; the file on the system is not a symbolic link while the file in the channel is. " % local_file)
     if info['filetype'] == 'symlink':
         src_link = info['symlink']
         dest_link = os.readlink(local_file)
         if src_link != os.readlink(local_file):
             return "Symbolic links differ. Channel: '%s' -> '%s'   System: '%s' -> '%s' \n " % (path,src_link, path, dest_link)
         return ""
     fromlines = info['file_contents'].splitlines(1)
     tolines = open(local_file, 'r').readlines()
     diff_output = difflib.unified_diff(fromlines, tolines, info['path'], local_file)
     first_row = second_row = ''
     try:
         first_row = diff_output.next()
         second_row = diff_output.next()
     except StopIteration:
         pass
     file_stat = os.lstat(local_file)
     local_info = r.make_stat_info(local_file, file_stat)
     # rhel4 do not support selinux
     if not 'selinux_ctx' in local_info:
         local_info['selinux_ctx'] = ''
     if 'selinux_ctx' not in info:
         info['selinux_ctx'] = ''
     if not first_row and not self.__attributes_differ(info, local_info):
         return ""
     else:
         template = "--- %s\t%s\tattributes: %s %s %s %s\tconfig channel: %s\trevision: %s"
         if not info.has_key('modified'):
             info['modified'] = ''
         first_row = template % (path, str(info['modified']), ostr_to_sym(info['filemode'], info['filetype']),
                     info['username'], info['groupname'], info['selinux_ctx'], channel,
                     info['revision'],
         )
         second_row = template % (local_file, f_date(datetime.fromtimestamp(local_info['mtime'])), ostr_to_sym(local_info['mode'], 'file'),
                     local_info['user'], local_info['group'], local_info['selinux_ctx'], 'local file', None
         )
     return first_row + '\n' + second_row + '\n' + ''.join(list(diff_output))
Beispiel #4
0
 def diff_file(self, channel, path, local_file, revision):
     r = self.repository
     try:
         info = r.get_raw_file_info(channel, path, revision)
         if info.has_key('encoding') and info['file_contents']:
             if info['encoding'] == 'base64':
                 info['file_contents'] = base64.decodestring(info['file_contents'])
             else:
                 die(9, 'Error: unknown encoding %s' % info['encoding'])
     except cfg_exceptions.RepositoryFileMissingError:
         die(2, "Error: no such file %s (revision %s) in config channel %s"
             % (path, revision, channel))
     if os.path.islink(local_file) and info['filetype'] != 'symlink' :
         die(8, "Cannot diff %s; the file on the system is a symbolic link while the file in the channel is not. " % local_file)
     if  info['filetype'] == 'symlink' and not os.path.islink(local_file) :
         die(8, "Cannot diff %s; the file on the system is not a symbolic link while the file in the channel is. " % local_file)
     if info['filetype'] == 'symlink':
         src_link = info['symlink']
         dest_link = os.readlink(local_file)
         if src_link != os.readlink(local_file):
             return "Symbolic links differ. Channel: '%s' -> '%s'   System: '%s' -> '%s' \n " % (path,src_link, path, dest_link)
         return ""
     fromlines = info['file_contents'].splitlines(1)
     tolines = open(local_file, 'r').readlines()
     diff_output = difflib.unified_diff(fromlines, tolines, info['path'], local_file)
     first_row = second_row = ''
     try:
         first_row = diff_output.next()
         second_row = diff_output.next()
     except StopIteration:
         pass
     file_stat = os.lstat(local_file)
     local_info = r.make_stat_info(local_file, file_stat)
     # rhel4 do not support selinux
     if not 'selinux_ctx' in local_info:
         local_info['selinux_ctx'] = ''
     if 'selinux_ctx' not in info:
         info['selinux_ctx'] = ''
     if not first_row and not self.__attributes_differ(info, local_info):
         return ""
     else:
         template = "--- %s\t%s\tattributes: %s %s %s %s\tconfig channel: %s\trevision: %s"
         if not info.has_key('modified'):
             info['modified'] = ''
         first_row = template % (path, str(info['modified']), ostr_to_sym(info['filemode'], info['filetype']),
                     info['username'], info['groupname'], info['selinux_ctx'], channel,
                     info['revision'],
         )
         second_row = template % (local_file, f_date(datetime.fromtimestamp(local_info['mtime'])), ostr_to_sym(local_info['mode'], 'file'),
                     local_info['user'], local_info['group'], local_info['selinux_ctx'], 'local file', None
         )
     return first_row + '\n' + second_row + '\n' + ''.join(list(diff_output))
Beispiel #5
0
    def diff_file(self, channel, path, local_file, revision):
        r = self.repository
        try:
            info = r.get_raw_file_info(channel, path, revision)
            if 'encoding' in info and info['file_contents']:
                if info['encoding'] == 'base64':
                    info['file_contents'] = base64.decodestring(bstr(info['file_contents']))
                else:
                    die(9, 'Error: unknown encoding %s' % info['encoding'])
        except cfg_exceptions.RepositoryFileMissingError:
            die(2, "Error: no such file %s (revision %s) in config channel %s"
                % (path, revision, channel))
        if os.path.islink(local_file) and info['filetype'] != 'symlink' :
            die(8, "Cannot diff %s; the file on the system is a symbolic link while the file in the channel is not. " % local_file)
        if  info['filetype'] == 'symlink' and not os.path.islink(local_file) :
            die(8, "Cannot diff %s; the file on the system is not a symbolic link while the file in the channel is. " % local_file)
        if info['filetype'] == 'symlink':
            src_link = info['symlink']
            dest_link = os.readlink(local_file)
            if src_link != os.readlink(local_file):
                return "Symbolic links differ. Channel: '%s' -> '%s'   System: '%s' -> '%s' \n " % (path,src_link, path, dest_link)
            return ""

        response_output = ""
        content_differs = False
        if 'is_binary' in info and info['is_binary'] == 'Y':
            from_content = info['file_contents']
            to_file = open(local_file, 'rb')
            to_content = to_file.read()
            to_file.close()
            if len(from_content) != len(to_content):
                content_differs = True
            else:
                for i in range(len(from_content)):
                    if from_content[i] != to_content[i]:
                         content_differs = True
                         break
            if content_differs:
                response_output = "Binary file content differs\n"
        else:
            fromlines = sstr(info['file_contents']).splitlines(1)
            tofile = open(local_file, 'r')
            tolines = tofile.readlines()
            tofile.close()
            diff_output = difflib.unified_diff(fromlines, tolines, info['path'], local_file)
            first_row = second_row = ''
            try:
                first_row = next(diff_output)
                # if content was same, exception thrown so following
                # lines don't execute
                content_differs = True
                second_row = next(diff_output)
                response_output = ''.join(list(diff_output))
            except StopIteration:
                pass

        file_stat = os.lstat(local_file)
        local_info = r.make_stat_info(local_file, file_stat)
        # rhel4 do not support selinux
        if not 'selinux_ctx' in local_info:
            local_info['selinux_ctx'] = ''
        if 'selinux_ctx' not in info:
            info['selinux_ctx'] = ''
        if not content_differs and not self.__attributes_differ(info, local_info):
            return ""
        else:
            template = "%s %s\t%s\tattributes: %s %s %s %s\tconfig channel: %s\trevision: %s"
            if 'modified' not in info:
                info['modified'] = ''
            first_row = template % ('---', path, str(info['modified']), ostr_to_sym(info['filemode'], info['filetype']),
                        info['username'], info['groupname'], info['selinux_ctx'], channel,
                        info['revision'],
            )
            second_row = template % ('+++', local_file, f_date(datetime.fromtimestamp(local_info['mtime'])), ostr_to_sym(local_info['mode'], 'file'),
                        local_info['user'], local_info['group'], local_info['selinux_ctx'], 'local file', None
            )
        return first_row + '\n' + second_row + '\n' + response_output
Beispiel #6
0
 def diff_file(self, channel, path, local_file, revision):
     r = self.repository
     try:
         info = r.get_raw_file_info(channel, path, revision)
         if info.has_key("encoding") and info["file_contents"]:
             if info["encoding"] == "base64":
                 info["file_contents"] = base64.decodestring(info["file_contents"])
             else:
                 die(9, "Error: unknown encoding %s" % info["encoding"])
     except cfg_exceptions.RepositoryFileMissingError:
         die(2, "Error: no such file %s (revision %s) in config channel %s" % (path, revision, channel))
     if os.path.islink(local_file) and info["filetype"] != "symlink":
         die(
             8,
             "Cannot diff %s; the file on the system is a symbolic link while the file in the channel is not. "
             % local_file,
         )
     if info["filetype"] == "symlink" and not os.path.islink(local_file):
         die(
             8,
             "Cannot diff %s; the file on the system is not a symbolic link while the file in the channel is. "
             % local_file,
         )
     if info["filetype"] == "symlink":
         src_link = info["symlink"]
         dest_link = os.readlink(local_file)
         if src_link != os.readlink(local_file):
             return "Symbolic links differ. Channel: '%s' -> '%s'   System: '%s' -> '%s' \n " % (
                 path,
                 src_link,
                 path,
                 dest_link,
             )
         return ""
     fromlines = info["file_contents"].splitlines(1)
     tolines = open(local_file, "r").readlines()
     diff_output = difflib.unified_diff(fromlines, tolines, info["path"], local_file)
     first_row = second_row = ""
     try:
         first_row = diff_output.next()
         second_row = diff_output.next()
     except StopIteration:
         pass
     file_stat = os.lstat(local_file)
     local_info = r.make_stat_info(local_file, file_stat)
     # rhel4 do not support selinux
     if not "selinux_ctx" in local_info:
         local_info["selinux_ctx"] = ""
     if "selinux_ctx" not in info:
         info["selinux_ctx"] = ""
     if not first_row and not self.__attributes_differ(info, local_info):
         return ""
     else:
         template = "--- %s\t%s\tattributes: %s %s %s %s\tconfig channel: %s\trevision: %s"
         if not info.has_key("modified"):
             info["modified"] = ""
         first_row = template % (
             path,
             str(info["modified"]),
             ostr_to_sym(info["filemode"], info["filetype"]),
             info["username"],
             info["groupname"],
             info["selinux_ctx"],
             channel,
             info["revision"],
         )
         second_row = template % (
             local_file,
             f_date(datetime.fromtimestamp(local_info["mtime"])),
             ostr_to_sym(local_info["mode"], "file"),
             local_info["user"],
             local_info["group"],
             local_info["selinux_ctx"],
             "local file",
             None,
         )
     return first_row + "\n" + second_row + "\n" + "".join(list(diff_output))
Beispiel #7
0
    def diff_file(self, channel, path, local_file, revision):
        r = self.repository
        try:
            info = r.get_raw_file_info(channel, path, revision)
            if 'encoding' in info and info['file_contents']:
                if info['encoding'] == 'base64':
                    info['file_contents'] = base64.decodestring(bstr(info['file_contents']))
                else:
                    die(9, 'Error: unknown encoding %s' % info['encoding'])
        except cfg_exceptions.RepositoryFileMissingError:
            die(2, "Error: no such file %s (revision %s) in config channel %s"
                % (path, revision, channel))
        if os.path.islink(local_file) and info['filetype'] != 'symlink' :
            die(8, "Cannot diff %s; the file on the system is a symbolic link while the file in the channel is not. " % local_file)
        if  info['filetype'] == 'symlink' and not os.path.islink(local_file) :
            die(8, "Cannot diff %s; the file on the system is not a symbolic link while the file in the channel is. " % local_file)
        if info['filetype'] == 'symlink':
            src_link = info['symlink']
            dest_link = os.readlink(local_file)
            if src_link != os.readlink(local_file):
                return "Symbolic links differ. Channel: '%s' -> '%s'   System: '%s' -> '%s' \n " % (path,src_link, path, dest_link)
            return ""

        response_output = ""
        content_differs = False
        if 'is_binary' in info and info['is_binary'] == 'Y':
            from_content = info['file_contents']
            to_file = open(local_file, 'rb')
            to_content = to_file.read()
            to_file.close()
            if len(from_content) != len(to_content):
                content_differs = True
            else:
                for i in range(len(from_content)):
                    if from_content[i] != to_content[i]:
                         content_differs = True
                         break
            if content_differs:
                response_output = "Binary file content differs\n"
        else:
            fromlines = sstr(info['file_contents']).splitlines(1)
            tofile = open(local_file, 'r')
            tolines = tofile.readlines()
            tofile.close()
            diff_output = difflib.unified_diff(fromlines, tolines, info['path'], local_file)
            first_row = second_row = ''
            try:
                first_row = next(diff_output)
                # if content was same, exception thrown so following
                # lines don't execute
                content_differs = True
                second_row = next(diff_output)
                response_output = ''.join(list(diff_output))
            except StopIteration:
                pass

        file_stat = os.lstat(local_file)
        local_info = r.make_stat_info(local_file, file_stat)
        # rhel4 do not support selinux
        if not 'selinux_ctx' in local_info:
            local_info['selinux_ctx'] = ''
        if 'selinux_ctx' not in info:
            info['selinux_ctx'] = ''
        if not content_differs and not self.__attributes_differ(info, local_info):
            return ""
        else:
            template = "%s %s\t%s\tattributes: %s %s %s %s\tconfig channel: %s\trevision: %s"
            if 'modified' not in info:
                info['modified'] = ''
            first_row = template % ('---', path, str(info['modified']), ostr_to_sym(info['filemode'], info['filetype']),
                        info['username'], info['groupname'], info['selinux_ctx'], channel,
                        info['revision'],
            )
            second_row = template % ('+++', local_file, f_date(datetime.fromtimestamp(local_info['mtime'])), ostr_to_sym(local_info['mode'], 'file'),
                        local_info['user'], local_info['group'], local_info['selinux_ctx'], 'local file', None
            )
        return first_row + '\n' + second_row + '\n' + response_output