Merge lp:~cjwatson/command-not-found/python3 into lp:~command-not-found-developers/command-not-found/trunk
- python3
- Merge into trunk
Status: | Merged |
---|---|
Merged at revision: | 153 |
Proposed branch: | lp:~cjwatson/command-not-found/python3 |
Merge into: | lp:~command-not-found-developers/command-not-found/trunk |
Diff against target: |
753 lines (+213/-165) 10 files modified
CommandNotFound/CommandNotFound.py (+58/-41) CommandNotFound/__init__.py (+3/-1) CommandNotFound/util.py (+17/-11) UnifiedDataExtractor/DebPackage.py (+21/-19) UnifiedDataExtractor/create-binary-database (+23/-18) UnifiedDataExtractor/diff-scan-data (+25/-20) UnifiedDataExtractor/scan (+32/-26) UnifiedDataExtractor/verify_scan_data.py (+24/-21) bash_command_not_found (+1/-1) command-not-found (+9/-7) |
To merge this branch: | bzr merge lp:~cjwatson/command-not-found/python3 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Zygmunt Krynicki | Approve | ||
Review via email: mp+110092@code.launchpad.net |
Commit message
Description of the change
Port to Python 3. This should all still run under Python 2 as well.
- 174. By Colin Watson
-
Fix "key is not bytes" thinko.
Barry Warsaw (barry) wrote : | # |
@Zigmunt: re: line 47 yep, I caught that one too, and Colin fixed it in the update.
Re the wrapping of list(range(2)) etc. I think you're right:
Python 3.2.3 (default, May 3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (soft, hard) = range(2)
>>> soft
0
>>> hard
1
Colin Watson (cjwatson) wrote : | # |
list(range(...)): That was indeed my misunderstanding from blindly believing 2to3. I tested it yesterday for something else and realised that was redundant. I've reverted those changes; please re-review?
- 175. By Colin Watson
-
Revert unnecessary list(range(...)) wrapping.
Zygmunt Krynicki (zyga) wrote : | # |
Looks perfect now, thanks!
Colin Watson (cjwatson) wrote : | # |
Great, thanks! Perhaps you could land this? I can help with rolling a
new Ubuntu package if you like, but I don't have commit access to
lp:command-not-found.
Michael Vogt (mvo) wrote : | # |
On Wed, Jun 13, 2012 at 11:40:40PM -0000, Colin Watson wrote:
> Great, thanks! Perhaps you could land this? I can help with rolling a
> new Ubuntu package if you like, but I don't have commit access to
> lp:command-not-found.
I landed this in lp:command-not-found now, thanks for the branch!
Cheers,
Michael
Preview Diff
1 | === modified file 'CommandNotFound/CommandNotFound.py' |
2 | --- CommandNotFound/CommandNotFound.py 2012-01-13 11:11:20 +0000 |
3 | +++ CommandNotFound/CommandNotFound.py 2012-06-13 16:12:19 +0000 |
4 | @@ -1,17 +1,27 @@ |
5 | # (c) Zygmunt Krynicki 2005, 2006, 2007, 2008 |
6 | # Licensed under GPL, see COPYING for the whole text |
7 | |
8 | -import gdbm |
9 | +from __future__ import print_function |
10 | + |
11 | +try: |
12 | + import dbm.gnu as gdbm |
13 | +except ImportError: |
14 | + import gdbm |
15 | import gettext |
16 | import grp |
17 | import os |
18 | import os.path |
19 | import posix |
20 | -import string |
21 | import sys |
22 | import subprocess |
23 | |
24 | -_ = gettext.translation("command-not-found", fallback=True).ugettext |
25 | +from functools import cmp_to_key |
26 | + |
27 | +if sys.version >= "3": |
28 | + _gettext_method = "gettext" |
29 | +else: |
30 | + _gettext_method = "ugettext" |
31 | +_ = getattr(gettext.translation("command-not-found", fallback=True), _gettext_method) |
32 | |
33 | |
34 | class BinaryDatabase(object): |
35 | @@ -21,12 +31,16 @@ |
36 | if filename.endswith(".db"): |
37 | try: |
38 | self.db = gdbm.open(filename, "r") |
39 | - except gdbm.error, err: |
40 | - print >> sys.stderr, "Unable to open binary database %s: %s" % (filename, err) |
41 | + except gdbm.error as err: |
42 | + print("Unable to open binary database %s: %s" % (filename, err), file=sys.stderr) |
43 | |
44 | def lookup(self, key): |
45 | - if self.db and self.db.has_key(key): |
46 | - return self.db[key] |
47 | + if not isinstance(key, bytes): |
48 | + # gdbm does not entirely handle Unicode strings; "self.db[key]" |
49 | + # works, but "key in self.db" does not. |
50 | + key = key.encode('utf-8') |
51 | + if self.db and key in self.db: |
52 | + return self.db[key].decode('utf-8') |
53 | else: |
54 | return None |
55 | |
56 | @@ -35,10 +49,9 @@ |
57 | |
58 | def __init__(self, filename): |
59 | self.rows = [] |
60 | - dbfile = file(filename) |
61 | - for line in (line.strip() for line in dbfile): |
62 | - self.rows.append(line.split("|")) |
63 | - dbfile.close() |
64 | + with open(filename) as dbfile: |
65 | + for line in (line.strip() for line in dbfile): |
66 | + self.rows.append(line.split("|")) |
67 | |
68 | def lookup(self, column, text): |
69 | result = [] |
70 | @@ -109,7 +122,8 @@ |
71 | self.priority_overrides = [] |
72 | p = os.path.join(data_dir, "priority.txt") |
73 | if os.path.exists(p): |
74 | - self.priority_overrides = map(string.strip, open(p).readlines()) |
75 | + with open(p) as priority_file: |
76 | + self.priority_overrides = [line.strip() for line in priority_file] |
77 | self.components = ['main', 'universe', 'contrib', 'restricted', |
78 | 'non-free', 'multiverse'] |
79 | self.components.reverse() |
80 | @@ -131,11 +145,11 @@ |
81 | for (package, comp) in packages: |
82 | possible_alternatives.append((w, package, comp)) |
83 | if len(possible_alternatives) > max_len: |
84 | - print >> sys.stderr, _("No command '%s' found, but there are %s similar ones") % (word, len(possible_alternatives)) |
85 | + print(_("No command '%s' found, but there are %s similar ones") % (word, len(possible_alternatives)), file=sys.stderr) |
86 | elif len(possible_alternatives) > 0: |
87 | - print >> sys.stderr, _("No command '%s' found, did you mean:") % word |
88 | + print(_("No command '%s' found, did you mean:") % word, file=sys.stderr) |
89 | for (w, p, c) in possible_alternatives: |
90 | - print >> sys.stderr, _(" Command '%s' from package '%s' (%s)") % (w, p, c) |
91 | + print(_(" Command '%s' from package '%s' (%s)") % (w, p, c), file=sys.stderr) |
92 | |
93 | def getPackages(self, command): |
94 | result = set() |
95 | @@ -145,12 +159,10 @@ |
96 | |
97 | def getBlacklist(self): |
98 | try: |
99 | - blacklist = file(os.sep.join((os.getenv("HOME", "/root"), ".command-not-found.blacklist"))) |
100 | - return [line.strip() for line in blacklist if line.strip() != ""] |
101 | + with open(os.sep.join((os.getenv("HOME", "/root"), ".command-not-found.blacklist"))) as blacklist: |
102 | + return [line.strip() for line in blacklist if line.strip() != ""] |
103 | except IOError: |
104 | return [] |
105 | - else: |
106 | - blacklist.close() |
107 | |
108 | def _getSourcesList(self): |
109 | try: |
110 | @@ -188,13 +200,18 @@ |
111 | yidx = self.components.index(y[1]) |
112 | except: |
113 | xidx = -1 |
114 | - return (yidx - xidx) or cmp(x, y) |
115 | + # http://python3porting.com/differences.html#comparisons |
116 | + return (yidx - xidx) or ((x > y) - (x < y)) |
117 | |
118 | def install_prompt(self, package_name): |
119 | if not "COMMAND_NOT_FOUND_INSTALL_PROMPT" in os.environ: |
120 | return |
121 | if package_name: |
122 | - answer = raw_input(_("Do you want to install it? (N/y)")) |
123 | + prompt = _("Do you want to install it? (N/y)") |
124 | + if sys.version >= '3': |
125 | + answer = input(prompt) |
126 | + else: |
127 | + answer = raw_input(prompt) |
128 | if sys.stdin.encoding and isinstance(answer, str): |
129 | # Decode the answer so that we get an unicode value |
130 | answer = answer.decode(sys.stdin.encoding) |
131 | @@ -204,7 +221,7 @@ |
132 | else: |
133 | command_prefix = "sudo " |
134 | install_command = "%sapt-get install %s" % (command_prefix, package_name) |
135 | - print >> sys.stdout, "%s" % install_command |
136 | + print("%s" % install_command, file=sys.stdout) |
137 | subprocess.call(install_command.split(), shell=False) |
138 | |
139 | def advise(self, command, ignore_installed=False): |
140 | @@ -225,16 +242,16 @@ |
141 | # check if we have it in a common prefix that may not be in the PATH |
142 | if prefixes and not ignore_installed: |
143 | if len(prefixes) == 1: |
144 | - print >> sys.stderr, _("Command '%(command)s' is available in '%(place)s'") % {"command": command, "place": os.path.join(prefixes[0], command)} |
145 | + print(_("Command '%(command)s' is available in '%(place)s'") % {"command": command, "place": os.path.join(prefixes[0], command)}, file=sys.stderr) |
146 | else: |
147 | - print >> sys.stderr, _("Command '%(command)s' is available in the following places") % {"command": command} |
148 | + print(_("Command '%(command)s' is available in the following places") % {"command": command}, file=sys.stderr) |
149 | for prefix in prefixes: |
150 | - print >> sys.stderr, " * %s" % os.path.join(prefix, command) |
151 | + print(" * %s" % os.path.join(prefix, command), file=sys.stderr) |
152 | missing = list(set(prefixes) - set(os.getenv("PATH", "").split(":"))) |
153 | if len(missing) > 0: |
154 | - print >> sys.stderr, _("The command could not be located because '%s' is not included in the PATH environment variable.") % ":".join(missing) |
155 | + print(_("The command could not be located because '%s' is not included in the PATH environment variable.") % ":".join(missing), file=sys.stderr) |
156 | if "sbin" in ":".join(missing): |
157 | - print >> sys.stderr, _("This is most likely caused by the lack of administrative privileges associated with your user account.") |
158 | + print(_("This is most likely caused by the lack of administrative privileges associated with your user account."), file=sys.stderr) |
159 | return False |
160 | |
161 | # do not give advice if we are in a situation where apt-get |
162 | @@ -249,31 +266,31 @@ |
163 | if len(packages) == 0: |
164 | self.print_spelling_suggestion(command) |
165 | elif len(packages) == 1: |
166 | - print >> sys.stderr, _("The program '%s' is currently not installed. ") % command, |
167 | + print(_("The program '%s' is currently not installed. ") % command, end="", file=sys.stderr) |
168 | if posix.geteuid() == 0: |
169 | - print >> sys.stderr, _("You can install it by typing:") |
170 | - print >> sys.stderr, "apt-get install %s" % packages[0][0] |
171 | + print(_("You can install it by typing:"), file=sys.stderr) |
172 | + print("apt-get install %s" % packages[0][0], file=sys.stderr) |
173 | self.install_prompt(packages[0][0]) |
174 | elif self.user_can_sudo: |
175 | - print >> sys.stderr, _("You can install it by typing:") |
176 | - print >> sys.stderr, "sudo apt-get install %s" % packages[0][0] |
177 | + print(_("You can install it by typing:"), file=sys.stderr) |
178 | + print("sudo apt-get install %s" % packages[0][0], file=sys.stderr) |
179 | self.install_prompt(packages[0][0]) |
180 | else: |
181 | - print >> sys.stderr, _("To run '%(command)s' please ask your administrator to install the package '%(package)s'") % {'command': command, 'package': packages[0][0]} |
182 | + print(_("To run '%(command)s' please ask your administrator to install the package '%(package)s'") % {'command': command, 'package': packages[0][0]}, file=sys.stderr) |
183 | if not packages[0][1] in self.sources_list: |
184 | - print >> sys.stderr, _("You will have to enable the component called '%s'") % packages[0][1] |
185 | + print(_("You will have to enable the component called '%s'") % packages[0][1], file=sys.stderr) |
186 | elif len(packages) > 1: |
187 | - packages.sort(self.sortByComponent) |
188 | - print >> sys.stderr, _("The program '%s' can be found in the following packages:") % command |
189 | + packages.sort(key=cmp_to_key(self.sortByComponent)) |
190 | + print(_("The program '%s' can be found in the following packages:") % command, file=sys.stderr) |
191 | for package in packages: |
192 | if package[1] in self.sources_list: |
193 | - print >> sys.stderr, " * %s" % package[0] |
194 | + print(" * %s" % package[0], file=sys.stderr) |
195 | else: |
196 | - print >> sys.stderr, " * %s" % package[0] + " (" + _("You will have to enable component called '%s'") % package[1] + ")" |
197 | + print(" * %s" % package[0] + " (" + _("You will have to enable component called '%s'") % package[1] + ")", file=sys.stderr) |
198 | if posix.geteuid() == 0: |
199 | - print >> sys.stderr, _("Try: %s <selected package>") % "apt-get install" |
200 | + print(_("Try: %s <selected package>") % "apt-get install", file=sys.stderr) |
201 | elif self.user_can_sudo: |
202 | - print >> sys.stderr, _("Try: %s <selected package>") % "sudo apt-get install" |
203 | + print(_("Try: %s <selected package>") % "sudo apt-get install", file=sys.stderr) |
204 | else: |
205 | - print >> sys.stderr, _("Ask your administrator to install one of them") |
206 | + print(_("Ask your administrator to install one of them"), file=sys.stderr) |
207 | return len(packages) > 0 |
208 | |
209 | === modified file 'CommandNotFound/__init__.py' |
210 | --- CommandNotFound/__init__.py 2011-08-10 13:02:29 +0000 |
211 | +++ CommandNotFound/__init__.py 2012-06-13 16:12:19 +0000 |
212 | @@ -1,1 +1,3 @@ |
213 | -from CommandNotFound import CommandNotFound |
214 | +from __future__ import absolute_import |
215 | + |
216 | +from CommandNotFound.CommandNotFound import CommandNotFound |
217 | |
218 | === modified file 'CommandNotFound/util.py' |
219 | --- CommandNotFound/util.py 2011-08-25 09:49:51 +0000 |
220 | +++ CommandNotFound/util.py 2012-06-13 16:12:19 +0000 |
221 | @@ -1,12 +1,18 @@ |
222 | # (c) Zygmunt Krynicki 2008 |
223 | # Licensed under GPL, see COPYING for the whole text |
224 | |
225 | +from __future__ import print_function |
226 | + |
227 | import gettext |
228 | import locale |
229 | import sys |
230 | import gettext |
231 | |
232 | -_ = gettext.translation("command-not-found", fallback=True).ugettext |
233 | +if sys.version >= "3": |
234 | + _gettext_method = "gettext" |
235 | +else: |
236 | + _gettext_method = "ugettext" |
237 | +_ = getattr(gettext.translation("command-not-found", fallback=True), _gettext_method) |
238 | |
239 | |
240 | def crash_guard(callback, bug_report_url, version): |
241 | @@ -16,21 +22,21 @@ |
242 | try: |
243 | try: |
244 | callback() |
245 | - except Exception, ex: |
246 | - print >> sys.stderr, _("Sorry, command-not-found has crashed! Please file a bug report at:") |
247 | - print >> sys.stderr, bug_report_url |
248 | - print >> sys.stderr, _("Please include the following information with the report:") |
249 | - print >> sys.stderr |
250 | - print >> sys.stderr, _("command-not-found version: %s") % version |
251 | - print >> sys.stderr, _("Python version: %d.%d.%d %s %d") % sys.version_info |
252 | + except Exception as ex: |
253 | + print(_("Sorry, command-not-found has crashed! Please file a bug report at:"), file=sys.stderr) |
254 | + print(bug_report_url, file=sys.stderr) |
255 | + print(_("Please include the following information with the report:"), file=sys.stderr) |
256 | + print(file=sys.stderr) |
257 | + print(_("command-not-found version: %s") % version, file=sys.stderr) |
258 | + print(_("Python version: %d.%d.%d %s %d") % sys.version_info, file=sys.stderr) |
259 | try: |
260 | import subprocess |
261 | subprocess.call(["lsb_release", "-i", "-d", "-r", "-c"], stdout=sys.stderr) |
262 | except (ImportError, OSError): |
263 | pass |
264 | - print >> sys.stderr, _("Exception information:") |
265 | - print >> sys.stderr |
266 | - print >> sys.stderr, ex |
267 | + print(_("Exception information:"), file=sys.stderr) |
268 | + print(file=sys.stderr) |
269 | + print(ex, file=sys.stderr) |
270 | try: |
271 | import traceback |
272 | traceback.print_exc() |
273 | |
274 | === modified file 'UnifiedDataExtractor/DebPackage.py' |
275 | --- UnifiedDataExtractor/DebPackage.py 2011-08-10 13:03:51 +0000 |
276 | +++ UnifiedDataExtractor/DebPackage.py 2012-06-13 16:12:19 +0000 |
277 | @@ -1,6 +1,8 @@ |
278 | # (c) Zygmunt Krynicki 2005, |
279 | # Licensed under GPL, see COPYING for the whole text |
280 | |
281 | +from __future__ import print_function |
282 | + |
283 | import warnings |
284 | warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning) |
285 | |
286 | @@ -170,15 +172,18 @@ |
287 | |
288 | def __init__(self, filename): |
289 | self.filename = filename |
290 | - self._sections = apt_pkg.ParseSection(self.getControlFile("control")) |
291 | + self._sections = apt_pkg.TagSection(self.getControlFile("control")) |
292 | |
293 | arch = property(lambda self: self._sections["Architecture"], None, None, "Architecture the package is compiled for") |
294 | |
295 | name = property(lambda self: self._sections["Package"], None, None, "Cannonical package name") |
296 | |
297 | def getControlFile(self, name): |
298 | - """ Returns the contents of given file in debian/ or None if it does not exits """ |
299 | - return apt_inst.debExtractControl(file(self.filename), name) |
300 | + """ Returns the contents of given file in debian/ or None if it does not exist """ |
301 | + try: |
302 | + return apt_inst.DebFile(self.filename).control.extractdata(name) |
303 | + except LookupError: |
304 | + return None |
305 | |
306 | @property |
307 | def items(self): |
308 | @@ -186,21 +191,18 @@ |
309 | Each file is represented by an instance of FileInfo """ |
310 | items = [] |
311 | |
312 | - def extract_cb(kind, name, target, mode, uid, gid, size, mtime, major, minor): |
313 | - if kind == "FILE": |
314 | - items.append(FileInfo(name, mode, uid, gid, size, mtime)) |
315 | - elif kind == "DIR": |
316 | - items.append(DirectoryInfo(name, mode, uid, gid, size, mtime)) |
317 | - elif kind == "SYMLINK": |
318 | - items.append(SymbolicLinkInfo(name, target, mode, uid, gid, size, mtime)) |
319 | - elif kind == "HARDLINK": |
320 | - items.append(HardLinkInfo(name, target, mode, uid, gid, size, mtime)) |
321 | - elif kind == "FIFO": |
322 | - items.append(FifoInfo(name, mode, uid, gid, size, mtime)) |
323 | + def extract_cb(member, data): |
324 | + if member.isfile(): |
325 | + items.append(FileInfo(member.name, member.mode, member.uid, member.gid, member.size, member.mtime)) |
326 | + elif member.isdir(): |
327 | + items.append(DirectoryInfo(member.name, member.mode, member.uid, member.gid, member.size, member.mtime)) |
328 | + elif member.issym(): |
329 | + items.append(SymbolicLinkInfo(member.name, member.linkname, member.mode, member.uid, member.gid, member.size, member.mtime)) |
330 | + elif member.islnk(): |
331 | + items.append(HardLinkInfo(member.name, member.linkname, member.mode, member.uid, member.gid, member.size, member.mtime)) |
332 | + elif member.isfifo(): |
333 | + items.append(FifoInfo(member.name, member.mode, member.uid, member.gid, member.size, member.mtime)) |
334 | else: |
335 | - print "unsupported kind: %s" % kind |
336 | - try: |
337 | - apt_inst.debExtract(open(self.filename), extract_cb, "data.tar.gz") |
338 | - except: |
339 | - apt_inst.debExtract(open(self.filename), extract_cb, "data.tar.bz2") |
340 | + print("unsupported member type: %s" % member) |
341 | + apt_inst.DebFile(self.filename).data.go(extract_cb) |
342 | return items |
343 | |
344 | === modified file 'UnifiedDataExtractor/create-binary-database' |
345 | --- UnifiedDataExtractor/create-binary-database 2012-01-11 14:32:50 +0000 |
346 | +++ UnifiedDataExtractor/create-binary-database 2012-06-13 16:12:19 +0000 |
347 | @@ -5,8 +5,10 @@ |
348 | import glob |
349 | import os |
350 | import os.path |
351 | -import gdbm |
352 | -import string |
353 | +try: |
354 | + import dbm.gnu as gdbm |
355 | +except ImportError: |
356 | + import gdbm |
357 | import subprocess |
358 | |
359 | class ProgramGroup(object): |
360 | @@ -16,7 +18,7 @@ |
361 | self.programs = dict() |
362 | def notice_package(self, package_name, user_visible_program_names): |
363 | for user_visible_program_name in user_visible_program_names: |
364 | - if not self.programs.has_key(user_visible_program_name): |
365 | + if user_visible_program_name not in self.programs: |
366 | self.programs[user_visible_program_name] = set() |
367 | self.programs[user_visible_program_name].add(package_name) |
368 | def write_database(self): |
369 | @@ -28,23 +30,26 @@ |
370 | def build_binary_databases(basedir=".", currentArchOnly=False): |
371 | myarch = subprocess.Popen( |
372 | ["dpkg","--print-architecture"], |
373 | - stdout=subprocess.PIPE).communicate()[0].strip() |
374 | + stdout=subprocess.PIPE, |
375 | + universal_newlines=True).communicate()[0].strip() |
376 | groups = dict() |
377 | for data_file in glob.glob("%s/*.data" % basedir): |
378 | - for line in map(string.strip, open(data_file)): |
379 | - if not line or line.startswith("#"): |
380 | - continue |
381 | - arch, repo, package_name, executables = line.split('|') |
382 | - if (currentArchOnly and arch != "all" and arch != myarch): |
383 | - continue |
384 | - group_name = "%s-%s" % (arch, repo) |
385 | - if not groups.has_key(group_name): |
386 | - groups[group_name] = ProgramGroup(arch, repo) |
387 | - user_visible_program_names = [ |
388 | - os.path.basename(executable) |
389 | - for executable in executables.split(',')] |
390 | - groups[group_name].notice_package( |
391 | - package_name, user_visible_program_names) |
392 | + with open(data_file) as fd: |
393 | + for line in fd: |
394 | + line = line.strip() |
395 | + if not line or line.startswith("#"): |
396 | + continue |
397 | + arch, repo, package_name, executables = line.split('|') |
398 | + if (currentArchOnly and arch != "all" and arch != myarch): |
399 | + continue |
400 | + group_name = "%s-%s" % (arch, repo) |
401 | + if group_name not in groups: |
402 | + groups[group_name] = ProgramGroup(arch, repo) |
403 | + user_visible_program_names = [ |
404 | + os.path.basename(executable) |
405 | + for executable in executables.split(',')] |
406 | + groups[group_name].notice_package( |
407 | + package_name, user_visible_program_names) |
408 | # write it out |
409 | for group in groups.values(): |
410 | group.write_database() |
411 | |
412 | === modified file 'UnifiedDataExtractor/diff-scan-data' |
413 | --- UnifiedDataExtractor/diff-scan-data 2009-06-30 08:01:27 +0000 |
414 | +++ UnifiedDataExtractor/diff-scan-data 2012-06-13 16:12:19 +0000 |
415 | @@ -1,7 +1,8 @@ |
416 | #!/usr/bin/python |
417 | |
418 | +from __future__ import print_function |
419 | + |
420 | import sys |
421 | -import string |
422 | import apt |
423 | |
424 | def normalize_line(line): |
425 | @@ -21,10 +22,12 @@ |
426 | new = set() |
427 | #func = normalize_line |
428 | func = pkg_only |
429 | - for l in map(func, open(old_scan_data)): |
430 | - old.add(l) |
431 | - for l in map(func, open(new_scan_data)): |
432 | - new.add(l) |
433 | + with open(old_scan_data) as old_scan_file: |
434 | + for l in map(func, old_scan_file): |
435 | + old.add(l) |
436 | + with open(new_scan_data) as new_scan_file: |
437 | + for l in map(func, new_scan_file): |
438 | + new.add(l) |
439 | return (set(new - old), set(old - new)) |
440 | |
441 | def get_comp_changes(oldf, newf): |
442 | @@ -36,12 +39,14 @@ |
443 | old = {} |
444 | new = {} |
445 | comp_changes = {} |
446 | - for l in map(func, open(oldf)): |
447 | - (comp, pkg) = l.split("|") |
448 | - old[pkg] = comp |
449 | - for l in map(func, open(newf)): |
450 | - (comp, pkg) = l.split("|") |
451 | - new[pkg] = comp |
452 | + with open(oldf) as oldfile: |
453 | + for l in map(func, oldfile): |
454 | + (comp, pkg) = l.split("|") |
455 | + old[pkg] = comp |
456 | + with open(newf) as newfile: |
457 | + for l in map(func, newfile): |
458 | + (comp, pkg) = l.split("|") |
459 | + new[pkg] = comp |
460 | for (pkg,comp) in old.items(): |
461 | if pkg in new and new[pkg] != comp: |
462 | comp_changes[pkg] = (comp, new[pkg]) |
463 | @@ -49,7 +54,7 @@ |
464 | |
465 | if __name__ == "__main__": |
466 | if len(sys.argv) < 3: |
467 | - print "need two files" |
468 | + print("need two files") |
469 | sys.exit(1) |
470 | |
471 | # oldfile, newfile given from commandline |
472 | @@ -59,21 +64,21 @@ |
473 | # first check for additions and removals |
474 | (new_pkgs, removed_pkgs) = get_new_and_removed_packages(oldf, newf) |
475 | |
476 | - print "--- new packages ---" |
477 | + print("--- new packages ---") |
478 | for line in new_pkgs: |
479 | - print line |
480 | + print(line) |
481 | |
482 | - print "\n\n" |
483 | - print "--- removed packages ---" |
484 | + print("\n\n") |
485 | + print("--- removed packages ---") |
486 | for line in removed_pkgs: |
487 | - print line |
488 | + print(line) |
489 | |
490 | # now do the component changes |
491 | - print "\n\n" |
492 | - print "--- changed component ---" |
493 | + print("\n\n") |
494 | + print("--- changed component ---") |
495 | changes = get_comp_changes(oldf, newf) |
496 | for (pkg, change) in changes.items(): |
497 | # change is a tuple with (old_comp, new_comp) |
498 | - print "%s changed from '%s' to '%s'" % (pkg, change[0], change[1]) |
499 | + print("%s changed from '%s' to '%s'" % (pkg, change[0], change[1])) |
500 | |
501 | |
502 | |
503 | === modified file 'UnifiedDataExtractor/scan' |
504 | --- UnifiedDataExtractor/scan 2007-01-05 15:51:46 +0000 |
505 | +++ UnifiedDataExtractor/scan 2012-06-13 16:12:19 +0000 |
506 | @@ -2,13 +2,18 @@ |
507 | # (c) Zygmunt Krynicki 2005, |
508 | # Licensed under GPL, see COPYING for the whole text |
509 | |
510 | +from __future__ import print_function |
511 | + |
512 | import os |
513 | import os.path |
514 | import sys |
515 | import re |
516 | import logging |
517 | import DebPackage |
518 | -import urllib |
519 | +try: |
520 | + from urllib.parse import unquote |
521 | +except ImportError: |
522 | + from urllib import unquote |
523 | import apt |
524 | import apt_pkg |
525 | |
526 | @@ -23,8 +28,8 @@ |
527 | def __str__(self): |
528 | return "%s|%s|%s|%s" % (self.arch, self.repo, self.name, ",".join(self.executables)) |
529 | def display(self): |
530 | - print "Package: %s for arch %s in repo %s" % (self.name, self.arch, self.repo) |
531 | - print "Executables: ", self.executables |
532 | + print("Package: %s for arch %s in repo %s" % (self.name, self.arch, self.repo)) |
533 | + print("Executables: ", self.executables) |
534 | |
535 | def strip_comment(line): |
536 | i = line.find('#') |
537 | @@ -60,7 +65,7 @@ |
538 | def nice_expand(*text_list): |
539 | return set(one_element_tuple_to_value(expand_values(*text_list))) |
540 | if contents != None: |
541 | - for line in contents.replace("\\\n","").splitlines(): |
542 | + for line in contents.decode(errors='replace').replace("\\\n","").splitlines(): |
543 | line = strip_comment(line).strip() |
544 | if line == "": |
545 | continue |
546 | @@ -164,7 +169,7 @@ |
547 | def analyze_package(filename): |
548 | try: |
549 | package = DebPackage.load(filename) |
550 | - except Exception, ex: |
551 | + except Exception as ex: |
552 | logging.error("Unable to process package %s: %s" % (filename, ex)) |
553 | return None |
554 | logging.info("PACKAGE: %s" % package.filename) |
555 | @@ -199,15 +204,15 @@ |
556 | pkgname = m.group(1) |
557 | ver = m.group(2) |
558 | # fix the quoting |
559 | - ver = urllib.unquote(ver) |
560 | + ver = unquote(ver) |
561 | pkgarch = m.group(3) |
562 | # check arch |
563 | if pkgarch != "all" and arch != pkgarch: |
564 | return False |
565 | # check distro |
566 | candVer = "xxx" |
567 | - if cache.has_key(pkgname): |
568 | - candVer = cache[pkgname].candidateVersion |
569 | + if pkgname in cache: |
570 | + candVer = getattr(cache[pkgname].candidate, "version", None) |
571 | # strip the epoch |
572 | if candVer and ":" in candVer: |
573 | candVer = candVer.split(":")[1] |
574 | @@ -224,10 +229,11 @@ |
575 | if package_info is None: |
576 | continue |
577 | #package_info.display() |
578 | - print >> output, package_info |
579 | + print(package_info, file=output) |
580 | |
581 | def scan(dir_path, output): |
582 | - os.path.walk(dir_path, dir_walk, output) |
583 | + for dirpath, dirnames, filenames in os.walk(dir_path): |
584 | + dir_walk(output, dirpath, filenames) |
585 | |
586 | if __name__ == "__main__": |
587 | global arch, cache |
588 | @@ -239,22 +245,22 @@ |
589 | filemode='w') |
590 | # init output |
591 | filename="scan.data" |
592 | - out = file(filename, "w") |
593 | - |
594 | - for arch in ["i386","amd64","powerpc"]: |
595 | - # init arch specific cache |
596 | - apt_pkg.Config.Set("APT::Architecture",arch) |
597 | - apt_pkg.Config.Set("Dir::state","./apt/") |
598 | - apt_pkg.Config.Set("Dir::Etc","./apt") |
599 | - cache = apt.Cache(apt.progress.OpProgress()) |
600 | - prog = apt.progress.FetchProgress() |
601 | - cache.update(prog) |
602 | - cache.open(apt.progress.OpProgress()) |
603 | - # now do the scan for this arch |
604 | - if len(sys.argv) == 2: |
605 | - scan(sys.argv[1],out) |
606 | - else: |
607 | - scan("mirror/archive.ubuntu.com/pool", out) |
608 | + |
609 | + with open(filename, "w") as out: |
610 | + for arch in ["i386","amd64","powerpc"]: |
611 | + # init arch specific cache |
612 | + apt_pkg.config.set("APT::Architecture",arch) |
613 | + apt_pkg.config.set("Dir::state","./apt/") |
614 | + apt_pkg.config.set("Dir::Etc","./apt") |
615 | + cache = apt.Cache(apt.progress.base.OpProgress()) |
616 | + prog = apt.progress.base.AcquireProgress() |
617 | + cache.update(prog) |
618 | + cache.open(apt.progress.base.OpProgress()) |
619 | + # now do the scan for this arch |
620 | + if len(sys.argv) == 2: |
621 | + scan(sys.argv[1],out) |
622 | + else: |
623 | + scan("mirror/archive.ubuntu.com/pool", out) |
624 | #scan("mirror/archive.ubuntu.com/pool/main/a") |
625 | #scan("mirror/archive.ubuntu.com/pool/universe/c/command-not-found") |
626 | #scan("mirror/archive.ubuntu.com/pool/main/g/gksu") |
627 | |
628 | === modified file 'UnifiedDataExtractor/verify_scan_data.py' |
629 | --- UnifiedDataExtractor/verify_scan_data.py 2011-08-10 13:02:29 +0000 |
630 | +++ UnifiedDataExtractor/verify_scan_data.py 2012-06-13 16:12:19 +0000 |
631 | @@ -4,6 +4,8 @@ |
632 | # Authors: |
633 | # Michael Vogt |
634 | |
635 | +from __future__ import print_function |
636 | + |
637 | import apt |
638 | import apt_pkg |
639 | import sys |
640 | @@ -11,30 +13,31 @@ |
641 | if __name__ == "__main__": |
642 | |
643 | if len(sys.argv) < 2: |
644 | - print "need scan.data argument" |
645 | + print("need scan.data argument") |
646 | sys.exit(1) |
647 | |
648 | scandata = sys.argv[1] |
649 | - apt_pkg.Config.Set("APT::Get::List-Cleanup", "false") |
650 | + apt_pkg.config.set("APT::Get::List-Cleanup", "false") |
651 | for arch in ("i386", "amd64"): |
652 | - print "Starting verification for '%s'" % arch |
653 | - apt_pkg.Config.Set("APT::Architecture", arch) |
654 | + print("Starting verification for '%s'" % arch) |
655 | + apt_pkg.config.set("APT::Architecture", arch) |
656 | cache = apt.Cache(rootdir="./apt/") |
657 | cache.update() |
658 | - cache.open(apt.progress.OpProgress()) |
659 | - for line in open(scandata): |
660 | - (march, comp, pkg, bin) = line.split("|") |
661 | - if march != arch: |
662 | - continue |
663 | - # check if package is in cache |
664 | - if not pkg in cache: |
665 | - print "ERROR: '%s' is not in cache" % pkg |
666 | - continue |
667 | - # check if the component is correct |
668 | - if not "/" in cache[pkg].section: |
669 | - realcomp = "main" |
670 | - else: |
671 | - realcomp = cache[pkg].section.split("/")[0] |
672 | - if comp != realcomp: |
673 | - print "ERROR: '%s' is in wrong component (claims '%s' but is in '%s'" % (pkg, comp, realcomp) |
674 | - print "done\n" |
675 | + cache.open(apt.progress.base.OpProgress()) |
676 | + with open(scandata) as scanfile: |
677 | + for line in scanfile: |
678 | + (march, comp, pkg, bin) = line.split("|") |
679 | + if march != arch: |
680 | + continue |
681 | + # check if package is in cache |
682 | + if not pkg in cache: |
683 | + print("ERROR: '%s' is not in cache" % pkg) |
684 | + continue |
685 | + # check if the component is correct |
686 | + if not "/" in cache[pkg].section: |
687 | + realcomp = "main" |
688 | + else: |
689 | + realcomp = cache[pkg].section.split("/")[0] |
690 | + if comp != realcomp: |
691 | + print("ERROR: '%s' is in wrong component (claims '%s' but is in '%s'" % (pkg, comp, realcomp)) |
692 | + print("done\n") |
693 | |
694 | === modified file 'bash_command_not_found' |
695 | --- bash_command_not_found 2008-03-07 09:21:09 +0000 |
696 | +++ bash_command_not_found 2012-06-13 16:12:19 +0000 |
697 | @@ -6,7 +6,7 @@ |
698 | |
699 | command_not_found_handle() { |
700 | if [ -x /usr/lib/command-not-found ]; then |
701 | - /usr/bin/python /usr/lib/command-not-found -- "$1" |
702 | + /usr/lib/command-not-found -- "$1" |
703 | return $? |
704 | else |
705 | return 127 |
706 | |
707 | === modified file 'command-not-found' |
708 | --- command-not-found 2012-01-13 11:11:20 +0000 |
709 | +++ command-not-found 2012-06-13 16:12:19 +0000 |
710 | @@ -2,7 +2,7 @@ |
711 | # (c) Zygmunt Krynicki 2005, 2006, 2007, 2008 |
712 | # Licensed under GPL, see COPYING for the whole text |
713 | |
714 | -from __future__ import absolute_import |
715 | +from __future__ import absolute_import, print_function |
716 | |
717 | |
718 | __version__ = "0.2.44" |
719 | @@ -22,7 +22,10 @@ |
720 | |
721 | def enable_i18n(): |
722 | cnf = gettext.translation("command-not-found", fallback=True) |
723 | - cnf.install(unicode=True) |
724 | + kwargs = {} |
725 | + if sys.version < '3': |
726 | + kwargs["unicode"] = True |
727 | + cnf.install(**kwargs) |
728 | locale.setlocale(locale.LC_ALL, '') |
729 | |
730 | |
731 | @@ -53,7 +56,8 @@ |
732 | |
733 | def main(): |
734 | enable_i18n() |
735 | - fix_sys_argv() |
736 | + if sys.version < '3': |
737 | + fix_sys_argv() |
738 | parser = LocaleOptionParser( |
739 | version=__version__, |
740 | usage=_("%prog [options] <command-name>")) |
741 | @@ -69,10 +73,8 @@ |
742 | (options, args) = parser.parse_args() |
743 | if len(args) == 1: |
744 | cnf = CommandNotFound(options.data_dir) |
745 | - # Note: we need to encode program name to utf-8 as gdbm does not seem |
746 | - # to handle unicode strings. |
747 | - if not cnf.advise(args[0].encode('utf-8'), options.ignore_installed) and not options.no_failure_msg: |
748 | - print >>sys.stderr, _("%s: command not found") % args[0] |
749 | + if not cnf.advise(args[0], options.ignore_installed) and not options.no_failure_msg: |
750 | + print(_("%s: command not found") % args[0], file=sys.stderr) |
751 | |
752 | if __name__ == "__main__": |
753 | crash_guard(main, BUG_REPORT_URL, __version__) |
47 + if key is not bytes:
I think you meant isinstance() here
74 - (PACKAGE, BASENAME_PATH) = range(2)
75 + (PACKAGE, BASENAME_PATH) = list(range(2))
299 - (SOFT, HARD) = range(2)
300 + (SOFT, HARD) = list(range(2))
(and some more), those changes seem unneeded, am I missing something?
Otherwise, wow, lots of old code that brings back memories, great work in porting that to python3! :)