Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
StevenBlack
GitHub Repository: StevenBlack/hosts
Path: blob/master/makeHosts.py
1181 views
1
#!/usr/bin/env python
2
3
# Script by gfyoung
4
# https://github.com/gfyoung
5
#
6
# This Python script will generate hosts files and update the readme file.
7
8
from __future__ import print_function
9
10
import argparse
11
import subprocess
12
import sys
13
14
15
def print_failure(msg):
16
"""
17
Print a failure message.
18
19
Parameters
20
----------
21
msg : str
22
The failure message to print.
23
"""
24
25
print("\033[91m" + msg + "\033[0m")
26
27
28
def update_hosts_file(*flags):
29
"""
30
Wrapper around running updateHostsFile.py
31
32
Parameters
33
----------
34
flags : varargs
35
Commandline flags to pass into updateHostsFile.py. For more info, run
36
the following command in the terminal or command prompt:
37
38
```
39
python updateHostsFile.py -h
40
```
41
"""
42
43
if subprocess.call([sys.executable, "updateHostsFile.py"] + list(flags)):
44
print_failure("Failed to update hosts file")
45
46
47
def update_readme_file():
48
"""
49
Wrapper around running updateReadme.py
50
"""
51
52
if subprocess.call([sys.executable, "updateReadme.py"]):
53
print_failure("Failed to update readme file")
54
55
56
def recursively_loop_extensions(extension, extensions, current_extensions):
57
"""
58
Helper function that recursively calls itself to prevent manually creating
59
all possible combinations of extensions.
60
61
Will call update_hosts_file for all combinations of extensions
62
"""
63
64
c_extensions = extensions.copy()
65
c_current_extensions = current_extensions.copy()
66
c_current_extensions.append(extension)
67
68
name = "-".join(c_current_extensions)
69
70
params = ("-a", "-n", "-o", "alternates/"+name, "-e") + tuple(c_current_extensions)
71
update_hosts_file(*params)
72
73
params = ("-a", "-n", "-s", "--nounifiedhosts", "-o", "alternates/"+name+"-only", "-e") + tuple(c_current_extensions)
74
update_hosts_file(*params)
75
76
while len(c_extensions) > 0:
77
recursively_loop_extensions(c_extensions.pop(0), c_extensions, c_current_extensions)
78
79
80
def main():
81
parser = argparse.ArgumentParser(
82
description="Creates custom hosts "
83
"file from hosts stored in "
84
"data subfolders."
85
)
86
parser.parse_args()
87
88
# Update the unified hosts file
89
update_hosts_file("-a")
90
91
# List of extensions we want to generate, we will loop over them recursively to prevent manual definitions
92
# Only add new extensions to the end of the array, to avoid relocating existing hosts-files
93
extensions = ["fakenews", "gambling", "porn", "social"]
94
95
while len(extensions) > 0:
96
recursively_loop_extensions(extensions.pop(0), extensions, [])
97
98
# Update the readme files.
99
update_readme_file()
100
101
102
if __name__ == "__main__":
103
main()
104
105