query_yes_no,
recursive_glob,
remove_old_hosts_file,
+ sort_sources,
strip_rule,
supports_color,
update_all_sources,
# End Project Settings
+class TestSortSources(Base):
+ def test_sort_sources_simple(self):
+ given = [
+ "sbc.io",
+ "example.com",
+ "github.com",
+ ]
+
+ expected = ["example.com", "github.com", "sbc.io"]
+
+ actual = sort_sources(given)
+
+ self.assertEqual(actual, expected)
+
+ def test_live_data(self):
+ given = [
+ "data/KADhosts/update.json",
+ "data/someonewhocares.org/update.json",
+ "data/StevenBlack/update.json",
+ "data/adaway.org/update.json",
+ "data/URLHaus/update.json",
+ "data/UncheckyAds/update.json",
+ "data/add.2o7Net/update.json",
+ "data/mvps.org/update.json",
+ "data/add.Spam/update.json",
+ "data/add.Dead/update.json",
+ "data/malwaredomainlist.com/update.json",
+ "data/Badd-Boyz-Hosts/update.json",
+ "data/hostsVN/update.json",
+ "data/yoyo.org/update.json",
+ "data/add.Risk/update.json",
+ "data/tiuxo/update.json",
+ "extensions/gambling/update.json",
+ "extensions/porn/clefspeare13/update.json",
+ "extensions/porn/sinfonietta-snuff/update.json",
+ "extensions/porn/tiuxo/update.json",
+ "extensions/porn/sinfonietta/update.json",
+ "extensions/fakenews/update.json",
+ "extensions/social/tiuxo/update.json",
+ "extensions/social/sinfonietta/update.json",
+ ]
+
+ expected = [
+ "data/StevenBlack/update.json",
+ "data/adaway.org/update.json",
+ "data/add.2o7Net/update.json",
+ "data/add.Dead/update.json",
+ "data/add.Risk/update.json",
+ "data/add.Spam/update.json",
+ "data/Badd-Boyz-Hosts/update.json",
+ "data/hostsVN/update.json",
+ "data/KADhosts/update.json",
+ "data/malwaredomainlist.com/update.json",
+ "data/mvps.org/update.json",
+ "data/someonewhocares.org/update.json",
+ "data/tiuxo/update.json",
+ "data/UncheckyAds/update.json",
+ "data/URLHaus/update.json",
+ "data/yoyo.org/update.json",
+ "extensions/fakenews/update.json",
+ "extensions/gambling/update.json",
+ "extensions/porn/clefspeare13/update.json",
+ "extensions/porn/sinfonietta/update.json",
+ "extensions/porn/sinfonietta-snuff/update.json",
+ "extensions/porn/tiuxo/update.json",
+ "extensions/social/sinfonietta/update.json",
+ "extensions/social/tiuxo/update.json",
+ ]
+
+ actual = sort_sources(given)
+
+ self.assertEqual(actual, expected)
+
+
# Prompt the User
class TestPromptForUpdate(BaseStdout, BaseMockDir):
def setUp(self):
# End Prompt the User
+def sort_sources(sources):
+ """
+ Sorts the sources.
+ The idea is that all Steven Black's list, file or entries
+ get on top and the rest sorted alphabetically.
+
+ Parameters
+ ----------
+ sources: list
+ The sources to sort.
+ """
+
+ result = sorted(
+ sources.copy(),
+ key=lambda x: x.lower().replace("-", "").replace("_", "").replace(" ", ""),
+ )
+
+ # Steven Black's repositories/files/lists should be on top!
+ steven_black_positions = [
+ x for x, y in enumerate(result) if "stevenblack" in y.lower()
+ ]
+
+ for index in steven_black_positions:
+ result.insert(0, result.pop(index))
+
+ return result
+
+
# Exclusion logic
def display_exclusion_options(common_exclusions, exclusion_pattern, exclusion_regexes):
"""
source_data_filename = sources_params["sourcedatafilename"]
- for source in recursive_glob(sources_params["datapath"], source_data_filename):
+ for source in sort_sources(
+ recursive_glob(sources_params["datapath"], source_data_filename)
+ ):
update_file = open(source, "r", encoding="UTF-8")
update_data = json.load(update_file)
sources_data.append(update_data)
for source in sources_params["extensions"]:
source_dir = path_join_robust(sources_params["extensionspath"], source)
- for update_file_path in recursive_glob(source_dir, source_data_filename):
+ for update_file_path in sort_sources(
+ recursive_glob(source_dir, source_data_filename)
+ ):
update_file = open(update_file_path, "r")
update_data = json.load(update_file)
# The transforms we support
transform_methods = {"jsonarray": jsonarray}
- all_sources = recursive_glob("*", source_data_filename)
+ all_sources = sort_sources(recursive_glob("*", source_data_filename))
for source in all_sources:
update_file = open(source, "r", encoding="UTF-8")