Skip to content

Commit 04a4ac9

Browse files
committed
Add -o/--offline mode, rely on the local lock file
1 parent fc179e3 commit 04a4ac9

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
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):
23+
def build_docs(root, builder, clear, serve, debug, offline):
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
#
@@ -110,6 +114,12 @@ def main(root):
110114
help="start a local server with live reload",
111115
action="store_true",
112116
)
117+
group.add_argument(
118+
"-o",
119+
"--offline",
120+
help="build on offline mode",
121+
action="store_true",
122+
)
113123
group.add_argument(
114124
"--check-links", help="Check whether all links are valid", action="store_true"
115125
)
@@ -127,6 +137,6 @@ def main(root):
127137
update_spec_lockfile(SPEC_CHECKSUM_URL, root / "src" / SPEC_LOCKFILE)
128138

129139
rendered = build_docs(
130-
root, "xml" if args.xml else "html", args.clear, args.serve, args.debug
140+
root, "xml" if args.xml else "html", args.clear, args.serve, args.debug, args.offline
131141
)
132142

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: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +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)
3131
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-
logger.error(error_message)
41-
raise FLSValidationError(error_message)
42-
32+
33+
if not offline_mode: # in offline_mode we ignore checking against the lock file
34+
# Check for differences against lock file
35+
has_differences, differences = check_fls_lock_consistency(app, env, raw_json_data)
36+
if has_differences:
37+
error_message = "The FLS specification has changed since the lock file was created:\n"
38+
for diff in differences:
39+
error_message += f" - {diff}\n"
40+
error_message += "\nPlease manually inspect FLS spec items whose checksums have changed as corresponding guidelines may need to account for these changes."
41+
logger.error(error_message)
42+
raise FLSValidationError(error_message)
43+
4344
# Check if all referenced FLS IDs exist
4445
check_fls_ids_correct(app, env, fls_ids)
4546

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

154155

155-
def gather_fls_paragraph_ids(json_url):
156+
def gather_fls_paragraph_ids(app, json_url):
156157
"""
157158
Gather all Ferrocene Language Specification paragraph IDs from the paragraph-ids.json file,
158159
including both container section IDs and individual paragraph IDs.
@@ -164,25 +165,36 @@ def gather_fls_paragraph_ids(json_url):
164165
Dictionary mapping paragraph IDs to metadata AND the complete raw JSON data
165166
"""
166167
logger.info("Gathering FLS paragraph IDs from %s", json_url)
168+
offline = app.config.offline
169+
lock_path = app.confdir / 'fls.lock'
167170

168171
# Dictionary to store all FLS IDs and their metadata
169172
all_fls_ids = {}
170173
raw_json_data = None
171174

172175
try:
173176
# Load the JSON file
174-
response = requests.get(json_url)
175-
response.raise_for_status() # Raise exception for HTTP errors
176-
177-
# Parse the JSON data
178-
try:
179-
raw_json_data = response.json()
180-
data = raw_json_data # Keep reference to the original data
181-
logger.debug("Successfully parsed JSON data")
182-
except json.JSONDecodeError as e:
183-
logger.error(f"Failed to parse JSON: {e}")
184-
logger.debug(f"Response content preview: {response.text[:500]}...")
185-
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+
186198

187199
# Check if we have the expected document structure
188200
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)