check_po_status.py 2.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3

# this script takes one po file from the repo and compares it to the new file
# generated by lektor to see if there are new strings to add.
# written by emmapeel, sponsored by the Tor Project.
# this script is published under the GPL v3 license.
# requires debian packages: python3-polib, gettext
'''
This script checks a directory with .po files for errors with links.
Translation errors with links are the most common errors on the
Tor Project websites.

Usage:

./check_po_status.py [BRANCHNAME]

Where branchname is the name of the translation branch and transifex component

'''
import sys
import os
import requests

try:
    import polib
except ImportError:
    sys.exit("You need to install python3-polib to use this program.")

try:
    BRANCHNAME = sys.argv[1]
except IndexError:
emmapeel's avatar
emmapeel committed
32
    sys.exit("Please run me with a lektor-based translation branch, i.e. `../check_po_status.py communitytpo-contentspot`")
33
34
35
36
37
38
39
40
41


def get_remote_po():
    '''
    we download a .po file from our translation repo so we can compare it to
    the new file we have generated after building the website
    '''
    po_link = 'https://gitweb.torproject.org/translation.git/plain/contents+es.po?h=' + BRANCHNAME
    r = requests.get(po_link, allow_redirects=True)
42
43
44
45
46
    try:
        open('oldcontents+es.po', 'wb').write(r.content)
        pofile = polib.pofile('oldcontents+es.po')
    except OSError:
        sys.exit("Please add the correct branch from https://gitweb.torproject.org/translation.git/ for this website when running me")
47
48
49
50
    return pofile

def get_local_po():
    ''' this is the local file that may have been modified by the build'''
51
52
53
54
    try:
        pofile = polib.pofile('i18n/contents+es.po')
    except OSError:
        sys.exit("Please run me after building lektor at least once")
55
56
57
58
59
60
61
62
    return pofile


def compare_pos(oldpo, newpo):
    ''' we compare both .po files to see if there are changes'''
    newstrings, stalestrings = [], []
    for entry in newpo:
        if entry not in oldpo:
63
            print(F'new string: {entry.msgid}')
64
65
66
67
            newstrings.append(entry.msgid)

    for entry in oldpo:
        if entry not in newpo:
68
            print(F'ERROR - stale string: {entry.msgid}')
69
70
71
72
73
74
75
76
77
78
79
80
            stalestrings.append(entry.msgid)

    return newstrings, stalestrings


newstrings, stalestrings = compare_pos(get_remote_po(), get_local_po())

if newstrings != []:
    print(F'There are {len(newstrings)} new strings that are not in Transifex')

if stalestrings != []:
    print(F'There are {len(stalestrings)} strings in Transifex that are not needed anymore')
emmapeel's avatar
emmapeel committed
81
82

os.remove('oldcontents+es.po')