|
17 | 17 | import koji |
18 | 18 | import mmdzanata |
19 | 19 | import mmdzanata.fedora |
20 | | -import os |
21 | 20 | import subprocess |
22 | 21 | import sys |
| 22 | +import shutil |
23 | 23 |
|
24 | 24 | from babel.messages import pofile |
| 25 | +from tempfile import TemporaryDirectory |
25 | 26 |
|
26 | 27 | gi.require_version('Modulemd', '1.0') |
27 | 28 | from gi.repository import Modulemd |
@@ -102,54 +103,53 @@ def extract(ctx, upload): |
102 | 103 | ctx.parent.obj['branch']), |
103 | 104 | debug=ctx.parent.obj['debug']) |
104 | 105 |
|
105 | | - try: |
106 | | - os.mkdir(ctx.parent.obj['branch'], mode=0o0770) |
107 | | - except OSError: |
108 | | - # Directory already exists |
109 | | - pass |
110 | | - |
111 | | - potfile = "%s/%s.pot" % ( |
112 | | - ctx.parent.obj['branch'], |
113 | | - ctx.parent.obj['zanata_translation_document']) |
114 | | - |
115 | | - with open(potfile, mode="wb") as f: |
116 | | - pofile.write_po(f, catalog, sort_by_file=True) |
117 | | - |
118 | | - print("Wrote extracted strings for %s to %s" % (ctx.obj['branch'], |
119 | | - potfile)) |
120 | | - |
121 | | - # Optionally upload the extracted strings directly to Zanata |
122 | | - if upload: |
123 | | - # Use the zanata-cli to upload the pot file |
124 | | - # It would be better to use the REST API directly here, but the XML |
125 | | - # payload format is not documented. |
126 | | - |
127 | | - # First ensure that the requested branch exists in Zanata |
128 | | - zanata_args = ['/usr/bin/zanata-cli', '-B', '-e', 'put-version', |
129 | | - '--url', ctx.parent.obj['zanata_url'], |
130 | | - '--version-project', ctx.parent.obj['zanata_project'], |
131 | | - '--version-slug', ctx.parent.obj['branch']] |
132 | | - result = subprocess.run(zanata_args, capture_output=True) |
133 | | - if result.returncode: |
134 | | - print(result.stderr.decode('utf-8')) |
135 | | - print(result.stdout.decode('utf-8')) |
136 | | - sys.exit(1) |
137 | | - |
138 | | - # Update the translatable strings for this branch |
139 | | - zanata_args = ['/usr/bin/zanata-cli', '-B', '-e', 'push', |
140 | | - '--url', ctx.parent.obj['zanata_url'], |
141 | | - '--project', ctx.parent.obj['zanata_project'], |
142 | | - '--project-type', 'gettext', |
143 | | - '--project-version', ctx.parent.obj['branch'], |
144 | | - '--src-dir', ctx.parent.obj['branch']] |
145 | | - result = subprocess.run(zanata_args, capture_output=True) |
146 | | - if result.returncode: |
147 | | - print(result.stderr.decode('utf-8')) |
148 | | - print(result.stdout.decode('utf-8')) |
149 | | - sys.exit(2) |
150 | | - |
151 | | - print("Uploaded translatable strings for %s to Zanata" % ( |
152 | | - ctx.parent.obj['branch'])) |
| 106 | + with TemporaryDirectory() as tdir: |
| 107 | + po_basename = "%s.pot" % ctx.parent.obj['zanata_translation_document'] |
| 108 | + potfile = "%s/%s" % (tdir, po_basename) |
| 109 | + |
| 110 | + with open(potfile, mode="wb") as f: |
| 111 | + pofile.write_po(f, catalog, sort_by_file=True) |
| 112 | + |
| 113 | + # Optionally upload the extracted strings directly to Zanata |
| 114 | + if upload: |
| 115 | + # Use the zanata-cli to upload the pot file |
| 116 | + # It would be better to use the REST API directly here, but the XML |
| 117 | + # payload format is not documented. |
| 118 | + |
| 119 | + # First ensure that the requested branch exists in Zanata |
| 120 | + zanata_args = ['/usr/bin/zanata-cli', '-B', '-e', 'put-version', |
| 121 | + '--url', ctx.parent.obj['zanata_url'], |
| 122 | + '--version-project', ctx.parent.obj['zanata_project'], |
| 123 | + '--version-slug', ctx.parent.obj['branch']] |
| 124 | + result = subprocess.run(zanata_args, capture_output=True) |
| 125 | + if result.returncode or ctx.parent.obj['debug']: |
| 126 | + print(result.stderr.decode('utf-8')) |
| 127 | + print(result.stdout.decode('utf-8')) |
| 128 | + if result.returncode: |
| 129 | + sys.exit(1) |
| 130 | + |
| 131 | + # Update the translatable strings for this branch |
| 132 | + zanata_args = ['/usr/bin/zanata-cli', '-B', '-e', 'push', |
| 133 | + '--url', ctx.parent.obj['zanata_url'], |
| 134 | + '--project', ctx.parent.obj['zanata_project'], |
| 135 | + '--project-type', 'gettext', |
| 136 | + '--project-version', ctx.parent.obj['branch'], |
| 137 | + '--src-dir', tdir] |
| 138 | + result = subprocess.run(zanata_args, capture_output=True) |
| 139 | + if result.returncode or ctx.parent.obj['debug']: |
| 140 | + print(result.stderr.decode('utf-8')) |
| 141 | + print(result.stdout.decode('utf-8')) |
| 142 | + if result.returncode: |
| 143 | + sys.exit(2) |
| 144 | + |
| 145 | + print("Uploaded translatable strings for %s to Zanata" % ( |
| 146 | + ctx.parent.obj['branch'])) |
| 147 | + |
| 148 | + else: |
| 149 | + # Move the temporary path to the current directory |
| 150 | + shutil.move(potfile, po_basename) |
| 151 | + print("Wrote extracted strings for %s to %s" % (ctx.obj['branch'], |
| 152 | + po_basename)) |
153 | 153 |
|
154 | 154 |
|
155 | 155 | ############################################################################## |
|
0 commit comments