124
124
125
125
126
126
# Logging and output
127
+ def log (msg ):
128
+ sys .stderr .write (msg )
129
+
127
130
def message (msg ):
128
131
return "[mbed] %s\n " % msg
129
132
130
- def log (msg , level = 1 ):
133
+ def info (msg , level = 1 ):
131
134
if level <= 0 or verbose :
132
- sys .stderr .write (message (msg ))
135
+ for line in msg .splitlines ():
136
+ log (message (line ))
133
137
134
138
def action (msg ):
135
- sys .stderr .write (message (msg ))
139
+ for line in msg .splitlines ():
140
+ log (message (line ))
136
141
137
142
def warning (msg ):
138
143
for line in msg .splitlines ():
139
- sys . stderr . write ("[mbed] WARNING: %s\n " % line )
140
- sys . stderr . write ("---\n " )
144
+ log ("[mbed] WARNING: %s\n " % line )
145
+ log ("---\n " )
141
146
142
147
def error (msg , code = - 1 ):
143
148
for line in msg .splitlines ():
144
- sys . stderr . write ("[mbed] ERROR: %s\n " % line )
145
- sys . stderr . write ("---\n " )
149
+ log ("[mbed] ERROR: %s\n " % line )
150
+ log ("---\n " )
146
151
sys .exit (code )
147
152
148
153
def progress_cursor ():
@@ -164,7 +169,7 @@ class ProcessException(Exception):
164
169
165
170
def popen (command , stdin = None , ** kwargs ):
166
171
# print for debugging
167
- log ('Exec "' + ' ' .join (command )+ '" in ' + os .getcwd ())
172
+ info ('Exec "' + ' ' .join (command )+ '" in ' + os .getcwd ())
168
173
try :
169
174
proc = subprocess .Popen (command , ** kwargs )
170
175
except OSError as e :
@@ -180,7 +185,7 @@ def popen(command, stdin=None, **kwargs):
180
185
181
186
def pquery (command , stdin = None , ** kwargs ):
182
187
if very_verbose :
183
- log ('Query "' + ' ' .join (command )+ '" in ' + os .getcwd ())
188
+ info ('Query "' + ' ' .join (command )+ '" in ' + os .getcwd ())
184
189
try :
185
190
proc = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , ** kwargs )
186
191
except OSError as e :
@@ -301,15 +306,15 @@ def checkout(rev, clean=False):
301
306
error ("Unable to fetch late mbed library revision" )
302
307
303
308
if rev != Bld .getrev ():
304
- log ("Cleaning up library build folder" )
309
+ info ("Cleaning up library build folder" )
305
310
for fl in os .listdir ('.' ):
306
311
if not fl .startswith ('.' ):
307
312
if os .path .isfile (fl ):
308
313
os .remove (fl )
309
314
else :
310
315
shutil .rmtree (fl )
311
316
312
- log ("Checkout \" %s\" in %s" % (rev , os .path .basename (os .getcwd ())))
317
+ info ("Checkout \" %s\" in %s" % (rev , os .path .basename (os .getcwd ())))
313
318
arch_url = m .group (1 ) + '/archive/' + rev + '.zip'
314
319
arch_dir = m .group (7 ) + '-' + rev
315
320
try :
@@ -375,14 +380,14 @@ def clone(url, name=None, depth=None, protocol=None):
375
380
popen ([hg_cmd , 'clone' , formaturl (url , protocol ), name ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
376
381
377
382
def add (dest ):
378
- log ("Adding reference \" %s\" " % dest )
383
+ info ("Adding reference \" %s\" " % dest )
379
384
try :
380
385
popen ([hg_cmd , 'add' , dest ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
381
386
except ProcessException :
382
387
pass
383
388
384
389
def remove (dest ):
385
- log ("Removing reference \" %s\" " % dest )
390
+ info ("Removing reference \" %s\" " % dest )
386
391
try :
387
392
popen ([hg_cmd , 'rm' , '-f' , dest ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
388
393
except ProcessException :
@@ -395,19 +400,19 @@ def publish(all_refs=None):
395
400
popen ([hg_cmd , 'push' ] + (['--new-branch' ] if all_refs else []) + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
396
401
397
402
def fetch ():
398
- log ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (os .getcwd ()))
403
+ info ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (os .getcwd ()))
399
404
popen ([hg_cmd , 'pull' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
400
405
401
406
def discard ():
402
- log ("Discarding local changes in \" %s\" " % os .path .basename (os .getcwd ()))
407
+ info ("Discarding local changes in \" %s\" " % os .path .basename (os .getcwd ()))
403
408
popen ([hg_cmd , 'update' , '-C' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
404
409
405
410
def checkout (rev , clean = False , clean_files = False ):
406
- log ("Checkout \" %s\" in %s to %s" % (rev , os .path .basename (os .getcwd ()), rev ))
411
+ info ("Checkout \" %s\" in %s to %s" % (rev , os .path .basename (os .getcwd ()), rev ))
407
412
if clean_files :
408
413
files = pquery ([hg_cmd , 'status' , '--no-status' , '-ui' ]).splitlines ()
409
414
for f in files :
410
- log ("Remove untracked file \" %s\" " % f )
415
+ info ("Remove untracked file \" %s\" " % f )
411
416
os .remove (f )
412
417
popen ([hg_cmd , 'update' ] + (['-C' ] if clean else []) + (['-r' , rev ] if rev else []) + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
413
418
@@ -544,14 +549,14 @@ def clone(url, name=None, depth=None, protocol=None):
544
549
popen ([git_cmd , 'clone' , formaturl (url , protocol ), name ] + (['--depth' , depth ] if depth else []) + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
545
550
546
551
def add (dest ):
547
- log ("Adding reference " + dest )
552
+ info ("Adding reference " + dest )
548
553
try :
549
554
popen ([git_cmd , 'add' , dest ] + (['-v' ] if very_verbose else []))
550
555
except ProcessException :
551
556
pass
552
557
553
558
def remove (dest ):
554
- log ("Removing reference " + dest )
559
+ info ("Removing reference " + dest )
555
560
try :
556
561
popen ([git_cmd , 'rm' , '-f' , dest ] + ([] if very_verbose else ['-q' ]))
557
562
except ProcessException :
@@ -576,29 +581,29 @@ def publish(all_refs=None):
576
581
error (err + "Working set is not on a branch." , 1 )
577
582
578
583
def fetch ():
579
- log ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (os .getcwd ()))
584
+ info ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (os .getcwd ()))
580
585
popen ([git_cmd , 'fetch' , '--all' , '--tags' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
581
586
582
587
def discard (clean_files = False ):
583
- log ("Discarding local changes in \" %s\" " % os .path .basename (os .getcwd ()))
588
+ info ("Discarding local changes in \" %s\" " % os .path .basename (os .getcwd ()))
584
589
pquery ([git_cmd , 'reset' , 'HEAD' ] + ([] if very_verbose else ['-q' ])) # unmarks files for commit
585
590
pquery ([git_cmd , 'checkout' , '.' ] + ([] if very_verbose else ['-q' ])) # undo modified files
586
591
pquery ([git_cmd , 'clean' , '-fd' ] + (['-x' ] if clean_files else []) + (['-q' ] if very_verbose else ['-q' ])) # cleans up untracked files and folders
587
592
588
593
def merge (dest ):
589
- log ("Merging \" %s\" with \" %s\" " % (os .path .basename (os .getcwd ()), dest ))
594
+ info ("Merging \" %s\" with \" %s\" " % (os .path .basename (os .getcwd ()), dest ))
590
595
popen ([git_cmd , 'merge' , dest ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
591
596
592
597
def checkout (rev , clean = False ):
593
598
if not rev :
594
599
return
595
- log ("Checkout \" %s\" in %s to %s" % (rev , os .path .basename (os .getcwd ()), rev ))
600
+ info ("Checkout \" %s\" in %s to %s" % (rev , os .path .basename (os .getcwd ()), rev ))
596
601
popen ([git_cmd , 'checkout' , rev ] + (['-f' ] if clean else []) + ([] if very_verbose else ['-q' ]))
597
602
if Git .isdetached (): # try to find associated refs to avoid detached state
598
603
refs = Git .getrefs (rev )
599
604
for ref in refs : # re-associate with a local or remote branch (rev is the same)
600
605
branch = re .sub (r'^(.*?)\/(.*?)$' , r'\2' , ref )
601
- log ("Revision \" %s\" matches a branch \" %s\" reference. Re-associating with branch" % (rev , branch ))
606
+ info ("Revision \" %s\" matches a branch \" %s\" reference. Re-associating with branch" % (rev , branch ))
602
607
popen ([git_cmd , 'checkout' , branch ] + ([] if very_verbose else ['-q' ]))
603
608
break
604
609
@@ -620,9 +625,9 @@ def update(rev=None, clean=False, clean_files=False, is_local=False):
620
625
else :
621
626
err = "Unable to update \" %s\" in \" %s\" .\n " % (os .path .basename (os .getcwd ()), os .getcwd ())
622
627
if not remote :
623
- log (err + "The local repository is not associated with a remote one." )
628
+ info (err + "The local repository is not associated with a remote one." )
624
629
if not branch :
625
- log (err + "Working set is not on a branch." )
630
+ info (err + "Working set is not on a branch." )
626
631
627
632
def status ():
628
633
return pquery ([git_cmd , 'status' , '-s' ] + (['-v' ] if very_verbose else []))
@@ -972,16 +977,16 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
972
977
973
978
# Try to clone with cache ref first
974
979
if cache and not os .path .isdir (path ):
975
- log ("Found matching cached repository in \" %s\" " % cache )
980
+ info ("Found matching cached repository in \" %s\" " % cache )
976
981
try :
977
982
if os .path .split (path )[0 ] and not os .path .isdir (os .path .split (path )[0 ]):
978
983
os .makedirs (os .path .split (path )[0 ])
979
984
980
- log ("Carbon copy from \" %s\" to \" %s\" " % (cache , path ))
985
+ info ("Carbon copy from \" %s\" to \" %s\" " % (cache , path ))
981
986
shutil .copytree (cache , path )
982
987
983
988
with cd (path ):
984
- log ("Update cached copy from remote repository" )
989
+ info ("Update cached copy from remote repository" )
985
990
scm .update (rev , True )
986
991
main = False
987
992
except (ProcessException , IOError ):
@@ -2268,7 +2273,7 @@ def main():
2268
2273
try :
2269
2274
very_verbose = pargs .very_verbose
2270
2275
verbose = very_verbose or pargs .verbose
2271
- log ('Working path \" %s\" (%s)' % (os .getcwd (), Repo .pathtype (cwd_root )))
2276
+ info ('Working path \" %s\" (%s)' % (os .getcwd (), Repo .pathtype (cwd_root )))
2272
2277
status = pargs .command (pargs )
2273
2278
except ProcessException as e :
2274
2279
error (
@@ -2282,7 +2287,7 @@ def main():
2282
2287
else :
2283
2288
error ('OS Error: %s' % e [1 ], e [0 ])
2284
2289
except KeyboardInterrupt as e :
2285
- log ('User aborted!' , - 1 )
2290
+ info ('User aborted!' , - 1 )
2286
2291
sys .exit (255 )
2287
2292
except Exception as e :
2288
2293
if very_verbose :
0 commit comments