Added support for automatically moving the hosts file into place and flushing the...
authorBen Limmer <redacted>
Sat, 13 Jul 2013 21:57:11 +0000 (15:57 -0600)
committerBen Limmer <redacted>
Sat, 13 Jul 2013 21:57:11 +0000 (15:57 -0600)
This addresses part of Issue #5, but I still need to do Windows.

updateHostsFile.py

index 6b2dcf83ff01a5d9503a32daaa5713d8dae3d93e..5c1ac9a4982712cd24191c5121bb74ba55d0ae11 100644 (file)
@@ -7,8 +7,10 @@
 # as sources into one, unique host file to keep you internet browsing happy.
 
 import os
+import platform
 import re
 import string
+import subprocess
 import sys
 import tempfile
 import urllib2
@@ -40,7 +42,7 @@ def main():
        finalizeFile(finalFile)
        updateReadme(numberOfRules)
        printSuccess('Success! Your shiny new hosts file has been prepared.\nIt contains ' + str(numberOfRules) + ' unique entries.')
-       print 'Copy the generated file to /etc/hosts or %SystemRoot%\system32\drivers\etc\hosts'
+       moveHostsFileIntoPlace(finalFile)
 
 # Prompt the User
 def promptForUpdate():
@@ -194,6 +196,18 @@ def updateReadme(numberOfRules):
                for line in open(README_TEMPLATE):
                        out.write(line.replace('@NUM_ENTRIES@', str(numberOfRules)))
 
+def moveHostsFileIntoPlace(finalFile):
+       if (os.name == 'posix'):
+               print 'Moving the file requires administrative privileges. You might need to enter your password.'
+               if(subprocess.call(["/usr/bin/sudo", "cp", os.path.abspath(finalFile.name), "/etc/hosts"])):
+                       printFailure("Moving the file failed.")
+               print 'Flushing the DNS Cache to utilize new hosts file...'
+               if (platform.system() == 'Darwin'):
+                       if(subprocess.call(["/usr/bin/sudo", "killall", "-HUP", "mDNSResponder"])):
+                               printFailure("Flushing the DNS Cache failed.")
+               else:
+                       if(subprocess.call(["/usr/bin/sudo", "/etc/rc.d/init.d/nscd", "restart"])):
+                               printFailure("Flushing the DNS Cache failed.")
 # End File Logic
 
 # Helper Functions
git clone https://git.99rst.org/PROJECT