mirror of
https://github.com/StevenBlack/hosts.git
synced 2024-07-07 13:02:27 +02:00
Update data regardless fo folder depth using glob.glob().
This commit is contained in:
parent
d1c3921eb7
commit
c022c2b230
@ -21,7 +21,7 @@ import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import glob
|
||||
from glob import glob
|
||||
import argparse
|
||||
import socket
|
||||
import json
|
||||
@ -69,7 +69,7 @@ def writeData(f, data):
|
||||
|
||||
# This function doesn't list hidden files
|
||||
def listdir_nohidden(path):
|
||||
return glob.glob(os.path.join(path, "*"))
|
||||
return glob(os.path.join(path, "*"))
|
||||
|
||||
# Project Settings
|
||||
BASEDIR_PATH = os.path.dirname(os.path.realpath(__file__))
|
||||
@ -128,7 +128,6 @@ def main():
|
||||
settings["sources"] = listdir_nohidden(settings["datapath"])
|
||||
settings["extensionsources"] = listdir_nohidden(settings["extensionspath"])
|
||||
|
||||
|
||||
# All our extensions folders...
|
||||
settings["extensions"] = [os.path.basename(item) for item in listdir_nohidden(settings["extensionspath"])]
|
||||
# ... intersected with the extensions passed-in as arguments, then sorted.
|
||||
@ -245,34 +244,26 @@ def matchesExclusions(strippedRule):
|
||||
|
||||
# Update Logic
|
||||
def updateAllSources():
|
||||
allsources = list(set(settings["sources"]) | set(settings["extensionsources"]))
|
||||
# Update all hosts files regardless of folder depth
|
||||
allsources = glob('*/**/' + settings["sourcedatafilename"])
|
||||
for source in allsources:
|
||||
if os.path.isdir(source):
|
||||
for updateURL in getUpdateURLsFromFile(source):
|
||||
print ("Updating source " + os.path.basename(source) + " from " + updateURL)
|
||||
updateFile = open(source, "r")
|
||||
updateData = json.load(updateFile)
|
||||
updateURL = updateData["url"]
|
||||
updateFile.close()
|
||||
|
||||
print ("Updating source " + os.path.dirname(source) + " from " + updateURL)
|
||||
# Cross-python call
|
||||
updatedFile = getFileByUrl(updateURL)
|
||||
try:
|
||||
updatedFile = updatedFile.replace("\r", "") #get rid of carriage-return symbols
|
||||
|
||||
# This is cross-python code
|
||||
dataFile = open(os.path.join(settings["datapath"], source, settings["datafilenames"]), "wb")
|
||||
dataFile = open(os.path.join(BASEDIR_PATH, os.path.dirname(source), settings["datafilenames"]), "wb")
|
||||
writeData(dataFile, updatedFile)
|
||||
dataFile.close()
|
||||
except:
|
||||
print ("Skipping.")
|
||||
|
||||
def getUpdateURLsFromFile(source):
|
||||
pathToUpdateFile = os.path.join(settings["datapath"], source, settings["sourcedatafilename"])
|
||||
if os.path.exists(pathToUpdateFile):
|
||||
updateFile = open(pathToUpdateFile, "r")
|
||||
updateData = json.load(updateFile)
|
||||
retURLs = [updateData["url"]]
|
||||
updateFile.close()
|
||||
else:
|
||||
retURLs = None
|
||||
printFailure("Warning: Can't find the update file for source " + source + "\n" +
|
||||
"Make sure that there's a file at " + pathToUpdateFile)
|
||||
return retURLs
|
||||
# End Update Logic
|
||||
|
||||
# File Logic
|
||||
|
Loading…
Reference in New Issue
Block a user