Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
#!/usr/bin/env python
2
3
import os, os.path, shutil, sys
4
import subprocess
5
6
CODIR = '/var/lib/pootle/checkouts'
7
LINKDIR = '/var/lib/pootle/translations'
8
9
def pipe(command):
10
    print ('Running ' + command)
11
    proc = subprocess.Popen(args = command,
12
                stdout = subprocess.PIPE,
13
                stderr = subprocess.PIPE,
14
                stdin = subprocess.PIPE,
15
                shell=True, close_fds=True)
16
    (output, error) = proc.communicate()
17
    ret = proc.returncode
18
    print output
19
    return output, error, ret
20
21
def make_symlink(src, dst):
22
    if not os.access(dst, os.F_OK):
23
        print ('  Linking ' + src + ' to ' + dst)
24
        # check if the relevant immediate toplevel directory exists
25
        if not os.access(os.path.dirname(dst), os.F_OK):
26
            os.mkdir (os.path.dirname(dst))
27
        os.symlink(src, dst)
28
        
29
def main():
30
    if len(sys.argv) < 4:
31
	print "Usage: " + sys.argv[0] + " category GIT_URL checkout_directory [branch]"
32
	print "Eg: " + sys.argv[0] + " xo_core git://dev.laptop.org/git/sugar sugar master"
33
	sys.exit()
34
    p_cat = sys.argv[1]
35
    git_url = sys.argv[2]
36
    p_dir = os.path.join(CODIR, p_cat, sys.argv[3])
37
    if len(sys.argv) == 5:
38
	branch = sys.argv[4]
39
    else:
40
	branch = "master" 
41
42
    # Check if the category directories exist. If not create them.
43
    if not os.access(os.path.join(CODIR, p_cat), os.F_OK):
44
        os.mkdir(os.path.join(CODIR, p_cat)) 
45
    if not os.access(os.path.join(LINKDIR, p_cat), os.F_OK):
46
        os.mkdir(os.path.join(LINKDIR, p_cat))
47
48
    cmd = 'git clone ' + git_url + ' ' + p_dir
49
    print '\n\n******** Cloning ' + git_url + ' to ' + p_dir + ' *********'
50
    pipe(cmd)
51
    os.chdir(p_dir)
52
    print '\n\n******** Switching to branch ' + branch + ' *********'
53
    cmd = ' git checkout --track -b ' + branch + ' origin/' + branch
54
    pipe(cmd)
55
    cmd = 'git pull'
56
    pipe(cmd)
57
    print '\n********* Git operation completed, moving to next stage ********'
58
59
    #FIXME: This works for now as all d.l.o projects since all use the GNU layout
60
    #FIXME: This needs to fixed in a later version
61
    podir = os.path.join(p_dir, 'po')
62
    if not os.access(podir, os.F_OK):
63
        print 'Error: Could not find PO file directory. Causes for this maybe non GNU layout for PO files or incorrect GIT URL.'
64
        sys.exit()
65
66
    potfile = None
67
    print '\n\n********* Searching for POT file *********'
68
    for i in os.listdir(podir):
69
        if i.endswith('.pot'):
70
            potfile = i
71
    if potfile == None: # This should not happen
72
        sys.exit()
73
    else:
74
        print '********* Found POT file ' + potfile + ' *********'
75
    
76
    print '********* Starting to symlink files *********'
77
    for i in os.listdir(podir):
78
        if i.endswith('.po'):
79
            src = os.path.join(podir, i)
80
            dst = os.path.join(LINKDIR, p_cat, i.replace('.po', ''), 
81
                    (os.path.basename(p_dir) +  ".po"))
82
            make_symlink(src, dst)
83
        if i.endswith('.pot'):
84
            src = os.path.join(podir, i)
85
            dst = os.path.join(LINKDIR, p_cat, 'templates', 
86
                    (os.path.basename(p_dir) + ".pot"))
87
            make_symlink(src,dst)
88
    print '********* Done Symlinking *********'
89
90
    
91
if __name__ == "__main__":
92
    main()