Skip to content

Commit f817ec1

Browse files
authored
Merge pull request #2142 from theotherjimmy/anti-remove-repo
[tools-build,make,test] Disallow parent of repo as --build args
2 parents b10276c + 9759382 commit f817ec1

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

tools/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT
3939
from utils import argparse_filestring_type
4040
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT, CLI_COLOR_MAP
41+
from utils import argparse_filestring_type, argparse_dir_not_parent
4142

4243
if __name__ == '__main__':
4344
start = time()
@@ -48,7 +49,7 @@
4849
parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
4950
default=None, help="The source (input) directory", action="append")
5051

51-
parser.add_argument("--build", dest="build_dir",
52+
parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
5253
default=None, help="The build (output) directory")
5354

5455
parser.add_argument("--no-archive", dest="no_archive", action="store_true",

tools/make.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from tools.build_api import mcu_toolchain_matrix
4646
from utils import argparse_filestring_type
4747
from utils import argparse_many
48+
from utils import argparse_dir_not_parent
4849
from argparse import ArgumentTypeError
4950
from tools.toolchains import mbedToolchain
5051
from tools.settings import CLI_COLOR_MAP
@@ -112,7 +113,7 @@
112113
default=None, help="The source (input) directory", action="append")
113114
parser.add_argument("--duration", type=int, dest="duration",
114115
default=None, help="Duration of the test")
115-
parser.add_argument("--build", dest="build_dir",
116+
parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
116117
default=None, help="The build (output) directory")
117118
parser.add_argument("-N", "--artifact-name", dest="artifact_name",
118119
default=None, help="The built project's name")

tools/project.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from tools.tests import TESTS, TEST_MAP
1313
from tools.tests import test_known, test_name_known
1414
from tools.targets import TARGET_NAMES
15+
from tools.libraries import LIBRARIES
1516
from utils import argparse_filestring_type, argparse_many
16-
from utils import argparse_force_lowercase_type, argparse_force_uppercase_type
17+
from utils import argparse_force_lowercase_type, argparse_force_uppercase_type, argparse_dir_not_parent
1718
from project_api import setup_project, perform_export, print_results, get_lib_symbols
1819

1920

@@ -59,6 +60,7 @@
5960
dest="build",
6061
action="store_true",
6162
default=False,
63+
type=argparse_dir_not_parent(ROOT),
6264
help="use the mbed library build, instead of the sources")
6365

6466
group.add_argument("-L", "--list-tests",

tools/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from tools.utils import mkdir, ToolException, NotSupportedException
3535
from tools.test_exporters import ReportExporter, ResultExporterType
3636
from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many
37+
from utils import argparse_dir_not_parent
3738
from tools.toolchains import mbedToolchain
3839
from tools.settings import CLI_COLOR_MAP
3940

@@ -57,7 +58,7 @@
5758
type=argparse_filestring_type,
5859
default=None, help="The source (input) directory (for sources other than tests). Defaults to current directory.", action="append")
5960

60-
parser.add_argument("--build", dest="build_dir",
61+
parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
6162
default=None, help="The build (output) directory")
6263

6364
parser.add_argument("-l", "--list", action="store_true", dest="list",

tools/utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import math
2222
from os import listdir, remove, makedirs
2323
from shutil import copyfile
24-
from os.path import isdir, join, exists, split, relpath, splitext
24+
from os.path import isdir, join, exists, split, relpath, splitext, abspath, commonprefix
2525
from subprocess import Popen, PIPE, STDOUT, call
2626
import json
2727
from collections import OrderedDict
@@ -307,3 +307,14 @@ def columnate(strings, seperator=", ", chars=80):
307307
append = append.ljust(total_width)
308308
output += append
309309
return output
310+
311+
# fail if argument provided is a parent of the specified directory
312+
def argparse_dir_not_parent(other):
313+
def parse_type(not_parent):
314+
abs_other = abspath(other)
315+
abs_not_parent = abspath(not_parent)
316+
if abs_not_parent == commonprefix([abs_not_parent, abs_other]):
317+
raise argparse.ArgumentTypeError("{0} may not be a parent directory of {1}".format(not_parent, other))
318+
else:
319+
return not_parent
320+
return parse_type

0 commit comments

Comments
 (0)