Skip to content

Commit ec722c1

Browse files
committed
Refactored command new to support non-SCM operations.
Added switches: * --program and --library to enforce behavior of "new" * --mbedlib to enable new programs based on the mbed library (mbed Classic) * --create-only to only create the program/library and not import mbed-os or mbed library (autmation) * changed "scm" from positional argument to --scm switch Also added support for "none" as option to --scm which prevents git and hg repositories to be created (plain folder programs support)
1 parent 70b57ea commit ec722c1

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

mbed/mbed.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,11 @@ def get_cfg(self, var, default_val=None):
11061106
def set_root(self):
11071107
return self.set_cfg('ROOT', '.')
11081108

1109+
def unset_root(self, path=None):
1110+
fl = os.path.join(path or self.path, self.config_file)
1111+
if os.path.isfile(fl):
1112+
os.remove(fl)
1113+
11091114
# Gets mbed OS dir (unified)
11101115
def get_os_dir(self):
11111116
if os.path.isdir(os.path.join(self.path, 'mbed-os')):
@@ -1250,7 +1255,7 @@ def subcommand(command):
12501255

12511256
def thunk(parsed_args):
12521257
argv = [arg['dest'] if 'dest' in arg else arg['name'] for arg in args]
1253-
argv = [(arg if isinstance(arg, basestring) else arg[-1]).strip('-')
1258+
argv = [(arg if isinstance(arg, basestring) else arg[-1]).strip('-').replace('-', '_')
12541259
for arg in argv]
12551260
argv = {arg: vars(parsed_args)[arg] for arg in argv
12561261
if vars(parsed_args)[arg] is not None}
@@ -1265,42 +1270,55 @@ def thunk(parsed_args):
12651270
# New command
12661271
@subcommand('new',
12671272
dict(name='name', help='Destination name or path'),
1268-
dict(name='scm', nargs='?', help='Source control management. Currently supported: %s. Default: git' % ', '.join([s.name for s in scms.values()])),
1269-
dict(name='--mbedlib', action='store_true', help='Add the mbed library to the program (instead of mbed-os).'),
1273+
dict(name='--scm', nargs='?', help='Source control management. Currently supported: %s. Default: git' % ', '.join([s.name for s in scms.values()])),
1274+
dict(name='--program', action='store_true', help='Force creation of an mbed program. Default: auto.'),
1275+
dict(name='--library', action='store_true', help='Force creation of an mbed library. Default: auto.'),
1276+
dict(name='--mbedlib', action='store_true', help='Add the mbed library instead of mbed-os into the program.'),
1277+
dict(name='--create-only', action='store_true', help='Only create program, do not import mbed-os or mbed library.'),
12701278
dict(name='--depth', nargs='?', help='Number of revisions to fetch the mbed OS repository when creating new program. Default: all revisions.'),
12711279
dict(name='--protocol', nargs='?', help='Transport protocol when fetching the mbed OS repository when creating new program. Supported: https, http, ssh, git. Default: inferred from URL.'),
1272-
help='Create a new program based on the specified source control management. Will create a new library when called from inside a local program. Supported SCMs: %s.' % (', '.join([s.name for s in scms.values()])))
1273-
def new(name, scm='git', mbedlib=False, depth=None, protocol=None):
1280+
help='Create a new program based on the specified source control management. Will create a new library when called from inside a program. Supported SCMs: %s.' % (', '.join([s.name for s in scms.values()])))
1281+
def new(name, scm='git', program=False, library=False, mbedlib=False, create_only=False, depth=None, protocol=None):
12741282
global cwd_root
12751283

12761284
d_path = name or os.getcwd()
1277-
if os.path.isdir(d_path):
1278-
if Repo.isrepo(d_path):
1279-
error("A %s already exists in \"%s\". Please select a different name or location." % (cwd_dest, d_path), 1)
1280-
if len(os.listdir(d_path)) > 1:
1281-
warning("Directory \"%s\" is not empty." % d_path)
1285+
p_path = Repo.findrepo(d_path) or d_path
1286+
if program and library:
1287+
error("Cannot use both --program and --library options.", 1)
1288+
elif not program and not library:
1289+
d_type = 'library' if os.path.abspath(p_path) != os.path.abspath(d_path) else 'program'
1290+
else:
1291+
d_type = 'library' if library else 'program'
12821292

1283-
# Find parent repository before the new one is created
1284-
p_path = Repo.findrepo(d_path)
1293+
if scm and scm != 'none':
1294+
if os.path.isdir(d_path) and Repo.isrepo(d_path):
1295+
repo = Repo.fromrepo(d_path)
1296+
if repo.scm.name != scm:
1297+
error("A repository already exists in \"%s\" based on %s. Please select a different name or location." % (d_path, scm), 1)
1298+
else:
1299+
repo_scm = [s for s in scms.values() if s.name == scm.lower()]
1300+
if not repo_scm:
1301+
error(
1302+
"You have specified invalid source control management system\n"
1303+
"Please specify one of the following SCMs: %s" % ', '.join([s.name for s in scms.values()]), 1)
1304+
repo_scm[0].init(d_path)
1305+
else:
1306+
scm = 'folder'
1307+
if not os.path.isdir(d_path):
1308+
os.mkdir(d_path)
12851309

1286-
repo_scm = [s for s in scms.values() if s.name == scm.lower()]
1287-
if not repo_scm:
1288-
error("Please specify one of the following source control management systems: %s" % ', '.join([s.name for s in scms.values()]), 1)
1310+
if len(os.listdir(d_path)) > 1:
1311+
warning("Directory \"%s\" is not empty." % d_path)
12891312

1290-
action("Creating new %s \"%s\" (%s)" % (cwd_dest, os.path.basename(d_path), repo_scm[0].name))
1291-
# Initialize repository
1292-
repo_scm[0].init(d_path)
1313+
action("Creating new %s \"%s\" (%s)" % (d_type, os.path.basename(d_path), scm))
1314+
p = Program(d_path)
12931315

1294-
if p_path: # It's a library
1295-
with cd(p_path):
1296-
sync()
1297-
else: # It's a program
1316+
if d_type == 'program':
12981317
# This helps sub-commands to display relative paths to the created program
12991318
cwd_root = os.path.abspath(d_path)
1300-
1301-
program = Program(d_path)
1302-
program.set_root()
1303-
if not program.get_os_dir() and not program.get_mbedlib_dir():
1319+
p.path = cwd_root
1320+
p.set_root()
1321+
if not create_only and not p.get_os_dir() and not p.get_mbedlib_dir():
13041322
url = mbed_lib_url if mbedlib else mbed_os_url
13051323
try:
13061324
with cd(d_path):
@@ -1309,9 +1327,13 @@ def new(name, scm='git', mbedlib=False, depth=None, protocol=None):
13091327
d = 'mbed' if mbedlib else 'mbed-os'
13101328
if os.path.isdir(os.path.join(d_path, d)):
13111329
rmtree_readonly(os.path.join(d_path, d))
1312-
raise
1330+
raise e
13131331
if d_path:
13141332
os.chdir(d_path)
1333+
else:
1334+
p.unset_root(d_path)
1335+
with cd(p_path):
1336+
sync()
13151337

13161338
Program(d_path).post_action()
13171339

0 commit comments

Comments
 (0)