Skip to content

Commit 0a80555

Browse files
committed
Rename log() to info() and introduce log() which handles output printing
1 parent 48f0172 commit 0a80555

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

mbed/mbed.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,30 @@
124124

125125

126126
# Logging and output
127+
def log(msg):
128+
sys.stderr.write(msg)
129+
127130
def message(msg):
128131
return "[mbed] %s\n" % msg
129132

130-
def log(msg, level=1):
133+
def info(msg, level=1):
131134
if level <= 0 or verbose:
132-
sys.stderr.write(message(msg))
135+
for line in msg.splitlines():
136+
log(message(line))
133137

134138
def action(msg):
135-
sys.stderr.write(message(msg))
139+
for line in msg.splitlines():
140+
log(message(line))
136141

137142
def warning(msg):
138143
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")
141146

142147
def error(msg, code=-1):
143148
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")
146151
sys.exit(code)
147152

148153
def progress_cursor():
@@ -164,7 +169,7 @@ class ProcessException(Exception):
164169

165170
def popen(command, stdin=None, **kwargs):
166171
# print for debugging
167-
log('Exec "'+' '.join(command)+'" in '+os.getcwd())
172+
info('Exec "'+' '.join(command)+'" in '+os.getcwd())
168173
try:
169174
proc = subprocess.Popen(command, **kwargs)
170175
except OSError as e:
@@ -180,7 +185,7 @@ def popen(command, stdin=None, **kwargs):
180185

181186
def pquery(command, stdin=None, **kwargs):
182187
if very_verbose:
183-
log('Query "'+' '.join(command)+'" in '+os.getcwd())
188+
info('Query "'+' '.join(command)+'" in '+os.getcwd())
184189
try:
185190
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
186191
except OSError as e:
@@ -301,15 +306,15 @@ def checkout(rev, clean=False):
301306
error("Unable to fetch late mbed library revision")
302307

303308
if rev != Bld.getrev():
304-
log("Cleaning up library build folder")
309+
info("Cleaning up library build folder")
305310
for fl in os.listdir('.'):
306311
if not fl.startswith('.'):
307312
if os.path.isfile(fl):
308313
os.remove(fl)
309314
else:
310315
shutil.rmtree(fl)
311316

312-
log("Checkout \"%s\" in %s" % (rev, os.path.basename(os.getcwd())))
317+
info("Checkout \"%s\" in %s" % (rev, os.path.basename(os.getcwd())))
313318
arch_url = m.group(1) + '/archive/' + rev + '.zip'
314319
arch_dir = m.group(7) + '-' + rev
315320
try:
@@ -375,14 +380,14 @@ def clone(url, name=None, depth=None, protocol=None):
375380
popen([hg_cmd, 'clone', formaturl(url, protocol), name] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
376381

377382
def add(dest):
378-
log("Adding reference \"%s\"" % dest)
383+
info("Adding reference \"%s\"" % dest)
379384
try:
380385
popen([hg_cmd, 'add', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
381386
except ProcessException:
382387
pass
383388

384389
def remove(dest):
385-
log("Removing reference \"%s\" " % dest)
390+
info("Removing reference \"%s\" " % dest)
386391
try:
387392
popen([hg_cmd, 'rm', '-f', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
388393
except ProcessException:
@@ -395,19 +400,19 @@ def publish(all_refs=None):
395400
popen([hg_cmd, 'push'] + (['--new-branch'] if all_refs else []) + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
396401

397402
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()))
399404
popen([hg_cmd, 'pull'] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
400405

401406
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()))
403408
popen([hg_cmd, 'update', '-C'] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
404409

405410
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))
407412
if clean_files:
408413
files = pquery([hg_cmd, 'status', '--no-status', '-ui']).splitlines()
409414
for f in files:
410-
log("Remove untracked file \"%s\"" % f)
415+
info("Remove untracked file \"%s\"" % f)
411416
os.remove(f)
412417
popen([hg_cmd, 'update'] + (['-C'] if clean else []) + (['-r', rev] if rev else []) + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
413418

@@ -544,14 +549,14 @@ def clone(url, name=None, depth=None, protocol=None):
544549
popen([git_cmd, 'clone', formaturl(url, protocol), name] + (['--depth', depth] if depth else []) + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
545550

546551
def add(dest):
547-
log("Adding reference "+dest)
552+
info("Adding reference "+dest)
548553
try:
549554
popen([git_cmd, 'add', dest] + (['-v'] if very_verbose else []))
550555
except ProcessException:
551556
pass
552557

553558
def remove(dest):
554-
log("Removing reference "+dest)
559+
info("Removing reference "+dest)
555560
try:
556561
popen([git_cmd, 'rm', '-f', dest] + ([] if very_verbose else ['-q']))
557562
except ProcessException:
@@ -576,29 +581,29 @@ def publish(all_refs=None):
576581
error(err+"Working set is not on a branch.", 1)
577582

578583
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()))
580585
popen([git_cmd, 'fetch', '--all', '--tags'] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
581586

582587
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()))
584589
pquery([git_cmd, 'reset', 'HEAD'] + ([] if very_verbose else ['-q'])) # unmarks files for commit
585590
pquery([git_cmd, 'checkout', '.'] + ([] if very_verbose else ['-q'])) # undo modified files
586591
pquery([git_cmd, 'clean', '-fd'] + (['-x'] if clean_files else []) + (['-q'] if very_verbose else ['-q'])) # cleans up untracked files and folders
587592

588593
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))
590595
popen([git_cmd, 'merge', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
591596

592597
def checkout(rev, clean=False):
593598
if not rev:
594599
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))
596601
popen([git_cmd, 'checkout', rev] + (['-f'] if clean else []) + ([] if very_verbose else ['-q']))
597602
if Git.isdetached(): # try to find associated refs to avoid detached state
598603
refs = Git.getrefs(rev)
599604
for ref in refs: # re-associate with a local or remote branch (rev is the same)
600605
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))
602607
popen([git_cmd, 'checkout', branch] + ([] if very_verbose else ['-q']))
603608
break
604609

@@ -620,9 +625,9 @@ def update(rev=None, clean=False, clean_files=False, is_local=False):
620625
else:
621626
err = "Unable to update \"%s\" in \"%s\".\n" % (os.path.basename(os.getcwd()), os.getcwd())
622627
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.")
624629
if not branch:
625-
log(err+"Working set is not on a branch.")
630+
info(err+"Working set is not on a branch.")
626631

627632
def status():
628633
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):
972977

973978
# Try to clone with cache ref first
974979
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)
976981
try:
977982
if os.path.split(path)[0] and not os.path.isdir(os.path.split(path)[0]):
978983
os.makedirs(os.path.split(path)[0])
979984

980-
log("Carbon copy from \"%s\" to \"%s\"" % (cache, path))
985+
info("Carbon copy from \"%s\" to \"%s\"" % (cache, path))
981986
shutil.copytree(cache, path)
982987

983988
with cd(path):
984-
log("Update cached copy from remote repository")
989+
info("Update cached copy from remote repository")
985990
scm.update(rev, True)
986991
main = False
987992
except (ProcessException, IOError):
@@ -2268,7 +2273,7 @@ def main():
22682273
try:
22692274
very_verbose = pargs.very_verbose
22702275
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)))
22722277
status = pargs.command(pargs)
22732278
except ProcessException as e:
22742279
error(
@@ -2282,7 +2287,7 @@ def main():
22822287
else:
22832288
error('OS Error: %s' % e[1], e[0])
22842289
except KeyboardInterrupt as e:
2285-
log('User aborted!', -1)
2290+
info('User aborted!', -1)
22862291
sys.exit(255)
22872292
except Exception as e:
22882293
if very_verbose:

0 commit comments

Comments
 (0)