Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 96 lines (74 sloc) 2.53 KB
#!/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)
ini_local = path.expanduser(path.join("~", ".get-stuff-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"]
# 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 =,, ....
if os.path.exists(ini_file):
f = open(ini_file)
sites = []
for line in f:
key, value = [each.strip() for each in line.split("=", 1)]
if key == "sites":
for item in [each.strip() for each in value.split(",")]:
return sites
return []
hosts_file = '/etc/hosts'
start_token = '## start-gsd'
end_token = '## end-gsd'
site_list = ini_to_array(ini_global) + ini_to_array(ini_local)
def rehash():
def work():
hFile = open(hosts_file, 'a+')
contents =
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("\t" + site, file=hFile)
print("\twww." + site, file=hFile)
print(end_token, file=hFile)
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]
def main():
if getpass.getuser() != 'root':
exit_error('Please run script as root.')
if len(sys.argv) != 2:
exit_error('usage: ' + sys.argv[0] + ' [work|play]')
{"work": work, "play": play}[sys.argv[1]]()
if __name__ == "__main__":
Something went wrong with that request. Please try again.