1 |
commit: c9a117bebeb04efcb731e47a12e79c4c8d065896 |
2 |
Author: Paul Varner <fuzzyray <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Nov 24 19:07:33 2015 +0000 |
4 |
Commit: Paul Varner <fuzzyray <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Nov 24 19:07:33 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/gentoolkit.git/commit/?id=c9a117be |
7 |
|
8 |
Change all open() calls to use Unicode. |
9 |
|
10 |
We are using the following import from portage: |
11 |
from portage import _encodings, _unicode_decode, _unicode_encode |
12 |
|
13 |
A generalized call using the definitions from portage looks like: |
14 |
with open(_unicode_encode(path), encoding=_encodings['fs']) as open_file |
15 |
|
16 |
The portage code has been in place since 2013 and using the definitions |
17 |
from portage ensures we maintain compatibility if portage changes. |
18 |
|
19 |
All portage versions in the tree contain the above code. |
20 |
|
21 |
pym/gentoolkit/deprecated/helpers.py | 4 +++- |
22 |
pym/gentoolkit/eclean/exclude.py | 4 +++- |
23 |
pym/gentoolkit/enalyze/rebuild.py | 4 ++-- |
24 |
pym/gentoolkit/equery/uses.py | 5 +++-- |
25 |
pym/gentoolkit/equery/which.py | 4 +++- |
26 |
pym/gentoolkit/eshowkw/keywords_header.py | 5 +++-- |
27 |
pym/gentoolkit/glsa/__init__.py | 5 ++++- |
28 |
pym/gentoolkit/helpers.py | 8 ++++---- |
29 |
pym/gentoolkit/revdep_rebuild/analyse.py | 3 ++- |
30 |
pym/gentoolkit/revdep_rebuild/cache.py | 12 ++++++++---- |
31 |
pym/gentoolkit/revdep_rebuild/collect.py | 8 +++++--- |
32 |
pym/gentoolkit/revdep_rebuild/settings.py | 4 +++- |
33 |
12 files changed, 43 insertions(+), 23 deletions(-) |
34 |
|
35 |
diff --git a/pym/gentoolkit/deprecated/helpers.py b/pym/gentoolkit/deprecated/helpers.py |
36 |
index 59d6a2c..c3a72dc 100644 |
37 |
--- a/pym/gentoolkit/deprecated/helpers.py |
38 |
+++ b/pym/gentoolkit/deprecated/helpers.py |
39 |
@@ -12,6 +12,7 @@ from __future__ import print_function |
40 |
import warnings |
41 |
|
42 |
import portage |
43 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
44 |
from gentoolkit import * |
45 |
from package import * |
46 |
from pprinter import warn |
47 |
@@ -99,7 +100,8 @@ def find_system_packages(prefilter=None): |
48 |
def find_world_packages(prefilter=None): |
49 |
"""Returns a tuple of lists, first list is resolved world packages, |
50 |
seond is unresolved package names.""" |
51 |
- f = open(portage.root+portage.WORLD_FILE) |
52 |
+ f = open(_unicode_encode(portage.root+portage.WORLD_FILE), |
53 |
+ encoding=_encodings['fs']) |
54 |
pkglist = f.readlines() |
55 |
resolved = [] |
56 |
unresolved = [] |
57 |
|
58 |
diff --git a/pym/gentoolkit/eclean/exclude.py b/pym/gentoolkit/eclean/exclude.py |
59 |
index a6422d0..5a13186 100644 |
60 |
--- a/pym/gentoolkit/eclean/exclude.py |
61 |
+++ b/pym/gentoolkit/eclean/exclude.py |
62 |
@@ -11,6 +11,7 @@ import os |
63 |
import sys |
64 |
import re |
65 |
import portage |
66 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
67 |
|
68 |
from gentoolkit.pprinter import warn |
69 |
|
70 |
@@ -81,7 +82,8 @@ def parseExcludeFile(filepath, output): |
71 |
} |
72 |
output("Parsing Exclude file: " + filepath) |
73 |
try: |
74 |
- file_ = open(filepath,"r") |
75 |
+ file_ = open(_unicode_encode(filepath), |
76 |
+ encoding=_encodings['fs'], mode="r") |
77 |
except IOError: |
78 |
raise ParseExcludeFileException("Could not open exclusion file: " + |
79 |
filepath) |
80 |
|
81 |
diff --git a/pym/gentoolkit/enalyze/rebuild.py b/pym/gentoolkit/enalyze/rebuild.py |
82 |
index 778fed4..3f9527a 100644 |
83 |
--- a/pym/gentoolkit/enalyze/rebuild.py |
84 |
+++ b/pym/gentoolkit/enalyze/rebuild.py |
85 |
@@ -27,7 +27,7 @@ from gentoolkit.atom import Atom |
86 |
|
87 |
|
88 |
import portage |
89 |
- |
90 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
91 |
|
92 |
def cpv_all_diff_use( |
93 |
cpvs=None, |
94 |
@@ -352,7 +352,7 @@ class Rebuild(ModuleBase): |
95 |
""" |
96 |
if not self.options["quiet"]: |
97 |
print(' - Saving file: %s' %filepath) |
98 |
- with open(filepath, "w") as output: |
99 |
+ with open(_unicode_encode(filepath), encoding=_encodings['fs'], mode="w") as output: |
100 |
output.write('\n'.join(data)) |
101 |
print(" - Done") |
102 |
|
103 |
|
104 |
diff --git a/pym/gentoolkit/equery/uses.py b/pym/gentoolkit/equery/uses.py |
105 |
index cedac96..7717710 100644 |
106 |
--- a/pym/gentoolkit/equery/uses.py |
107 |
+++ b/pym/gentoolkit/equery/uses.py |
108 |
@@ -21,6 +21,7 @@ from getopt import gnu_getopt, GetoptError |
109 |
from glob import glob |
110 |
|
111 |
from portage import settings |
112 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
113 |
|
114 |
import gentoolkit.pprinter as pp |
115 |
from gentoolkit import errors |
116 |
@@ -135,7 +136,7 @@ def get_global_useflags(): |
117 |
# Get global USE flag descriptions |
118 |
try: |
119 |
path = os.path.join(settings["PORTDIR"], 'profiles', 'use.desc') |
120 |
- with open(path) as open_file: |
121 |
+ with open(_unicode_encode(path), encoding=_encodings['fs']) as open_file: |
122 |
for line in open_file: |
123 |
if line.startswith('#'): |
124 |
continue |
125 |
@@ -155,7 +156,7 @@ def get_global_useflags(): |
126 |
for path in glob(os.path.join(settings["PORTDIR"], |
127 |
'profiles', 'desc', '*.desc')): |
128 |
try: |
129 |
- with open(path) as open_file: |
130 |
+ with open(_unicode_encode(path), encoding=_encodings['fs']) as open_file: |
131 |
for line in open_file: |
132 |
if line.startswith('#'): |
133 |
continue |
134 |
|
135 |
diff --git a/pym/gentoolkit/equery/which.py b/pym/gentoolkit/equery/which.py |
136 |
index da60a1b..ea03b90 100644 |
137 |
--- a/pym/gentoolkit/equery/which.py |
138 |
+++ b/pym/gentoolkit/equery/which.py |
139 |
@@ -26,6 +26,8 @@ from gentoolkit import errors |
140 |
from gentoolkit.equery import format_options, mod_usage |
141 |
from gentoolkit.query import Query |
142 |
|
143 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
144 |
+ |
145 |
# ======= |
146 |
# Globals |
147 |
# ======= |
148 |
@@ -60,7 +62,7 @@ def print_help(with_description=True): |
149 |
|
150 |
def print_ebuild(ebuild_path): |
151 |
"""Output the ebuild to std_out""" |
152 |
- with open(ebuild_path) as f: |
153 |
+ with open(_unicode_encode(ebuild_path), encoding=_encodings['fs']) as f: |
154 |
lines = f.readlines() |
155 |
print("\n\n") |
156 |
print("".join(lines)) |
157 |
|
158 |
diff --git a/pym/gentoolkit/eshowkw/keywords_header.py b/pym/gentoolkit/eshowkw/keywords_header.py |
159 |
index aaf1e8c..9ca0364 100644 |
160 |
--- a/pym/gentoolkit/eshowkw/keywords_header.py |
161 |
+++ b/pym/gentoolkit/eshowkw/keywords_header.py |
162 |
@@ -6,6 +6,7 @@ __all__ = ['keywords_header'] |
163 |
|
164 |
import portage |
165 |
import os |
166 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
167 |
from portage import settings as ports |
168 |
from portage.output import colorize |
169 |
from gentoolkit.eshowkw.display_pretty import colorize_string |
170 |
@@ -30,7 +31,7 @@ def load_profile_data(portdir=None, repo='gentoo'): |
171 |
|
172 |
try: |
173 |
arch_list = os.path.join(portdir, 'profiles', 'arch.list') |
174 |
- with open(arch_list) as f: |
175 |
+ with open(_unicode_encode(arch_list), encoding=_encodings['fs']) as f: |
176 |
for line in f: |
177 |
line = line.split('#', 1)[0].strip() |
178 |
if line: |
179 |
@@ -46,7 +47,7 @@ def load_profile_data(portdir=None, repo='gentoo'): |
180 |
None: 3, |
181 |
} |
182 |
profiles_list = os.path.join(portdir, 'profiles', 'profiles.desc') |
183 |
- with open(profiles_list) as f: |
184 |
+ with open(_unicode_encode(profiles_list), encoding=_encodings['fs']) as f: |
185 |
for line in f: |
186 |
line = line.split('#', 1)[0].split() |
187 |
if line: |
188 |
|
189 |
diff --git a/pym/gentoolkit/glsa/__init__.py b/pym/gentoolkit/glsa/__init__.py |
190 |
index a9eb30b..0d670b7 100644 |
191 |
--- a/pym/gentoolkit/glsa/__init__.py |
192 |
+++ b/pym/gentoolkit/glsa/__init__.py |
193 |
@@ -35,9 +35,11 @@ if sys.version_info[0:2] < (2,3): |
194 |
|
195 |
try: |
196 |
import portage |
197 |
+ from portage import _encodings, _unicode_decode, _unicode_encode |
198 |
except ImportError: |
199 |
sys.path.insert(0, "/usr/lib/portage/pym") |
200 |
import portage |
201 |
+ from portage import _encodings, _unicode_decode, _unicode_encode |
202 |
|
203 |
|
204 |
# Note: the space for rgt and rlt is important !! |
205 |
@@ -702,7 +704,8 @@ class Glsa: |
206 |
@returns: None |
207 |
""" |
208 |
if not self.isInjected(): |
209 |
- checkfile = open(self.config["CHECKFILE"], "a+") |
210 |
+ checkfile = open(_unicode_encode(self.config["CHECKFILE"]), |
211 |
+ encoding=_encodings['fs'], mode="a+") |
212 |
checkfile.write(self.nr+"\n") |
213 |
checkfile.close() |
214 |
return None |
215 |
|
216 |
diff --git a/pym/gentoolkit/helpers.py b/pym/gentoolkit/helpers.py |
217 |
index 55fecdb..f9da6cd 100644 |
218 |
--- a/pym/gentoolkit/helpers.py |
219 |
+++ b/pym/gentoolkit/helpers.py |
220 |
@@ -27,11 +27,11 @@ __docformat__ = 'epytext' |
221 |
|
222 |
import os |
223 |
import re |
224 |
-import codecs |
225 |
from functools import partial |
226 |
from itertools import chain |
227 |
|
228 |
import portage |
229 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
230 |
|
231 |
from gentoolkit import pprinter as pp |
232 |
from gentoolkit import errors |
233 |
@@ -194,8 +194,8 @@ class ChangeLog(object): |
234 |
|
235 |
result = [] |
236 |
partial_entries = [] |
237 |
- with codecs.open(self.changelog_path, encoding="utf-8", |
238 |
- errors="replace") as log: |
239 |
+ with open(_unicode_encode(self.changelog_path), |
240 |
+ encoding=_encodings['fs'], errors="replace") as log: |
241 |
for line in log: |
242 |
if line.startswith('#'): |
243 |
continue |
244 |
@@ -464,7 +464,7 @@ def get_bintree_cpvs(predicate=None): |
245 |
def print_file(path): |
246 |
"""Display the contents of a file.""" |
247 |
|
248 |
- with open(path, "rb") as open_file: |
249 |
+ with open(_unicode_encode(path), encoding=_encodings['fs'], mode="rb") as open_file: |
250 |
lines = open_file.read() |
251 |
pp.uprint(lines.strip()) |
252 |
|
253 |
|
254 |
diff --git a/pym/gentoolkit/revdep_rebuild/analyse.py b/pym/gentoolkit/revdep_rebuild/analyse.py |
255 |
index c0e7231..0f89b03 100644 |
256 |
--- a/pym/gentoolkit/revdep_rebuild/analyse.py |
257 |
+++ b/pym/gentoolkit/revdep_rebuild/analyse.py |
258 |
@@ -8,6 +8,7 @@ import os |
259 |
import re |
260 |
import time |
261 |
|
262 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
263 |
from portage.output import bold, blue, yellow, green |
264 |
|
265 |
from .stuff import scan |
266 |
@@ -82,7 +83,7 @@ def extract_dependencies_from_la(la, libraries, to_check, logger): |
267 |
if not os.path.exists(_file): |
268 |
continue |
269 |
|
270 |
- for line in open(_file, 'r').readlines(): |
271 |
+ for line in open(_unicode_encode(_file), encoding=_encodings['fs'], mode='r').readlines(): |
272 |
line = line.strip() |
273 |
if line.startswith('dependency_libs='): |
274 |
match = re.match("dependency_libs='([^']+)'", line) |
275 |
|
276 |
diff --git a/pym/gentoolkit/revdep_rebuild/cache.py b/pym/gentoolkit/revdep_rebuild/cache.py |
277 |
index 3d925d8..31ee2c9 100644 |
278 |
--- a/pym/gentoolkit/revdep_rebuild/cache.py |
279 |
+++ b/pym/gentoolkit/revdep_rebuild/cache.py |
280 |
@@ -8,6 +8,7 @@ from __future__ import print_function |
281 |
import os |
282 |
import time |
283 |
|
284 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
285 |
from portage.output import red |
286 |
from .settings import DEFAULTS |
287 |
|
288 |
@@ -29,7 +30,8 @@ def read_cache(temp_path=DEFAULTS['DEFAULT_TMP_DIR']): |
289 |
} |
290 |
try: |
291 |
for key,val in ret.items(): |
292 |
- _file = open(os.path.join(temp_path, key)) |
293 |
+ _file = open(_unicode_encode(os.path.join(temp_path, key)), |
294 |
+ encoding=_encodings['fs']) |
295 |
for line in _file.readlines(): |
296 |
val.add(line.strip()) |
297 |
#libraries.remove('\n') |
298 |
@@ -52,12 +54,14 @@ def save_cache(logger, to_save={}, temp_path=DEFAULTS['DEFAULT_TMP_DIR']): |
299 |
os.makedirs(temp_path) |
300 |
|
301 |
try: |
302 |
- _file = open(os.path.join(temp_path, 'timestamp'), 'w') |
303 |
+ _file = open(_unicode_encode(os.path.join(temp_path, 'timestamp')), |
304 |
+ encoding=_encodings['fs'], mode='w') |
305 |
_file.write(str(int(time.time()))) |
306 |
_file.close() |
307 |
|
308 |
for key,val in to_save.items(): |
309 |
- _file = open(os.path.join(temp_path, key), 'w') |
310 |
+ _file = open(_unicode_encode(os.path.join(temp_path, key)), |
311 |
+ encoding=_encodings['fs'], mode='w') |
312 |
for line in val: |
313 |
_file.write(line + '\n') |
314 |
_file.close() |
315 |
@@ -85,7 +89,7 @@ def check_temp_files(temp_path=DEFAULTS['DEFAULT_TMP_DIR'], max_delay=3600, |
316 |
return False |
317 |
|
318 |
try: |
319 |
- _file = open(timestamp_path) |
320 |
+ _file = open(_unicode_encode(timestamp_path), encoding=_encodings['fs']) |
321 |
timestamp = int(_file.readline()) |
322 |
_file .close() |
323 |
except Exception as ex: |
324 |
|
325 |
diff --git a/pym/gentoolkit/revdep_rebuild/collect.py b/pym/gentoolkit/revdep_rebuild/collect.py |
326 |
index 2a431cb..758bcf7 100644 |
327 |
--- a/pym/gentoolkit/revdep_rebuild/collect.py |
328 |
+++ b/pym/gentoolkit/revdep_rebuild/collect.py |
329 |
@@ -11,6 +11,7 @@ import stat |
330 |
import sys |
331 |
|
332 |
import portage |
333 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
334 |
from portage.output import blue, yellow |
335 |
from .settings import parse_revdep_config |
336 |
|
337 |
@@ -34,7 +35,7 @@ def parse_conf(conf_file, visited=None, logger=None): |
338 |
|
339 |
for conf in conf_file: |
340 |
try: |
341 |
- with open(conf) as _file: |
342 |
+ with open(_unicode_encode(conf), encoding=_encodings['fs']) as _file: |
343 |
for line in _file.readlines(): |
344 |
line = line.strip() |
345 |
if line.startswith('#'): |
346 |
@@ -74,8 +75,9 @@ def prepare_search_dirs(logger, settings): |
347 |
lib_dirs = set(['/lib', '/usr/lib', ]) |
348 |
|
349 |
#try: |
350 |
- with open(os.path.join( |
351 |
- portage.root, settings['DEFAULT_ENV_FILE']), 'r') as _file: |
352 |
+ with open(_unicode_encode(os.path.join( |
353 |
+ portage.root, settings['DEFAULT_ENV_FILE'])), |
354 |
+ encoding=_encodings['fs'], mode='r') as _file: |
355 |
for line in _file.readlines(): |
356 |
line = line.strip() |
357 |
match = re.match("^export (ROOT)?PATH='([^']+)'", line) |
358 |
|
359 |
diff --git a/pym/gentoolkit/revdep_rebuild/settings.py b/pym/gentoolkit/revdep_rebuild/settings.py |
360 |
index 08220f8..257bd3a 100644 |
361 |
--- a/pym/gentoolkit/revdep_rebuild/settings.py |
362 |
+++ b/pym/gentoolkit/revdep_rebuild/settings.py |
363 |
@@ -11,6 +11,7 @@ import re |
364 |
import glob |
365 |
|
366 |
import portage |
367 |
+from portage import _encodings, _unicode_decode, _unicode_encode |
368 |
|
369 |
DEFAULTS = { |
370 |
'DEFAULT_LD_FILE': os.path.join(portage.root, 'etc/ld.so.conf'), |
371 |
@@ -136,7 +137,8 @@ def parse_revdep_config(revdep_confdir): |
372 |
masked_files = os.environ.get('LD_LIBRARY_MASK', '') |
373 |
|
374 |
for _file in os.listdir(revdep_confdir): |
375 |
- for line in open(os.path.join(revdep_confdir, _file)): |
376 |
+ for line in open(_unicode_encode(os.path.join(revdep_confdir, _file)), |
377 |
+ encoding=_encodings['fs']): |
378 |
line = line.strip() |
379 |
#first check for comment, we do not want to regex all lines |
380 |
if not line.startswith('#'): |