1
- #!/usr/bin/env python2
1
+ #!/usr/bin/env python
2
2
3
3
# Copyright (c) 2016 ARM Limited, All Rights Reserved
4
4
# SPDX-License-Identifier: Apache-2.0
18
18
# pylint: disable=too-many-nested-blocks, too-many-public-methods, too-many-instance-attributes, too-many-statements
19
19
# pylint: disable=invalid-name, missing-docstring, bad-continuation
20
20
21
+ from __future__ import print_function
22
+ from future .builtins .iterators import zip
23
+ from past .builtins import basestring
24
+
25
+ try :
26
+ from urllib .parse import urlparse , quote
27
+ from urllib .request import urlopen
28
+ except ImportError :
29
+ from urlparse import urlparse
30
+ from urllib2 import urlopen
31
+ from urllib import quote
32
+
21
33
import traceback
22
34
import sys
23
35
import re
28
40
import stat
29
41
import errno
30
42
import ctypes
31
- from itertools import chain , izip , repeat
32
- from urlparse import urlparse
33
- import urllib
34
- import urllib2
35
- import zipfile
43
+ from itertools import chain , repeat
36
44
import argparse
37
45
import tempfile
46
+ import zipfile
38
47
39
48
40
49
# Application version
@@ -184,7 +193,7 @@ def progress_cursor():
184
193
progress_spinner = progress_cursor ()
185
194
186
195
def progress ():
187
- sys .stdout .write (progress_spinner . next ())
196
+ sys .stdout .write (next (progress_spinner ))
188
197
sys .stdout .flush ()
189
198
sys .stdout .write ('\b ' )
190
199
@@ -211,10 +220,10 @@ def popen(command, stdin=None, **kwargs):
211
220
try :
212
221
proc = subprocess .Popen (command , ** kwargs )
213
222
except OSError as e :
214
- if e [0 ] == errno .ENOENT :
223
+ if e . args [0 ] == errno .ENOENT :
215
224
error (
216
225
"Could not execute \" %s\" .\n "
217
- "Please verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (command [0 ], command [0 ]), e [0 ])
226
+ "Please verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (command [0 ], command [0 ]), e . args [0 ])
218
227
else :
219
228
raise e
220
229
@@ -227,10 +236,10 @@ def pquery(command, output_callback=None, stdin=None, **kwargs):
227
236
try :
228
237
proc = subprocess .Popen (command , bufsize = 0 , stdout = subprocess .PIPE , stderr = subprocess .PIPE , ** kwargs )
229
238
except OSError as e :
230
- if e [0 ] == errno .ENOENT :
239
+ if e . args [0 ] == errno .ENOENT :
231
240
error (
232
241
"Could not execute \" %s\" .\n "
233
- "Please verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (command [0 ], command [0 ]), e [0 ])
242
+ "Please verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (command [0 ], command [0 ]), e . args [0 ])
234
243
else :
235
244
raise e
236
245
@@ -251,12 +260,12 @@ def pquery(command, output_callback=None, stdin=None, **kwargs):
251
260
stdout , _ = proc .communicate (stdin )
252
261
253
262
if very_verbose :
254
- log (str ( stdout ).strip ()+ "\n " )
263
+ log (stdout . decode ( "utf-8" ).strip () + "\n " )
255
264
256
265
if proc .returncode != 0 :
257
266
raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), getcwd ())
258
267
259
- return stdout
268
+ return stdout . decode ( "utf-8" )
260
269
261
270
def rmtree_readonly (directory ):
262
271
if os .path .islink (directory ):
@@ -346,15 +355,15 @@ def clone(url, path=None, depth=None, protocol=None):
346
355
except Exception as e :
347
356
if os .path .isdir (path ):
348
357
rmtree_readonly (path )
349
- error (e [1 ], e [0 ])
358
+ error (e . args [1 ], e . args [0 ])
350
359
351
360
def fetch_rev (url , rev ):
352
361
rev_file = os .path .join ('.' + Bld .name , '.rev-' + rev + '.zip' )
353
362
try :
354
363
if not os .path .exists (rev_file ):
355
364
action ("Downloading library build \" %s\" (might take a minute)" % rev )
356
365
outfd = open (rev_file , 'wb' )
357
- inurl = urllib2 . urlopen (url )
366
+ inurl = urlopen (url )
358
367
outfd .write (inurl .read ())
359
368
outfd .close ()
360
369
except :
@@ -393,7 +402,7 @@ def checkout(rev, clean=False):
393
402
Bld .unpack_rev (rev )
394
403
Bld .seturl (url + '/' + rev )
395
404
except Exception as e :
396
- error (e [1 ], e [0 ])
405
+ error (e . args [1 ], e . args [0 ])
397
406
398
407
def update (rev = None , clean = False , clean_files = False , is_local = False ):
399
408
return Bld .checkout (rev , clean )
@@ -513,7 +522,7 @@ def outgoing():
513
522
pquery ([hg_cmd , 'outgoing' ])
514
523
return 1
515
524
except ProcessException as e :
516
- if e [0 ] != 1 :
525
+ if e . args [0 ] != 1 :
517
526
raise e
518
527
return 0
519
528
@@ -567,8 +576,9 @@ def geturl():
567
576
568
577
def getrev ():
569
578
if os .path .isfile (os .path .join ('.hg' , 'dirstate' )):
579
+ from io import open
570
580
with open (os .path .join ('.hg' , 'dirstate' ), 'rb' ) as f :
571
- return '' .join ('% 02x' % ord ( i ) for i in f .read (6 ))
581
+ return "" .join ('{: 02x}' . format ( x ) for x in bytearray ( f .read (6 ) ))
572
582
else :
573
583
return ""
574
584
@@ -1282,7 +1292,7 @@ def write(self):
1282
1292
1283
1293
ref = url .rstrip ('/' ) + '/' + (('' if self .is_build else '#' ) + self .rev if self .rev else '' )
1284
1294
action ("Updating reference \" %s\" -> \" %s\" " % (relpath (cwd_root , self .path ) if cwd_root != self .path else self .name , ref ))
1285
- with open (self .lib , 'wb ' ) as f :
1295
+ with open (self .lib , 'w ' ) as f :
1286
1296
f .write (ref + "\n " )
1287
1297
1288
1298
def rm_untracked (self ):
@@ -1295,7 +1305,7 @@ def rm_untracked(self):
1295
1305
def url2cachedir (self , url ):
1296
1306
up = urlparse (formaturl (url , 'https' ))
1297
1307
if self .cache and up and up .netloc :
1298
- return os .path .join (self .cache , urllib . quote (up .netloc ), urllib . quote (re .sub (r'^/' , '' , up .path )))
1308
+ return os .path .join (self .cache , quote (up .netloc ), quote (re .sub (r'^/' , '' , up .path )))
1299
1309
1300
1310
def get_cache (self , url , scm ):
1301
1311
cpath = self .url2cachedir (url )
@@ -1992,7 +2002,7 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=Fa
1992
2002
if ignore :
1993
2003
warning (err )
1994
2004
else :
1995
- error (err , e [0 ])
2005
+ error (err , e . args [0 ])
1996
2006
else :
1997
2007
err = "Unable to clone repository (%s)" % url
1998
2008
if ignore :
@@ -2142,7 +2152,7 @@ def publish(all_refs=None, msg=None, top=True):
2142
2152
if top :
2143
2153
action ("Nothing to publish to the remote repository (the source tree is unmodified)" )
2144
2154
except ProcessException as e :
2145
- if e [0 ] != 1 :
2155
+ if e . args [0 ] != 1 :
2146
2156
raise e
2147
2157
2148
2158
@@ -2209,7 +2219,7 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
2209
2219
if ignore :
2210
2220
warning (err )
2211
2221
else :
2212
- error (err , e [0 ])
2222
+ error (err , e . args [0 ])
2213
2223
2214
2224
repo .rm_untracked ()
2215
2225
if top and cwd_type == 'library' :
@@ -2341,7 +2351,7 @@ def sync(recursive=True, keep_refs=False, top=True):
2341
2351
def list_ (detailed = False , prefix = '' , p_path = None , ignore = False ):
2342
2352
repo = Repo .fromrepo ()
2343
2353
2344
- print "%s (%s)" % (prefix + (relpath (p_path , repo .path ) if p_path else repo .name ), ((repo .url + ('#' + str (repo .rev )[:12 ] if repo .rev else '' ) if detailed else repo .revtype (repo .rev , fmt = 6 )) or 'no revision' ))
2354
+ print ( "%s (%s)" % (prefix + (relpath (p_path , repo .path ) if p_path else repo .name ), ((repo .url + ('#' + str (repo .rev )[:12 ] if repo .rev else '' ) if detailed else repo .revtype (repo .rev , fmt = 6 )) or 'no revision' ) ))
2345
2355
2346
2356
for i , lib in enumerate (sorted (repo .libs , key = lambda l : l .path )):
2347
2357
nprefix = (prefix [:- 3 ] + ('| ' if prefix [- 3 ] == '|' else ' ' )) if prefix else ''
@@ -2374,16 +2384,16 @@ def releases_(detailed=False, unstable=False, recursive=False, prefix='', p_path
2374
2384
rels .append (tag [1 ] + " %s%s" % ('#' + tag [0 ] if detailed else "" , " <- current" if tag [1 ] in revtags else "" ))
2375
2385
2376
2386
# Print header
2377
- print "%s (%s)" % (prefix + (relpath (p_path , repo .path ) if p_path else repo .name ), ((repo .url + ('#' + str (repo .rev )[:12 ] if repo .rev else '' ) if detailed else repo .revtype (repo .rev , fmt = 6 )) or 'no revision' ))
2387
+ print ( "%s (%s)" % (prefix + (relpath (p_path , repo .path ) if p_path else repo .name ), ((repo .url + ('#' + str (repo .rev )[:12 ] if repo .rev else '' ) if detailed else repo .revtype (repo .rev , fmt = 6 )) or 'no revision' ) ))
2378
2388
2379
2389
# Print list of tags
2380
2390
rprefix = (prefix [:- 3 ] + ('| ' if prefix [- 3 ] == '|' else ' ' )) if recursive and prefix else ''
2381
2391
rprefix += '| ' if recursive and len (repo .libs ) > 1 else ' '
2382
2392
if len (rels ):
2383
2393
for rel in rels :
2384
- print rprefix + '* ' + rel
2394
+ print ( rprefix + '* ' + rel )
2385
2395
else :
2386
- print rprefix + 'No release tags detected'
2396
+ print ( rprefix + 'No release tags detected' )
2387
2397
2388
2398
if recursive :
2389
2399
for i , lib in enumerate (sorted (repo .libs , key = lambda l : l .path )):
@@ -2466,8 +2476,8 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2466
2476
# Compile configuration
2467
2477
popen ([python_cmd , os .path .join (tools_dir , 'get_config.py' )]
2468
2478
+ ['-t' , tchain , '-m' , target ]
2469
- + list (chain .from_iterable (izip (repeat ('--profile' ), profile or [])))
2470
- + list (chain .from_iterable (izip (repeat ('--source' ), source )))
2479
+ + list (chain .from_iterable (zip (repeat ('--profile' ), profile or [])))
2480
+ + list (chain .from_iterable (zip (repeat ('--source' ), source )))
2471
2481
+ (['-v' ] if verbose else [])
2472
2482
+ (list (chain .from_iterable (izip (repeat ('--prefix' ), config_prefix ))) if config_prefix else []),
2473
2483
env = env )
@@ -2484,10 +2494,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2484
2494
build_path = os .path .join (os .path .relpath (program .path , orig_path ), program .build_dir , 'libraries' , os .path .basename (orig_path ), target , tchain )
2485
2495
2486
2496
popen ([python_cmd , '-u' , os .path .join (tools_dir , 'build.py' )]
2487
- + list (chain .from_iterable (izip (repeat ('-D' ), macros )))
2497
+ + list (chain .from_iterable (zip (repeat ('-D' ), macros )))
2488
2498
+ ['-t' , tchain , '-m' , target ]
2489
- + list (chain .from_iterable (izip (repeat ('--profile' ), profile or [])))
2490
- + list (chain .from_iterable (izip (repeat ('--source' ), source )))
2499
+ + list (chain .from_iterable (zip (repeat ('--profile' ), profile or [])))
2500
+ + list (chain .from_iterable (zip (repeat ('--source' ), source )))
2491
2501
+ ['--build' , build_path ]
2492
2502
+ (['-c' ] if clean else [])
2493
2503
+ (['--artifact-name' , artifact_name ] if artifact_name else [])
@@ -2500,10 +2510,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2500
2510
build_path = os .path .join (os .path .relpath (program .path , orig_path ), program .build_dir , target , tchain )
2501
2511
2502
2512
popen ([python_cmd , '-u' , os .path .join (tools_dir , 'make.py' )]
2503
- + list (chain .from_iterable (izip (repeat ('-D' ), macros )))
2513
+ + list (chain .from_iterable (zip (repeat ('-D' ), macros )))
2504
2514
+ ['-t' , tchain , '-m' , target ]
2505
- + list (chain .from_iterable (izip (repeat ('--profile' ), profile or [])))
2506
- + list (chain .from_iterable (izip (repeat ('--source' ), source )))
2515
+ + list (chain .from_iterable (zip (repeat ('--profile' ), profile or [])))
2516
+ + list (chain .from_iterable (zip (repeat ('--source' ), source )))
2507
2517
+ ['--build' , build_path ]
2508
2518
+ (['-c' ] if clean else [])
2509
2519
+ (['--artifact-name' , artifact_name ] if artifact_name else [])
@@ -2590,9 +2600,9 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
2590
2600
2591
2601
if compile_list :
2592
2602
popen ([python_cmd , '-u' , os .path .join (tools_dir , 'test.py' ), '--list' ]
2593
- + list (chain .from_iterable (izip (repeat ('--profile' ), profile or [])))
2603
+ + list (chain .from_iterable (list ( izip (repeat ('--profile' ), profile or []) )))
2594
2604
+ ['-t' , tchain , '-m' , target ]
2595
- + list (chain .from_iterable (izip (repeat ('--source' ), source )))
2605
+ + list (chain .from_iterable (zip (repeat ('--source' ), source )))
2596
2606
+ (['-n' , tests_by_name ] if tests_by_name else [])
2597
2607
+ (['-v' ] if verbose else [])
2598
2608
+ (['--app-config' , app_config ] if app_config else [])
@@ -2606,11 +2616,11 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
2606
2616
program .ignore_build_dir ()
2607
2617
2608
2618
popen ([python_cmd , '-u' , os .path .join (tools_dir , 'test.py' )]
2609
- + list (chain .from_iterable (izip (repeat ('-D' ), macros )))
2610
- + list (chain .from_iterable (izip (repeat ('--profile' ), profile or [])))
2619
+ + list (chain .from_iterable (zip (repeat ('-D' ), macros )))
2620
+ + list (chain .from_iterable (zip (repeat ('--profile' ), profile or [])))
2611
2621
+ ['-t' , tchain , '-m' , target ]
2612
2622
+ (['-c' ] if clean else [])
2613
- + list (chain .from_iterable (izip (repeat ('--source' ), source )))
2623
+ + list (chain .from_iterable (zip (repeat ('--source' ), source )))
2614
2624
+ ['--build' , build_path ]
2615
2625
+ ['--test-spec' , test_spec ]
2616
2626
+ (['-n' , tests_by_name ] if tests_by_name else [])
@@ -2947,7 +2957,7 @@ def get_size_(path):
2947
2957
action ("Repository cache is %s." % str (cfg ['cache' ]).upper ())
2948
2958
action ("Cache location \" %s\" " % cfg ['cache_dir' ])
2949
2959
else :
2950
- print cmd
2960
+ print ( cmd )
2951
2961
error ("Invalid cache command. Please see \" mbed cache --help\" for valid commands." )
2952
2962
2953
2963
@@ -2963,11 +2973,6 @@ def main():
2963
2973
# Help messages adapt based on current dir
2964
2974
cwd_root = getcwd ()
2965
2975
2966
- if sys .version_info [0 ] != 2 or sys .version_info [1 ] < 7 :
2967
- error (
2968
- "mbed CLI is compatible with Python version >= 2.7 and < 3.0\n "
2969
- "Please refer to the online guide available at https://github.com/ARMmbed/mbed-cli" )
2970
-
2971
2976
# Parse/run command
2972
2977
if len (sys .argv ) <= 1 :
2973
2978
help_ ()
@@ -2988,14 +2993,14 @@ def main():
2988
2993
except ProcessException as e :
2989
2994
error (
2990
2995
"\" %s\" returned error code %d.\n "
2991
- "Command \" %s\" in \" %s\" " % (e [1 ], e [0 ], e [2 ], e [3 ]), e [0 ])
2996
+ "Command \" %s\" in \" %s\" " % (e . args [1 ], e . args [0 ], e . args [2 ], e . args [3 ]), e . args [0 ])
2992
2997
except OSError as e :
2993
- if e [0 ] == errno .ENOENT :
2998
+ if e . args [0 ] == errno .ENOENT :
2994
2999
error (
2995
3000
"Could not detect one of the command-line tools.\n "
2996
- "You could retry the last command with \" -v\" flag for verbose output\n " , e [0 ])
3001
+ "You could retry the last command with \" -v\" flag for verbose output\n " , e . args [0 ])
2997
3002
else :
2998
- error ('OS Error: %s' % e [1 ], e [0 ])
3003
+ error ('OS Error: %s' % e . args [1 ], e . args [0 ])
2999
3004
except KeyboardInterrupt :
3000
3005
info ('User aborted!' , - 1 )
3001
3006
sys .exit (255 )
0 commit comments