Import argparse, and add an --auto flag to run the update and merge with no prompts.
authorSteven Black <redacted>
Thu, 11 Feb 2016 03:35:09 +0000 (22:35 -0500)
committerSteven Black <redacted>
Thu, 11 Feb 2016 03:35:09 +0000 (22:35 -0500)
updateHostsFile.py

index 4c1ee84ae4a52313f4dc5a019fb889257371f1b2..776fafb55255a4d66b5de43b06a34e2594119ca7 100644 (file)
@@ -20,6 +20,7 @@ import subprocess
 import sys
 import tempfile
 import glob
+import argparse
 # zip files are not used actually, support deleted
 # StringIO is not needed in Python 3
 # Python 3 works differently with urlopen
@@ -91,7 +92,16 @@ COMMON_EXCLUSIONS = ['hulu.com']
 exclusionRegexs = []
 numberOfRules   = 0
 
+auto = False
+
 def main():
+    parser = argparse.ArgumentParser(description="Creates an amalgamated hosts file from hosts stored in data subfolders.")
+    parser.add_argument("--auto", "-a", dest="auto", default=False, action='store_true', help="Run without prompting.")
+    args = parser.parse_args()
+
+    global auto
+    auto = args.auto
+
     promptForUpdate()
     promptForExclusions()
     mergeFile = createInitialFile()
@@ -113,14 +123,14 @@ def promptForUpdate():
         except:
             printFailure("ERROR: No 'hosts' file in the folder, try creating one manually")
 
-    response = query_yes_no("Do you want to update all data sources?")
+    response = "yes" if auto else query_yes_no("Do you want to update all data sources?")
     if response == "yes":
         updateAllSources()
     else:
         print ("OK, we\'ll stick with what we\'ve  got locally.")
 
 def promptForExclusions():
-    response = query_yes_no("Do you want to exclude any domains?\n" +
+    response = "no" if auto else query_yes_no("Do you want to exclude any domains?\n" +
                             "For example, hulu.com video streaming must be able to access " +
                             "its tracking and ad servers in order to play video.")
     if response == "yes":
@@ -136,7 +146,7 @@ def promptForMoreCustomExclusions():
         return False
 
 def promptForMove(finalFile):
-    response = query_yes_no("Do you want to replace your existing hosts file " +
+    response = "no" if auto else query_yes_no("Do you want to replace your existing hosts file " +
                             "with the newly generated file?")
     if response == "yes":
         moveHostsFileIntoPlace(finalFile)
git clone https://git.99rst.org/PROJECT