[PATCH] Maintaince script for l10n files and commits

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]