35
35
# msg/js/<LANG>.js for every language <LANG> defined in msg/js/<LANG>.json.
36
36
37
37
import sys
38
- if sys .version_info [0 ] != 2 :
39
- raise Exception ("Blockly build only compatible with Python 2.x.\n "
40
- "You are using: " + sys .version )
41
38
42
- import errno , glob , httplib , json , os , re , subprocess , threading , urllib
39
+ import errno , glob , json , os , re , subprocess , threading , codecs , functools
40
+
41
+ if sys .version_info [0 ] == 2 :
42
+ import httplib
43
+ from urllib import urlencode
44
+ else :
45
+ import http .client as httplib
46
+ from urllib .parse import urlencode
47
+ from importlib import reload
43
48
44
49
REMOTE_COMPILER = "remote"
45
50
@@ -194,7 +199,7 @@ def format_js(self, code):
194
199
195
200
key_whitelist = self .closure_env .keys ()
196
201
197
- keys_pipe_separated = reduce (lambda accum , key : accum + "|" + key , key_whitelist )
202
+ keys_pipe_separated = functools . reduce (lambda accum , key : accum + "|" + key , key_whitelist )
198
203
begin_brace = re .compile (r"\{(?!%s)" % (keys_pipe_separated ,))
199
204
200
205
end_brace = re .compile (r"\}" )
@@ -336,7 +341,7 @@ def do_compile_local(self, params, target_filename):
336
341
return dict (
337
342
compiledCode = stdout ,
338
343
statistics = dict (
339
- originalSize = reduce (lambda v , size : v + size , filesizes , 0 ),
344
+ originalSize = functools . reduce (lambda v , size : v + size , filesizes , 0 ),
340
345
compressedSize = len (stdout ),
341
346
)
342
347
)
@@ -373,9 +378,10 @@ def do_compile_remote(self, params, target_filename):
373
378
374
379
headers = {"Content-type" : "application/x-www-form-urlencoded" }
375
380
conn = httplib .HTTPSConnection ("closure-compiler.appspot.com" )
376
- conn .request ("POST" , "/compile" , urllib . urlencode (remoteParams ), headers )
381
+ conn .request ("POST" , "/compile" , urlencode (remoteParams ), headers )
377
382
response = conn .getresponse ()
378
- json_str = response .read ()
383
+ # Decode is necessary for Python 3.4 compatibility
384
+ json_str = response .read ().decode ("utf-8" )
379
385
conn .close ()
380
386
381
387
# Parse the JSON response.
@@ -388,12 +394,12 @@ def file_lookup(name):
388
394
n = int (name [6 :]) - 1
389
395
return filenames [n ]
390
396
391
- if json_data . has_key ( "serverErrors" ) :
397
+ if "serverErrors" in json_data :
392
398
errors = json_data ["serverErrors" ]
393
399
for error in errors :
394
400
print ("SERVER ERROR: %s" % target_filename )
395
401
print (error ["error" ])
396
- elif json_data . has_key ( "errors" ) :
402
+ elif "errors" in json_data :
397
403
errors = json_data ["errors" ]
398
404
for error in errors :
399
405
print ("FATAL ERROR" )
@@ -405,7 +411,7 @@ def file_lookup(name):
405
411
print ((" " * error ["charno" ]) + "^" )
406
412
sys .exit (1 )
407
413
else :
408
- if json_data . has_key ( "warnings" ) :
414
+ if "warnings" in json_data :
409
415
warnings = json_data ["warnings" ]
410
416
for warning in warnings :
411
417
print ("WARNING" )
@@ -422,11 +428,11 @@ def file_lookup(name):
422
428
return False
423
429
424
430
def write_output (self , target_filename , remove , json_data ):
425
- if not json_data . has_key ( "compiledCode" ) :
431
+ if "compiledCode" not in json_data :
426
432
print ("FATAL ERROR: Compiler did not return compiledCode." )
427
433
sys .exit (1 )
428
434
429
- code = HEADER + "\n " + json_data ["compiledCode" ]
435
+ code = HEADER + "\n " + json_data ["compiledCode" ]. decode ( "utf-8" )
430
436
code = code .replace (remove , "" )
431
437
432
438
# Trim down Google's (and only Google's) Apache licences.
@@ -500,7 +506,7 @@ def _rebuild(self, srcs, dests):
500
506
# If a destination file was missing, rebuild.
501
507
return True
502
508
else :
503
- print ("Error checking file creation times: " + e )
509
+ print ("Error checking file creation times: " + str ( e ) )
504
510
505
511
def run (self ):
506
512
# The files msg/json/{en,qqq,synonyms}.json depend on msg/messages.js.
@@ -573,7 +579,7 @@ def exclude_horizontal(item):
573
579
test_args = [closure_compiler , os .path .join ("build" , "test_input.js" )]
574
580
test_proc = subprocess .Popen (test_args , stdin = subprocess .PIPE , stdout = subprocess .PIPE )
575
581
(stdout , _ ) = test_proc .communicate ()
576
- assert stdout == read (os .path .join ("build" , "test_expect.js" ))
582
+ assert stdout . decode ( "utf-8" ) == read (os .path .join ("build" , "test_expect.js" ))
577
583
578
584
print ("Using local compiler: %s ...\n " % CLOSURE_COMPILER_NPM )
579
585
except (ImportError , AssertionError ):
@@ -602,11 +608,11 @@ def exclude_horizontal(item):
602
608
developers.google.com/blockly/guides/modify/web/closure""" )
603
609
sys .exit (1 )
604
610
605
- search_paths = calcdeps .ExpandDirectories (
606
- ["core" , os .path .join (closure_root , closure_library )])
611
+ search_paths = list ( calcdeps .ExpandDirectories (
612
+ ["core" , os .path .join (closure_root , closure_library )]))
607
613
608
- search_paths_horizontal = filter (exclude_vertical , search_paths )
609
- search_paths_vertical = filter (exclude_horizontal , search_paths )
614
+ search_paths_horizontal = list ( filter (exclude_vertical , search_paths ) )
615
+ search_paths_vertical = list ( filter (exclude_horizontal , search_paths ) )
610
616
611
617
closure_env = {
612
618
"closure_dir" : closure_dir ,
0 commit comments