Skip to content

Commit 34ed3b1

Browse files
committed
Support Python 2
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
1 parent 03f8944 commit 34ed3b1

3 files changed

Lines changed: 64 additions & 54 deletions

File tree

mmdzanata/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# This module provides utility functions for interacting with Zanata
1515
# translations of Fedora-style modules.
1616

17+
from __future__ import print_function
18+
1719
import sys
1820
import gi
1921
import requests

mmdzanata/cli.py

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# For more information on free software, see
1212
# <https://www.gnu.org/philosophy/free-sw.en.html>.
1313

14+
from __future__ import print_function
15+
1416
import click
1517
import gi
1618
import koji
@@ -20,9 +22,9 @@
2022
import subprocess
2123
import sys
2224
import shutil
25+
import tempfile
2326

2427
from babel.messages import pofile
25-
from tempfile import TemporaryDirectory
2628

2729
gi.require_version('Modulemd', '1.0')
2830
from gi.repository import Modulemd
@@ -112,59 +114,63 @@ def extract(ctx, upload):
112114
ctx.parent.obj['branch']),
113115
debug=ctx.parent.obj['debug'])
114116

115-
with TemporaryDirectory() as tdir:
116-
po_basename = "%s.pot" % ctx.parent.obj['zanata_translation_document']
117-
potfile = "%s/%s" % (tdir, po_basename)
118-
119-
with open(potfile, mode="wb") as f:
120-
pofile.write_po(f, catalog, sort_by_file=True)
121-
122-
# Optionally upload the extracted strings directly to Zanata
123-
if upload:
124-
# Use the zanata-cli to upload the pot file
125-
# It would be better to use the REST API directly here, but the XML
126-
# payload format is not documented.
127-
128-
# First ensure that the requested branch exists in Zanata
129-
zanata_args = [
130-
'/usr/bin/zanata-cli', '-B', '-e', 'put-version',
131-
'--url', ctx.parent.obj['zanata_url'],
132-
'--version-project', ctx.parent.obj['zanata_project'],
133-
'--version-slug', ctx.parent.obj['branch'],
134-
'--user-config', ctx.parent.obj['zanata_user_config']
135-
]
136-
result = subprocess.run(zanata_args, capture_output=True)
137-
if result.returncode or ctx.parent.obj['debug']:
138-
print(result.stderr.decode('utf-8'))
139-
print(result.stdout.decode('utf-8'))
140-
if result.returncode:
141-
sys.exit(1)
142-
143-
# Update the translatable strings for this branch
144-
zanata_args = [
145-
'/usr/bin/zanata-cli', '-B', '-e', 'push',
146-
'--url', ctx.parent.obj['zanata_url'],
147-
'--project', ctx.parent.obj['zanata_project'],
148-
'--project-type', 'gettext',
149-
'--project-version', ctx.parent.obj['branch'],
150-
'--src-dir', tdir,
151-
'--user-config', ctx.parent.obj['zanata_user_config']
152-
]
153-
result = subprocess.run(zanata_args, capture_output=True)
154-
if result.returncode or ctx.parent.obj['debug']:
155-
print(result.stderr.decode('utf-8'))
156-
print(result.stdout.decode('utf-8'))
157-
if result.returncode:
158-
sys.exit(2)
159-
160-
print("Uploaded translatable strings for %s to Zanata" % (
161-
ctx.parent.obj['branch']))
162-
163-
else:
164-
# Move the temporary path to the current directory
165-
shutil.move(potfile, po_basename)
166-
print("Wrote extracted strings for %s to %s" % (ctx.obj['branch'],
167-
po_basename))
117+
# Create a temporary directory to hold data while we generate it
118+
tdir = tempfile.mkdtemp()
119+
120+
po_basename = "%s.pot" % ctx.parent.obj['zanata_translation_document']
121+
potfile = "%s/%s" % (tdir, po_basename)
122+
123+
with open(potfile, mode="wb") as f:
124+
pofile.write_po(f, catalog, sort_by_file=True)
125+
126+
# Optionally upload the extracted strings directly to Zanata
127+
if upload:
128+
# Use the zanata-cli to upload the pot file
129+
# It would be better to use the REST API directly here, but the XML
130+
# payload format is not documented.
131+
132+
# First ensure that the requested branch exists in Zanata
133+
zanata_args = [
134+
'/usr/bin/zanata-cli', '-B', '-e', 'put-version',
135+
'--url', ctx.parent.obj['zanata_url'],
136+
'--version-project', ctx.parent.obj['zanata_project'],
137+
'--version-slug', ctx.parent.obj['branch'],
138+
'--user-config', ctx.parent.obj['zanata_user_config']
139+
]
140+
result = subprocess.run(zanata_args, capture_output=True)
141+
if result.returncode or ctx.parent.obj['debug']:
142+
print(result.stderr.decode('utf-8'))
143+
print(result.stdout.decode('utf-8'))
144+
if result.returncode:
145+
sys.exit(1)
146+
147+
# Update the translatable strings for this branch
148+
zanata_args = [
149+
'/usr/bin/zanata-cli', '-B', '-e', 'push',
150+
'--url', ctx.parent.obj['zanata_url'],
151+
'--project', ctx.parent.obj['zanata_project'],
152+
'--project-type', 'gettext',
153+
'--project-version', ctx.parent.obj['branch'],
154+
'--src-dir', tdir,
155+
'--user-config', ctx.parent.obj['zanata_user_config']
156+
]
157+
result = subprocess.run(zanata_args, capture_output=True)
158+
if result.returncode or ctx.parent.obj['debug']:
159+
print(result.stderr.decode('utf-8'))
160+
print(result.stdout.decode('utf-8'))
161+
if result.returncode:
162+
sys.exit(2)
163+
164+
print("Uploaded translatable strings for %s to Zanata" % (
165+
ctx.parent.obj['branch']))
166+
167+
else:
168+
# Move the temporary path to the current directory
169+
shutil.move(potfile, po_basename)
170+
print("Wrote extracted strings for %s to %s" % (ctx.obj['branch'],
171+
po_basename))
172+
# Clean up the temporary directory
173+
shutil.rmtree(tdir)
168174

169175

170176
##############################################################################

mmdzanata/fedora/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# For more information on free software, see
1212
# <https://www.gnu.org/philosophy/free-sw.en.html>.
1313

14+
from __future__ import print_function
15+
1416
import requests
1517

1618
KOJI_URL = 'https://koji.fedoraproject.org/kojihub'

0 commit comments

Comments
 (0)