update_po_authors.py 3.4 KB

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