1818import click
1919import mmdzanata
2020import requests
21-
2221from babel .messages import pofile
2322
2423
@@ -53,7 +52,12 @@ def get_tags_for_fedora_branch(branch):
5352 '%s-modular-updates-testing-pending' % branch ]
5453
5554
56- @click .command ()
55+ ##############################################################################
56+ # Common options for all commands #
57+ ##############################################################################
58+
59+ @click .group ()
60+ @click .option ('--debug/--no-debug' , default = False )
5761@click .option ('-k' , '--koji-url' ,
5862 default = 'https://koji.fedoraproject.org/kojihub' ,
5963 type = str , help = """
@@ -71,7 +75,8 @@ def get_tags_for_fedora_branch(branch):
7175(Default: https://fedora.zanata.org/)
7276""" ,
7377 metavar = "<zanata_project>" )
74- @click .option ('-p' , '--zanata-project' , default = "fedora-modularity-translations" ,
78+ @click .option ('-p' , '--zanata-project' ,
79+ default = "fedora-modularity-translations" ,
7580 type = str , help = """
7681The Zanata project
7782(Default: fedora-modularity-translations)
@@ -82,37 +87,70 @@ def get_tags_for_fedora_branch(branch):
8287The project version.
8388(Default: rawhide)
8489""" , metavar = "[f28, f29, rawhide, ...]" )
85- def main (branch , koji_url , zanata_url , zanata_project ,
86- zanata_project_version ):
90+ @click .pass_context
91+ def cli (ctx , debug , branch , koji_url , zanata_url , zanata_project ,
92+ zanata_project_version ):
93+
94+ ctx .obj = dict ()
95+ ctx .obj ['debug' ] = debug
96+
97+ ctx .obj ['session' ] = koji .ClientSession (koji_url )
98+
99+ if branch == "rawhide" :
100+ ctx .obj ['branch' ] = get_fedora_rawhide_version (ctx .obj ['session' ])
101+
102+ ctx .obj ['zanata_url' ] = zanata_url
103+ ctx .obj ['zanata_project' ] = zanata_project
104+ ctx .obj ['zanata_project_version' ] = zanata_project_version
105+
106+
107+ ##############################################################################
108+ # Subcommands #
109+ ##############################################################################
110+
111+ @cli .command ()
112+ @click .option ('--upload/--no-upload' , default = True ,
113+ help = 'Whether to automatically push extracted strings to '
114+ 'Zanata' )
115+ @click .option ('-p' , '--potfile' , default = "fedora-modularity-translations.pot" ,
116+ help = "The name of the gettext POT file to contain the "
117+ "extracted strings. (Default: "
118+ "fedora-modularity-translations.pot)" ,
119+ type = click .Path ())
120+ @click .pass_context
121+ def extract (ctx , upload , potfile ):
87122 """
88123 Extract translations from all modules included in a particular version of
89124 Fedora or EPEL.
90125 """
91- session = koji .ClientSession (koji_url )
92-
93- if branch == "rawhide" :
94- branch = get_fedora_rawhide_version (session )
95126
96127 catalog = mmdzanata .get_module_catalog_from_tags (
97- session , get_tags_for_fedora_branch (branch ), debug = True )
128+ ctx .parent .obj ['session' ], get_tags_for_fedora_branch (
129+ ctx .parent .obj ['branch' ]),
130+ debug = ctx .parent .obj ['debug' ])
98131
99- with open ("fedora-modularity-translations.pot" , mode = "wb" ) as f :
132+ with open (potfile , mode = "wb" ) as f :
100133 pofile .write_po (f , catalog , sort_by_file = True )
101134
102- # Use the zanata-cli to upload the pot file
103- # It would be better to use the REST API directly here, but the XML payload
104- # format is not documented.
105- zanata_args = ['/usr/bin/zanata-cli' , '-B' , 'push' ,
106- '--url' , zanata_url ,
107- '--project' , zanata_project ,
108- '--project-type' , 'gettext' ,
109- '--project-version' , zanata_project_version ]
110- result = subprocess .run (zanata_args , capture_output = True )
111- if result .returncode :
112- print (result .stderr )
113- print (result .stdout )
114- sys .exit (1 )
135+ print ("Wrote extracted strings to %s" % potfile )
136+
137+ # Optionally upload the extracted strings directly to Zanata
138+ if upload :
139+ # Use the zanata-cli to upload the pot file
140+ # It would be better to use the REST API directly here, but the XML
141+ # payload format is not documented.
142+ zanata_args = ['/usr/bin/zanata-cli' , '-B' , 'push' ,
143+ '--url' , ctx .parent .obj ['zanata_url' ],
144+ '--project' , ctx .parent .obj ['zanata_project' ],
145+ '--project-type' , 'gettext' ,
146+ '--project-version' , ctx .parent .obj [
147+ 'zanata_project_version' ]]
148+ result = subprocess .run (zanata_args , capture_output = True )
149+ if result .returncode :
150+ print (result .stderr )
151+ print (result .stdout )
152+ sys .exit (1 )
115153
116154
117155if __name__ == "__main__" :
118- main ( )
156+ cli ( obj = {} )
0 commit comments