1 | #!/usr/bin/env python |
---|
2 | import zipfile |
---|
3 | import shutil |
---|
4 | import os |
---|
5 | import re |
---|
6 | import unicodedata |
---|
7 | from tempfile import mkstemp |
---|
8 | from os import remove, close |
---|
9 | import sys |
---|
10 | from time import sleep |
---|
11 | |
---|
12 | name_zip=str(sys.argv[1]) |
---|
13 | #name_zip="/home/lliurex/essent.jclic.zip" |
---|
14 | extract_dir="/net/server-sync/share/jclic-aula/jclic_uploads" |
---|
15 | dir_jclic_orig="/net/server-sync/share/jclic-aula/jclic_uploads/0rig_jclic" |
---|
16 | #extract_dir="/tmp/jclic-aula" |
---|
17 | index="index.html" |
---|
18 | jclic_js="http://server/jclic-aula/jclic.min.js" |
---|
19 | jclic_plugin="http://server/jclic-aula/jclicplugin.js" |
---|
20 | jar_path_lib="http://server/jclic-aula/jars" |
---|
21 | sufix=".lock" |
---|
22 | |
---|
23 | #sys.exit() |
---|
24 | |
---|
25 | def title_file(line): |
---|
26 | try: |
---|
27 | line = unicodedata.normalize('NFKD', unicode(line, 'utf8')) |
---|
28 | line = line.encode('ascii', 'ignore') |
---|
29 | line = line.decode("utf-8") |
---|
30 | clean=re.compile('<.*?>') |
---|
31 | title=re.sub(clean, '', line) |
---|
32 | title=title.lstrip() |
---|
33 | title=unicode(title.strip('\n')) |
---|
34 | title=re.sub('\W+',' ', title ) |
---|
35 | print "Titulo: %s"%title |
---|
36 | return [True, title] |
---|
37 | |
---|
38 | except Exception as e: |
---|
39 | return [False,str(e)] |
---|
40 | |
---|
41 | #def title_file |
---|
42 | |
---|
43 | |
---|
44 | def remove_utf8(s): |
---|
45 | |
---|
46 | try: |
---|
47 | char="" |
---|
48 | |
---|
49 | if type(s) == str: |
---|
50 | s=s.decode("utf-8") |
---|
51 | |
---|
52 | for c in unicodedata.normalize("NFD",s): |
---|
53 | c = c.encode('ascii', 'ignore') |
---|
54 | c=c.lower() |
---|
55 | c=c.decode() |
---|
56 | c=c.replace("-", "_") |
---|
57 | |
---|
58 | #if len(c) > 0 and unicodedata.category(c) not in ['Mn','Ps','Pc',"Pe","Z","C","S"] : |
---|
59 | if len(c) > 0 and unicodedata.category(c) in ["Ll","Nd"] or c==".": |
---|
60 | char+=c |
---|
61 | |
---|
62 | else: |
---|
63 | if len(c)>0: |
---|
64 | char+="_" |
---|
65 | |
---|
66 | return char |
---|
67 | |
---|
68 | except Exception as e: |
---|
69 | print e |
---|
70 | return [False,str(e)] |
---|
71 | |
---|
72 | |
---|
73 | def media_line (linea): |
---|
74 | try: |
---|
75 | linea = unicodedata.normalize('NFKD', unicode(linea, 'utf8')) |
---|
76 | linea = linea.encode('ascii', 'ignore') |
---|
77 | linea = linea.decode("utf-8") |
---|
78 | finded = re.search(r'(<media name=")([\s\S]*?)(")', linea) |
---|
79 | ruta=finded.group(2) |
---|
80 | head, tail = os.path.split(ruta) |
---|
81 | ruta=remove_utf8(tail) |
---|
82 | new_text = re.sub(r'(<media name=")([\s\S\d]*?)(")', r'\g<1>%s\3'%ruta, linea) |
---|
83 | new_text2 = re.sub(r'(file=")([\s\S\d]*?)(")', r'\g<1>%s\3'%ruta, new_text) |
---|
84 | return [True, new_text2] |
---|
85 | |
---|
86 | except Exception as e: |
---|
87 | print e |
---|
88 | return [False,str(e)] |
---|
89 | |
---|
90 | #def media_line |
---|
91 | |
---|
92 | def image_line (linea): |
---|
93 | try: |
---|
94 | linea = unicodedata.normalize('NFKD', unicode(linea, 'utf8')) |
---|
95 | linea = linea.encode('ascii', 'ignore') |
---|
96 | linea = linea.decode("utf-8") |
---|
97 | finded = re.search(r'(image=")([\s\S]*?)(")', linea) |
---|
98 | ruta=finded.group(2) |
---|
99 | head, tail = os.path.split(ruta) |
---|
100 | ruta=remove_utf8(tail) |
---|
101 | new_text = re.sub(r'(image=")([\s\S]*?)(")', r'\g<1>%s\3'%ruta, linea) |
---|
102 | return [True, new_text] |
---|
103 | |
---|
104 | except Exception as e: |
---|
105 | return [False,str(e)] |
---|
106 | |
---|
107 | #def media_line |
---|
108 | |
---|
109 | def sound_line (linea): |
---|
110 | try: |
---|
111 | linea = unicodedata.normalize('NFKD', unicode(linea, 'utf8')) |
---|
112 | linea = linea.encode('ascii', 'ignore') |
---|
113 | linea = linea.decode("utf-8") |
---|
114 | finded = re.search(r'(file=")([\s\S]*?)(")', linea) |
---|
115 | ruta=finded.group(2) |
---|
116 | head, tail = os.path.split(ruta) |
---|
117 | ruta=remove_utf8(tail) |
---|
118 | new_text = re.sub(r'(file=")([\s\S]*?)(")', r'\g<1>%s\3'%ruta, linea) |
---|
119 | return [True, new_text] |
---|
120 | |
---|
121 | except Exception as e: |
---|
122 | return [False,str(e)] |
---|
123 | |
---|
124 | #def sound_line |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | def index_write(extract_dira,indexa,title_in,jclic_plugin_path,jar_path,name_jclic_used): |
---|
130 | try: |
---|
131 | index_path=os.path.join(extract_dira, indexa) |
---|
132 | print index_path |
---|
133 | print "dentro con %s %s %s %s"%(title_in,jclic_plugin_path,jar_path,name_jclic_used) |
---|
134 | |
---|
135 | html_str=""" |
---|
136 | <!DOCTYPE HTML PUBLIC> |
---|
137 | <html> |
---|
138 | <head> |
---|
139 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
---|
140 | <title>%s</title> |
---|
141 | <script language="JavaScript" SRC="%s" type="text/javascript"></script> |
---|
142 | </head> |
---|
143 | <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="window.focus();"> |
---|
144 | <script language="JavaScript"> |
---|
145 | <!-- |
---|
146 | setJarBase('%s'); |
---|
147 | setLanguage('null'); |
---|
148 | writePlugin('%s', '100%%', '100%%'); |
---|
149 | --> |
---|
150 | </script> |
---|
151 | </body> |
---|
152 | </html> |
---|
153 | """%(title_in,jclic_plugin_path,jar_path,name_jclic_used) |
---|
154 | |
---|
155 | print "fin" |
---|
156 | print "abriendo index path para escribir fichero en %s"%index_path |
---|
157 | |
---|
158 | html_index=open(index_path, "w") |
---|
159 | html_index.write(html_str) |
---|
160 | html_index.close() |
---|
161 | print "Created index.html in: %s"%index_path |
---|
162 | |
---|
163 | except Exception as e: |
---|
164 | print e |
---|
165 | return [False,str(e)] |
---|
166 | |
---|
167 | # def index_write |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | def index_create (jclic_file): |
---|
174 | try: |
---|
175 | tilte="No Exist edit index.html" |
---|
176 | with open(jclic_file) as f: |
---|
177 | for line in f: |
---|
178 | #obtengo el titulo del index |
---|
179 | if "<title>" in line or "<Title>" in line: |
---|
180 | title=title_file(line)[1] |
---|
181 | return [True,title] |
---|
182 | |
---|
183 | except Exception as e: |
---|
184 | return [False,str(e)] |
---|
185 | |
---|
186 | #def index_create |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | def jclic_review (jclic_filer): |
---|
193 | |
---|
194 | try: |
---|
195 | fh, jclic_tmp=mkstemp() |
---|
196 | with open(jclic_tmp,'w') as new_file: |
---|
197 | with open(jclic_filer) as f: |
---|
198 | for line in f: |
---|
199 | #Modifico las rutas de los ficheros... |
---|
200 | if "<media name" in line: |
---|
201 | line=media_line(line)[1] |
---|
202 | if "image=" in line: |
---|
203 | line=image_line(line)[1] |
---|
204 | if "sound id=" in line: |
---|
205 | line=sound_line(line)[1] |
---|
206 | new_file.write(line) |
---|
207 | close(fh) |
---|
208 | remove(jclic_filer) |
---|
209 | shutil.move(jclic_tmp, jclic_filer) |
---|
210 | print "New jclic file created in: %s"%jclic_filer |
---|
211 | return [True] |
---|
212 | |
---|
213 | except Exception as e: |
---|
214 | return [False,str(e)] |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | def isNotEmpty(s): |
---|
219 | return bool(s and s.strip()) |
---|
220 | #def_isNotEmpty |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | |
---|
226 | # ######################################################## |
---|
227 | |
---|
228 | # ###################MAIN PROGRAM###################### |
---|
229 | try: |
---|
230 | if not os.path.exists(dir_jclic_orig): |
---|
231 | os.makedirs(dir_jclic_orig) |
---|
232 | |
---|
233 | if not os.path.exists(dir_jclic_orig): |
---|
234 | print |
---|
235 | print "Your system cannot permit to www-data write in: %s"%extract_dir |
---|
236 | print |
---|
237 | sys.exit() |
---|
238 | |
---|
239 | |
---|
240 | #Comprueba que el fichero que se le pasa existe y no es un fantasma |
---|
241 | if not os.path.exists(name_zip): |
---|
242 | print |
---|
243 | print "File not find in: %s"%name_zip |
---|
244 | print |
---|
245 | sys.exit() |
---|
246 | |
---|
247 | #Creo un lock y bloqueo la transformacion para que solo un usuario la haga, sino saldra sin hacer nada. |
---|
248 | orig_name_file=os.path.basename(name_zip) |
---|
249 | name_zip_lock=os.path.join(dir_jclic_orig,orig_name_file+sufix) |
---|
250 | print name_zip_lock |
---|
251 | |
---|
252 | if os.path.exists(name_zip_lock): |
---|
253 | print |
---|
254 | print "File %s is blocked because other user is transforming it, please wait"%name_zip |
---|
255 | print |
---|
256 | sys.exit() |
---|
257 | else: |
---|
258 | os.mknod(name_zip_lock) |
---|
259 | |
---|
260 | #Inicializo unas variables |
---|
261 | |
---|
262 | html5=False |
---|
263 | orig_name_file=os.path.basename(name_zip) |
---|
264 | dir_name=os.path.splitext(os.path.basename(name_zip))[0] |
---|
265 | #orig_name_file |
---|
266 | |
---|
267 | |
---|
268 | if dir_name.endswith(".jclic"): |
---|
269 | dir_name=os.path.splitext(dir_name)[0] |
---|
270 | extract_dir=os.path.join(extract_dir, dir_name) |
---|
271 | |
---|
272 | |
---|
273 | print "-----------------------------------" |
---|
274 | print "Generating index.html for %s"%name_zip |
---|
275 | print "-----------------------------------" |
---|
276 | |
---|
277 | #Test directory to extract and directory to save older file |
---|
278 | if not os.path.exists (extract_dir): |
---|
279 | os.makedirs(extract_dir) |
---|
280 | |
---|
281 | os.system("setfacl -m group:www-data:rwx %s"%extract_dir) |
---|
282 | os.system("setfacl -m default:group:www-data:rwx %s"%extract_dir) |
---|
283 | |
---|
284 | if not os.path.exists (dir_jclic_orig): |
---|
285 | os.makedirs(dir_jclic_orig) |
---|
286 | |
---|
287 | #Busco el ficero jclic para saber el nombre real |
---|
288 | with zipfile.ZipFile(name_zip,"r") as zip_file: |
---|
289 | for member in zip_file.namelist(): |
---|
290 | filename=os.path.basename(member) |
---|
291 | #Me aseguro que no es una linea en blanco |
---|
292 | if filename.endswith(".jclic"): |
---|
293 | #print "tengo el fichero jclic: %s"%filename |
---|
294 | tilte="Untiled" |
---|
295 | with zip_file.open(filename) as f: |
---|
296 | for line in f: |
---|
297 | #obtengo el titulo del index |
---|
298 | if "<title>" in line or "<Title>" in line: |
---|
299 | title=title_file(line)[1] |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | if (title.lower()).strip() in ["","sin nombre", "sense nom"]: |
---|
305 | title=dir_name |
---|
306 | print "Titulo sin nombre" |
---|
307 | |
---|
308 | print "Jclic Titulo: %s"%title |
---|
309 | |
---|
310 | #Creacion del index.html |
---|
311 | jclic_filename=os.path.basename(name_zip) |
---|
312 | print "genero el index con extract_dir: %s -index: %s - title: %s - jclic_js: %s - jar_path_lib: %s - name_zip: %s "%(extract_dir,index,title,jclic_plugin,jar_path_lib,jclic_filename) |
---|
313 | index_write(extract_dir,index,title,jclic_plugin,jar_path_lib,jclic_filename) |
---|
314 | |
---|
315 | new_name=os.path.join(extract_dir, jclic_filename) |
---|
316 | shutil.move(name_zip,new_name) |
---|
317 | |
---|
318 | |
---|
319 | os.remove(name_zip_lock) |
---|
320 | #sleep(4) |
---|
321 | |
---|
322 | sys.exit() |
---|
323 | |
---|
324 | except Exception as e: |
---|
325 | print e |
---|
326 | sys.exit() |
---|
327 | |
---|
328 | |
---|
329 | |
---|