Skip to content

Commit efd83ab

Browse files
committed
Add support for add/remove commands in non-SCM
1 parent 3bf6302 commit efd83ab

File tree

1 file changed

+47
-54
lines changed

1 file changed

+47
-54
lines changed

mbed/mbed.py

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,6 @@ def clone(url, path=None, depth=None, protocol=None):
243243
except Exception as e:
244244
error(e[1], e[0])
245245

246-
def add(dest):
247-
return True
248-
249-
def remove(dest):
250-
if os.path.isfile(dest):
251-
os.remove(dest)
252-
return True
253-
254-
def commit():
255-
error("mbed library builds do not support committing")
256-
257-
def publish(all=None):
258-
return False
259-
260246
def fetch(url, rev):
261247
tmp_file = '.rev-' + rev + '.zip'
262248
arch_dir = 'mbed-' + rev
@@ -379,10 +365,6 @@ def remove(dest):
379365
popen([hg_cmd, 'rm', '-f', dest] + (['-v'] if verbose else ['-q']))
380366
except ProcessException:
381367
pass
382-
try:
383-
os.remove(dest)
384-
except OSError:
385-
pass
386368

387369
def commit():
388370
popen([hg_cmd, 'commit'] + (['-v'] if verbose else ['-q']))
@@ -552,10 +534,6 @@ def remove(dest):
552534
popen([git_cmd, 'rm', '-f', dest] + ([] if verbose else ['-q']))
553535
except ProcessException:
554536
pass
555-
try:
556-
os.remove(dest)
557-
except OSError:
558-
pass
559537

560538
def commit():
561539
popen([git_cmd, 'commit', '-a'] + (['-v'] if verbose else ['-q']))
@@ -803,20 +781,26 @@ def fromlib(cls, lib=None):
803781
@classmethod
804782
def fromrepo(cls, path=None):
805783
repo = cls()
784+
repo.is_cwd = True
806785
if path is None:
807786
path = Repo.findrepo(os.getcwd())
808-
if path is None:
809-
error(
810-
"Cannot find the program or library in the current path \"%s\".\n"
811-
"Please change your working directory to a different location or use \"mbed new\" to create a new program." % os.getcwd(), 1)
787+
if path:
788+
repo.is_cwd = False
789+
else:
790+
path = os.getcwd()
791+
warning(
792+
"Could not find mbed program in current path. Assuming current dir.\n"
793+
"You can fix this by calling \"mbed new .\" in the root of your program")
812794

813795
repo.path = os.path.abspath(path)
814796
repo.name = os.path.basename(repo.path)
815797

816798
repo.sync()
817799

818-
if repo.scm is None:
819-
error("Current folder is not a supported repository", -1)
800+
if not repo.is_cwd and repo.scm is None:
801+
warning(
802+
"Program \"%s\" in \"%s\" does not use source control management.\n"
803+
"To fix this you should use \"mbed new .\" in the root of your program." % (repo.name, repo.path))
820804

821805
return repo
822806

@@ -833,7 +817,7 @@ def findrepo(cls, path=None):
833817
path = os.path.abspath(path or os.getcwd())
834818

835819
while cd(path):
836-
if Repo.isrepo(path):
820+
if os.path.isfile(os.path.join(path, Program.config_file)) or Repo.isrepo(path):
837821
return path
838822

839823
tpath = path
@@ -887,7 +871,7 @@ def sync(self):
887871
if os.path.isdir(self.path):
888872
try:
889873
self.scm = self.getscm()
890-
if self.scm.name == 'bld':
874+
if self.scm and self.scm.name == 'bld':
891875
self.is_build = True
892876
except ProcessException:
893877
pass
@@ -931,8 +915,13 @@ def getrev(self, *args, **kwargs):
931915
def add(self, *args, **kwargs):
932916
return self.__scm_call('add', *args, **kwargs)
933917

934-
def remove(self, *args, **kwargs):
935-
return self.__scm_call('remove', *args, **kwargs)
918+
def remove(self, dest, *args, **kwargs):
919+
if os.path.isfile(dest):
920+
try:
921+
os.remove(dest)
922+
except OSError:
923+
pass
924+
return self.__scm_call('remove', dest, *args, **kwargs)
936925

937926
def ignores(self, *args, **kwargs):
938927
return self.__scm_call('ignores', *args, **kwargs)
@@ -1050,31 +1039,27 @@ class Program(object):
10501039

10511040
def __init__(self, path=None, print_warning=False):
10521041
path = os.path.abspath(path or os.getcwd())
1053-
1054-
self.path = os.getcwd()
1042+
self.path = path
10551043
self.is_cwd = True
10561044

10571045
while cd(path):
10581046
tpath = path
1059-
if Repo.isrepo(path):
1047+
if os.path.isfile(os.path.join(path, Program.config_file)):
10601048
self.path = path
10611049
self.is_cwd = False
1062-
self.is_repo = True
1050+
break
1051+
10631052
path = os.path.split(path)[0]
10641053
if tpath == path: # Reached root.
10651054
break
10661055

10671056
self.name = os.path.basename(self.path)
10681057

10691058
# is_cwd flag indicates that current dir is assumed to be root, not root repo
1070-
if self.is_cwd:
1071-
err = (
1059+
if self.is_cwd and print_warning:
1060+
warning(
10721061
"Could not find mbed program in current path. Assuming current dir.\n"
1073-
"You can fix this by calling \"mbed new .\" in the root dir of your program")
1074-
if print_warning:
1075-
warning(err)
1076-
else:
1077-
error(err, 1)
1062+
"You can fix this by calling \"mbed new .\" in the root of your program")
10781063

10791064
# Sets config value
10801065
def set_cfg(self, var, val):
@@ -1110,6 +1095,9 @@ def get_cfg(self, var, default_val=None):
11101095
return m.group(2)
11111096
return default_val
11121097

1098+
def set_root(self):
1099+
return self.set_cfg('ROOT', '.')
1100+
11131101
# Gets mbed OS dir (unified)
11141102
def get_os_dir(self):
11151103
if os.path.isdir(os.path.join(self.path, 'mbed-os')):
@@ -1296,19 +1284,19 @@ def new(name, scm='git', depth=None, protocol=None):
12961284
cwd_root = os.path.abspath(d_path)
12971285

12981286
program = Program(d_path)
1287+
program.set_root()
12991288
if not program.get_os_dir():
13001289
try:
13011290
with cd(d_path):
1302-
add(mbed_os_url, depth=depth, protocol=protocol)
1291+
add(mbed_os_url, depth=depth, protocol=protocol, top=False)
13031292
except Exception as e:
13041293
if os.path.isdir(os.path.join(d_path, 'mbed-os')):
13051294
rmtree_readonly(os.path.join(d_path, 'mbed-os'))
13061295
raise e
13071296
if d_path:
13081297
os.chdir(d_path)
13091298

1310-
program = Program(d_path)
1311-
program.post_action()
1299+
Program(d_path).post_action()
13121300

13131301

13141302
# Import command
@@ -1343,9 +1331,13 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
13431331
for _, scm in sorted_scms:
13441332
try:
13451333
scm.clone(repo.url, repo.path, depth=depth, protocol=protocol)
1334+
repo.scm = scm
1335+
repo.ignores()
1336+
13461337
with cd(repo.path):
1338+
Program(repo.path).set_root()
13471339
try:
1348-
scm.update(repo.rev, True)
1340+
repo.update(repo.rev, True)
13491341
except ProcessException as e:
13501342
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
13511343
if depth:
@@ -1374,8 +1366,7 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
13741366
deploy(ignore=ignore, depth=depth, protocol=protocol, top=False)
13751367

13761368
if top:
1377-
program = Program(repo.path)
1378-
program.post_action()
1369+
Program(repo.path).post_action()
13791370

13801371

13811372
# Deploy command
@@ -1398,8 +1389,7 @@ def deploy(ignore=False, depth=None, protocol=None, top=True):
13981389
repo.ignore(relpath(repo.path, lib.path))
13991390

14001391
if top:
1401-
program = Program(repo.path)
1402-
program.post_action()
1392+
Program(repo.path).post_action()
14031393

14041394

14051395
# Add library command
@@ -1410,7 +1400,7 @@ def deploy(ignore=False, depth=None, protocol=None, top=True):
14101400
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
14111401
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
14121402
help='Add a library and its dependencies into the current %s or specified destination path.' % cwd_type)
1413-
def add(url, path=None, ignore=False, depth=None, protocol=None):
1403+
def add(url, path=None, ignore=False, depth=None, protocol=None, top=True):
14141404
repo = Repo.fromrepo()
14151405

14161406
lib = Repo.fromurl(url, path)
@@ -1421,6 +1411,9 @@ def add(url, path=None, ignore=False, depth=None, protocol=None):
14211411
lib.write()
14221412
repo.add(lib.lib)
14231413

1414+
if top:
1415+
Program(repo.path).post_action()
1416+
14241417

14251418
# Remove library
14261419
@subcommand('remove',
@@ -1432,6 +1425,7 @@ def remove(path):
14321425
error("Could not find library in path (%s)" % path, 1)
14331426

14341427
lib = Repo.fromrepo(path)
1428+
action("Removing library \"%s\" in \"%s\"" % (lib.name, lib.path))
14351429
rmtree_readonly(lib.path)
14361430
repo.remove(lib.lib)
14371431
repo.unignore(relpath(repo.path, lib.path))
@@ -1569,8 +1563,7 @@ def update(rev=None, clean=False, force=False, ignore=False, top=True, depth=Non
15691563
update(lib.rev, clean, force, ignore=ignore, top=False)
15701564

15711565
if top:
1572-
program = Program(repo.path)
1573-
program.post_action()
1566+
Program(repo.path).post_action()
15741567

15751568

15761569
# Synch command

0 commit comments

Comments
 (0)