From 5e08804cecd7e15bb12240669f0ed5e169f24496 Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 26 Apr 2016 15:46:14 +0200 Subject: [PATCH 1/5] Use 'with open() as' to automaticly close() files --- updateHostsFile.py | 61 +++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index c7191594e..645239951 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -174,12 +174,8 @@ def promptForExclusions(): if not settings["auto"]: print ("OK, we'll only exclude domains in the whitelist.") -def promptForMoreCustomExclusions(): - response = query_yes_no("Do you have more domains you want to enter?") - if response == "yes": - return True - else: - return False +def promptForMoreCustomExclusions(question="Do you have more domains you want to enter?"): + return query_yes_no(question) == "yes" def promptForMove(finalFile): @@ -212,7 +208,7 @@ def gatherCustomExclusions(): domainFromUser = myInput("Enter the domain you want to exclude (e.g. facebook.com): ") if isValidDomainFormat(domainFromUser): excludeDomain(domainFromUser) - if promptForMoreCustomExclusions() is False: + if not promptForMoreCustomExclusions(): return def excludeDomain(domain): @@ -231,11 +227,7 @@ def updateAllSources(): allsources = list(set(settings["sources"]) | set(settings["extensionsources"])) for source in allsources: if os.path.isdir(source): - updateURLs = getUpdateURLsFromFile(source) - if not len(updateURLs): - continue - - for updateURL in updateURLs: + for updateURL in getUpdateURLsFromFile(source): print ("Updating source " + os.path.basename(source) + " from " + updateURL) # Cross-python call updatedFile = getFileByUrl(updateURL) @@ -266,28 +258,27 @@ def getUpdateURLsFromFile(source): def getUpdateURLFromFile(source): pathToUpdateFile = os.path.join(settings["datapath"], source, settings["updateurlfilename"]) if os.path.exists(pathToUpdateFile): - updateFile = open(pathToUpdateFile, "r") - retURL = updateFile.readline().strip() - updateFile.close() - else: - retURL = None - printFailure("Warning: Can't find the update file for source " + source + "\n" + - "Make sure that there's a file at " + pathToUpdateFile) - return retURL + with open(pathToUpdateFile, "r") as updateFile: + return updateFile.readline().strip() + printFailure("Warning: Can't find the update file for source " + source + "\n" + + "Make sure that there's a file at " + pathToUpdateFile) + return None # End Update Logic # File Logic def createInitialFile(): mergeFile = tempfile.NamedTemporaryFile() for source in settings["sources"]: - curFile = open(os.path.join(settings["datapath"], source, settings["datafilenames"]), "r") - #Done in a cross-python way - writeData(mergeFile, curFile.read()) + filename = os.path.join(settings["datapath"], source, settings["datafilenames"]) + with open(curFile, "r"): + #Done in a cross-python way + writeData(mergeFile, curFile.read()) for source in settings["extensions"]: - curFile = open(os.path.join(settings["extensionspath"], source, settings["datafilenames"]), "r") - #Done in a cross-python way - writeData(mergeFile, curFile.read()) + filename = os.path.join(settings["extensionspath"], source, settings["datafilenames"]) + with open(filename, "r") as curFile: + #Done in a cross-python way + writeData(mergeFile, curFile.read()) return mergeFile @@ -304,10 +295,8 @@ def removeDupsAndExcl(mergeFile): os.makedirs(settings["outputpath"]) # Another mode is required to read and write the file in Python 3 - if Python3: - finalFile = open(os.path.join(settings["outputpath"], "hosts"), "w+b") - else: - finalFile = open(os.path.join(settings["outputpath"], "hosts"), "w+") + finalFile = open(os.path.join(settings["outputpath"], "hosts"), + "w+b" if Python3 else "w+") mergeFile.seek(0) # reset file pointer hostnames = set() @@ -333,9 +322,7 @@ def removeDupsAndExcl(mergeFile): continue strippedRule = stripRule(line) #strip comments - if len(strippedRule) == 0: - continue - if matchesExclusions(strippedRule): + if not strippedRule or matchesExclusions(strippedRule): continue hostname, normalizedRule = normalizeRule(strippedRule) # normalize rule for exclude in exclusions: @@ -358,7 +345,7 @@ def normalizeRule(rule): if result: hostname, suffix = result.group(2,3) hostname = hostname.lower().strip() # explicitly lowercase and trim the hostname - if suffix is not "": + if suffix: # add suffix as comment only, not as a separate host return hostname, "%s %s #%s\n" % (settings["targetip"], hostname, suffix) else: @@ -419,10 +406,8 @@ def updateReadmeData(): if settings["extensions"]: extensionsKey = "-".join(settings["extensions"]) - generationData = {} - generationData["location"] = os.path.join(settings["outputsubfolder"], "") - generationData["entries"] = settings["numberofrules"] - + generationData = {"location": os.path.join(settings["outputsubfolder"], "") + "entries": settings["numberofrules"]} settings["readmedata"][extensionsKey] = generationData with open(settings["readmedatafilename"], "w") as f: json.dump(settings["readmedata"], f) From 46877f22c49e00d18518cbf3a689587c52c0cb46 Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 26 Apr 2016 15:49:31 +0200 Subject: [PATCH 2/5] Update updateHostsFile.py --- updateHostsFile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 645239951..92d6e2403 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -270,7 +270,7 @@ def createInitialFile(): mergeFile = tempfile.NamedTemporaryFile() for source in settings["sources"]: filename = os.path.join(settings["datapath"], source, settings["datafilenames"]) - with open(curFile, "r"): + with open(filename, "r") as curFile: #Done in a cross-python way writeData(mergeFile, curFile.read()) From 9ec9f22dda69fb15c6322eb31fc60fd2028748c5 Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 26 Apr 2016 16:03:36 +0200 Subject: [PATCH 3/5] Update updateHostsFile.py --- updateHostsFile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 92d6e2403..4cddb09e9 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -406,7 +406,7 @@ def updateReadmeData(): if settings["extensions"]: extensionsKey = "-".join(settings["extensions"]) - generationData = {"location": os.path.join(settings["outputsubfolder"], "") + generationData = {"location": os.path.join(settings["outputsubfolder"], ""), "entries": settings["numberofrules"]} settings["readmedata"][extensionsKey] = generationData with open(settings["readmedatafilename"], "w") as f: From 1b46ffd85bfd30aee154fa5c5e1477f11803acc9 Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 26 Apr 2016 16:11:51 +0200 Subject: [PATCH 4/5] Update updateHostsFile.py --- updateHostsFile.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 4cddb09e9..944f8bc96 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -373,10 +373,10 @@ def writeOpeningHeader(finalFile): finalFile.seek(0) #write at the top writeData(finalFile, "# This hosts file is a merged collection of hosts from reputable sources,\n") writeData(finalFile, "# with a dash of crowd sourcing via Github\n#\n") - writeData(finalFile, "# Date: " + time.strftime("%B %d %Y", time.gmtime()) + "\n") + writeData(finalFile, "# Date: {:B %d %Y}\n".format(time.gmtime())) if settings["extensions"]: writeData(finalFile, "# Extensions added to this file: " + ", ".join(settings["extensions"]) + "\n") - writeData(finalFile, "# Number of unique domains: " + "{:,}".format(settings["numberofrules"]) + "\n#\n") + writeData(finalFile, "# Number of unique domains: " + "{:,}\n#\n".format(settings["numberofrules"])) writeData(finalFile, "# Fetch the latest version of this file: https://raw.githubusercontent.com/StevenBlack/hosts/master/"+ os.path.join(settings["outputsubfolder"],"") + "hosts\n") writeData(finalFile, "# Project home page: https://github.com/StevenBlack/hosts\n#\n") writeData(finalFile, "# ===============================================================\n") @@ -466,7 +466,7 @@ def removeOldHostsFile(): # hotfix since merging with an already e open(oldFilePath, "a").close() # create if already removed, so remove wont raise an error if settings["backup"]: - backupFilePath = os.path.join(BASEDIR_PATH, "hosts-{0}".format(time.strftime("%Y-%m-%d-%H-%M-%S"))) + backupFilePath = os.path.join(BASEDIR_PATH, "hosts-{}".format(time.strftime("%Y-%m-%d-%H-%M-%S"))) shutil.copy(oldFilePath, backupFilePath) # make a backup copy, marking the date in which the list was updated os.remove(oldFilePath) From 854f2410a1d845d5c64a8c5dd1fa43810f0d3ecd Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 26 Apr 2016 17:52:00 +0200 Subject: [PATCH 5/5] Update updateHostsFile.py --- updateHostsFile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 944f8bc96..7f124e37a 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -373,7 +373,7 @@ def writeOpeningHeader(finalFile): finalFile.seek(0) #write at the top writeData(finalFile, "# This hosts file is a merged collection of hosts from reputable sources,\n") writeData(finalFile, "# with a dash of crowd sourcing via Github\n#\n") - writeData(finalFile, "# Date: {:B %d %Y}\n".format(time.gmtime())) + writeData(finalFile, "# Date: " + time.strftime("%B %d %Y", time.gmtime()) + "\n") if settings["extensions"]: writeData(finalFile, "# Extensions added to this file: " + ", ".join(settings["extensions"]) + "\n") writeData(finalFile, "# Number of unique domains: " + "{:,}\n#\n".format(settings["numberofrules"]))