1 |
commit: 745976728dea89598b910f8308c3b948b9c5750c |
2 |
Author: Alexander Bersenev <bay <AT> hackerdom <DOT> ru> |
3 |
AuthorDate: Sat Sep 24 01:49:14 2011 +0000 |
4 |
Commit: Александр Берсенев <bay <AT> hackerdom <DOT> ru> |
5 |
CommitDate: Sat Sep 24 01:49:14 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/autodep.git;a=commit;h=74597672 |
7 |
|
8 |
unicode fixes |
9 |
|
10 |
--- |
11 |
NOTES | 6 + |
12 |
portage_with_autodep/pym/_emerge/EventsAnalyser.py | 40 ++++++-- |
13 |
portage_with_autodep/pym/_emerge/EventsLogger.py | 102 +++++++++++--------- |
14 |
portage_with_autodep/pym/portage/const.py | 1 + |
15 |
4 files changed, 92 insertions(+), 57 deletions(-) |
16 |
|
17 |
diff --git a/NOTES b/NOTES |
18 |
index 99a214d..fa18c55 100644 |
19 |
--- a/NOTES |
20 |
+++ b/NOTES |
21 |
@@ -31,3 +31,9 @@ This is few notes mainly for myself. |
22 |
} |
23 |
} |
24 |
|
25 |
+5. Names of files is always raw strings(non-unicode) |
26 |
+ |
27 |
+6. Commands to launch custom emerge: |
28 |
+export PATH="/home/bay/autodep/portage_with_autodep/bin:$PATH" |
29 |
+export PYTHONPATH="/home/bay/autodep/portage_with_autodep/pym:$PYTHONPATH" |
30 |
+ |
31 |
|
32 |
diff --git a/portage_with_autodep/pym/_emerge/EventsAnalyser.py b/portage_with_autodep/pym/_emerge/EventsAnalyser.py |
33 |
index 2dc2d35..e6e21be 100644 |
34 |
--- a/portage_with_autodep/pym/_emerge/EventsAnalyser.py |
35 |
+++ b/portage_with_autodep/pym/_emerge/EventsAnalyser.py |
36 |
@@ -209,17 +209,30 @@ class GentoolkitUtils: |
37 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE, |
38 |
bufsize=4096) |
39 |
|
40 |
- out,err=proc.communicate("\n".join(listtocheck).encode("utf8")) |
41 |
+ out,err=proc.communicate(b"\n".join(listtocheck)) |
42 |
|
43 |
- lines=out.decode("utf8").split("\n") |
44 |
+ lines=out.split(b"\n") |
45 |
#print lines |
46 |
line_re=re.compile(r"^([^ ]+)\s+\(([^)]+)\)$") |
47 |
for line in lines: |
48 |
+ try: |
49 |
+ line=line.decode("utf-8") |
50 |
+ except UnicodeDecodeError: |
51 |
+ portage.util.writemsg("Util qfile returned non-utf8 string: %s\n" % line) |
52 |
+ |
53 |
+ #import pdb; pdb.set_trace() |
54 |
+ |
55 |
if len(line)==0: |
56 |
continue |
57 |
match=line_re.match(line) |
58 |
if match: |
59 |
- ret[match.group(2)]=match.group(1) |
60 |
+ try: |
61 |
+ ret[match.group(2).encode("utf-8")]=match.group(1) |
62 |
+ except UnicodeEncodeError: |
63 |
+ portage.util.writemsg( |
64 |
+ "Util qfile failed to encode string %s to unicode\n" % |
65 |
+ match.group(2)) |
66 |
+ |
67 |
else: |
68 |
portage.util.writemsg("Util qfile returned unparsable string: %s\n" % line) |
69 |
|
70 |
@@ -243,7 +256,7 @@ class GentoolkitUtils: |
71 |
|
72 |
out,err=proc.communicate() |
73 |
|
74 |
- ret=out.decode("utf8").split("\n") |
75 |
+ ret=out.split(b"\n") |
76 |
if ret==['']: |
77 |
ret=[] |
78 |
except OSError as e: |
79 |
@@ -265,7 +278,7 @@ class GentoolkitUtils: |
80 |
|
81 |
out,err=proc.communicate() |
82 |
|
83 |
- ret=out.decode("utf8").split("\n") |
84 |
+ ret=out.split(b"\n") |
85 |
except OSError as e: |
86 |
portage.util.writemsg("Error while launching qfile: %s\n" % e) |
87 |
|
88 |
@@ -390,7 +403,8 @@ class EventsAnalyser: |
89 |
stagesorder={"clean":1,"setup":2,"unpack":3,"prepare":4,"configure":5,"compile":6,"test":7, |
90 |
"install":8,"preinst":9,"postinst":10,"prerm":11,"postrm":12,"unknown":13} |
91 |
packagesinfo=self.packagesinfo |
92 |
- # print information grouped by package |
93 |
+ # print information grouped by package |
94 |
+ print(packagesinfo.keys()) |
95 |
for package in sorted(packagesinfo): |
96 |
# not showing special directory package |
97 |
if package=="directory": |
98 |
@@ -474,7 +488,12 @@ class EventsAnalyser: |
99 |
|
100 |
for filename in filenames: |
101 |
event_info=tuple(filenames[filename]) |
102 |
- portage.util.writemsg(" %-56s %-21s\n" % (filename,action[event_info])) |
103 |
+ try: |
104 |
+ portage.util.writemsg( |
105 |
+ " %-56s %-21s\n" % (filename.decode('utf-8'),action[event_info])) |
106 |
+ except UnicodeDecodeError: |
107 |
+ portage.util.writemsg( |
108 |
+ " %-56s %-21s\n" % ('NON-UTF8-FILENAME',action[event_info])) |
109 |
filescounter+=1 |
110 |
if filescounter>10: |
111 |
portage.util.writemsg(" ... and %d more ...\n" % (len(filenames)-10)) |
112 |
@@ -493,7 +512,7 @@ class EventsAnalyser: |
113 |
""" some basic heuristics here to cut part of packages """ |
114 |
|
115 |
excluded_paths=set( |
116 |
- ['/etc/sandbox.d/'] |
117 |
+ [b'/etc/sandbox.d/'] |
118 |
) |
119 |
|
120 |
excluded_packages=set( |
121 |
@@ -524,8 +543,9 @@ class EventsAnalyser: |
122 |
continue |
123 |
|
124 |
# test 1: package is not useful if all files are *.desktop or *.xml or *.m4 |
125 |
- if not (f.endswith(".desktop") or f.endswith(".xml") or f.endswith(".m4") or f.endswith(".pc")): |
126 |
- break |
127 |
+ if not (f.endswith(b".desktop") or f.endswith(b".xml") or |
128 |
+ f.endswith(b".m4") or f.endswith(b".pc")): |
129 |
+ break |
130 |
else: |
131 |
return False # we get here if cycle ends not with break |
132 |
|
133 |
|
134 |
diff --git a/portage_with_autodep/pym/_emerge/EventsLogger.py b/portage_with_autodep/pym/_emerge/EventsLogger.py |
135 |
index 68b3c67..1ade9fd 100644 |
136 |
--- a/portage_with_autodep/pym/_emerge/EventsLogger.py |
137 |
+++ b/portage_with_autodep/pym/_emerge/EventsLogger.py |
138 |
@@ -100,62 +100,69 @@ class EventsLogger(threading.Thread): |
139 |
continue |
140 |
|
141 |
#import pdb; pdb.set_trace() |
142 |
- try: |
143 |
- message=record.decode("utf8").split("\0") |
144 |
- except UnicodeDecodeError: |
145 |
- print("Bad message %s" % record) |
146 |
- continue |
147 |
+ #try: |
148 |
+ message=record.split(b"\0") |
149 |
+ #except UnicodeDecodeError: |
150 |
+ # print("Bad message %s" % record) |
151 |
+ # continue |
152 |
|
153 |
# continue |
154 |
|
155 |
#print(message) |
156 |
|
157 |
try: |
158 |
- if message[4]=="ASKING": |
159 |
- if self.filter_proc(message[1],message[2],message[3]): |
160 |
- s.sendall(b"ALLOW\0") |
161 |
- else: |
162 |
- # TODO: log through portage infrastructure |
163 |
- #print("Blocking an access to %s" % message[2]) |
164 |
- s.sendall(b"DENY\0") |
165 |
+ eventname,filename,stage,result=message[1:5] |
166 |
+ eventname=eventname.decode("utf-8") |
167 |
+ stage=stage.decode("utf-8") |
168 |
+ result=result.decode("utf-8") |
169 |
+ except IndexError: |
170 |
+ print("IndexError while parsing %s" % record) |
171 |
+ except ValueError: |
172 |
+ print("ValueError while parsing %s" % record) |
173 |
+ except UnicodeDecodeError: |
174 |
+ print("UnicodeDecodeError while parsing %s" % record) |
175 |
+ |
176 |
+ if result=="ASKING": |
177 |
+ if self.filter_proc(eventname,filename,stage): |
178 |
+ s.sendall(b"ALLOW\0") |
179 |
else: |
180 |
- eventname,filename,stage,result=message[1:5] |
181 |
- |
182 |
- if not stage in self.events: |
183 |
- self.events[stage]=[{},{}] |
184 |
+ # TODO: log through portage infrastructure |
185 |
+ #print("Blocking an access to %s" % message[2]) |
186 |
+ s.sendall(b"DENY\0") |
187 |
+ else: |
188 |
+ if not stage in self.events: |
189 |
+ self.events[stage]=[{},{}] |
190 |
+ |
191 |
+ hashofsucesses=self.events[stage][0] |
192 |
+ hashoffailures=self.events[stage][1] |
193 |
+ |
194 |
+ if result=="DENIED": |
195 |
+ print("Blocking an access to %s" % filename) |
196 |
+ |
197 |
+ if result=="OK": |
198 |
+ if not filename in hashofsucesses: |
199 |
+ hashofsucesses[filename]=[False,False] |
200 |
+ |
201 |
+ readed_or_writed=hashofsucesses[filename] |
202 |
+ |
203 |
+ if eventname=="read": |
204 |
+ readed_or_writed[0]=True |
205 |
+ elif eventname=="write": |
206 |
+ readed_or_writed[1]=True |
207 |
|
208 |
- hashofsucesses=self.events[stage][0] |
209 |
- hashoffailures=self.events[stage][1] |
210 |
- |
211 |
- if result=="DENIED": |
212 |
- print("Blocking an access to %s" % filename) |
213 |
- |
214 |
- if result=="OK": |
215 |
- if not filename in hashofsucesses: |
216 |
- hashofsucesses[filename]=[False,False] |
217 |
- |
218 |
- readed_or_writed=hashofsucesses[filename] |
219 |
- |
220 |
- if eventname=="read": |
221 |
- readed_or_writed[0]=True |
222 |
- elif eventname=="write": |
223 |
- readed_or_writed[1]=True |
224 |
- |
225 |
- elif result[0:3]=="ERR" or result=="DENIED": |
226 |
- if not filename in hashoffailures: |
227 |
- hashoffailures[filename]=[False,False] |
228 |
- notfound_or_blocked=hashoffailures[filename] |
229 |
- |
230 |
- if result=="ERR/2": |
231 |
- notfound_or_blocked[0]=True |
232 |
- elif result=="DENIED": |
233 |
- notfound_or_blocked[1]=True |
234 |
- |
235 |
- else: |
236 |
- print("Error in logger module<->analyser protocol") |
237 |
+ elif result[0:3]=="ERR" or result=="DENIED": |
238 |
+ if not filename in hashoffailures: |
239 |
+ hashoffailures[filename]=[False,False] |
240 |
+ notfound_or_blocked=hashoffailures[filename] |
241 |
+ |
242 |
+ if result=="ERR/2": |
243 |
+ notfound_or_blocked[0]=True |
244 |
+ elif result=="DENIED": |
245 |
+ notfound_or_blocked[1]=True |
246 |
+ |
247 |
+ else: |
248 |
+ print("Error in logger module<->analyser protocol") |
249 |
|
250 |
- except IndexError: |
251 |
- print("IndexError while parsing %s" % record) |
252 |
except IOError as e: |
253 |
if e.errno!=4: # handling "Interrupted system call" errors |
254 |
raise |
255 |
@@ -177,4 +184,5 @@ class EventsLogger(threading.Thread): |
256 |
|
257 |
# We assume portage clears tmp folder, so no deleting a socket file |
258 |
# We assume that no new socket data will arrive after this moment |
259 |
+ #print(self.events) |
260 |
return self.events |
261 |
|
262 |
diff --git a/portage_with_autodep/pym/portage/const.py b/portage_with_autodep/pym/portage/const.py |
263 |
index f34398d..2a391db 100644 |
264 |
--- a/portage_with_autodep/pym/portage/const.py |
265 |
+++ b/portage_with_autodep/pym/portage/const.py |
266 |
@@ -68,6 +68,7 @@ BASH_BINARY = "/bin/bash" |
267 |
MOVE_BINARY = "/bin/mv" |
268 |
PRELINK_BINARY = "/usr/sbin/prelink" |
269 |
AUTODEP_LIBRARY = "/usr/lib/file_hook.so" |
270 |
+#AUTODEP_LIBRARY = "/home/bay/autodep/src/hook_lib/file_hook.so" |
271 |
|
272 |
|
273 |
INVALID_ENV_FILE = "/etc/spork/is/not/valid/profile.env" |