|
11 | 11 | # For more information on free software, see |
12 | 12 | # <https://www.gnu.org/philosophy/free-sw.en.html>. |
13 | 13 |
|
| 14 | +from __future__ import print_function |
| 15 | + |
14 | 16 | import click |
15 | 17 | import gi |
16 | 18 | import koji |
|
20 | 22 | import subprocess |
21 | 23 | import sys |
22 | 24 | import shutil |
| 25 | +import tempfile |
23 | 26 |
|
24 | 27 | from babel.messages import pofile |
25 | | -from tempfile import TemporaryDirectory |
26 | 28 |
|
27 | 29 | gi.require_version('Modulemd', '1.0') |
28 | 30 | from gi.repository import Modulemd |
@@ -112,59 +114,63 @@ def extract(ctx, upload): |
112 | 114 | ctx.parent.obj['branch']), |
113 | 115 | debug=ctx.parent.obj['debug']) |
114 | 116 |
|
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) |
168 | 174 |
|
169 | 175 |
|
170 | 176 | ############################################################################## |
|
0 commit comments