From 36e7dfaa0ecd2b2d4b332aa0dc5c2855ba8aa946 Mon Sep 17 00:00:00 2001 From: David-Wobrock Date: Sun, 24 Feb 2019 18:18:45 +0100 Subject: [PATCH] Do not create cache folder if no-cache option --- django_migration_linter/cache.py | 9 ---- django_migration_linter/migration.py | 33 ++++++++------ django_migration_linter/migration_linter.py | 48 ++++++++++++--------- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/django_migration_linter/cache.py b/django_migration_linter/cache.py index aa28578..d64ef3e 100644 --- a/django_migration_linter/cache.py +++ b/django_migration_linter/cache.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import hashlib import os import pickle @@ -41,11 +40,3 @@ class Cache(dict): def save(self): with open(self.filename, "wb") as f: pickle.dump(self, f, protocol=2) - - @staticmethod - def md5(path): - hash_md5 = hashlib.md5() - with open(path, "rb") as f: - for chunk in iter(lambda: f.read(4096), b""): - hash_md5.update(chunk) - return hash_md5.hexdigest() diff --git a/django_migration_linter/migration.py b/django_migration_linter/migration.py index 004f64c..c24a2ae 100644 --- a/django_migration_linter/migration.py +++ b/django_migration_linter/migration.py @@ -1,12 +1,21 @@ -from django_migration_linter.utils import split_migration_path - - -class Migration(object): - def __init__(self, abs_path): - self.abs_path = abs_path - self.app_name, self.name = split_migration_path(self.abs_path) - - def __lt__(self, other): - if not isinstance(other, Migration): - return NotImplemented - return self.abs_path < other.abs_path +import hashlib + +from django_migration_linter.utils import split_migration_path + + +class Migration(object): + def __init__(self, abs_path): + self.abs_path = abs_path + self.app_name, self.name = split_migration_path(self.abs_path) + + def __lt__(self, other): + if not isinstance(other, Migration): + return NotImplemented + return self.abs_path < other.abs_path + + def get_md5hash(self): + hash_md5 = hashlib.md5() + with open(self.abs_path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() diff --git a/django_migration_linter/migration_linter.py b/django_migration_linter/migration_linter.py index 2b557ca..ee6651c 100644 --- a/django_migration_linter/migration_linter.py +++ b/django_migration_linter/migration_linter.py @@ -61,9 +61,9 @@ class MigrationLinter(object): self.nb_total = 0 # Initialise cache. Read from old, write to new to prune old entries. - self.old_cache = Cache(self.django_path, self.cache_path) - self.new_cache = Cache(self.django_path, self.cache_path) if not self.no_cache: + self.old_cache = Cache(self.django_path, self.cache_path) + self.new_cache = Cache(self.django_path, self.cache_path) self.old_cache.load() def lint_migration(self, migration): @@ -72,22 +72,9 @@ class MigrationLinter(object): print("({0}, {1})... ".format(app_name, migration_name), end="") self.nb_total += 1 - md5hash = self.old_cache.md5(migration.abs_path) - if md5hash in self.old_cache: - if self.old_cache[md5hash]["result"] == "IGNORE": - print("IGNORE (cached)") - self.nb_ignored += 1 - elif self.old_cache[md5hash]["result"] == "OK": - print("OK (cached)") - self.nb_valid += 1 - else: - print("ERR (cached)") - self.nb_erroneous += 1 - - if "errors" in self.old_cache[md5hash]: - self.print_errors(self.old_cache[md5hash]["errors"]) - - self.new_cache[md5hash] = self.old_cache[md5hash] + md5hash = migration.get_md5hash() + if not self.no_cache and md5hash in self.old_cache: + self.lint_cached_migration(md5hash) return if self.should_ignore_migration(app_name, migration_name): @@ -101,20 +88,39 @@ class MigrationLinter(object): if analysis_result["ignored"]: print("IGNORE") - self.new_cache[md5hash] = {"result": "IGNORE"} self.nb_ignored += 1 + if not self.no_cache: + self.new_cache[md5hash] = {"result": "IGNORE"} return if not errors: print("OK") - self.new_cache[md5hash] = {"result": "OK"} self.nb_valid += 1 + if not self.no_cache: + self.new_cache[md5hash] = {"result": "OK"} return print("ERR") - self.new_cache[md5hash] = {"result": "ERR", "errors": errors} self.nb_erroneous += 1 self.print_errors(errors) + if not self.no_cache: + self.new_cache[md5hash] = {"result": "ERR", "errors": errors} + + def lint_cached_migration(self, md5hash): + cached_value = self.old_cache[md5hash] + if cached_value["result"] == "IGNORE": + print("IGNORE (cached)") + self.nb_ignored += 1 + elif cached_value["result"] == "OK": + print("OK (cached)") + self.nb_valid += 1 + else: + print("ERR (cached)") + self.nb_erroneous += 1 + if "errors" in cached_value: + self.print_errors(cached_value["errors"]) + + self.new_cache[md5hash] = cached_value @staticmethod def print_errors(errors):