~lorenzo-carbonell/utext/trunk

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
#
'''
from distutils.core import setup
'''
from distutils.core import setup
from DistUtilsExtra.command import *
from distutils import cmd
from distutils.command.install_data import install_data as _install_data
from distutils.command.build import build as _build
 
import msgfmt
import os
import glob
import shlex
import subprocess
import shutil
import polib
import ConfigParser
import codecs

DATA_FILES = [
	('/opt/extras.ubuntu.com/utext/bin',glob.glob('bin/*')),
	('/opt/extras.ubuntu.com/utext/share/utext',['debian/changelog']),
	('/opt/extras.ubuntu.com/utext/share/utext',glob.glob('src/*.py')),
	('/opt/extras.ubuntu.com/utext/share/utext/themes',glob.glob('src/themes/*')),
	('/opt/extras.ubuntu.com/utext/share/utext/icons',glob.glob('data/icons/*.svg')),
	('/opt/extras.ubuntu.com/utext/share/utext/social',glob.glob('data/social/*.svg')),
	('/opt/extras.ubuntu.com/utext/share/utext/icons',['data/icons/utext.svg']),
	('/opt/extras.ubuntu.com/utext/share/utext/pdfkit',glob.glob('src/pdfkit/*.py')),
	('/usr/share/applications',['data/extras-utext.desktop']),
	('/usr/share/mime/packages',['data/markdown.xml']),
	('/usr/share/icons/gnome/scalable/mimetypes',['data/icons/text-x-web-markdown.svg']),
	]

MAIN_DIR = os.getcwd()
DATA_DIR = os.path.join(MAIN_DIR,'data')
DEBIAN_DIR = os.path.join(MAIN_DIR,'debian')
LANGUAGES_DIR = os.path.join(MAIN_DIR,'po')
SRC_DIR = os.path.join(MAIN_DIR,'src')
TEMPLATE = os.path.join(LANGUAGES_DIR,'po.pot')
CHANGELOG = os.path.join(DEBIAN_DIR,'changelog')
f = open(CHANGELOG,'r')
line = f.readline()
f.close()
pos=line.find('(')
posf=line.find('-',pos)
APP = line[:pos].strip().lower()
VERSION = line[pos+1:posf].strip()
APPNAME = APP.title()
AUTHOR = 'Lorenzo Carbonell'
AUTHOR_EMAIL = 'lorenzo.carbonell.cerezo@gmail.com'
URL = 'http://www.atareao.es'
LICENSE = 'GNU General Public License (GPL)'
COMPILED_LANGUAGE_FILE = '%s.mo'%APP

def get_entry(filein,msgid):
	try:
		po = polib.pofile(filein)
		print po.metadata['Content-Type']
		for entry in po:
			if entry.msgid == msgid:
				return entry.msgstr
	except Exception,e:
		print filein, e
		pass
	return None

########################################################################
def ejecuta(comando):
	print ('Ejecutando... %s'%comando)
	args = shlex.split(comando)
	p = subprocess.Popen(args, bufsize=10000, stdout=subprocess.PIPE)
	valor = p.communicate()[0]
	return valor
########################################################################
def list_src():
	file_txt = os.path.join(MAIN_DIR,'files.txt')
	f = open(file_txt,'w')
	for file in glob.glob(os.path.join(SRC_DIR,'*.py')):
		f.write('%s\n'%file)
	for file in glob.glob(os.path.join(MAIN_DIR,'*.desktop.in')):
		f.write('%s\n'%file)
	f.close()
	return file_txt

def list_languages():
	lans = []
	file_txt =os.path.join(LANGUAGES_DIR,'languages.txt')
	if os.path.exists(file_txt)==True:
		f = open(file_txt,'r')
		for linea in f.readlines():
			lan = linea[:-1]
			print lan
			lans.append(lan)
		f.close()
	for file in glob.glob(os.path.join(LANGUAGES_DIR,'*.po')):
		lan = os.path.splitext(os.path.basename(file))[0]
		if lan not in lans:
			lans.append(lan)
	f = open(file_txt,'w')
	for lan in lans:
		f.write('%s\n'%lan)
	f.close()
	return file_txt

def update_translations():
	file_txt =os.path.join(LANGUAGES_DIR,'languages.txt')
	f = open(file_txt,'r')
	for file in f.readlines():
		lan = file[:-1]
		file = os.path.join(LANGUAGES_DIR,lan+'.po')
		print '############################################################'
		print lan
		print '############################################################'
		if os.path.exists(file):
			command = 'msgmerge -U %s %s'%(file,TEMPLATE)
		else:
			command = 'msginit --output-file=%s --input=%s --locale=%s'%(file,TEMPLATE,lan)
		print ejecuta(command)
		edit_language_file(file)
	f.close()
	
def edit_language_file(file):
	po = polib.pofile(file)
	lang = file.split('/')[-1:][0].split('.')[0]
	po.metadata['Project-Id-Version'] = '%s %s'%(APP,VERSION)
	po.metadata['Report-Msgid-Bugs-To'] = AUTHOR_EMAIL
	po.metadata['Language'] = lang
	po.metadata['Content-Type'] = 'text/plain; charset=UTF-8'
	po.metadata['Content-Transfer-Encoding'] = '8bit'
	po.save()

def update_desktop_file_fp():
	lns = []
	for filein in glob.glob('./po/*.po'):
		ln = os.path.splitext(os.path.split(filein)[1])[0]
		lns.append(ln)
	for filedesktopin in glob.glob('*.desktop.in'):
		desktopfile = ConfigParser.ConfigParser()		
		desktopfile.optionxform = str
		desktopfile.readfp(codecs.open(filedesktopin,encoding = 'utf-8',mode='r'))
		print(desktopfile.sections())
		if len(lns)>0:
			for asection in desktopfile.sections():
				for entry in desktopfile.items(asection):
					if entry[0].startswith('_'):
						for ln in lns:
							desktopfile.set(asection,'$%s[%s]'%(entry[0][1:],ln),"_('%s')"%entry[1])
		with codecs.open(filedesktopin,encoding = 'utf-8',mode='w') as outputfile:
			desktopfile.write(outputfile)


def update_desktop_file():
	lns = []
	for filein in glob.glob('./po/*.po'):
		ln = os.path.splitext(os.path.split(filein)[1])[0]
		lns.append(ln)
	for filedesktopin in glob.glob('*.desktop.in'):
		desktopfilename = os.path.splitext(os.path.split(filedesktopin)[1])[0]
		print desktopfilename
		fileout = os.path.join(DATA_DIR,desktopfilename)
		print fileout
		if os.path.exists(fileout):
			os.remove(fileout)
		fileout = codecs.open('./data/%s'%desktopfilename,encoding = 'utf-8',mode='w')
		#
		desktopfile = ConfigParser.ConfigParser()
		desktopfile.optionxform = str
		desktopfile.readfp(codecs.open('./%s.in'%desktopfilename,encoding = 'utf-8',mode='r'))
		if len(lns)>0:
			for nsection,asection in enumerate(desktopfile.sections()):
				if nsection>0:
					fileout.write('\n')
				fileout.write('['+asection+']\n')
				for entry in desktopfile.items(asection):
					if  not entry[0].startswith('$'):
						if entry[0].startswith('_'):
							fileout.write('%s = %s\n'%(entry[0][1:],entry[1]))
						else:
							fileout.write('%s = %s\n'%(entry[0],entry[1]))
				for entry in desktopfile.items(asection):
					if entry[0].startswith('_') and not entry[0].startswith('$'):
						for ln in lns:
							filepo = os.path.join(LANGUAGES_DIR,'%s.po'%ln)
							msgstr = get_entry(filepo,entry[1])
							print filepo
							if not msgstr or msgstr == '':
								msgstr = entry[1]
								
							print '%s[%s]=%s'%(entry[0][1:],ln,msgstr)
							fileout.write('%s[%s] = %s\n'%(entry[0][1:],ln,msgstr))
		fileout.close()
	
def remove_security_copies():
	for file in glob.glob(os.path.join(LANGUAGES_DIR,'*.po~')):
		os.remove(file)

def delete_it(file):
	if os.path.exists(file):
		if os.path.isdir(file):
			shutil.rmtree(file)
		else:
			os.remove(file)
			
def remove_files(dir,ext):
	files = []
	for file in glob.glob(os.path.join(dir,'*')):
		if file != None and os.path.exists(file):
			if file and os.path.isdir(file):
				morefiles = remove_files(file,ext)
				if morefiles:
					files.extend(morefiles)
			else:
				files.append(file)
	for file in files:
		if os.path.splitext(file)[1] == ext:
			os.remove(file)

def remove_compiled_files(dir):
	remove_files(dir,'.pyc')

def remove_languages_saved_files(dir):
	remove_files(dir,'.po~')

def babilon():
	print '############################################################'
	print 'Parent dir -> %s'%MAIN_DIR
	print 'Languages dir -> %s'%LANGUAGES_DIR
	print 'Source dir -> %s'%SRC_DIR
	print '############################################################'
	print 'Updating Desktop File First Part'
	print '############################################################'
	update_desktop_file_fp()
	print '############################################################'
	print 'Updating template'
	print '############################################################'
	files_file = list_src()	
	command = 'xgettext --msgid-bugs-address=%s --language=Python --keyword=_ --keyword=N_ --output=%s --files-from=%s'%(AUTHOR_EMAIL,TEMPLATE,files_file)
	print ejecuta(command)
	delete_it(files_file)
	print '############################################################'
	print 'List languages'
	print '############################################################'
	#
	list_languages()
	#
	print '############################################################'
	print 'Updating translations'
	print '############################################################'
	update_translations()
	print '############################################################'
	print 'Updating Desktop File'
	print '############################################################'
	update_desktop_file()
	print '############################################################'
	print 'Removing security copies'
	print '############################################################'
	remove_security_copies()	
		
	
class clean_and_compile(cmd.Command):
	description = 'Clean and compile languages'
	def initialize_options(self):
		pass
 
	def finalize_options(self):
		pass
 
	def run(self):
		remove_compiled_files(SRC_DIR)
		babilon()

class translate(build_extra.build_extra):
	sub_commands = build_extra.build_extra.sub_commands + [('clean_and_compile', None)]
	def run(self):
		build_extra.build_extra.run(self)
		pass
	
	
class build_trans(cmd.Command):
	description = 'Compile .po files into .mo files'
	def initialize_options(self):
		pass
 
	def finalize_options(self):
		pass
 
	def run(self):
		po_dir = os.path.join(os.path.dirname(os.curdir), 'po')
		for path, names, filenames in os.walk(po_dir):
			for f in filenames:
				if f.endswith('.po'):
					lang = f[:len(f) - 3]
					src = os.path.join(path, f)
					dest_path = os.path.join('build', 'locale-langpack', lang, 'LC_MESSAGES')
					dest = os.path.join(dest_path, COMPILED_LANGUAGE_FILE)
					if not os.path.exists(dest_path):
						os.makedirs(dest_path)
					if not os.path.exists(dest):
						print 'Compiling %s' % src
						msgfmt.make(src, dest)
					else:
						src_mtime = os.stat(src)[8]
						dest_mtime = os.stat(dest)[8]
						if src_mtime > dest_mtime:
							print 'Compiling %s' % src
							msgfmt.make(src, dest)
							
class build(build_extra.build_extra):
	sub_commands = build_extra.build_extra.sub_commands + [('build_trans', None)]
	def run(self):
		build_extra.build_extra.run(self)

class install_data(_install_data):
	def run(self):
		for lang in os.listdir('build/locale-langpack/'):
			lang_dir = os.path.join('/opt/extras.ubuntu.com/utext/share', 'locale-langpack', lang, 'LC_MESSAGES')
			lang_file = os.path.join('build', 'locale-langpack', lang, 'LC_MESSAGES', COMPILED_LANGUAGE_FILE)
			self.data_files.append( (lang_dir, [lang_file]) )
		_install_data.run(self)	
		
setup(name=APP,
	version=VERSION,
	author=AUTHOR,
	author_email=AUTHOR_EMAIL,
	url=URL,
	license=LICENSE,
	data_files=DATA_FILES,
	cmdclass={'build': build,
	'translate':translate,
	'clean_and_compile':clean_and_compile,
	'build_trans': build_trans,
	'install_data': install_data,})