Skip to content

Commit 2316149

Browse files
committed
Add an '--offline' flag to avoid network requests
If you want to use circup but don't have an internet connection, you currently cannot. This commit skips all network requests and will try to perform the commands with whatever data is already downloaded.
1 parent 646c3c5 commit 2316149

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed

circup/bundle.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class Bundle:
2525
All the links and file names for a bundle
2626
"""
2727

28+
#: Avoid requests to the internet
29+
offline = False
30+
2831
def __init__(self, repo):
2932
"""
3033
Initialise a Bundle created from its github info.
@@ -132,9 +135,12 @@ def latest_tag(self):
132135
:return: The most recent tag value for the project.
133136
"""
134137
if self._latest is None:
135-
self._latest = get_latest_release_from_url(
136-
self.url + "/releases/latest", logger
137-
)
138+
if self.offline:
139+
self._latest = self._available[-1] if len(self._available) > 0 else None
140+
else:
141+
self._latest = get_latest_release_from_url(
142+
self.url + "/releases/latest", logger
143+
)
138144
return self._latest
139145

140146
@property

circup/command_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ def ensure_bundle_tag(bundle, tag):
145145
146146
:return: If the bundle is available.
147147
"""
148+
if tag is None:
149+
logger.warning("Bundle version requested is 'None'.")
150+
return False
151+
148152
do_update = False
149153
if tag in bundle.available_tags:
150154
for platform in PLATFORMS:
@@ -155,6 +159,12 @@ def ensure_bundle_tag(bundle, tag):
155159
do_update = True
156160

157161
if do_update:
162+
if Bundle.offline:
163+
logger.info(
164+
"Bundle version not available but skipping update in offline mode."
165+
)
166+
return False
167+
158168
logger.info("New version available (%s).", tag)
159169
try:
160170
get_bundle(bundle, tag)

circup/commands.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@
6868
" You can optionally set an environment variable CIRCUP_WEBWORKFLOW_PASSWORD"
6969
" instead of passing this argument. If both exist the CLI arg takes precedent.",
7070
)
71+
@click.option(
72+
"--offline",
73+
is_flag=True,
74+
help="Prevents Circup from accessing the internet for any reason. "
75+
"Without this flag, Circup will fail with an error if it needs to access "
76+
"the network and the network is not available. With this flag, Circup "
77+
"will attempt to proceed without the network if possible. Circup will "
78+
"only use bundles downloaded locally even if there might be newer "
79+
"versions available.",
80+
)
7181
@click.option(
7282
"--timeout",
7383
default=30,
@@ -107,6 +117,7 @@ def main( # pylint: disable=too-many-locals
107117
host,
108118
port,
109119
password,
120+
offline,
110121
timeout,
111122
board_id,
112123
cpy_version,
@@ -121,6 +132,7 @@ def main( # pylint: disable=too-many-locals
121132
ctx.obj["BUNDLE_TAGS"] = (
122133
parse_cli_bundle_tags(bundle_versions) if len(bundle_versions) > 0 else None
123134
)
135+
Bundle.offline = offline
124136

125137
if password is None:
126138
password = os.getenv("CIRCUP_WEBWORKFLOW_PASSWORD")
@@ -177,20 +189,22 @@ def main( # pylint: disable=too-many-locals
177189

178190
logger.info("### Started Circup ###")
179191

180-
# If a newer version of circup is available, print a message.
181-
logger.info("Checking for a newer version of circup")
182-
version = get_circup_version()
183-
if version:
184-
update_checker.update_check("circup", version)
192+
if offline:
193+
logger.info(
194+
"'--offline' flag present, all update checks requiring the network will be skipped."
195+
)
196+
else:
197+
# If a newer version of circup is available, print a message.
198+
logger.info("Checking for a newer version of circup")
199+
version = get_circup_version()
200+
if version:
201+
update_checker.update_check("circup", version)
185202

186203
# stop early if the command is boardless
187204
if ctx.invoked_subcommand in BOARDLESS_COMMANDS or "--help" in sys.argv:
188205
return
189206

190207
ctx.obj["DEVICE_PATH"] = device_path
191-
latest_version = get_latest_release_from_url(
192-
"https://github.com/adafruit/circuitpython/releases/latest", logger
193-
)
194208

195209
if device_path is None or not ctx.obj["backend"].is_device_present():
196210
click.secho("Could not find a connected CircuitPython device.", fg="red")
@@ -206,22 +220,27 @@ def main( # pylint: disable=too-many-locals
206220
board_id, device_path, cpy_version
207221
)
208222
)
209-
try:
210-
if VersionInfo.parse(cpy_version) < VersionInfo.parse(latest_version):
211-
click.secho(
212-
"A newer version of CircuitPython ({}) is available.".format(
213-
latest_version
214-
),
215-
fg="green",
216-
)
217-
if board_id:
218-
url_download = f"https://circuitpython.org/board/{board_id}"
219-
else:
220-
url_download = "https://circuitpython.org/downloads"
221-
click.secho("Get it here: {}".format(url_download), fg="green")
222-
except ValueError as ex:
223-
logger.warning("CircuitPython has incorrect semver value.")
224-
logger.warning(ex)
223+
224+
if not offline:
225+
latest_version = get_latest_release_from_url(
226+
"https://github.com/adafruit/circuitpython/releases/latest", logger
227+
)
228+
try:
229+
if VersionInfo.parse(cpy_version) < VersionInfo.parse(latest_version):
230+
click.secho(
231+
"A newer version of CircuitPython ({}) is available.".format(
232+
latest_version
233+
),
234+
fg="green",
235+
)
236+
if board_id:
237+
url_download = f"https://circuitpython.org/board/{board_id}"
238+
else:
239+
url_download = "https://circuitpython.org/downloads"
240+
click.secho("Get it here: {}".format(url_download), fg="green")
241+
except ValueError as ex:
242+
logger.warning("CircuitPython has incorrect semver value.")
243+
logger.warning(ex)
225244

226245

227246
@main.command()
@@ -392,7 +411,7 @@ def install(
392411
upgrade,
393412
)
394413

395-
if stubs:
414+
if stubs and not Bundle.offline:
396415
# Check we are in a virtual environment
397416
if not is_virtual_env_active():
398417
if is_global_install_ok is None:
@@ -728,6 +747,10 @@ def bundle_add(ctx, bundle):
728747
)
729748
return
730749

750+
if Bundle.offline:
751+
click.secho("Cannot add new bundle when '--offline' flag is present.", fg="red")
752+
return
753+
731754
bundles_dict = get_bundles_local_dict()
732755
modified = False
733756
for bundle_repo in bundle:

0 commit comments

Comments
 (0)