-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scripts): add a script to update port comments (#17600)
Also updates all existing port comments Currently this relies on an unreleased mathlibtools, which can be installed with `pip install git+https://github.com/leanprover-community/mathlib-tools`
- Loading branch information
1 parent
4fcbc82
commit 36e2bac
Showing
13 changed files
with
119 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from mathlibtools.file_status import PortStatus | ||
from pathlib import Path | ||
|
||
import re | ||
from dataclasses import asdict, dataclass | ||
from typing import Any, Dict, Optional, Union | ||
import textwrap | ||
|
||
import requests | ||
import yaml | ||
|
||
status = PortStatus.deserialize_old() | ||
|
||
src_path = Path(__file__).parent.parent / 'src' | ||
|
||
def make_comment(fstatus): | ||
return textwrap.dedent(f"""\ | ||
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4. | ||
> https://github.com/leanprover-community/mathlib4/pull/{fstatus.mathlib4_pr} | ||
> Any changes to this file require a corresponding PR to mathlib4.""") | ||
|
||
def replace_range(src, pos, end_pos, new): | ||
return src[:pos] + new + src[end_pos:] | ||
|
||
|
||
def add_port_status(fcontent, fstatus): | ||
module_comment = re.search('/-!\s*(.*?)-/', fcontent, re.MULTILINE | re.DOTALL) | ||
assert module_comment | ||
module_comment_start = module_comment.start(1) | ||
module_comment_end = module_comment.end(1) | ||
|
||
comment_re = re.compile( | ||
r"^((?:> )?)THIS FILE IS SYNCHRONIZED WITH MATHLIB4\." | ||
r"(?:\n\1[^\n]+)*\n*", | ||
re.MULTILINE | ||
) | ||
header_re = re.compile('^#[^\n]*\n?', re.MULTILINE) | ||
|
||
existing_label = comment_re.search(fcontent, module_comment_start, module_comment_end) | ||
existing_header = header_re.search(fcontent, module_comment_start, module_comment_end) | ||
|
||
if not existing_label: | ||
rest = fcontent[existing_header.end():module_comment_end] | ||
trailing_whitespace = "\n" if rest.strip() else "" | ||
fcontent = replace_range(fcontent, existing_header.end(), existing_header.end(), | ||
"\n" + make_comment(f_status) + trailing_whitespace) | ||
else: | ||
if existing_label.end() <= existing_header.start(): | ||
rest = fcontent[existing_header.end():module_comment_end] | ||
trailing_whitespace = "\n" if rest.strip() else "" | ||
fcontent = replace_range(fcontent, existing_header.end(), existing_header.end(), | ||
"\n" + make_comment(f_status) + trailing_whitespace) | ||
fcontent = replace_range(fcontent, existing_label.start(), existing_label.end(), "") | ||
elif existing_header.end() <= existing_label.start(): | ||
rest = fcontent[existing_label.end():module_comment_end] | ||
trailing_whitespace = "\n" if rest.strip() else "" | ||
fcontent = replace_range(fcontent, existing_label.start(), existing_label.end(), "") | ||
fcontent = replace_range(fcontent, existing_header.end(), existing_header.end(), | ||
"\n" + make_comment(f_status) + trailing_whitespace) | ||
else: | ||
assert False | ||
return fcontent | ||
|
||
def fname_for(import_path): | ||
return src_path / Path(*import_path.split('.')).with_suffix('.lean') | ||
|
||
|
||
for iname, f_status in status.file_statuses.items(): | ||
if f_status.ported: | ||
fname = fname_for(iname) | ||
with open(fname) as f: | ||
fcontent = f.read() | ||
fcontent = add_port_status(fcontent, f_status) | ||
with open(fname, 'w') as f: | ||
f.write(fcontent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters