-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenindex.py
executable file
·106 lines (92 loc) · 2.98 KB
/
genindex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import logging
import argparse
from jinja2 import Environment, FileSystemLoader
import time
import gencontent
from utils import get_isolist
import genservernews
def main():
logger = logging.getLogger("mirrors-genindex")
ch = logging.StreamHandler()
OUTDIR = (
os.getenv("OUTDIR")
or gencontent.OUTDIR
or os.getenv("HTTPDIR")
or gencontent.HTTPDIR
)
parser = argparse.ArgumentParser(
description="USTC Mirrors Index Page Generator",
epilog="Brought to you by LUG@USTC.",
)
parser.add_argument(
"-d",
"--outdir",
type=str,
default=OUTDIR,
help="specify output directory",
)
parser.add_argument(
"-o",
"--output",
type=str,
default=os.path.join(OUTDIR, "index.html"),
help="specify full output file path, and will overwrite outdir option",
)
parser.add_argument(
"-v",
"--verbose",
help="show verbose information",
action="store_true",
)
parser.add_argument(
"-m",
"--mirrorz",
type=str,
default=None,
help="set mirrorz output file path, mirrorz.json will not be generated if not set",
)
args = parser.parse_args()
LOGLEVEL = logging.DEBUG if args.verbose else logging.INFO
logger.setLevel(LOGLEVEL)
ch.setLevel(LOGLEVEL)
ch.setFormatter(logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s"))
logger.addHandler(ch)
logger.debug('received command arguments: "{}"'.format(str(args)))
OUTDIR = args.outdir or OUTDIR
OUTFILE = args.output
BASEDIR = os.path.dirname(__file__)
logger.info('OUTFILE is "%(OUTFILE)s"' % locals())
logger.info('BASEDIR is "%(BASEDIR)s"' % locals())
env = Environment(loader=FileSystemLoader(os.path.join(BASEDIR, "templates")))
template = env.get_template("index.html")
logger.debug("begin parsing template...")
repolist = list(gencontent.genRepoList())
revproxy = gencontent.getOthers()
isoinfo = get_isolist()
newslist = genservernews.getServerNews(logger)
parsed_template = template.render(
repolist=repolist,
revproxy=revproxy,
isoinfo=isoinfo,
newslist=newslist,
now=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
)
logger.info("begin index file writing...")
with open(OUTFILE, "w") as fout:
fout.write(parsed_template)
fout.write(os.linesep)
logger.info("index file completed!")
if args.mirrorz:
# Put genmirrorz inside genindex to save time and I/O generating files
import genmirrorz
logger.info("begin mirrorz.json file writing...")
with open(args.mirrorz, "w") as fout:
fout.write(genmirrorz.getMirrorzJson(repolist, isoinfo))
fout.write(os.linesep)
logger.info("mirrorz.json file completed!")
logger.info("exiting...")
if __name__ == "__main__":
main()