|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import os, sys, getopt, shutil |
| 5 | + |
| 6 | + |
| 7 | +def main(argv): |
| 8 | + input_file = '' |
| 9 | + try: |
| 10 | + opts, args = getopt.getopt(argv, "hvi:", ["file=", "version"]) |
| 11 | + except getopt.GetoptError: |
| 12 | + usage() |
| 13 | + sys.exit(2) |
| 14 | + for opt, arg in opts: |
| 15 | + if opt == '-h': |
| 16 | + usage() |
| 17 | + sys.exit() |
| 18 | + elif opt in ("-v", "--version"): |
| 19 | + version() |
| 20 | + sys.exit() |
| 21 | + elif opt in ("-i", "--file"): |
| 22 | + input_file = arg |
| 23 | + print 'Input file:', input_file |
| 24 | + format_file(input_file) |
| 25 | + |
| 26 | + |
| 27 | +def format_file(input_file): |
| 28 | + |
| 29 | + for root, folder_names, file_names in os.walk(input_file): |
| 30 | + for file_name in file_names: |
| 31 | + if file_name.endswith("_strings.xml"): |
| 32 | + # 获取语言前缀 |
| 33 | + prefix = file_name.split("_s")[0] |
| 34 | + print root, file_name, prefix |
| 35 | + new_dir = root + "/values-" + prefix |
| 36 | + # 创建values目录 |
| 37 | + if not os.path.exists(new_dir): |
| 38 | + os.mkdir(new_dir) |
| 39 | + new_file_path = new_dir + "/" + file_name |
| 40 | + # 移动字符文件 |
| 41 | + shutil.move(root + "/" + file_name, new_file_path) |
| 42 | + # 切换目录 |
| 43 | + os.chdir(new_dir) |
| 44 | + # 重命名文件 |
| 45 | + os.rename(file_name, "strings.xml") |
| 46 | + # 切回上一级目录 |
| 47 | + os.chdir("../") |
| 48 | + |
| 49 | + |
| 50 | +def usage(): |
| 51 | + print 'format_string_files.py -i <string file>' |
| 52 | + |
| 53 | + |
| 54 | +def version(): |
| 55 | + print "version:1.0.0" |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + main(sys.argv[1:]) |
0 commit comments