def management_diff(self, dict): log_debug(1) self._get_and_validate_session(dict) param_names = ['config_channel_src', 'revision_src', 'path', ] for p in param_names: val = dict.get(p) if val is None: raise rhnFault(4007, "No content sent for `%s'" % p) log_debug(4, "Params sent", dict) path = dict['path'] config_channel_src = dict['config_channel_src'] revision_src = dict.get('revision_src') fsrc = self._get_file_revision(config_channel_src, revision_src, path) config_channel_dst = dict.get('config_channel_dst') if config_channel_dst is None: config_channel_dst = config_channel_src revision_dst = dict.get('revision_dst') fdst = self._get_file_revision(config_channel_dst, revision_dst, path) if fsrc['label'] != fdst['label']: raise rhnFault(4017, "Path %s is a %s in channel %s while it is a %s in channel %s" % (path, fsrc['label'], config_channel_src, fdst['label'], config_channel_dst), explain=0) if fsrc['label'] == 'symlink': if (fsrc["symlink"] != fdst['symlink']) or self.__attributes_differ(fsrc, fdst): (first_row, second_row) = self.__header(path, fsrc, config_channel_src, fdst, config_channel_dst) first_row += ' target: %s' % fsrc["symlink"] second_row += ' target: %s' % fdst["symlink"] return first_row + "\n" + second_row + "\n" return "" diff = difflib.unified_diff( fsrc['file_content'], fdst['file_content'], path, path, fsrc['modified'], fdst['modified'], lineterm='') try: first_row = next(diff) except StopIteration: return "" if not first_row.startswith('---'): # Hmm, weird return first_row + '\n'.join(list(diff)) try: second_row = next(diff) except StopIteration: second_row = '' if not second_row.startswith('+++'): # Hmm, weird return second_row + '\n'.join(list(diff)) (first_row, second_row) = self.__header(path, fsrc, config_channel_src, fdst, config_channel_dst) return first_row + "\n" + second_row + '\n' + '\n'.join(list(diff))
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 "" fromlines = sstr(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 = next(diff_output) second_row = next(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 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 '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' + ''.join(list(diff_output))
def _executemany(self, *args, **kwargs): # cx_Oracle expects the first arg to be the statement if not kwargs: return 0 # Compute number of values max_array_size = 25 i = iter(list(kwargs.values())) firstval = next(i) array_size = len(firstval) if array_size == 0: return 0 chunk_size = min(max_array_size, array_size) pdict = {} for k in kwargs.iterkeys(): pdict[k] = None arr = [] for i in range(chunk_size): arr.append(pdict.copy()) # Now arr is an array of the desired size rowcount = 0 start = 0 while start < array_size: item_count = min(array_size - start, chunk_size) # Trim the array if it is too big if item_count != chunk_size: arr = arr[:item_count] for i in range(item_count): pdict = arr[i] for k, v in kwargs.items(): pdict[k] = to_string(v[start + i]) # We clear self->bindVariables so that list of all nulls # in the previous chunk which caused the type to be set to # string does not affect our chunk which may have number # there. self._real_cursor.setinputsizes(**{}) # arr is now a list of dictionaries. Each dictionary contains the # data for one execution of the query where the key is the column # name and the value self explanatory. self._real_cursor.executemany(None, arr) self.description = self._real_cursor.description rowcount = rowcount + self._real_cursor.rowcount start = start + chunk_size return rowcount
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
def management_diff(self, dict): log_debug(1) self._get_and_validate_session(dict) param_names = [ 'config_channel_src', 'revision_src', 'path', ] for p in param_names: val = dict.get(p) if val is None: raise rhnFault(4007, "No content sent for `%s'" % p) log_debug(4, "Params sent", dict) path = dict['path'] config_channel_src = dict['config_channel_src'] revision_src = dict.get('revision_src') fsrc = self._get_file_revision(config_channel_src, revision_src, path) config_channel_dst = dict.get('config_channel_dst') if config_channel_dst is None: config_channel_dst = config_channel_src revision_dst = dict.get('revision_dst') fdst = self._get_file_revision(config_channel_dst, revision_dst, path) if fsrc['label'] != fdst['label']: raise rhnFault( 4017, "Path %s is a %s in channel %s while it is a %s in channel %s" % (path, fsrc['label'], config_channel_src, fdst['label'], config_channel_dst), explain=0) if fsrc['label'] == 'symlink': if (fsrc["symlink"] != fdst['symlink']) or self.__attributes_differ(fsrc, fdst): (first_row, second_row) = self.__header(path, fsrc, config_channel_src, fdst, config_channel_dst) first_row += ' target: %s' % fsrc["symlink"] second_row += ' target: %s' % fdst["symlink"] return first_row + "\n" + second_row + "\n" return "" diff = difflib.unified_diff(fsrc['file_content'], fdst['file_content'], path, path, fsrc['modified'], fdst['modified'], lineterm='') try: first_row = next(diff) except StopIteration: return "" if not first_row.startswith('---'): # Hmm, weird return first_row + '\n'.join(list(diff)) try: second_row = next(diff) except StopIteration: second_row = '' if not second_row.startswith('+++'): # Hmm, weird return second_row + '\n'.join(list(diff)) (first_row, second_row) = self.__header(path, fsrc, config_channel_src, fdst, config_channel_dst) return first_row + "\n" + second_row + '\n' + '\n'.join(list(diff))
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 "" fromlines = sstr(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 = next(diff_output) second_row = next(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 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 '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' + ''.join( list(diff_output))