py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-02-05 21:24:28 +01:00
|
|
|
|
2020-03-22 21:52:38 +01:00
|
|
|
import html
|
2016-11-24 19:45:25 +01:00
|
|
|
import os
|
2021-07-03 08:22:44 +02:00
|
|
|
import sys
|
2020-06-11 00:54:34 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
from typing import Any, Dict, List, Set
|
|
|
|
|
2021-07-03 08:22:44 +02:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
|
|
|
|
# check for the venv
|
|
|
|
from tools.lib import sanity_check
|
|
|
|
|
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2016-11-24 19:45:25 +01:00
|
|
|
|
|
|
|
Call = Dict[str, Any]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def clean_up_pattern(s: str) -> str:
|
2016-11-24 19:45:25 +01:00
|
|
|
paren_level = 0
|
|
|
|
in_braces = False
|
2021-02-12 08:20:45 +01:00
|
|
|
result = ""
|
2016-11-24 19:45:25 +01:00
|
|
|
prior_char = None
|
|
|
|
for c in s:
|
2021-02-12 08:20:45 +01:00
|
|
|
if c == "(":
|
2016-11-24 19:45:25 +01:00
|
|
|
paren_level += 1
|
2021-02-12 08:20:45 +01:00
|
|
|
if c == "<" and prior_char == "P":
|
2016-11-24 19:45:25 +01:00
|
|
|
in_braces = True
|
|
|
|
if in_braces or (paren_level == 0):
|
2021-02-12 08:20:45 +01:00
|
|
|
if c != "?":
|
2016-11-24 19:45:25 +01:00
|
|
|
result += c
|
2021-02-12 08:20:45 +01:00
|
|
|
if c == ")":
|
2016-11-24 19:45:25 +01:00
|
|
|
paren_level -= 1
|
2021-02-12 08:20:45 +01:00
|
|
|
if c == ">":
|
2016-11-24 19:45:25 +01:00
|
|
|
in_braces = False
|
|
|
|
prior_char = c
|
|
|
|
return result
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def fix_test_name(s: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return s.replace("zerver.tests.", "")
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def create_single_page(pattern: str, out_dir: str, href: str, calls: List[Call]) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fn = out_dir + "/" + href
|
|
|
|
with open(fn, "w") as f:
|
2021-02-12 08:19:30 +01:00
|
|
|
f.write(
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2016-11-24 19:45:25 +01:00
|
|
|
<style>
|
|
|
|
.test {
|
|
|
|
margin: 20px;
|
|
|
|
}
|
|
|
|
</style>
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
f.write(f"<h3>{html.escape(pattern)}</h3>\n")
|
|
|
|
calls.sort(key=lambda call: call["status_code"])
|
2016-11-24 19:45:25 +01:00
|
|
|
for call in calls:
|
2021-02-12 08:20:45 +01:00
|
|
|
f.write("<hr>")
|
|
|
|
f.write("\n{}".format(fix_test_name(call["test_name"])))
|
2016-11-24 19:45:25 +01:00
|
|
|
f.write('<div class="test">')
|
2021-02-12 08:20:45 +01:00
|
|
|
f.write(call["url"])
|
|
|
|
f.write("<br>\n")
|
|
|
|
f.write(call["method"] + "<br>\n")
|
|
|
|
f.write("status code: {}<br>\n".format(call["status_code"]))
|
|
|
|
f.write("<br>")
|
|
|
|
f.write("</div>")
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def create_user_docs() -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
fn = "var/url_coverage.txt" # TODO: make path more robust, maybe use json suffix
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
out_dir = "var/api_docs"
|
2016-11-24 19:45:25 +01:00
|
|
|
try:
|
|
|
|
os.mkdir(out_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
main_page = out_dir + "/index.html"
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(main_page, "w") as f:
|
2021-02-12 08:19:30 +01:00
|
|
|
f.write(
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2016-11-24 19:45:25 +01:00
|
|
|
<style>
|
|
|
|
li {
|
|
|
|
list-style-type: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
</style>
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(fn, "rb") as coverage:
|
|
|
|
calls = [orjson.loads(line) for line in coverage]
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2020-04-22 01:09:50 +02:00
|
|
|
pattern_dict: Dict[str, List[Call]] = defaultdict(list)
|
2016-11-24 19:45:25 +01:00
|
|
|
for call in calls:
|
2021-02-12 08:20:45 +01:00
|
|
|
if "pattern" in call:
|
|
|
|
pattern = clean_up_pattern(call["pattern"])
|
2016-11-24 19:45:25 +01:00
|
|
|
if pattern:
|
|
|
|
pattern_dict[pattern].append(call)
|
|
|
|
|
|
|
|
patterns = set(pattern_dict.keys())
|
|
|
|
|
|
|
|
tups = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("api/v1/external", "webhooks"),
|
|
|
|
("api/v1", "api"),
|
|
|
|
("json", "legacy"),
|
2016-11-24 19:45:25 +01:00
|
|
|
]
|
|
|
|
|
2020-09-02 08:14:51 +02:00
|
|
|
groups: Dict[str, Set[str]] = {}
|
2016-11-24 19:45:25 +01:00
|
|
|
for prefix, name in tups:
|
|
|
|
groups[name] = {p for p in patterns if p.startswith(prefix)}
|
|
|
|
patterns -= groups[name]
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
groups["other"] = patterns
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
for name in ["api", "legacy", "webhooks", "other"]:
|
|
|
|
f.write(name + " endpoints:\n\n")
|
|
|
|
f.write("<ul>\n")
|
2016-11-24 19:45:25 +01:00
|
|
|
for pattern in sorted(groups[name]):
|
2021-02-12 08:20:45 +01:00
|
|
|
href = pattern.replace("/", "-") + ".html"
|
2020-06-10 06:41:04 +02:00
|
|
|
link = f'<a href="{href}">{html.escape(pattern)}</a>'
|
2021-02-12 08:20:45 +01:00
|
|
|
f.write("<li>" + link + "</li>\n")
|
2016-11-24 19:45:25 +01:00
|
|
|
create_single_page(pattern, out_dir, href, pattern_dict[pattern])
|
2021-02-12 08:20:45 +01:00
|
|
|
f.write("</ul>")
|
|
|
|
f.write("\n")
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
print(f"open {main_page}")
|
2016-11-24 19:45:25 +01:00
|
|
|
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if __name__ == "__main__":
|
2016-11-24 19:45:25 +01:00
|
|
|
create_user_docs()
|