def _process_file(self, *args): src, dst = args[:2] type = args[3] if type == 'symlink': if not os.path.exists(dst): print("Symbolic link '%s' is missing" % dst) return if not os.path.islink(dst): print("Path '%s' is not a symbolic link" % dst) return #dst is a symlink, so just tell the user we're skipping the entry srclink = os.readlink(src) destlink = os.readlink(dst) if srclink != destlink: print( "Symbolic links differ. Channel: '%s' -> '%s' System: '%s' -> '%s' " % (dst, srclink, dst, destlink)) elif type == 'file': sys.stdout.write(''.join( diff(src, dst, srcname=dst, dstname=dst, display_diff=(self.options.display_diff or get_config('display_diff')))))
def _process_file(self, *args): src, dst = args[:2] type = args[3] file_info = args[4] if type == 'symlink': if not os.path.exists(dst): print("Symbolic link '%s' is missing" % dst) return if not os.path.islink(dst): print("Path '%s' is not a symbolic link" % dst) return #dst is a symlink, so just tell the user we're skipping the entry srclink = os.readlink(src) destlink = os.readlink(dst) if srclink != destlink: print( "Symbolic links differ. Channel: '%s' -> '%s' System: '%s' -> '%s' " % (dst, srclink, dst, destlink)) elif type == 'file': if 'is_binary' in file_info and file_info['is_binary'] == 'Y': src_content = dst_content = None content_differs = False src_file = open(src, 'rb') src_content = src_file.read() src_file.close() if os.access(dst, os.R_OK): dst_file = open(dst, 'rb') dst_content = dst_file.read() dst_file.close() if not dst_content or len(src_content) != len(dst_content): content_differs = True else: for i in range(len(src_content)): if src_content[i] != dst_content[i]: content_differs = True break if content_differs: sys.stdout.write("Binary file content differs.\n") else: sys.stdout.write(''.join( diff(src, dst, srcname=dst, dstname=dst, display_diff=(self.options.display_diff or get_config('display_diff')))))
def _process_file(self, *args): src, dst= args [:2] type = args[3] file_info = args[4] if type == 'symlink': if not os.path.exists(dst): print("Symbolic link '%s' is missing" % dst) return if not os.path.islink(dst): print("Path '%s' is not a symbolic link" % dst) return #dst is a symlink, so just tell the user we're skipping the entry srclink = os.readlink(src) destlink = os.readlink(dst) if srclink != destlink: print("Symbolic links differ. Channel: '%s' -> '%s' System: '%s' -> '%s' " % (dst,srclink, dst, destlink)) elif type == 'file': if 'is_binary' in file_info and file_info['is_binary'] == 'Y': src_content = dst_content = None content_differs = False src_file = open(src, 'rb') src_content = src_file.read() src_file.close() if os.access(dst, os.R_OK): dst_file = open(dst, 'rb') dst_content = dst_file.read() dst_file.close() else: print("File %s that is not readable. Re-deployment of configuration file is recommended." % dst) return if len(src_content) != len(dst_content): content_differs = True else: for i in range(len(src_content)): if src_content[i] != dst_content[i]: content_differs = True break if content_differs: sys.stdout.write("Binary file %s differs.\n" % (dst)) else: sys.stdout.write(''.join(diff(src, dst, srcname=dst, dstname=dst, display_diff= (self.options.display_diff or get_config('display_diff')))))
def _process_file(self, *args): src, dst= args [:2] type = args[3] if type == 'symlink': if not os.path.exists(dst): print("Symbolic link '%s' is missing" % dst) return if not os.path.islink(dst): print("Path '%s' is not a symbolic link" % dst) return #dst is a symlink, so just tell the user we're skipping the entry srclink = os.readlink(src) destlink = os.readlink(dst) if srclink != destlink: print("Symbolic links differ. Channel: '%s' -> '%s' System: '%s' -> '%s' " % (dst,srclink, dst, destlink)) elif type == 'file': sys.stdout.write(''.join(diff(src, dst, srcname=dst, dstname=dst, display_diff= (self.options.display_diff or get_config('display_diff')))))
def diff(self, file_struct): self._validate_struct(file_struct) temp_file, temp_dirs = self.process(file_struct) path = file_struct['path'] sectx_result = '' owner_result = '' group_result = '' perm_result = '' result = '' stat_err = 0 try: cur_stat = os.lstat(path) except: stat_err = 1 if file_struct['filetype'] != 'symlink': if not stat_err: #check for owner differences cur_uid = cur_stat[stat.ST_UID] try: cur_user = pwd.getpwuid(cur_uid)[0] except KeyError: #Orphan UID with no name,return unknown cur_user = "******" % (cur_uid, ) else: cur_user = "******" if cur_user == file_struct['username']: owner_result = "" else: owner_result = "User name differ: actual: [%s], expected: [%s]\n" % ( cur_user, file_struct['username']) if not stat_err: #check for group differences cur_gid = cur_stat[stat.ST_GID] try: cur_group = grp.getgrgid(cur_gid)[0] except KeyError: #Orphan GID with no name,return unknown cur_group = "unknown(GID %d)" % (cur_gid, ) else: cur_group = "missing" if cur_group == file_struct['groupname']: group_result = "" else: group_result = "Group name differ: actual: [%s], expected: [%s]\n" % ( cur_group, file_struct['groupname']) #check for permissions differences if not stat_err: cur_perm = str(oct(stat.S_IMODE(cur_stat[stat.ST_MODE]))) else: cur_perm = "missing" #rip off the leading '0' from the mode returned by stat() if cur_perm[0] == '0': cur_perm = cur_perm[1:] #perm_status gets displayed with the verbose option. if cur_perm == str(file_struct['filemode']): perm_result = "" else: perm_result = "File mode differ: actual: [%s], expected: [%s]\n" % ( cur_perm, file_struct['filemode']) try: cur_sectx = lgetfilecon(path)[1] except OSError: # workarounding BZ 690238 cur_sectx = None if cur_sectx == None: cur_sectx = '' if 'selinux_ctx' in file_struct and file_struct['selinux_ctx']: if cur_sectx != file_struct['selinux_ctx']: sectx_result = "SELinux contexts differ: actual: [%s], expected: [%s]\n" % ( cur_sectx, file_struct['selinux_ctx']) if file_struct['filetype'] == 'directory': if os.path.isdir(file_struct['path']): result = '' else: result = "Deployed directory is no longer a directory!" elif file_struct['filetype'] == 'symlink': try: curlink = os.readlink(path) newlink = os.readlink(temp_file) if curlink == newlink: result = '' else: result = "Link targets differ for [%s]: actual: [%s], expected: [%s]\n" % ( path, curlink, newlink) except OSError: e = sys.exc_info()[1] if e.errno == 22: result = "Deployed symlink is no longer a symlink!" else: raise e else: result = ''.join( diff(temp_file, path, display_diff=get_config('display_diff'))) if temp_file: os.unlink(temp_file) return owner_result + group_result + perm_result + sectx_result + result
def diff(self, file_struct): self._validate_struct(file_struct) temp_file, temp_dirs = self.process(file_struct) path = file_struct['path'] sectx_result = '' owner_result = '' group_result = '' perm_result = '' result = '' stat_err = 0 try: cur_stat = os.lstat(path) except: stat_err = 1 if file_struct['filetype'] != 'symlink': if not stat_err: #check for owner differences cur_uid = cur_stat[stat.ST_UID] try: cur_user = pwd.getpwuid(cur_uid)[0] except KeyError: #Orphan UID with no name,return unknown cur_user = "******" % (cur_uid,) else: cur_user = "******" if cur_user == file_struct['username']: owner_result = "" else: owner_result = "User name differ: actual: [%s], expected: [%s]\n" % (cur_user, file_struct['username']) if not stat_err: #check for group differences cur_gid = cur_stat[stat.ST_GID] try: cur_group = grp.getgrgid(cur_gid)[0] except KeyError: #Orphan GID with no name,return unknown cur_group = "unknown(GID %d)" % (cur_gid,) else: cur_group = "missing" if cur_group == file_struct['groupname']: group_result = "" else: group_result = "Group name differ: actual: [%s], expected: [%s]\n" % (cur_group, file_struct['groupname']) #check for permissions differences if not stat_err: cur_perm = format(stat.S_IMODE(cur_stat[stat.ST_MODE]), 'o') else: cur_perm = "missing" #rip off the leading '0' from the mode returned by stat() if cur_perm[0] == '0': cur_perm = cur_perm[1:] #perm_status gets displayed with the verbose option. if cur_perm == str(file_struct['filemode']): perm_result = "" else: perm_result = "File mode differ: actual: [%s], expected: [%s]\n" % (cur_perm, file_struct['filemode']) try: cur_sectx = lgetfilecon(path)[1] except OSError: # workarounding BZ 690238 cur_sectx = None if cur_sectx == None: cur_sectx = '' if 'selinux_ctx' in file_struct and file_struct['selinux_ctx']: if cur_sectx != file_struct['selinux_ctx']: sectx_result = "SELinux contexts differ: actual: [%s], expected: [%s]\n" % (cur_sectx, file_struct['selinux_ctx']) if file_struct['filetype'] == 'directory': if os.path.isdir(file_struct['path']): result = '' else: result = "Deployed directory is no longer a directory!" elif file_struct['filetype'] == 'symlink': try: curlink = os.readlink(path) newlink = os.readlink(temp_file) if curlink == newlink: result = '' else: result = "Link targets differ for [%s]: actual: [%s], expected: [%s]\n" % (path, curlink, newlink) except OSError: e = sys.exc_info()[1] if e.errno == 22: result = "Deployed symlink is no longer a symlink!" else: raise e else: result = ''.join(diff(temp_file, path, display_diff=get_config('display_diff'), is_binary=False if file_struct['is_binary'] == 'N' else True)) if temp_file: os.unlink(temp_file) return owner_result + group_result + perm_result + sectx_result + result
try: curlink = os.readlink(path) newlink = os.readlink(temp_file) if curlink == newlink: result = '' else: result = "Link targets differ for [%s]: actual: [%s], expected: [%s]\n" % ( path, curlink, newlink) except OSError, e: if e.errno == 22: result = "Deployed symlink is no longer a symlink!" else: raise e else: result = ''.join( diff(temp_file, path, display_diff=get_config('display_diff'))) if temp_file: os.unlink(temp_file) return owner_result + group_result + perm_result + sectx_result + result def _validate_struct(self, file_struct): for k in self.file_struct_fields.keys(): if not file_struct.has_key(k): # XXX raise Exception, "Missing key %s" % k def diff(src, dst, srcname=None, dstname=None, display_diff=False): def f_content(path, name): statinfo = None
elif file_struct['filetype'] == 'symlink': try: curlink = os.readlink(path) newlink = os.readlink(temp_file) if curlink == newlink: result = '' else: result = "Link targets differ for [%s]: actual: [%s], expected: [%s]\n" % (path, curlink, newlink) except OSError, e: if e.errno == 22: result = "Deployed symlink is no longer a symlink!" else: raise e else: result = ''.join(diff(temp_file, path, display_diff=get_config('display_diff'))) if temp_file: os.unlink(temp_file) return owner_result + group_result + perm_result + sectx_result + result def _validate_struct(self, file_struct): for k in self.file_struct_fields.keys(): if not file_struct.has_key(k): # XXX raise Exception, "Missing key %s" % k def diff(src, dst, srcname=None, dstname=None, display_diff=False): def f_content(path, name): statinfo = None