update_po_authors.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # A simple script that adds all authors from transifex, which are
  4. # listed in comments at the beginning of the file, to the
  5. # 'translator-credits' translations - where launchpad added them
  6. # and which are shown in stk.
  7. #
  8. # First rename the transifex files:
  9. # for i in supertuxkartpot_*; do mv $i ${i##supertuxkartpot_}; done
  10. #
  11. # Usage: update_po_authors.py PATH_TO/LANGUAGE.po
  12. #
  13. # IMPORTANT note: this script must be run on a file downloaded
  14. # from transifex, not on a file on which this script had
  15. # been run before (otherwise the transifex authors would
  16. # be added again and again)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  17. #
  18. # Less important note: The specified file is overwritten without
  19. # a backup! Make sure the git status is unmodified.
  20. #
  21. import re
  22. import sys
  23. if __name__ == "__main__":
  24. if len(sys.argv) < 2:
  25. print("Usage: getpo_authors.py PATH_TO_PO_FILE")
  26. sys.exit(-1)
  27. for filename in sys.argv[1:]:
  28. print("Processing file %s" % filename)
  29. f = open(filename, "r")
  30. if not f:
  31. print("Can not find", filename)
  32. exit
  33. lines = f.readlines()
  34. f.close()
  35. # There are two sources of contributors:
  36. # 1) Entries in the header from transifex (after "Translators:"
  37. # till the first msgid string.
  38. # 2) Entries as "translator-credits". These shouldn't be there,
  39. # but sometimes occur (e.g. because an old version of this
  40. # script was used that did not support these entries).
  41. # Assemble all those entries in one line
  42. found = 0
  43. contributions = ""
  44. line_count = 0
  45. new_authors = []
  46. author_list = []
  47. # Find new and old authors with a simple finite state machine:
  48. # ============================================================
  49. i = 0
  50. while i < len(lines):
  51. line = lines[i][:-1] # remove \n
  52. i = i + 1
  53. if line=="# Translators:":
  54. while i <len(lines) and lines[i][:5]!="msgid":
  55. line = lines[i][:-1] # remove \n
  56. if line [:14]!="# FIRST AUTHOR":
  57. new_authors.append(line[2:])
  58. author_list.append(line[2:])
  59. i = i + 1
  60. elif line[:26] == "msgid \"translator-credits\"":
  61. line = lines[i][:-2] # remove \"\n at end of line
  62. # Ignore the fist entry (which is "msgstr ...").
  63. authors = line.split("\\n")[1:]
  64. for j in range(len(authors)):
  65. s = authors[j].strip()
  66. if s!="":
  67. author_list.append(s)
  68. # Then remove the msgid and msgstr
  69. del lines[i] # msgstr
  70. del lines[i-1] # msgid
  71. del lines[i-2] # filename/line number info
  72. del lines[i-3] # empty line
  73. # Delete all email addresses and URLs (esp. old launchpads)
  74. # ---------------------------------------------------------
  75. email = re.compile(r" *[^ ]+@[^ ]+ *")
  76. url = re.compile(r" *https?://[^ ]*")
  77. for i, author in enumerate(author_list):
  78. g = email.search(author)
  79. if g:
  80. author_list[i] = author[:g.start()] +", " \
  81. + author[g.end():]
  82. g = url.search(author)
  83. if g:
  84. author_list[i] = author[:g.start()] \
  85. + author[g.end():]
  86. # Remove duplicated entries
  87. # -------------------------
  88. d = {}
  89. new_list = []
  90. for i, author in enumerate(author_list):
  91. if not d.get(author, None):
  92. new_list.append(author)
  93. d[author]=1
  94. author_list = new_list
  95. all_authors_string = reduce(lambda x,y: x+"\\n"+y, author_list, "")
  96. credits_line = "msgstr \"Launchpad Contributions:%s\"\n"%all_authors_string
  97. lines.append("\n")
  98. lines.append("#: src/states_screens/credits.cpp:209\n")
  99. lines.append("msgid \"translator-credits\"\n")
  100. lines.append(credits_line)
  101. # Overwrite old file
  102. f = open(filename, "w")
  103. for i in lines:
  104. f.write(i)
  105. f.close()
  106. print("Done with %s,"% filename)