From 3af2c5ae1414758d0e69113bea6b7ed078077d09 Mon Sep 17 00:00:00 2001 From: Bartek Szatkowski Date: Tue, 11 Oct 2016 13:30:20 -0500 Subject: [PATCH] Accept profile name as well as file path for tools/make.py --profile --profile can be used with just a profile name eg. default, debug as long as .json file is in default profile directory. --- tools/options.py | 13 +++++++++++-- tools/utils.py | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tools/options.py b/tools/options.py index 0c3b016d95e..8ffab53bc91 100644 --- a/tools/options.py +++ b/tools/options.py @@ -16,12 +16,13 @@ """ from json import load from os.path import join, dirname +from os import listdir from argparse import ArgumentParser from tools.toolchains import TOOLCHAINS from tools.targets import TARGET_NAMES from tools.utils import argparse_force_uppercase_type, \ argparse_lowercase_hyphen_type, argparse_many, \ - argparse_filestring_type, args_error + argparse_filestring_type, args_error, argparse_profile_filestring_type def get_default_options_parser(add_clean=True, add_options=True, add_app_config=False): @@ -73,7 +74,9 @@ def get_default_options_parser(add_clean=True, add_options=True, if add_options: parser.add_argument("--profile", dest="profile", action="append", - type=argparse_filestring_type, + type=argparse_profile_filestring_type, + help="Build profile to use. Can be either path to json" \ + "file or one of the default one ({})".format(", ".join(list_profiles())), default=[]) if add_app_config: parser.add_argument("--app-config", default=None, dest="app_config", @@ -82,6 +85,12 @@ def get_default_options_parser(add_clean=True, add_options=True, return parser +def list_profiles(): + """Lists available build profiles + + Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only + """ + return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")] def extract_profile(parser, options, toolchain): """Extract a Toolchain profile from parsed options diff --git a/tools/utils.py b/tools/utils.py index 9ad74de63dd..734dfae0e07 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -22,7 +22,7 @@ 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 import json from collections import OrderedDict @@ -435,6 +435,19 @@ def argparse_filestring_type(string): 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/.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