Skip to content

Commit b1e4f76

Browse files
committed
New feature: --offline mode
1 parent 16ce2d6 commit b1e4f76

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

mbed/mbed.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ def error(msg, code=-1):
167167
sys.stderr.write("---\n")
168168
sys.exit(code)
169169

170+
def offline_warning(offline, top=True):
171+
if top and offline:
172+
log("\n")
173+
log(".=============================== OFFLINE MODE ================================.\n")
174+
log("| Offline mode is enabled. No connections to remote repositories will be |\n")
175+
log("| made and only locally cached repositories will be used. |\n")
176+
log("| This might break some actions if non-cached repositories are referenced. |\n")
177+
log("'============================================================================='\n\n")
178+
170179
def progress_cursor():
171180
while True:
172181
for cursor in '|/-\\':
@@ -1179,7 +1188,7 @@ def remove(self, dest, *args, **kwargs):
11791188
pass
11801189
return self.scm.remove(dest, *args, **kwargs)
11811190

1182-
def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
1191+
def clone(self, url, path, rev=None, depth=None, protocol=None, offline=False, **kwargs):
11831192
# Sorted so repositories that match urls are attempted first
11841193
info("Trying to guess source control management tool. Supported SCMs: %s" % ', '.join([s.name for s in scms.values()]))
11851194
for scm in scms.values():
@@ -1202,7 +1211,7 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
12021211
info("Update cached copy from remote repository")
12031212
if not rev:
12041213
rev = scm.default_branch
1205-
scm.update(rev, True)
1214+
scm.update(rev, True, is_local=offline)
12061215
main = False
12071216
except (ProcessException, IOError):
12081217
info("Discarding cached repository")
@@ -1211,6 +1220,8 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
12111220

12121221
# Main clone routine if the clone with cache ref failed (might occur if cache ref is dirty)
12131222
if main:
1223+
if offline:
1224+
error("Unable to clone repository \"%s\" in offline mode ('--offline' used)." % url)
12141225
try:
12151226
scm.clone(url, path, depth=depth, protocol=protocol, **kwargs)
12161227
except ProcessException:
@@ -1816,14 +1827,16 @@ def thunk(parsed_args):
18161827
dict(name='--create-only', action='store_true', help='Only create a program, do not import mbed-os or mbed library.'),
18171828
dict(name='--depth', nargs='?', help='Number of revisions to fetch the mbed OS repository when creating new program. Default: all revisions.'),
18181829
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.'),
1830+
dict(name='--offline', action='store_true', help='Offline mode will force the use of locally cached repositories and prevent requests to remote repositories.'),
18191831
help='Create new mbed program or library',
18201832
description=(
18211833
"Creates a new mbed program if executed within a non-program location.\n"
18221834
"Alternatively creates an mbed library if executed within an existing program.\n"
18231835
"When creating new program, the latest mbed-os release will be downloaded/added\n unless --create-only is specified.\n"
18241836
"Supported source control management: git, hg"))
1825-
def new(name, scm='git', program=False, library=False, mbedlib=False, create_only=False, depth=None, protocol=None):
1837+
def new(name, scm='git', program=False, library=False, mbedlib=False, create_only=False, depth=None, protocol=None, offline=False):
18261838
global cwd_root
1839+
offline_warning(offline)
18271840

18281841
d_path = os.path.abspath(name or getcwd())
18291842
p_path = os.path.dirname(d_path)
@@ -1900,14 +1913,16 @@ def new(name, scm='git', program=False, library=False, mbedlib=False, create_onl
19001913
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
19011914
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
19021915
dict(name='--insecure', action='store_true', help='Allow insecure repository URLs. By default mbed CLI imports only "safe" URLs, e.g. based on standard ports - 80, 443 and 22. This option enables the use of arbitrary URLs/ports.'),
1916+
dict(name='--offline', action='store_true', help='Offline mode will force the use of locally cached repositories and prevent requests to remote repositories.'),
19031917
hidden_aliases=['im', 'imp'],
19041918
help='Import program from URL',
19051919
description=(
19061920
"Imports mbed program and its dependencies from a source control based URL\n"
19071921
"(GitHub, Bitbucket, mbed.org) into the current directory or specified\npath.\n"
19081922
"Use 'mbed add <URL>' to add a library into an existing program."))
1909-
def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=False, top=True):
1923+
def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=False, offline=False, top=True):
19101924
global cwd_root
1925+
offline_warning(offline, top)
19111926

19121927
# translate 'mbed-os' to https://github.com/ARMmbed/mbed-os
19131928
orig_url = url
@@ -1931,11 +1946,11 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=Fa
19311946
error("Directory \"%s\" is not empty. Please ensure that the destination folder is empty." % repo.path, 1)
19321947

19331948
if not Repo.isurl(orig_url) and os.path.exists(orig_url):
1934-
warning("Importing from a local folder \"%s\", not from a URL" % orig_url)
1949+
warning("Importing from a local folder \"%s\", not from a URL" % orig_url)
19351950

19361951
text = "Importing program" if top else "Adding library"
19371952
action("%s \"%s\" from \"%s\"%s" % (text, relpath(cwd_root, repo.path), formaturl(repo.url, protocol), ' at '+(repo.revtype(repo.rev))))
1938-
if repo.clone(repo.url, repo.path, rev=repo.rev, depth=depth, protocol=protocol):
1953+
if repo.clone(repo.url, repo.path, rev=repo.rev, depth=depth, protocol=protocol, offline=offline):
19391954
with cd(repo.path):
19401955
Program(repo.path).set_root()
19411956
try:
@@ -1962,7 +1977,7 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=Fa
19621977
cwd_root = repo.path
19631978

19641979
with cd(repo.path):
1965-
deploy(ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, top=False)
1980+
deploy(ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, offline=offline, top=False)
19661981

19671982
if top:
19681983
Program(repo.path).post_action()
@@ -1976,17 +1991,19 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=Fa
19761991
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
19771992
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
19781993
dict(name='--insecure', action='store_true', help='Allow insecure repository URLs. By default mbed CLI imports only "safe" URLs, e.g. based on standard ports - 80, 443 and 22. This option enables the use of arbitrary URLs/ports.'),
1994+
dict(name='--offline', action='store_true', help='Offline mode will force the use of locally cached repositories and prevent requests to remote repositories.'),
19791995
hidden_aliases=['ad'],
19801996
help='Add library from URL',
19811997
description=(
19821998
"Adds mbed library and its dependencies from a source control based URL\n"
19831999
"(GitHub, Bitbucket, mbed.org) into an existing program.\n"
19842000
"Use 'mbed import <URL>' to import as a program"))
1985-
def add(url, path=None, ignore=False, depth=None, protocol=None, insecure=False, top=True):
1986-
repo = Repo.fromrepo()
2001+
def add(url, path=None, ignore=False, depth=None, protocol=None, insecure=False, offline=False, top=True):
2002+
offline_warning(offline, top)
19872003

2004+
repo = Repo.fromrepo()
19882005
lib = Repo.fromurl(url, path)
1989-
import_(lib.fullurl, lib.path, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, top=False)
2006+
import_(lib.fullurl, lib.path, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, offline=offline, top=False)
19902007
repo.ignore(relpath(repo.path, lib.path))
19912008
lib.sync()
19922009

@@ -2023,22 +2040,24 @@ def remove(path):
20232040
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
20242041
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
20252042
dict(name='--insecure', action='store_true', help='Allow insecure repository URLs. By default mbed CLI imports only "safe" URLs, e.g. based on standard ports - 80, 443 and 22. This option enables the use of arbitrary URLs/ports.'),
2043+
dict(name='--offline', action='store_true', help='Offline mode will force the use of locally cached repositories and prevent requests to remote repositories.'),
20262044
help='Find and add missing libraries',
20272045
description=(
20282046
"Import missing dependencies in an existing program or library.\n"
20292047
"Use 'mbed import <URL>' and 'mbed add <URL>' instead of cloning manually and\n"
20302048
"then running 'mbed deploy'"))
2031-
def deploy(ignore=False, depth=None, protocol=None, insecure=False, top=True):
2049+
def deploy(ignore=False, depth=None, protocol=None, insecure=False, offline=False, top=True):
2050+
offline_warning(offline, top)
2051+
20322052
repo = Repo.fromrepo()
20332053
repo.ignores()
2034-
20352054
for lib in repo.libs:
20362055
if os.path.isdir(lib.path):
20372056
if lib.check_repo():
20382057
with cd(lib.path):
2039-
update(lib.rev, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, top=False)
2058+
update(lib.rev, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, offline=offline, top=False)
20402059
else:
2041-
import_(lib.fullurl, lib.path, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, top=False)
2060+
import_(lib.fullurl, lib.path, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, offline=offline, top=False)
20422061
repo.ignore(relpath(repo.path, lib.path))
20432062

20442063
if top:
@@ -2108,14 +2127,17 @@ def publish(all_refs=None, msg=None, top=True):
21082127
dict(name='--depth', nargs='?', help='Number of revisions to fetch from the remote repository. Default: all revisions.'),
21092128
dict(name='--protocol', nargs='?', help='Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.'),
21102129
dict(name='--insecure', action='store_true', help='Allow insecure repository URLs. By default mbed CLI imports only "safe" URLs, e.g. based on standard ports - 80, 443 and 22. This option enables the use of arbitrary URLs/ports.'),
2130+
dict(name='--offline', action='store_true', help='Offline mode will force the use of locally cached repositories and prevent requests to remote repositories.'),
21112131
dict(name=['-l', '--latest-deps'], action='store_true', help='Update all dependencies to the latest revision of their current branch. WARNING: Ignores lib files'),
21122132
hidden_aliases=['up'],
21132133
help='Update to branch, tag, revision or latest',
21142134
description=(
21152135
"Updates the current program or library and its dependencies to specified\nbranch, tag or revision.\n"
21162136
"Alternatively fetches from associated remote repository URL and updates to the\n"
21172137
"latest revision in the current branch."))
2118-
def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=False, depth=None, protocol=None, insecure=False, latest_deps=False, top=True):
2138+
def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=False, depth=None, protocol=None, insecure=False, offline=False, latest_deps=False, top=True):
2139+
offline_warning(offline, top)
2140+
21192141
if top and clean:
21202142
sync()
21212143

@@ -2148,11 +2170,13 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
21482170
repo.revtype(rev)))
21492171

21502172
try:
2151-
repo.update(rev, clean, clean_files, repo.is_local)
2173+
repo.update(rev, clean, clean_files, offline or repo.is_local)
21522174
except ProcessException as e:
21532175
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(rev))
2154-
if depth:
2155-
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev)))
2176+
if offline:
2177+
err = err + "\nThis might be caused by offline mode ('--offline' used).\nYou should try without offline mode."
2178+
elif depth:
2179+
err = err + ("\nThis might be caused by the '--depth' option, which prevents fetching the whole revision history." % (repo.revtype(repo.rev)))
21562180
if ignore:
21572181
warning(err)
21582182
else:
@@ -2203,11 +2227,11 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
22032227
# Import missing repos and update to revs
22042228
for lib in repo.libs:
22052229
if not os.path.isdir(lib.path):
2206-
import_(lib.fullurl, lib.path, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, top=False)
2230+
import_(lib.fullurl, lib.path, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, offline=offline, top=False)
22072231
repo.ignore(relpath(repo.path, lib.path))
22082232
else:
22092233
with cd(lib.path):
2210-
update(None if latest_deps else lib.rev, clean=clean, clean_files=clean_files, clean_deps=clean_deps, ignore=ignore, protocol=protocol, insecure=insecure, latest_deps=latest_deps, top=False)
2234+
update(None if latest_deps else lib.rev, clean=clean, clean_files=clean_files, clean_deps=clean_deps, ignore=ignore, depth=depth, protocol=protocol, insecure=insecure, offline=offline, latest_deps=latest_deps, top=False)
22112235

22122236
if top:
22132237
program = Program(repo.path)

0 commit comments

Comments
 (0)