Updated generator script to 0.2. Now the script can generate documentation
[e_cidadania.git] / docs / scripts / generate-docs.py
blobf1aeb515f335de48e20bc7408ae221b8cc7a9b18
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """ This script download the latest git version of e-cidadania, compiles
5 the documentation and places it in the documentation website
6 """
8 import sys
9 import os
10 import subprocess
11 import argparse
14 __author__ = "Oscar Carballal Prego"
15 __license__ = "GPLv3"
16 __version__ = "0.2"
17 __email__ = "oscar.carballal@cidadania.coop"
18 __status__ = "Stable/Production"
20 class Documents():
22 """
23 Document class.
24 """
25 def __init__(self):
27 """
28 Declare variables.
29 """
30 self.cwd = os.getcwd()
31 self.langs = ["es", "en", "gl"]
32 self.formats = ["html", "latex", "latexpdf"]
34 # We don't include cidadania's server repository because it
35 # needs authentication and some specific settings.
36 self.repos = [
37 "git://github.com/cidadania/e-cidadania.git",
38 "git://github.com/oscarcp/e-cidadania.git",
39 "git://gitorious.org/e-cidadania/mainline.git",
40 "git://repo.or.cz/e_cidadania.git",
43 def download_code(self, branch='master'):
45 """
46 Download the latest code from the e-cidadania repositories. It the
47 clone fails it will try with the next repository until it finds
48 a working one.
49 """
50 i = 0
51 print "\n >> Getting e-cidadania codebase from %s..." % self.repos[i].split('/')[2]
52 print "DEBUG: BRANCH=%s" % branch
53 done = False
54 while not done:
55 if i <= (len(self.repos) - 1):
56 try:
57 get_code = subprocess.check_call('git clone -b ' + branch + ' ' + self.repos[i] + ' ../ecidadania > /dev/null 2>&1', shell=True)
58 done = True
59 except:
60 print " -- Couldn't get the code from %s" % self.repos[i].split('/')[2]
61 i += 1
62 else:
63 import shutil
64 print "\n EE Couldn't get the e-cidadania codebase. This can be caused by an old copy of the codebase."
65 print " -- Trying to delete the old codebase..."
66 try:
67 os.chdir('../')
68 shutil.rmtree('ecidadania/')
69 print " -- Code succesfully deleted. Please run the application again.\n"
70 os.chdir('scripts/')
71 except:
72 print " -- There was some error trying to delete the old codebase. Exiting.\n"
73 sys.exit()
75 def compile_docs(self):
77 """
78 Compile all the documentation and languages at once.
79 """
80 os.chdir(self.cwd + '/../ecidadania/docs/')
81 sys.stdout.write("\n >> Compiling documentation... ")
82 sys.stdout.flush()
84 i = 0
85 done = False
86 while not done:
87 if i < (len(self.formats) - 1):
88 try:
89 sys.stdout.write('(%s) ' % self.formats[i])
90 sys.stdout.flush()
91 gen_docs = subprocess.check_call('make ' + self.formats[i] + ' > /dev/null 2>&1', shell=True)
92 if gen_docs == 0:
93 i += 1
94 except:
95 print " -- Couldn't compile the %s documentation." % self.formats[i]
96 i += 1
97 elif i == (len(self.formats) - 1):
98 try:
99 sys.stdout.write('(%s) ' % self.formats[i])
100 sys.stdout.flush()
101 gen_docs = subprocess.check_call('make ' + self.formats[i] + ' > /dev/null 2>&1', shell=True)
102 if gen_docs == 0:
103 i += 1
104 done = True
105 except:
106 print " -- Couldn't compile the %s documentation." % self.formats[i]
107 i += 1
108 else:
109 sys.exit("\n EE Couldn't generate documentation. Exiting.\n")
110 print "\n"
112 def pack_latex(self):
115 Package the LaTeX documentation into a tar.gz
117 print " >> Packaging the LaTeX files..."
118 import tarfile
120 os.chdir(os.getcwd() + '/build/latex/')
121 i = 0
122 while i <= (len(self.langs) - 1):
123 tar = tarfile.open(os.getcwd() + "/../../%s/latest-%s.tar.gz" % (self.langs[i], self.langs[i]), "w:gz")
124 tar.add(self.langs[i])
125 tar.close()
126 i += 1
129 def copy_docs(self):
132 Copy the generated documentation into their respective directories.
134 os.chdir("../../")
136 c = 0
137 while c <= (len(self.formats) - 1):
138 print " >> Copying the %s documentation..." % self.formats[c]
139 sys.stdout.write(" >> done ")
140 sys.stdout.flush()
142 i = 0
143 while i <= (len(self.langs) - 1):
144 if self.formats[c] == 'latexpdf':
145 try:
146 copy_latexpdf = subprocess.check_call('cp -R build/latex/' + self.langs[i] + '/e-cidadania.pdf ../../' + self.langs[i] + '/latest-' + self.langs[i] + '.pdf', shell=True)
147 except:
148 print " -- Couldn't copy the " + self.langs[i] + " documentation."
149 pass
150 sys.stdout.write("(%s) " % self.langs[i])
151 sys.stdout.flush()
152 i += 1
153 elif self.formats[c] == 'html':
154 try:
155 copy_html = subprocess.check_call('cp -R build/' + self.formats[c] + '/' + self.langs[i] + '/* ../../' + self.langs[i] + '/latest', shell=True)
156 except:
157 print " -- Couldn't copy the " + self.langs[i] + " documentation."
158 pass
159 sys.stdout.write("(%s) " % self.langs[i])
160 sys.stdout.flush()
161 i += 1
162 elif self.formats[c] == 'latex':
163 try:
164 copy_latex = subprocess.check_call('cp -R ' + self.langs[i] + '/latest-' + self.langs[i] + '.tar.gz' + ' ../../' + self.langs[i], shell=True)
165 except:
166 print " -- Couldn't copy the " + self.langs[i] + " documentation."
167 print " EE Couldn't copy one or all the documentation! Exiting."
168 sys.exit(1)
169 sys.stdout.write("(%s) " % self.langs[i])
170 sys.stdout.flush()
171 i += 1
172 print "\n"
173 c += 1
175 def make_all(self, branch):
176 if len(sys.argv) == 1:
177 self.download_code(branch)
178 else:
179 self.download_code(sys.argv[1])
180 self.compile_docs()
181 self.pack_latex()
182 self.copy_docs()
184 doc = Documents()
185 if len(sys.argv) == 1:
186 doc.make_all('master')
187 else:
188 doc.make_all(sys.argv[1])