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
|
|
|
|
|
|
|
# check for the venv
|
|
|
|
from lib import sanity_check
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
sanity_check.check_venv(__file__)
|
2016-11-24 19:45:25 +01:00
|
|
|
|
2020-03-22 21:52:38 +01:00
|
|
|
import html
|
2016-11-24 19:45:25 +01:00
|
|
|
import os
|
|
|
|
import pprint
|
2020-06-11 00:54:34 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
from typing import Any, Dict, List, Set
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2016-11-24 19:45:25 +01:00
|
|
|
|
|
|
|
Call = Dict[str, Any]
|
|
|
|
|
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
|
|
|
|
result = ''
|
|
|
|
prior_char = None
|
|
|
|
for c in s:
|
|
|
|
if c == '(':
|
|
|
|
paren_level += 1
|
|
|
|
if c == '<' and prior_char == 'P':
|
|
|
|
in_braces = True
|
|
|
|
if in_braces or (paren_level == 0):
|
|
|
|
if c != '?':
|
|
|
|
result += c
|
|
|
|
if c == ')':
|
|
|
|
paren_level -= 1
|
|
|
|
if c == '>':
|
|
|
|
in_braces = False
|
|
|
|
prior_char = c
|
|
|
|
return result
|
|
|
|
|
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 encode_info(info: Any) -> str:
|
2016-11-24 19:45:25 +01:00
|
|
|
try:
|
|
|
|
result = ''
|
|
|
|
try:
|
2020-08-07 01:09:47 +02:00
|
|
|
info = orjson.loads(info)
|
2016-11-24 19:45:25 +01:00
|
|
|
result = '(stringified)\n'
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception:
|
2016-11-24 19:45:25 +01:00
|
|
|
pass
|
2020-03-22 21:52:38 +01:00
|
|
|
result += html.escape(pprint.pformat(info, indent=4))
|
2016-11-24 19:45:25 +01:00
|
|
|
return '<pre>' + result + '</pre>'
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception:
|
2016-11-24 19:45:25 +01:00
|
|
|
pass
|
|
|
|
try:
|
2020-03-22 21:52:38 +01:00
|
|
|
return html.escape(str(info))
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception:
|
2016-11-24 19:45:25 +01:00
|
|
|
pass
|
|
|
|
return 'NOT ENCODABLE'
|
|
|
|
|
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:
|
2016-11-24 19:45:25 +01:00
|
|
|
return s.replace('zerver.tests.', '')
|
|
|
|
|
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:
|
2016-11-24 19:45:25 +01:00
|
|
|
fn = out_dir + '/' + href
|
|
|
|
with open(fn, 'w') as f:
|
|
|
|
f.write('''
|
|
|
|
<style>
|
|
|
|
.test {
|
|
|
|
margin: 20px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
''')
|
2020-06-10 06:41:04 +02:00
|
|
|
f.write(f'<h3>{html.escape(pattern)}</h3>\n')
|
2016-11-24 19:45:25 +01:00
|
|
|
calls.sort(key=lambda call: call['status_code'])
|
|
|
|
for call in calls:
|
|
|
|
f.write('<hr>')
|
2020-06-10 06:41:04 +02:00
|
|
|
f.write('\n{}'.format(fix_test_name(call['test_name'])))
|
2016-11-24 19:45:25 +01:00
|
|
|
f.write('<div class="test">')
|
2020-10-09 02:58:00 +02:00
|
|
|
f.write(call['url'])
|
2016-11-24 19:45:25 +01:00
|
|
|
f.write('<br>\n')
|
|
|
|
f.write(call['method'] + '<br>\n')
|
2020-06-10 06:41:04 +02:00
|
|
|
f.write('status code: {}<br>\n'.format(call['status_code']))
|
2016-11-24 19:45:25 +01:00
|
|
|
f.write('<br>')
|
|
|
|
f.write('</div>')
|
|
|
|
|
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:
|
2017-05-17 22:48:56 +02:00
|
|
|
fn = 'var/url_coverage.txt' # TODO: make path more robust, maybe use json suffix
|
2016-11-24 19:45:25 +01:00
|
|
|
|
|
|
|
out_dir = 'var/api_docs'
|
|
|
|
try:
|
|
|
|
os.mkdir(out_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
main_page = out_dir + '/index.html'
|
|
|
|
|
|
|
|
with open(main_page, 'w') as f:
|
|
|
|
f.write('''
|
|
|
|
<style>
|
|
|
|
li {
|
|
|
|
list-style-type: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
''')
|
|
|
|
|
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:
|
|
|
|
if 'pattern' in call:
|
|
|
|
pattern = clean_up_pattern(call['pattern'])
|
|
|
|
if pattern:
|
|
|
|
pattern_dict[pattern].append(call)
|
|
|
|
|
|
|
|
patterns = set(pattern_dict.keys())
|
|
|
|
|
|
|
|
tups = [
|
|
|
|
('api/v1/external', 'webhooks'),
|
|
|
|
('api/v1', 'api'),
|
|
|
|
('json', 'legacy'),
|
|
|
|
]
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
groups['other'] = patterns
|
|
|
|
|
|
|
|
for name in ['api', 'legacy', 'webhooks', 'other']:
|
2016-11-28 23:29:01 +01:00
|
|
|
f.write(name + ' endpoints:\n\n')
|
2016-11-24 19:45:25 +01:00
|
|
|
f.write('<ul>\n')
|
|
|
|
for pattern in sorted(groups[name]):
|
|
|
|
href = pattern.replace('/', '-') + '.html'
|
2020-06-10 06:41:04 +02:00
|
|
|
link = f'<a href="{href}">{html.escape(pattern)}</a>'
|
2016-11-24 19:45:25 +01:00
|
|
|
f.write('<li>' + link + '</li>\n')
|
|
|
|
create_single_page(pattern, out_dir, href, pattern_dict[pattern])
|
|
|
|
f.write('</ul>')
|
|
|
|
f.write('\n')
|
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f'open {main_page}')
|
2016-11-24 19:45:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
create_user_docs()
|