forked from viccherubini/get-shit-done
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-shit-done.py
101 lines (78 loc) · 3.05 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
#!/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_file = path.expanduser(path.join("~", ".get-shit-done.ini"))
if "linux" in sys.platform:
restart_network_command = ["/etc/init.d/networking", "restart"]
elif "darwin" in sys.platform:
restart_network_command = ["dscacheutil", "-flushcache"]
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]
hosts_file = '/etc/hosts'
start_token = '## start-gsd'
end_token = '## end-gsd'
site_list = ['reddit.com', 'forums.somethingawful.com', 'somethingawful.com',
'digg.com', 'break.com', 'news.ycombinator.com', 'infoq.com',
'bebo.com', 'twitter.com', 'facebook.com', 'blip.com',
'youtube.com', 'vimeo.com', 'delicious.com', 'flickr.com',
'friendster.com', 'hi5.com', 'linkedin.com', 'livejournal.com',
'meetup.com', 'myspace.com', 'plurk.com', 'stickam.com',
'stumbleupon.com', 'yelp.com', 'slashdot.org']
def sites_from_ini(ini_file):
# This allows you to specify sites in a file using the following format.
# site_list is defined above so this will not be used by default.
# sites = google.com, facebook.com, quora.com ....
if os.path.exists(ini_file):
ini_file_handle = open(ini_file)
for line in ini_file_handle:
key, value = [each.strip() for each in line.split("=", 1)]
if key == "sites":
for item in [each.strip() for each in value.split(",")]:
site_list.append(item)
def rehash():
if "cygwin" in sys.platform:
print ('Network restart not required.')
else:
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 len(sys.argv) != 2:
exit_error('usage: ' + sys.argv[0] + ' [work|play]')
{"work": work, "play": play}[sys.argv[1]]()
if __name__ == "__main__":
sites_from_ini(ini_file)
main()