2020-04-03 13:23:32 +00:00
|
|
|
import collections
|
2019-04-08 16:01:54 +00:00
|
|
|
import contextlib
|
2020-06-30 18:22:07 +00:00
|
|
|
import datetime
|
2020-01-29 13:34:12 +00:00
|
|
|
import multiprocessing
|
2019-04-08 16:01:54 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2020-02-03 10:33:47 +00:00
|
|
|
import sys
|
2020-03-30 11:39:26 +00:00
|
|
|
import socket
|
2019-04-08 16:01:54 +00:00
|
|
|
import tempfile
|
2020-01-29 13:34:12 +00:00
|
|
|
import threading
|
2019-04-08 16:01:54 +00:00
|
|
|
|
2020-06-30 18:22:07 +00:00
|
|
|
import jinja2
|
2020-04-03 13:23:32 +00:00
|
|
|
import yaml
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def temp_dir():
|
2022-03-22 16:39:58 +00:00
|
|
|
path = tempfile.mkdtemp(dir=os.environ.get("TEMP"))
|
2019-04-08 16:01:54 +00:00
|
|
|
try:
|
|
|
|
yield path
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
|
2020-03-30 11:39:26 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def cd(new_cwd):
|
|
|
|
old_cwd = os.getcwd()
|
|
|
|
os.chdir(new_cwd)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(old_cwd)
|
|
|
|
|
|
|
|
|
|
|
|
def get_free_port():
|
|
|
|
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
2022-03-22 16:39:58 +00:00
|
|
|
s.bind(("", 0))
|
2020-03-30 11:39:26 +00:00
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
return s.getsockname()[1]
|
|
|
|
|
|
|
|
|
2020-01-29 13:34:12 +00:00
|
|
|
def run_function_in_parallel(func, args_list, threads=False):
|
2020-04-01 09:45:48 +00:00
|
|
|
processes = []
|
|
|
|
exit_code = 0
|
2020-01-29 13:34:12 +00:00
|
|
|
for task in args_list:
|
2020-04-01 09:45:48 +00:00
|
|
|
cls = threading.Thread if threads else multiprocessing.Process
|
|
|
|
processes.append(cls(target=func, args=task))
|
|
|
|
processes[-1].start()
|
|
|
|
for process in processes:
|
|
|
|
process.join()
|
|
|
|
if not threads:
|
|
|
|
if process.exitcode and not exit_code:
|
|
|
|
exit_code = process.exitcode
|
|
|
|
if exit_code:
|
|
|
|
sys.exit(exit_code)
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read_md_file(path):
|
|
|
|
in_meta = False
|
|
|
|
meta = {}
|
|
|
|
meta_text = []
|
|
|
|
content = []
|
|
|
|
if os.path.exists(path):
|
2022-03-22 16:39:58 +00:00
|
|
|
with open(path, "r") as f:
|
2020-04-03 13:23:32 +00:00
|
|
|
for line in f:
|
2022-03-22 16:39:58 +00:00
|
|
|
if line.startswith("---"):
|
2020-04-03 13:23:32 +00:00
|
|
|
if in_meta:
|
|
|
|
in_meta = False
|
2022-03-22 16:39:58 +00:00
|
|
|
meta = yaml.full_load("".join(meta_text))
|
2020-04-03 13:23:32 +00:00
|
|
|
else:
|
|
|
|
in_meta = True
|
|
|
|
else:
|
|
|
|
if in_meta:
|
|
|
|
meta_text.append(line)
|
|
|
|
else:
|
|
|
|
content.append(line)
|
2022-03-22 16:39:58 +00:00
|
|
|
return meta, "".join(content)
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_md_file(path, meta, content):
|
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
with open(path, "w") as f:
|
2020-04-03 13:23:32 +00:00
|
|
|
if meta:
|
2022-03-22 16:39:58 +00:00
|
|
|
print("---", file=f)
|
2020-04-03 13:23:32 +00:00
|
|
|
yaml.dump(meta, f)
|
2022-03-22 16:39:58 +00:00
|
|
|
print("---", file=f)
|
|
|
|
if not content.startswith("\n"):
|
|
|
|
print("", file=f)
|
2020-04-03 13:23:32 +00:00
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
def represent_ordereddict(dumper, data):
|
|
|
|
value = []
|
|
|
|
for item_key, item_value in data.items():
|
|
|
|
node_key = dumper.represent_data(item_key)
|
|
|
|
node_value = dumper.represent_data(item_value)
|
|
|
|
|
|
|
|
value.append((node_key, node_value))
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
return yaml.nodes.MappingNode("tag:yaml.org,2002:map", value)
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
yaml.add_representer(collections.OrderedDict, represent_ordereddict)
|
2020-06-30 18:22:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_jinja2_filters(env):
|
|
|
|
import website
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-06-30 18:22:07 +00:00
|
|
|
chunk_size = 10240
|
2022-03-22 16:39:58 +00:00
|
|
|
env.filters["chunks"] = lambda line: [
|
|
|
|
line[i : i + chunk_size] for i in range(0, len(line), chunk_size)
|
|
|
|
]
|
|
|
|
env.filters["adjust_markdown_html"] = website.adjust_markdown_html
|
|
|
|
env.filters["to_rfc882"] = lambda d: datetime.datetime.strptime(
|
|
|
|
d, "%Y-%m-%d"
|
|
|
|
).strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2020-06-30 18:22:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_jinja2_env(args):
|
|
|
|
import mdx_clickhouse
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-06-30 18:22:07 +00:00
|
|
|
env = jinja2.Environment(
|
2022-03-22 16:39:58 +00:00
|
|
|
loader=jinja2.FileSystemLoader(
|
|
|
|
[args.website_dir, os.path.join(args.docs_dir, "_includes")]
|
|
|
|
),
|
|
|
|
extensions=["jinja2.ext.i18n", "jinja2_highlight.HighlightExtension"],
|
2020-06-30 18:22:07 +00:00
|
|
|
)
|
2022-03-22 16:39:58 +00:00
|
|
|
env.extend(jinja2_highlight_cssclass="syntax p-3 my-3")
|
|
|
|
translations_dir = os.path.join(args.website_dir, "locale")
|
2020-06-30 18:22:07 +00:00
|
|
|
env.install_gettext_translations(
|
2022-03-22 16:39:58 +00:00
|
|
|
mdx_clickhouse.get_translations(translations_dir, "en"), newstyle=True
|
2020-06-30 18:22:07 +00:00
|
|
|
)
|
|
|
|
init_jinja2_filters(env)
|
|
|
|
return env
|