forked from viccherubini/get-shit-done
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-shit-done.py
executable file
·104 lines (81 loc) · 2.83 KB
/
get-shit-done.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
#!/usr/bin/env python
from __future__ import print_function
import sys
import getpass
import subprocess
import os
from os import path
def exit_error(error):
print(error, file=sys.stderr)
exit(1)
ini_local = path.expanduser(path.join("~", ".get-shit-done.ini"))
ini_global = './sites.ini'
if "linux" in sys.platform:
restart_network_command = ["/etc/init.d/networking", "restart"]
elif "darwin" in sys.platform:
restart_network_command = ["dscacheutil", "-flushcache"]
elif "win32" in sys.platform:
restart_network_command = ["ipconfig", "/flushdns"]
else:
# Intention isn't to exit, as it still works, but just requires some
# intervention on the user's part.
message = '"Please contribute DNS cache flush command on GitHub."'
restart_network_command = ['echo', message]
def ini_to_array(ini_file):
# this enables the ini file to be written like
# sites = google.com, facebook.com, quora.com ....
if os.path.exists(ini_file):
f = open(ini_file)
sites = []
for line in f:
key, value = [each.strip() for each in line.partition("=")[::2]]
if key == "sites":
for item in [each.strip() for each in value.split(",")]:
sites.append(item)
return sites
else:
return []
hosts_file = '/etc/hosts'
if "win32" in sys.platform:
hosts_file = '/Windows/System32/drivers/etc/hosts'
start_token = '## start-gsd'
end_token = '## end-gsd'
site_list = ini_to_array(ini_global) + ini_to_array(ini_local)
def rehash():
subprocess.check_call(restart_network_command)
def work():
hFile = open(hosts_file, 'a+')
contents = hFile.read()
if start_token in contents and end_token in contents:
exit_error("Work mode already set.")
print(start_token, file=hFile)
# remove duplicates by converting list to a set
for site in set(site_list):
print("127.0.0.1\t" + site, file=hFile)
print("127.0.0.1\twww." + site, file=hFile)
print(end_token, file=hFile)
rehash()
def play():
hosts_file_handle = open(hosts_file, "r+")
lines = hosts_file_handle.readlines()
startIndex = -1
for index, line in enumerate(lines):
if line.strip() == start_token:
startIndex = index
if startIndex > -1:
lines = lines[0:startIndex]
hosts_file_handle.seek(0)
hosts_file_handle.write(''.join(lines))
hosts_file_handle.truncate()
rehash()
def main():
if getpass.getuser() != 'root' and 'win32' not in sys.platform:
exit_error('Please run script as root.')
if len(sys.argv) != 2:
exit_error('usage: ' + sys.argv[0] + ' [work|play]')
try:
{"work": work, "play": play}[sys.argv[1]]()
except KeyError:
exit_error('usage: ' + sys.argv[0] + ' [work|play]')
if __name__ == "__main__":
main()