Skip to content

Commit 16f85f5

Browse files
committed
Add -o/--offline mode, rely on the local lock file
1 parent 5a99ee7 commit 16f85f5

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

builder/build_cli.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
SPEC_CHECKSUM_URL = "https://spec.ferrocene.dev/paragraph-ids.json"
2121
SPEC_LOCKFILE = "spec.lock"
2222

23-
def build_docs(root, builder, clear, serve, debug, spec_lock_consistency_check):
23+
def build_docs(root, builder, clear, serve, debug, offline, spec_lock_consistency_check):
2424
dest = root / "build"
2525

2626
args = ["-b", builder, "-d", dest / "doctrees"]
27+
28+
if offline:
29+
args.append("-D")
30+
args.append("offline=1")
2731
if debug:
2832
# Disable parallel builds and show exceptions in debug mode.
2933
#
@@ -128,6 +132,12 @@ def main(root):
128132
help="start a local server with live reload",
129133
action="store_true",
130134
)
135+
group.add_argument(
136+
"-o",
137+
"--offline",
138+
help="build on offline mode",
139+
action="store_true",
140+
)
131141
group.add_argument(
132142
"--check-links", help="Check whether all links are valid", action="store_true"
133143
)
@@ -145,6 +155,6 @@ def main(root):
145155
update_spec_lockfile(SPEC_CHECKSUM_URL, root / "src" / SPEC_LOCKFILE)
146156

147157
rendered = build_docs(
148-
root, "xml" if args.xml else "html", args.clear, args.serve, args.debug, not args.ignore_spec_lock_diff
158+
root, "xml" if args.xml else "html", args.clear, args.serve, args.debug, args.offline, not args.ignore_spec_lock_diff
149159
)
150160

exts/coding_guidelines/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ def merge_domaindata(self, docnames, other):
3434

3535
def setup(app):
3636

37+
38+
# Retrieve the passed value
39+
40+
ignore_fls_checks = False
3741
app.add_domain(CodingGuidelinesDomain)
42+
app.add_config_value("offline", False, "env") # register offline option
3843
app.add_config_value(
3944
name="spec_std_docs_url",
4045
default="https://doc.rust-lang.org/stable/std",

exts/coding_guidelines/fls_checks.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,27 @@ def check_fls(app, env):
2020
"""Main checking function for FLS validation"""
2121
# First make sure all guidelines have correctly formatted FLS IDs
2222
check_fls_exists_and_valid_format(app, env)
23+
offline_mode = env.config.offline
2324

2425
# Gather all FLS paragraph IDs from the specification and get the raw JSON
25-
fls_ids, raw_json_data = gather_fls_paragraph_ids(fls_paragraph_ids_url)
26-
26+
fls_ids, raw_json_data = gather_fls_paragraph_ids(app, fls_paragraph_ids_url)
2727
# Error out if we couldn't get the raw JSON data
2828
if not raw_json_data:
2929
error_message = f"Failed to retrieve or parse the FLS specification from {fls_paragraph_ids_url}"
3030
logger.error(error_message)
31-
raise FLSValidationError(error_message)
32-
33-
# Check for differences against lock file
34-
has_differences, differences = check_fls_lock_consistency(app, env, raw_json_data)
35-
if has_differences:
36-
error_message = "The FLS specification has changed since the lock file was created:\n"
37-
for diff in differences:
38-
error_message += f" - {diff}\n"
39-
error_message += "\nPlease manually inspect FLS spec items whose checksums have changed as corresponding guidelines may need to account for these changes."
40-
error_message += "\nOnce resolved, you may run the following to update the local spec lock file:"
41-
error_message += "\n\t./make.py --update-spec-lock-file"
42-
logger.error(error_message)
43-
raise FLSValidationError(error_message)
44-
31+
raise FLSValidationError(error_message)
32+
if not offline_mode: # in offline mode, ignore checking against the lock file
33+
# Check for differences against lock file
34+
has_differences, differences = check_fls_lock_consistency(app, env, raw_json_data)
35+
if has_differences:
36+
error_message = "The FLS specification has changed since the lock file was created:\n"
37+
for diff in differences:
38+
error_message += f" - {diff}\n"
39+
error_message += "\nPlease manually inspect FLS spec items whose checksums have changed as corresponding guidelines may need to account for these changes."
40+
error_message += "\nOnce resolved, you may run the following to update the local spec lock file:"
41+
error_message += "\n\t./make.py --update-spec-lock-file"
42+
logger.error(error_message)
43+
raise FLSValidationError(error_message)
4544
# Check if all referenced FLS IDs exist
4645
check_fls_ids_correct(app, env, fls_ids)
4746

@@ -154,7 +153,7 @@ def check_fls_ids_correct(app, env, fls_ids):
154153
logger.info("All FLS references in guidelines are valid")
155154

156155

157-
def gather_fls_paragraph_ids(json_url):
156+
def gather_fls_paragraph_ids(app, json_url):
158157
"""
159158
Gather all Ferrocene Language Specification paragraph IDs from the paragraph-ids.json file,
160159
including both container section IDs and individual paragraph IDs.
@@ -166,25 +165,36 @@ def gather_fls_paragraph_ids(json_url):
166165
Dictionary mapping paragraph IDs to metadata AND the complete raw JSON data
167166
"""
168167
logger.info("Gathering FLS paragraph IDs from %s", json_url)
168+
offline = app.config.offline
169+
lock_path = app.confdir / 'fls.lock'
169170

170171
# Dictionary to store all FLS IDs and their metadata
171172
all_fls_ids = {}
172173
raw_json_data = None
173174

174175
try:
175176
# Load the JSON file
176-
response = requests.get(json_url)
177-
response.raise_for_status() # Raise exception for HTTP errors
178-
179-
# Parse the JSON data
180-
try:
181-
raw_json_data = response.json()
182-
data = raw_json_data # Keep reference to the original data
183-
logger.debug("Successfully parsed JSON data")
184-
except json.JSONDecodeError as e:
185-
logger.error(f"Failed to parse JSON: {e}")
186-
logger.debug(f"Response content preview: {response.text[:500]}...")
187-
raise
177+
if not offline:
178+
response = requests.get(json_url)
179+
response.raise_for_status() # Raise exception for HTTP errors
180+
# Parse the JSON data
181+
try:
182+
raw_json_data = response.json()
183+
data = raw_json_data # Keep reference to the original data
184+
logger.debug("Successfully parsed JSON data")
185+
except json.JSONDecodeError as e:
186+
logger.error(f"Failed to parse JSON: {e}")
187+
logger.debug(f"Response content preview: {response.text[:500]}...")
188+
raise
189+
190+
else : # if online mode is on read from the lock file
191+
if not lock_path.exists():
192+
logger.warning(f"No FLS lock file found at {lock_path}") # TODO: returns an error
193+
return False, []
194+
with open(lock_path, 'r', encoding='utf-8') as f:
195+
raw_json_data=f.read()
196+
data = json.loads(raw_json_data)
197+
188198

189199
# Check if we have the expected document structure
190200
if 'documents' not in data:

exts/coding_guidelines/fls_linking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def load_fls_ids(app):
2626
"""Load FLS IDs and their URLs."""
2727
try:
2828
from . import fls_checks
29-
fls_ids, _ = fls_checks.gather_fls_paragraph_ids(app.config.fls_paragraph_ids_url)
29+
fls_ids, _ = fls_checks.gather_fls_paragraph_ids(app, app.config.fls_paragraph_ids_url )
3030
return {fls_id: data['url'] for fls_id, data in fls_ids.items()}
3131
except Exception as e:
3232
logger.error(f"Failed to load FLS IDs: {e}")

0 commit comments

Comments
 (0)