git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jiang Xin <worldhello.net@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git List <git@vger.kernel.org>,
	avarab@gmail.com, Jiang Xin <worldhello.net@gmail.com>
Subject: [PATCH] Maintaince script for l10n files and commits
Date: Thu,  8 Mar 2012 02:47:14 +0800	[thread overview]
Message-ID: <1331146034-85804-1-git-send-email-worldhello.net@gmail.com> (raw)

Usage of this script:

 * rake commits      : Check commit logs written with non-ascii chars,
                       but without the correct encoding settings.
                       Always report Non-ascii in subject line as error.

 * rake pot          : Print the summary of the update of git.pot file

 * rake XX.po        : Create or update XX.po from the git.po tempolate file

 * rake check[XX.po] : Syntax check on XX.po

Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
---
 po/Rakefile |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 157 insertions(+)
 create mode 100644 po/Rakefile

diff --git a/po/Rakefile b/po/Rakefile
new file mode 100644
index 00000..e581b
--- /dev/null
+++ b/po/Rakefile
@@ -0,0 +1,157 @@
+require 'tempfile'
+
+POTFILE="git.pot"
+
+class NonAsciiInSubjectError < Exception
+end
+
+class BadEncodingError < Exception
+end
+
+def shellout(cmd)
+    pipe = IO.popen(cmd)
+    pipe.readlines
+end
+
+desc "Syntax check on XX.po, or all .po files if nothing provided."
+task :check, :po_file do |t, args|
+    if args[:po_file]
+        if File.exists? args[:po_file]
+            system("msgfmt -o /dev/null --check --statistics #{args[:po_file]}")
+        else
+            $stderr.puts "File #{args[:po_file]} does not exist."
+        end
+    else
+        FileList["*.po"].each do |po_file|
+            puts "=" * 72
+            puts "Check #{po_file}..."
+            system("msgfmt -o /dev/null --check --statistics #{po_file}")
+        end
+    end
+end
+
+desc "Show summary of updates of git.pot"
+task :pot do
+    status = shellout("git status --porcelain -- #{POTFILE}")
+    new = []
+    dropped = []
+    tmpfile = Tempfile.new('git.pot')
+    if status.empty?
+        puts "Nothing changed."
+    else
+        ENV["LANGUAGE"] = "C"
+        system("git show HEAD:./git.pot > #{tmpfile.path}")
+        msgcmp = shellout("msgcmp -N --use-untranslated #{tmpfile.path} #{POTFILE} 2>&1")
+        msgcmp.each do |line|
+            if m = /^.*:([0-9]+): this message is used but not defined in/.match(line)
+                new << m[1]
+            elsif m = /^.*:([0-9]+): warning: this message is not used/.match(line)
+                dropped << m[1]
+            end
+        end
+        puts "Update of #{POTFILE}:"
+        puts
+        if not new.empty?
+            puts " * Add #{new.count} new l10n string#{new.count>1 ? "s":""}" +
+                 " in the new generated \"git.pot\" file at" +
+                 " line#{new.count>1? "s":""}:"
+            puts "   " + new.join(", ")
+            puts
+        end
+        if not dropped.empty?
+            puts " * Remove #{dropped.count} l10n string#{dropped.count>1 ?
+                 "s":""} from the old \"git.pot\" file at line" +
+                 "#{dropped.count>1 ? "s":""}:"
+            puts "   " + dropped.join(", ")
+        end
+    end
+end
+
+# raise Exception if commit has bad encoding setting
+def verify_commit_encoding(commit, log)
+    subject = 0
+    non_ascii = nil
+    encoding = nil
+    log.each do |line|
+        if line.chomp!.empty?
+            # next line would be the commit log subject line,
+            # if no previous empty line found.
+            subject += 1
+            next
+        end
+        if subject == 0 and line =~ /^encoding /
+            encoding = line.chomp.sub(/^encoding /, '')
+        end
+        # non-ascii found in commit log
+        if match = /([^[:alnum:][:punct:][:space:]]+)/.match(line)
+            non_ascii = "#{line} << #{match[1][0..9]}"
+            # subject must be written in english
+            raise NonAsciiInSubjectError.new(non_ascii) if subject == 1
+        end
+        # subject has only one line
+        subject += 1 if subject == 1
+        # break if there are non-asciis and has already checked subject line
+        break if non_ascii && subject > 0
+    end
+
+    return if not non_ascii
+
+    encoding = 'UTF-8' if not encoding
+    cmd = "python -c \"s='''#{
+              log.collect!{
+                |x| x.chomp.gsub(/['"]/, "")
+              }.join(' - ')}'''; s.decode('#{encoding}')\" 2>/dev/null"
+    raise BadEncodingError.new(non_ascii) if not system(cmd)
+end
+
+desc "Check commits for bad encoding settings."
+task :commits, :from, :to do |t, args|
+    from = args[:from] || 'origin/master'
+    to = args[:to] || 'HEAD'
+    commits = shellout("git rev-list #{from}..#{to}")
+    commits.each do |c|
+        c.chomp!
+        log = shellout("git cat-file commit #{c}")
+        begin
+            verify_commit_encoding(c, log)
+        rescue BadEncodingError => e
+            $stderr.puts "=" * 78
+            $stderr.puts "Error: Bad encoding setting found in commit #{c[0,7]}:"
+            $stderr.puts "       >> #{e.message}"
+            $stderr.puts
+            log.each {|line| puts "\t" + line.chomp}
+        rescue NonAsciiInSubjectError => e
+            $stderr.puts "=" * 78
+            $stderr.puts "Error: Non-AscII found in subject in commit #{c[0,7]}:"
+            $stderr.puts "       >> #{e.message}"
+            $stderr.puts
+            log.each {|line| puts "\t" + line.chomp}
+        end
+    end
+end
+
+
+desc "Create or update XX.po file from git.pot"
+task "XX.po" do
+    $stderr.puts "Use your real locale file, such as zh_CN.po"
+end
+
+# Update XX.po even if timestamp of XX.po is newer
+FileList["*.po"].each do |t|
+    task t => POTFILE
+end
+
+rule '.po' => POTFILE do |t|
+    if File.exist?(t.name)
+        system("msgmerge --add-location --backup=off -U #{t.name} #{t.source}")
+    else
+        system("msginit -i #{t.source} --locale=#{t.name.sub(/.po$/, '')}")
+    end
+    mofile="build/locale/#{t.name.sub(/.po$/, '')}/LC_MESSAGES/git.mo"
+    FileUtils.mkdir_p File.dirname(mofile)
+    system("msgfmt -o #{mofile} --check --statistics #{t.name}")
+end
+
+task :default do
+    system("rake -T")
+end
-- 
1.7.9.2.330.gaa956.dirty

             reply	other threads:[~2012-03-07 18:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-07 18:47 Jiang Xin [this message]
2012-03-07 19:17 ` [PATCH] Maintaince script for l10n files and commits Junio C Hamano
2012-03-08 16:05   ` Jiang Xin
2012-03-08 20:41     ` Junio C Hamano
2012-03-09  0:57       ` Jiang Xin
2012-03-09  6:08       ` [PATCH v2] " Jiang Xin
2012-03-09  6:20         ` David Aguilar
2012-03-09  6:31           ` Jiang Xin
2012-03-10  0:40         ` Junio C Hamano
2012-03-10  9:17       ` [PATCH v3] " Jiang Xin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331146034-85804-1-git-send-email-worldhello.net@gmail.com \
    --to=worldhello.net@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).