33from os .path import dirname , join , isfile , realpath , relpath , split
44from zipfile import ZipFile
55import sys
6- sys .path .insert (0 , 'buildlib/jinja2.egg' )
6+
7+ python_major_version = sys .version_info [0 ]
8+
9+ sys .path .insert (0 , 'buildlib/jinja2-py{}.egg' .format (python_major_version ))
710sys .path .insert (0 , 'buildlib' )
811
912from fnmatch import fnmatch
@@ -67,10 +70,14 @@ def render(template, dest, **kwargs):
6770
6871 template = environment .get_template (template )
6972 text = template .render (** kwargs )
73+ if python_major_version == 2 :
74+ text = text .encode ('utf-8' )
75+ else :
76+ text = bytes (text , 'utf-8' )
7077
71- f = file (dest , 'wb' )
72- f .write (text . encode ( 'utf-8' ) )
73- f .close ()
78+ with open (dest , 'wb' ) as fileh :
79+ fileh .write (text )
80+ fileh .close ()
7481
7582
7683def compile_dir (dfn ):
@@ -183,7 +190,7 @@ def select(fn):
183190 tf = tarfile .open (tfn , 'w:gz' , format = tarfile .USTAR_FORMAT )
184191 dirs = []
185192 for fn , afn in files :
186- print '%s: %s' % (tfn , fn )
193+ print ( '%s: %s' % (tfn , fn ) )
187194 dn = dirname (afn )
188195 if dn not in dirs :
189196 # create every dirs first if not exist yet
@@ -219,7 +226,9 @@ def make_package(args):
219226
220227 args .numeric_version = str (version_code )
221228
222- args .name = args .name .decode ('utf-8' )
229+ if python_major_version == 2 :
230+ args .name = args .name .decode ('utf-8' )
231+
223232 if args .icon_name :
224233 args .icon_name = args .icon_name .decode ('utf-8' )
225234
@@ -301,8 +310,8 @@ def make_package(args):
301310 subprocess .call ([ANDROID , 'update' , 'project' , '-p' , '.' , '-t' ,
302311 'android-{}' .format (args .sdk_version )])
303312 except (OSError , IOError ):
304- print 'An error occured while calling' , ANDROID , 'update'
305- print 'Your PATH must include android tools.'
313+ print ( 'An error occured while calling' , ANDROID , 'update' )
314+ print ( 'Your PATH must include android tools.' )
306315 sys .exit (- 1 )
307316
308317 # Delete the old assets.
@@ -341,7 +350,7 @@ def make_package(args):
341350 if args .add_jar :
342351 for jarname in args .add_jar :
343352 if not os .path .exists (jarname ):
344- print 'Requested jar does not exist: {}' .format (jarname )
353+ print ( 'Requested jar does not exist: {}' .format (jarname ) )
345354 sys .exit (- 1 )
346355 shutil .copy (jarname , 'libs' )
347356
@@ -350,8 +359,8 @@ def make_package(args):
350359 for arg in args .command :
351360 subprocess .check_call ([ANT , arg ])
352361 except (OSError , IOError ):
353- print 'An error occured while calling' , ANT
354- print 'Did you install ant on your system ?'
362+ print ( 'An error occured while calling' , ANT )
363+ print ( 'Did you install ant on your system ?' )
355364 sys .exit (- 1 )
356365
357366if __name__ == '__main__' :
0 commit comments