Fix compiler warning due to missing function prototype.
[svn.git] / tools / po / l10n-report.py
blobe51bf6aded32bd5a97b60d6a78b5b33c6ab11c94
1 #!/usr/bin/env python
3 """Usage: l10n-report.py [OPTION...]
5 Send the l10n translation status report to an email address. If the
6 email address is not specified, print in stdout.
8 Options:
10 -h, --help Show this help message.
12 -m, --to-email-id Send the l10n translation status report to this
13 email address.
14 """
16 import sys
17 import getopt
18 import os
19 import re
20 import subprocess
22 def usage_and_exit(errmsg=None):
23 """Print a usage message, plus an ERRMSG (if provided), then exit.
24 If ERRMSG is provided, the usage message is printed to stderr and
25 the script exits with a non-zero error code. Otherwise, the usage
26 message goes to stdout, and the script exits with a zero
27 errorcode."""
28 if errmsg is None:
29 stream = sys.stdout
30 else:
31 stream = sys.stderr
32 print >> stream, __doc__
33 if errmsg:
34 print >> stream, "\nError: %s" % (errmsg)
35 sys.exit(2)
36 sys.exit(0)
39 class l10nReport:
40 def __init__(self, to_email_id="bhuvan@collab.net"):
41 self.to_email_id = to_email_id
42 self.from_email_id = "<dev@subversion.tigris.org>"
44 def safe_command(self, cmd_and_args, cmd_in=""):
45 [stdout, stderr] = subprocess.Popen(cmd_and_args, \
46 stdin=subprocess.PIPE, \
47 stdout=subprocess.PIPE, \
48 stderr=subprocess.PIPE).communicate(input=cmd_in)
49 return stdout, stderr
51 def match(self, pattern, string):
52 match = re.compile(pattern).search(string)
53 if match and match.groups():
54 return match.group(1)
55 else:
56 return None
58 def get_msgattribs(self, file):
59 msgout = self.safe_command(['msgattrib', '--translated', file])[0]
60 grepout = self.safe_command(['grep', '-E', '^msgid *"'], msgout)[0]
61 sedout = self.safe_command(['sed', '1d'], grepout)[0]
62 trans = self.safe_command(['wc', '-l'], sedout)[0]
64 msgout = self.safe_command(['msgattrib', '--untranslated', file])[0]
65 grepout = self.safe_command(['grep', '-E', '^msgid *"'], msgout)[0]
66 sedout = self.safe_command(['sed', '1d'], grepout)[0]
67 untrans = self.safe_command(['wc', '-l'], sedout)[0]
69 msgout = self.safe_command(['msgattrib', '--only-fuzzy', file])[0]
70 grepout = self.safe_command(['grep', '-E', '^msgid *"'], msgout)[0]
71 sedout = self.safe_command(['sed', '1d'], grepout)[0]
72 fuzzy = self.safe_command(['wc', '-l'], sedout)[0]
74 msgout = self.safe_command(['msgattrib', '--only-obsolete', file])[0]
75 grepout = self.safe_command(['grep', '-E', '^#~ msgid *"'], msgout)[0]
76 obsolete = self.safe_command(['wc', '-l'], grepout)[0]
78 return int(trans), int(untrans), int(fuzzy), int(obsolete)
80 def pre_l10n_report(self):
81 # svn revert --recursive subversion/po
82 cmd = ['svn', 'revert', '--recursive', 'subversion/po']
83 stderr = self.safe_command(cmd)[1]
84 if stderr:
85 print >> sys.stderr, "\nError: %s" % (stderr)
86 sys.exit(0)
88 # svn update
89 cmd = ['svn', 'update']
90 stderr = self.safe_command(cmd)[1]
91 if stderr:
92 print >> sys.stderr, "\nError: %s" % (stderr)
93 sys.exit(0)
95 # tools/po/po-update.sh
96 cmd = ['sh', 'tools/po/po-update.sh']
97 self.safe_command(cmd)
100 def main():
101 # Parse the command-line options and arguments.
102 try:
103 opts, args = getopt.gnu_getopt(sys.argv[1:], "hm:",
104 ["help",
105 "to-email-id=",
107 except getopt.GetoptError, msg:
108 usage_and_exit(msg)
110 to_email_id = None
111 for opt, arg in opts:
112 if opt in ("-h", "--help"):
113 usage_and_exit()
114 elif opt in ("-m", "--to-email-id"):
115 to_email_id = arg
117 l10n = l10nReport()
118 os.chdir("%s/../.." % os.path.dirname(os.path.abspath(sys.argv[0])))
119 l10n.pre_l10n_report()
120 [info_out, info_err] = l10n.safe_command(['svn', 'info'])
121 if info_err:
122 print >> sys.stderr, "\nError: %s" % (info_err)
123 sys.exit(0)
125 po_dir = 'subversion/po'
126 branch_name = l10n.match('URL:.*/svn/(\S+)', info_out)
127 [info_out, info_err] = l10n.safe_command(['svnversion', po_dir])
128 if info_err:
129 print >> sys.stderr, "\nError: %s" % (info_err)
130 sys.exit(0)
132 wc_version = re.sub('[MS]', '', info_out)
133 title = "Translation status report for %s r%s" % \
134 (branch_name, wc_version)
136 os.chdir(po_dir)
137 files = os.listdir('.')
138 files.sort()
139 format_head = "%6s %7s %7s %7s %7s" % ("lang", "trans", "untrans",
140 "fuzzy", "obs")
141 format_line = "--------------------------------------"
142 print "\n%s\n%s\n%s" % (title, format_head, format_line)
144 body = ""
145 for file in files:
146 lang = l10n.match('(.*).po$', file)
147 if not lang:
148 continue
149 [trans, untrans, fuzzy, obsolete] = l10n.get_msgattribs(file)
150 po_format = "%6s %7d %7d %7d %7d" %\
151 (lang, trans, untrans, fuzzy, obsolete)
152 body += "%s\n" % po_format
153 print po_format
155 if to_email_id:
156 email_from = "From: SVN DEV <noreply@subversion.tigris.org>"
157 email_to = "To: %s" % to_email_id
158 email_sub = "Subject: [l10n] Translation status report for %s r%s" \
159 % (branch_name, wc_version)
161 msg = "%s\n%s\n%s\n%s\n%s\n%s\n%s" % (email_from, email_to,\
162 email_sub, title, format_head, format_line, body)
164 cmd = ['sendmail', '-t']
165 l10n.safe_command(cmd, msg)
166 print "The report is sent to '%s' email id." % to_email_id
167 else:
168 print "\nYou have not passed '-m' option, so email is not sent."
170 if __name__ == "__main__":
171 main()