Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-sdk-tools by
Diff: utils.py
- Revision:
- 32:8ea194f6145b
- Parent:
- 29:1210849dba19
--- a/utils.py Mon Aug 29 11:56:59 2016 +0100 +++ b/utils.py Wed Jan 04 11:58:24 2017 -0600 @@ -22,12 +22,17 @@ from os import listdir, remove, makedirs from shutil import copyfile from os.path import isdir, join, exists, split, relpath, splitext, abspath -from os.path import commonprefix, normpath +from os.path import commonprefix, normpath, dirname from subprocess import Popen, PIPE, STDOUT, call +from math import ceil import json from collections import OrderedDict import logging +def remove_if_in(lst, thing): + if thing in lst: + lst.remove(thing) + def compile_worker(job): """Standard task runner used for compiling @@ -282,9 +287,8 @@ parser - the ArgumentParser object that parsed the command line message - what went wrong """ - print "\n\n%s\n\n" % message - parser.print_help() - sys.exit() + parser.error(message) + sys.exit(2) def construct_enum(**enums): @@ -432,6 +436,19 @@ raise argparse.ArgumentTypeError( "{0}"" does not exist in the filesystem.".format(string)) +def argparse_profile_filestring_type(string): + """ An argument parser that verifies that a string passed in is either + absolute path or a file name (expanded to + mbed-os/tools/profiles/<fname>.json) of a existing file""" + fpath = join(dirname(__file__), "profiles/{}.json".format(string)) + if exists(string): + return string + elif exists(fpath): + return fpath + else: + raise argparse.ArgumentTypeError( + "{0} does not exist in the filesystem.".format(string)) + def columnate(strings, separator=", ", chars=80): """ render a list of strings as a in a bunch of columns @@ -470,3 +487,30 @@ else: return not_parent return parse_type + +def argparse_deprecate(replacement_message): + """fail if argument is provided with deprecation warning""" + def parse_type(_): + """The parser type""" + raise argparse.ArgumentTypeError("Deprecated." + replacement_message) + return parse_type + +def print_large_string(large_string): + """ Breaks a string up into smaller pieces before print them + + This is a limitation within Windows, as detailed here: + https://bugs.python.org/issue11395 + + Positional arguments: + large_string - the large string to print + """ + string_limit = 1000 + large_string_len = len(large_string) + num_parts = int(ceil(float(large_string_len) / float(string_limit))) + for string_part in range(num_parts): + start_index = string_part * string_limit + if string_part == num_parts - 1: + print large_string[start_index:] + else: + end_index = ((string_part + 1) * string_limit) - 1 + print large_string[start_index:end_index],