1616import click
1717import gi
1818import koji
19- import mmdzanata
20- import mmdzanata .fedora
2119import os
20+ import os .path
2221import subprocess
2322import sys
2423import shutil
2928gi .require_version ('Modulemd' , '1.0' )
3029from gi .repository import Modulemd
3130
31+ from mmdzanata import get_module_catalog_from_tags , get_modulemd_translations
32+ from mmdzanata .fedora import get_fedora_rawhide_version , \
33+ get_tags_for_fedora_branch , KOJI_URL
3234
3335##############################################################################
3436# Common options for all commands #
3840@click .group ()
3941@click .option ('--debug/--no-debug' , default = False )
4042@click .option ('-k' , '--koji-url' ,
41- default = mmdzanata . fedora . KOJI_URL ,
43+ default = KOJI_URL ,
4244 type = str , help = "The URL of the Koji build system." ,
4345 show_default = True ,
4446 metavar = "<URL>" )
4547@click .option ('-b' , '--branch' , default = "rawhide" , type = str ,
4648 help = "The distribution release" ,
4749 metavar = "<branch_name>" )
48- @click .option ('-z' , '--zanata-url' ,
49- default = mmdzanata .fedora .ZANATA_URL ,
50- type = str , help = "The Zanata URL" ,
51- show_default = True ,
52- metavar = "<zanata_project>" )
53- @click .option ('-p' , '--zanata-project' ,
54- default = mmdzanata .fedora .ZANATA_PROJECT ,
55- type = str , help = "The Zanata project" ,
56- show_default = True ,
57- metavar = "<zanata_project>" )
58- @click .option ('-f' , '--zanata-translation-document' ,
59- default = mmdzanata .fedora .ZANATA_DOCUMENT ,
60- help = "The name of the translated file in Zanata." ,
61- show_default = True ,
62- metavar = "<translation_document>" )
63- @click .option ('-c' , '--zanata-user-config' ,
64- default = lambda : "%s/.config/zanata.ini" % (
65- os .environ .get ("HOME" , '~' )),
66- help = "Path to the Zanata User Config INI file" ,
67- type = click .Path (exists = True ))
6850@click .pass_context
69- def cli (ctx , debug , branch , koji_url , zanata_url , zanata_project ,
70- zanata_translation_document , zanata_user_config ):
51+ def cli (ctx , debug , branch , koji_url ):
7152 """Tools for managing modularity translations."""
7253
7354 ctx .obj = dict ()
@@ -78,13 +59,7 @@ def cli(ctx, debug, branch, koji_url, zanata_url, zanata_project,
7859 ctx .obj ['branch' ] = branch
7960
8061 if branch == "rawhide" :
81- ctx .obj ['branch' ] = mmdzanata .fedora .get_fedora_rawhide_version (
82- ctx .obj ['session' ])
83-
84- ctx .obj ['zanata_url' ] = zanata_url
85- ctx .obj ['zanata_project' ] = zanata_project
86- ctx .obj ['zanata_translation_document' ] = zanata_translation_document
87- ctx .obj ['zanata_user_config' ] = zanata_user_config
62+ ctx .obj ['branch' ] = get_fedora_rawhide_version (ctx .obj ['session' ])
8863
8964##############################################################################
9065# Subcommands #
@@ -96,114 +71,97 @@ def cli(ctx, debug, branch, koji_url, zanata_url, zanata_project,
9671
9772
9873@cli .command ()
99- @click .option ('--upload/--no-upload' , default = False ,
100- help = 'Whether to automatically push extracted strings to '
101- 'Zanata' ,
102- show_default = True )
74+ @click .option ('-p' , '--pot-file' ,
75+ default = 'fedora-modularity-translations.pot' ,
76+ type = click .File (mode = 'wb' , atomic = True , lazy = True ),
77+ show_default = True ,
78+ metavar = "<PATH>" ,
79+ help = "Path to the portable object template (POT) file to hold "
80+ "the translatable strings." )
10381@click .pass_context
104- def extract (ctx , upload ):
82+ def extract (ctx , pot_file ):
10583 """
10684 Extract translatable strings from modules.
10785
10886 Extract translations from all modules included in a particular version of
10987 Fedora or EPEL.
11088 """
11189
112- catalog = mmdzanata . get_module_catalog_from_tags (
113- ctx .parent .obj ['session' ], mmdzanata . fedora . get_tags_for_fedora_branch (
90+ catalog = get_module_catalog_from_tags (
91+ ctx .parent .obj ['session' ], get_tags_for_fedora_branch (
11492 ctx .parent .obj ['branch' ]),
11593 debug = ctx .parent .obj ['debug' ])
11694
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 )
95+ pofile .write_po (pot_file , catalog , sort_by_file = True )
96+
97+ print ("Wrote extracted strings for %s to %s" % (ctx .obj ['branch' ],
98+ pot_file .name ))
17499
175100
176101##############################################################################
177102# `mmdzanata generate_metadata` #
178103##############################################################################
179104
180105@cli .command ()
106+ @click .option ('-d' , '--pofile-dir' ,
107+ default = '.' ,
108+ help = "Path to a directory containing portable object (.po) "
109+ "translation files" ,
110+ type = click .Path (exists = True , dir_okay = True , resolve_path = True ,
111+ readable = True ))
112+ @click .option ('-y' , '--yaml-file' ,
113+ default = 'fedora-modularity-translations.yaml' ,
114+ type = click .File (mode = 'wb' , atomic = True , lazy = True ),
115+ show_default = True ,
116+ metavar = "<PATH>" ,
117+ help = "Path to the YAML file to hold the translated strings in "
118+ "modulemd-translations format." )
181119@click .pass_context
182- def generate_metadata (ctx ):
120+ def generate_metadata (ctx , pofile_dir , yaml_file ):
183121 """
184122 Generate modulemd-translations YAML.
185123
186124 :return: 0 on successful creation of modulemd-translation,
187125 nonzero on failure.
188126 """
189127
190- zanata_rest_url = "%s/rest" % ctx .parent .obj ['zanata_url' ]
128+ # Process all .po files in the provided directory
129+ translation_files = [f for f in os .listdir (pofile_dir ) if
130+ os .path .isfile ((os .path .join (pofile_dir , f ))) and
131+ f .endswith (".po" )]
132+ translations = get_modulemd_translations (translation_files ,
133+ debug = ctx .parent .obj ['debug' ])
134+
135+ yaml_file .write (Modulemd .dumps (sorted (translations )).encode ('utf-8' ))
136+
137+ print ("Wrote modulemd-translations YAML to %s" % yaml_file .name )
138+
139+
140+ @cli .command ()
141+
142+ @click .pass_context
143+ def experiment (ctx , pofile_dir ):
144+ """
145+ Experiment with Babel
146+ """
191147
192- translations = mmdzanata .get_modulemd_translations (
193- zanata_rest_url ,
194- ctx .parent .obj ['zanata_project' ],
195- ctx .parent .obj ['branch' ],
196- ctx .parent .obj ['zanata_translation_document' ],
197- ctx .parent .obj ['debug' ]
198- )
148+ # Process all .po files in the provided directory
149+ translation_files = [f for f in os .listdir (pofile_dir ) if
150+ os .path .isfile ((os .path .join (pofile_dir , f ))) and
151+ f .endswith (".po" )]
199152
200- translation_file = "%s-%s.yaml" % (
201- ctx .parent .obj ['zanata_translation_document' ],
202- ctx .parent .obj ['branch' ])
153+ catalogs = dict ()
154+ for f in translation_files :
155+ with open (f , 'r' ) as infile :
156+ catalog = pofile .read_po (infile )
157+ catalogs [catalog .locale_identifier ] = catalog
203158
204- Modulemd .dump (sorted (translations ), translation_file )
159+ translations = get_modulemd_translations_from_catalog_dict (
160+ catalogs )
205161
206- print ("Wrote modulemd-translations YAML to %s" % translation_file )
162+ if ctx .parent .obj ['debug' ]:
163+ for translation in translations :
164+ print (translation .dumps ())
207165
208166
209167if __name__ == "__main__" :
0 commit comments