2022-07-05 12:39:02 -04:00
|
|
|
#!/usr/bin/env python3
|
2016-03-23 00:35:26 -04:00
|
|
|
|
|
|
|
# Script by Steven Black
|
|
|
|
# https://github.com/StevenBlack
|
|
|
|
#
|
|
|
|
# This Python script will update the readme files in this repo.
|
|
|
|
|
2018-08-10 15:52:20 +02:00
|
|
|
import json
|
2016-03-23 00:35:26 -04:00
|
|
|
import os
|
|
|
|
import time
|
2018-08-10 15:52:20 +02:00
|
|
|
from string import Template
|
2016-03-23 00:35:26 -04:00
|
|
|
|
|
|
|
# Project Settings
|
2017-05-15 15:13:39 -04:00
|
|
|
BASEDIR_PATH = os.path.dirname(os.path.realpath(__file__))
|
2019-07-13 13:40:13 +02:00
|
|
|
README_TEMPLATE = os.path.join(BASEDIR_PATH, "readme_template.md")
|
|
|
|
README_FILENAME = "readme.md"
|
2016-03-23 00:35:26 -04:00
|
|
|
README_DATA_FILENAME = "readmeData.json"
|
|
|
|
|
|
|
|
|
2017-05-15 23:57:14 -04:00
|
|
|
def main():
|
2019-07-13 13:40:13 +02:00
|
|
|
s = Template(
|
|
|
|
"${description} | [Readme](https://github.com/StevenBlack/"
|
|
|
|
"hosts/blob/master/${location}readme.md) | "
|
|
|
|
"[link](https://raw.githubusercontent.com/StevenBlack/"
|
|
|
|
"hosts/master/${location}hosts) | "
|
|
|
|
"${fmtentries} | "
|
|
|
|
"[link](http://sbc.io/hosts/${location}hosts)"
|
|
|
|
)
|
2020-05-27 16:40:55 +03:00
|
|
|
with open(README_DATA_FILENAME, "r", encoding="utf-8", newline="\n") as f:
|
2017-05-15 15:13:39 -04:00
|
|
|
data = json.load(f)
|
2016-03-23 00:35:26 -04:00
|
|
|
|
2018-08-10 15:56:15 +02:00
|
|
|
keys = list(data.keys())
|
2017-05-17 23:17:37 -04:00
|
|
|
# Sort by the number of en-dashes in the key
|
|
|
|
# and then by the key string itself.
|
2023-05-23 20:03:43 +02:00
|
|
|
keys.sort(key=lambda item: (item.replace("-only", "").count("-"), item.replace("-only", "")))
|
2016-12-11 13:44:51 -05:00
|
|
|
|
2017-05-15 15:13:39 -04:00
|
|
|
toc_rows = ""
|
2016-03-24 01:08:48 -04:00
|
|
|
for key in keys:
|
2016-03-24 17:55:23 -04:00
|
|
|
data[key]["fmtentries"] = "{:,}".format(data[key]["entries"])
|
2016-03-24 01:08:48 -04:00
|
|
|
if key == "base":
|
2019-07-13 13:40:13 +02:00
|
|
|
data[key]["description"] = "Unified hosts = **(adware + malware)**"
|
2016-03-24 01:08:48 -04:00
|
|
|
else:
|
2023-05-23 20:03:43 +02:00
|
|
|
if data[key]["no_unified_hosts"]:
|
|
|
|
data[key]["description"] = (
|
|
|
|
"**" + key.replace("-only", "").replace("-", " + ") + "**"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
data[key]["description"] = (
|
|
|
|
"Unified hosts **+ " + key.replace("-", " + ") + "**"
|
|
|
|
)
|
2016-03-24 01:08:48 -04:00
|
|
|
|
2020-02-23 12:37:34 +01:00
|
|
|
if "\\" in data[key]["location"]:
|
|
|
|
data[key]["location"] = data[key]["location"].replace("\\", "/")
|
|
|
|
|
2017-05-15 15:13:39 -04:00
|
|
|
toc_rows += s.substitute(data[key]) + "\n"
|
2016-03-24 01:08:48 -04:00
|
|
|
|
2017-05-15 15:13:39 -04:00
|
|
|
row_defaults = {
|
2016-12-11 12:48:57 -05:00
|
|
|
"name": "",
|
|
|
|
"homeurl": "",
|
2017-10-24 23:17:19 -04:00
|
|
|
"url": "",
|
2018-03-17 23:04:57 -04:00
|
|
|
"license": "",
|
2019-07-13 13:40:13 +02:00
|
|
|
"issues": "",
|
2022-06-09 23:10:03 -04:00
|
|
|
"description": "",
|
2019-07-13 13:40:13 +02:00
|
|
|
}
|
2016-12-11 12:48:57 -05:00
|
|
|
|
2019-07-13 13:40:13 +02:00
|
|
|
t = Template(
|
2022-06-09 23:10:03 -04:00
|
|
|
"${name} |[link](${homeurl})"
|
|
|
|
" | [raw](${url}) | ${license} | [issues](${issues})| ${description}"
|
2019-07-13 13:40:13 +02:00
|
|
|
)
|
2022-07-21 22:37:02 -04:00
|
|
|
size_history_graph = ""
|
2016-03-23 00:35:26 -04:00
|
|
|
for key in keys:
|
2023-05-23 20:03:43 +02:00
|
|
|
extensions = key.replace("-only", "").replace("-", ", ")
|
2017-05-15 15:13:39 -04:00
|
|
|
extensions_str = "* Extensions: **" + extensions + "**."
|
2023-05-23 20:03:43 +02:00
|
|
|
if data[key]["no_unified_hosts"]:
|
|
|
|
extensions_header = "Limited to the extensions: " + extensions
|
|
|
|
else:
|
|
|
|
extensions_header = "Unified hosts file with " + extensions + " extensions"
|
2017-05-15 15:13:39 -04:00
|
|
|
|
|
|
|
source_rows = ""
|
|
|
|
source_list = data[key]["sourcesdata"]
|
|
|
|
|
|
|
|
for source in source_list:
|
|
|
|
this_row = {}
|
|
|
|
this_row.update(row_defaults)
|
|
|
|
this_row.update(source)
|
|
|
|
source_rows += t.substitute(this_row) + "\n"
|
|
|
|
|
2020-02-24 18:18:17 +01:00
|
|
|
with open(
|
2020-05-27 21:59:34 +02:00
|
|
|
os.path.join(data[key]["location"], README_FILENAME),
|
|
|
|
"wt",
|
|
|
|
encoding="utf-8",
|
|
|
|
newline="\n",
|
2020-02-24 18:18:17 +01:00
|
|
|
) as out:
|
2020-05-27 16:40:55 +03:00
|
|
|
for line in open(README_TEMPLATE, encoding="utf-8", newline="\n"):
|
2019-07-13 13:40:13 +02:00
|
|
|
line = line.replace(
|
|
|
|
"@GEN_DATE@", time.strftime("%B %d %Y", time.gmtime())
|
|
|
|
)
|
|
|
|
line = line.replace("@EXTENSIONS@", extensions_str)
|
|
|
|
line = line.replace("@EXTENSIONS_HEADER@", extensions_header)
|
|
|
|
line = line.replace(
|
|
|
|
"@NUM_ENTRIES@", "{:,}".format(data[key]["entries"])
|
|
|
|
)
|
|
|
|
line = line.replace(
|
|
|
|
"@SUBFOLDER@", os.path.join(data[key]["location"], "")
|
|
|
|
)
|
|
|
|
line = line.replace("@TOCROWS@", toc_rows)
|
|
|
|
line = line.replace("@SOURCEROWS@", source_rows)
|
2024-10-01 17:29:07 -04:00
|
|
|
# insert the size graph on the home readme only, for now.
|
|
|
|
if key == "base":
|
|
|
|
line = line.replace(
|
|
|
|
"@SIZEHISTORY@", size_history_graph
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
line = line.replace(
|
|
|
|
"@SIZEHISTORY@", "")
|
|
|
|
|
2018-08-10 15:56:15 +02:00
|
|
|
out.write(line)
|
2016-03-23 00:35:26 -04:00
|
|
|
|
2018-08-10 16:00:33 +02:00
|
|
|
|
2016-03-23 00:35:26 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|