Merge ~boginw/langpack-o-matic:master into langpack-o-matic:master

Proposed by Bogi
Status: Merged
Approved by: Sebastien Bacher
Approved revision: 9dd4165257a7ecc562ea88061e184faff935fab8
Merged at revision: a1e6f3b2c5af4172c7a0225e2d344690be75b62f
Proposed branch: ~boginw/langpack-o-matic:master
Merge into: langpack-o-matic:master
Diff against target: 119 lines (+34/-8)
5 files modified
.gitignore (+5/-0)
README.md (+14/-0)
import (+11/-7)
lib/makepkg.py (+1/-1)
lib/static_translations.py (+3/-0)
Reviewer Review Type Date Requested Status
Sebastien Bacher Approve
Review via email: mp+433141@code.launchpad.net

Commit message

Added basic readme and made scripts compatible with Python 3

Description of the change

As Python 2 was sunset on the 1st of January 2020 if the project should stay useful it must be compatible with Python 3.

This merge proposal mainly focuses on the `import` script. Most of the code is already compatible with Python 3, but there are a few places where `urlopen` is used from `urllib2`, instead of `urllib.request`. This merge proposal proposes using a fallback import like is already used in `lib/static_translations.py`. Along with strings being used in contexts where binary strings are expected. In these places, `.encode()` is introduced.

Finally, this merge proposal introduces a basic README.md, including the prerequisites which I had to follow in order to get the import script to work.

To post a comment you must log in.
Revision history for this message
Sebastien Bacher (seb128) wrote :

Thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2new file mode 100644
3index 0000000..3ce1e87
4--- /dev/null
5+++ b/.gitignore
6@@ -0,0 +1,5 @@
7+updated-packages
8+bin/msgequal
9+__pycache__/
10+*.py[cod]
11+out
12diff --git a/README.md b/README.md
13new file mode 100644
14index 0000000..5e5840e
15--- /dev/null
16+++ b/README.md
17@@ -0,0 +1,14 @@
18+# Langpack-o-matic
19+
20+Prerequisites:
21+* libgettextpo-dev
22+* Python 3
23+* gettext
24+* debhelper
25+
26+Before running, `msgequal` will have to be compiled:
27+
28+```bash
29+cd bin
30+make
31+```
32diff --git a/import b/import
33index 48477ff..310ed7c 100755
34--- a/import
35+++ b/import
36@@ -1,4 +1,4 @@
37-#!/usr/bin/env python
38+#!/usr/bin/env python3
39
40 # this is part of langpack-o-matic, by Martin Pitt <martin.pitt@canonical.com>
41 #
42@@ -21,7 +21,11 @@ import tempfile
43 import shutil
44 import optparse
45 import logging
46-import urllib
47+try:
48+ from urllib.request import urlopen
49+except ImportError:
50+ # python 2 fallback
51+ from urllib2 import urlopen
52 import json
53
54 DEFAULT_MIRROR = 'http://archive.ubuntu.com/ubuntu'
55@@ -160,7 +164,7 @@ def write_po(locale, domain, pkgdir, contents):
56 # used msgequal for English.
57 msgequal = subprocess.Popen(['bin/msgequal', '-', dest],
58 stdin=subprocess.PIPE)
59- msgequal.communicate(contents)
60+ msgequal.communicate(contents.encode())
61 assert msgequal.returncode == 0
62 else:
63 f = open(dest, 'w')
64@@ -192,7 +196,7 @@ def normalize_po(contents):
65 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
66 stderr=subprocess.PIPE)
67 try:
68- (out, err) = msgfmt.communicate(contents)
69+ (out, err) = msgfmt.communicate(contents.encode())
70 except OSError as e:
71 logging.warning('msgfmt failed with OSError: %s, not normalizing', str(e))
72 return (contents, 0)
73@@ -210,9 +214,9 @@ def normalize_po(contents):
74 (out, err) = msgunfmt.communicate(out)
75
76 # remove X-Launchpad-Export-Date:
77- export_date = out.find('\n"X-Launchpad-Export-Date: ')
78+ export_date = out.find(b'\n"X-Launchpad-Export-Date: ')
79 if export_date >= 0:
80- out = out[:export_date] + out[out.index('\n', export_date + 1):]
81+ out = out[:export_date] + out[out.index(b'\n', export_date + 1):]
82 return (out, num_strings)
83
84
85@@ -402,7 +406,7 @@ def get_translatable_counts(release):
86 os.environ.get('TRANSLATION_STATS_URL', 'http://people.canonical.com/~people-l10n/data/ubuntu-l10n/'),
87 '%s_%s_potemplate-stats.json' % (distribution, release.split('-')[0]))
88
89- f = urllib.urlopen(url)
90+ f = urlopen(url)
91 data = json.load(f)
92 f.close()
93
94diff --git a/lib/makepkg.py b/lib/makepkg.py
95index e792f68..b8b1147 100644
96--- a/lib/makepkg.py
97+++ b/lib/makepkg.py
98@@ -70,7 +70,7 @@ def make_pkg(skeleton, path, lpmacros, extra_tar=None):
99 tardata = gzip.open(extra_tar).read()
100 else:
101 tardata = fcontents(extra_tar)
102- f = open(target, 'w')
103+ f = open(target, 'wb')
104 f.write(tardata)
105 f.close()
106
107diff --git a/lib/static_translations.py b/lib/static_translations.py
108index 5237a7b..3f104f2 100644
109--- a/lib/static_translations.py
110+++ b/lib/static_translations.py
111@@ -171,6 +171,9 @@ def _get_release_interval(distro, min, max):
112 max_num = _file_map('maps/releaseversions-' + distro, max.split('-')[0])
113 result = []
114
115+ def cmp(a, b):
116+ return (a > b) - (a < b)
117+
118 def _cmp(v1, v2):
119 '''Compare two Ubuntu release numbers'''
120

Subscribers

People subscribed via source and target branches