Fixed: Exclude domains in the whitelist (2)
authorFrancoGag <redacted>
Sat, 7 Nov 2015 20:54:36 +0000 (17:54 -0300)
committerFrancoGag <redacted>
Sat, 7 Nov 2015 20:54:36 +0000 (17:54 -0300)
Now it probably works with Python 3 and the code is much cleaner.

updateHostsFile.py

index f151df34008279593a9fc15ef6720d924db777fa..b6be9c8b24e53fd2912a8c626c6f65366422a0b4 100755 (executable)
@@ -78,12 +78,10 @@ README_TEMPLATE = os.path.join(BASEDIR_PATH, 'readme_template.md')
 README_FILE = os.path.join(BASEDIR_PATH, 'readme.md')
 TARGET_HOST = '0.0.0.0'
 WHITELIST_FILE = os.path.join(BASEDIR_PATH, 'whitelist')
-HOSTS_FILE = os.path.join(BASEDIR_PATH, 'hosts')
 
 # Exclusions
 EXCLUSION_PATTERN = '([a-zA-Z\d-]+\.){0,}' #append domain the end
 EXCLUSIONS = []
-
 # Common domains to exclude
 COMMON_EXCLUSIONS = ['hulu.com']
 
@@ -97,46 +95,11 @@ def main():
        mergeFile = createInitialFile()
        finalFile = removeDups(mergeFile)
        finalizeFile(finalFile)
-       excludeFromFile()
        updateReadme(numberOfRules)
        printSuccess('Success! Your shiny new hosts file has been prepared.\nIt contains ' + "{:,}".format( numberOfRules ) + ' unique entries.')
 
        promptForMove(finalFile)
 
-# Exclusion from file
-def excludeFromFile():
-    global numberOfRules
-    if os.path.isfile(WHITELIST_FILE):
-       with open(WHITELIST_FILE, "r") as ins:
-               for line in ins:
-                       EXCLUSIONS.append(line)
-        f = open(HOSTS_FILE)
-        output = []
-        for line in f:
-            write = 'true'
-            for domain in EXCLUSIONS:
-                if domain in line:
-                    write = 'false'
-                    numberOfRules -= 1
-                    break
-            if (write == 'true'):
-                output.append(line)   
-        f.close()
-        f = open(HOSTS_FILE, 'w')
-        f.writelines(output)
-        f.close()
-        f = open(HOSTS_FILE)
-        output = []
-        for line in f:
-            if 'unique entries' not in line:
-                output.append(line)  
-            else:
-                output.append('# Merging these sources produced ' + "{:,}".format( numberOfRules ) + ' unique entries\n')
-        f.close()
-        f = open(HOSTS_FILE, 'w')
-        f.writelines(output)
-        f.close()
-
 # Prompt the User
 def promptForUpdate():
        response = query_yes_no("Do you want to update all data sources?")
@@ -244,6 +207,10 @@ def createInitialFile():
 
 def removeDups(mergeFile):
        global numberOfRules
+       if os.path.isfile(WHITELIST_FILE):
+               with open(WHITELIST_FILE, "r") as ins:
+                       for line in ins:
+                               EXCLUSIONS.append(line)
 
     # Another mode is required to read and write the file in Python 3
        finalFile = open(os.path.join(BASEDIR_PATH, 'hosts'), 'r+b')
@@ -252,6 +219,7 @@ def removeDups(mergeFile):
        hostnames = set()
        hostnames.add("localhost")
        for line in mergeFile.readlines():
+               write = 'true'
         # Explicit encoding
                line = line.decode("UTF-8")
                # Testing the first character doesn't require startswith
@@ -264,8 +232,11 @@ def removeDups(mergeFile):
                if matchesExclusions(strippedRule):
                        continue
                hostname, normalizedRule = normalizeRule(strippedRule) # normalize rule
-
-               if normalizedRule and (hostname not in hostnames):
+               for exclude in EXCLUSIONS:
+                       if (exclude in line):
+                               write = 'false'
+                               break
+               if normalizedRule and (hostname not in hostnames) and (write == 'true'):
                        writeData(finalFile, normalizedRule)
                        hostnames.add(hostname)
                        numberOfRules += 1
git clone https://git.99rst.org/PROJECT