1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import sys
17 import os
18
19 import handler_base
20 from config_common.file_utils import diff
21 from config_common.local_config import get as get_config
22
23 -class Handler(handler_base.HandlerBase):
24 _options_table = [
25 handler_base.HandlerBase._option_class(
26 '-d', '--diff-secure-files', dest='display_diff',
27 action="store_true", default=False,
28 help="Force diff to display the diff for secure files.",
29 )
30 ]
31 _usage_options = handler_base.HandlerBase._usage_options + " [ files ... ]"
33 src, dst= args [:2]
34 type = args[3]
35 file_info = args[4]
36
37 if type == 'symlink':
38 if not os.path.exists(dst):
39 print("Symbolic link '%s' is missing" % dst)
40 return
41
42 if not os.path.islink(dst):
43 print("Path '%s' is not a symbolic link" % dst)
44 return
45
46
47 srclink = os.readlink(src)
48 destlink = os.readlink(dst)
49 if srclink != destlink:
50 print("Symbolic links differ. Channel: '%s' -> '%s' System: '%s' -> '%s' " % (dst,srclink, dst, destlink))
51 elif type == 'file':
52 if 'is_binary' in file_info and file_info['is_binary'] == 'Y':
53 src_content = dst_content = None
54 content_differs = False
55 src_file = open(src, 'rb')
56 src_content = src_file.read()
57 src_file.close()
58 if os.access(dst, os.R_OK):
59 dst_file = open(dst, 'rb')
60 dst_content = dst_file.read()
61 dst_file.close()
62 else:
63 print("File %s that is not readable. Re-deployment of configuration file is recommended." % dst)
64 return
65 if len(src_content) != len(dst_content):
66 content_differs = True
67 else:
68 for i in range(len(src_content)):
69 if src_content[i] != dst_content[i]:
70 content_differs = True
71 break
72 if content_differs:
73 sys.stdout.write("Binary file %s differs.\n" % (dst))
74 else:
75 sys.stdout.write(''.join(diff(src, dst, srcname=dst, dstname=dst,
76 display_diff=
77 (self.options.display_diff or get_config('display_diff')))))
78