update_po_authors.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # A simple script that adds all authors from transifex, which are
  3. # listed in comments at the beginning of the file, to the
  4. # 'translator-credits' translations - where launchpad added them
  5. # and which are shown in stk.
  6. #
  7. # Usage: update_po_authors.py PATH_TO/LANGUAGE.po
  8. #
  9. # IMPORTANT note: this script must be run on a file downloaded
  10. # from transifex, not on a file on which this script had
  11. # been run before (otherwise the transifex authors would
  12. # be added again and again)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  13. #
  14. # Less important note: The specified file is overwritten without
  15. # a backup! Make sure the git status is unmodified.
  16. import re
  17. import sys
  18. if __name__ == "__main__":
  19. if len(sys.argv)!=2:
  20. print "Usage: getpo_authors.py PATH_TO_PO_FILE"
  21. sys.exit(-1)
  22. f = open(sys.argv[1], "r")
  23. if not f:
  24. print "Can not find", sys.argv[1]
  25. exit
  26. lines = f.readlines()
  27. f.close()
  28. new_authors = []
  29. found = 0
  30. # Find all authors with a simple finite state machine:
  31. contributions = -1
  32. line_count = 0
  33. for i in lines:
  34. line = i[:-1] # remove \n
  35. if line=="# Translators:":
  36. found = 1
  37. elif found and line[:2]=="# " and line [:14]!="# FIRST AUTHOR":
  38. new_authors.append(line[2:])
  39. elif line[:5]=="msgid":
  40. found = 0
  41. elif line[:31]== "msgstr \"Launchpad Contributions":
  42. contributions = line_count
  43. line_count = line_count + 1
  44. # Delete all email addresses - not sure if the authors
  45. # would want them to be published
  46. email=re.compile(" *<.*@.*\..*> *") # one @ and one dot at least
  47. for i in range(len(new_authors)):
  48. g = email.search(new_authors[i])
  49. if g:
  50. new_authors[i] = new_authors[i][:g.start()] \
  51. + new_authors[i][g.end():]
  52. # Get the old authors from the translator-credits string:
  53. if contributions>0:
  54. # Ignore the first entry, which is "msgstr ...", and the
  55. # last two characters, which are the '"\n'.
  56. old_authors = lines[contributions][:-2].split("\\n")[1:]
  57. for i in range(len(old_authors)):
  58. old_authors[i] = old_authors[i].strip()
  59. else:
  60. old_authors=[]
  61. all_authors = old_authors + new_authors;
  62. all_authors = sorted(all_authors, key=lambda x: x.lower())
  63. all_authors_string = reduce(lambda x,y: x+"\\n"+y, all_authors, "")
  64. credits_line = "msgstr \"Launchpad Contributions:%s\"\n"%all_authors_string
  65. # If no old authors exists, write a new entry:
  66. if contributions==-1:
  67. lines.append("\n")
  68. lines.append("#: src/states_screens/credits.cpp:209\n")
  69. lines.append("msgid \"translator-credits\"\n")
  70. lines.append(credits_line)
  71. else:
  72. # Otherwise just replace the old contribution string
  73. lines[contributions] = credits_line
  74. # Overwrite old file
  75. f = open(sys.argv[1], "w")
  76. for i in lines:
  77. f.write(i)
  78. f.close()