Skip to content

Commit b014b4d

Browse files
committed
Create proper string extraction tool
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
1 parent 72a1faa commit b014b4d

2 files changed

Lines changed: 98 additions & 25 deletions

File tree

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import click
1919
import mmdzanata
2020
import requests
21-
2221
from 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="""
7681
The Zanata project
7782
(Default: fedora-modularity-translations)
@@ -82,37 +87,70 @@ def get_tags_for_fedora_branch(branch):
8287
The 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

117155
if __name__ == "__main__":
118-
main()
156+
cli(obj={})

setup.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
# This file is part of mmdzanata
4+
# Copyright (C) 2017-2018 Stephen Gallagher
5+
#
6+
# Fedora-License-Identifier: MIT
7+
# SPDX-2.0-License-Identifier: MIT
8+
# SPDX-3.0-License-Identifier: MIT
9+
#
10+
# This program is free software.
11+
# For more information on the license, see COPYING.
12+
# For more information on free software, see
13+
# <https://www.gnu.org/philosophy/free-sw.en.html>.
14+
15+
from setuptools import setup
16+
17+
setup(
18+
name='mmdzanata',
19+
version='0.1',
20+
packages=['mmdzanata'],
21+
url='',
22+
license='MIT',
23+
author='Stephen Gallagher',
24+
author_email='sgallagh@redhat.com',
25+
description='Tools for working with translations of modulemd',
26+
install_requires=[
27+
'click',
28+
'koji',
29+
'requests',
30+
'babel',
31+
],
32+
entry_points={
33+
'console_scripts': ['mmdzanata=mmdzanata.cli:cli'],
34+
},
35+
)

0 commit comments

Comments
 (0)