Merge branch 'master' into do-not-check-git-folder
This commit is contained in:
18 files changed
+107
-43
No files matched your search
@@ -16,7 +16,7 @@ import hashlib
|
||||
import os
|
||||
import pickle
|
||||
|
||||
from django_migration_linter.utils import split_path, compose_migration_path
|
||||
from .utils import split_path
|
||||
|
||||
|
||||
class Cache(dict):
|
||||
@@ -42,8 +42,7 @@ class Cache(dict):
|
||||
with open(self.filename, "wb") as f:
|
||||
pickle.dump(self, f, protocol=2)
|
||||
|
||||
def md5(self, app_name, migration):
|
||||
path = compose_migration_path(self.django_folder, app_name, migration)
|
||||
def md5(self, path):
|
||||
hash_md5 = hashlib.md5()
|
||||
with open(path, "rb") as f:
|
||||
for chunk in iter(lambda: f.read(4096), b""):
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
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
|
||||
@@ -19,10 +19,10 @@ import re
|
||||
from subprocess import Popen, PIPE
|
||||
import sys
|
||||
|
||||
from django_migration_linter.cache import Cache
|
||||
from django_migration_linter.constants import DEFAULT_CACHE_PATH, MIGRATION_FOLDER_NAME
|
||||
from django_migration_linter.utils import split_migration_path
|
||||
from . import utils
|
||||
from .cache import Cache
|
||||
from .constants import DEFAULT_CACHE_PATH, MIGRATION_FOLDER_NAME
|
||||
from .migration import Migration
|
||||
from .utils import is_directory, is_django_project, is_git_project, clean_bytes_to_str
|
||||
from .sql_analyser import analyse_sql_statements
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -31,13 +31,13 @@ logger = logging.getLogger(__name__)
|
||||
class MigrationLinter(object):
|
||||
def __init__(self, project_path, **kwargs):
|
||||
# Verify correctness
|
||||
if not utils.is_directory(project_path):
|
||||
if not is_directory(project_path):
|
||||
raise ValueError(
|
||||
"The given path {0} does not seem to be a directory.".format(
|
||||
project_path
|
||||
)
|
||||
)
|
||||
if not utils.is_django_project(project_path):
|
||||
if not is_django_project(project_path):
|
||||
raise ValueError(
|
||||
("The given path {0} does not " "seem to be a django project.").format(
|
||||
project_path
|
||||
@@ -72,11 +72,13 @@ class MigrationLinter(object):
|
||||
if not self.no_cache:
|
||||
self.old_cache.load()
|
||||
|
||||
def lint_migration(self, app_name, migration_name):
|
||||
def lint_migration(self, migration):
|
||||
app_name = migration.app_name
|
||||
migration_name = migration.name
|
||||
print("({0}, {1})... ".format(app_name, migration_name), end="")
|
||||
self.nb_total += 1
|
||||
|
||||
md5hash = self.old_cache.md5(app_name, migration_name)
|
||||
md5hash = self.old_cache.md5(migration.abs_path)
|
||||
if md5hash in self.old_cache:
|
||||
if self.old_cache[md5hash]["result"] == "IGNORE":
|
||||
print("IGNORE (cached)")
|
||||
@@ -140,7 +142,7 @@ class MigrationLinter(object):
|
||||
|
||||
# Lint those migrations
|
||||
for m in migrations:
|
||||
self.lint_migration(*m)
|
||||
self.lint_migration(m)
|
||||
|
||||
if not self.no_cache:
|
||||
self.new_cache.save()
|
||||
@@ -182,9 +184,7 @@ class MigrationLinter(object):
|
||||
)
|
||||
|
||||
sql_statements = []
|
||||
for line in map(
|
||||
utils.clean_bytes_to_str, sqlmigrate_process.stdout.readlines()
|
||||
):
|
||||
for line in map(clean_bytes_to_str, sqlmigrate_process.stdout.readlines()):
|
||||
sql_statements.append(line)
|
||||
sqlmigrate_process.wait()
|
||||
if sqlmigrate_process.returncode != 0:
|
||||
@@ -203,19 +203,18 @@ class MigrationLinter(object):
|
||||
).format(self.django_path, git_commit_id)
|
||||
logger.info("Executing {0}".format(git_diff_command))
|
||||
diff_process = Popen(git_diff_command, shell=True, stdout=PIPE, stderr=PIPE)
|
||||
for line in map(utils.clean_bytes_to_str, diff_process.stdout.readlines()):
|
||||
for line in map(clean_bytes_to_str, diff_process.stdout.readlines()):
|
||||
# Only gather lines that include added migrations
|
||||
if (
|
||||
re.search(r"\/{0}\/.*\.py".format(MIGRATION_FOLDER_NAME), line)
|
||||
and "__init__" not in line
|
||||
):
|
||||
app_name, migration_name = split_migration_path(line)
|
||||
migrations.append((app_name, migration_name))
|
||||
migrations.append(Migration(os.path.join(self.django_path, line)))
|
||||
diff_process.wait()
|
||||
|
||||
if diff_process.returncode != 0:
|
||||
output = []
|
||||
for line in map(utils.clean_bytes_to_str, diff_process.stderr.readlines()):
|
||||
for line in map(clean_bytes_to_str, diff_process.stderr.readlines()):
|
||||
output.append(line)
|
||||
logger.error("Error while git diff command:\n{}".format("".join(output)))
|
||||
raise Exception("Error while executing git diff command")
|
||||
@@ -231,8 +230,7 @@ class MigrationLinter(object):
|
||||
and file_name != "__init__.py"
|
||||
):
|
||||
full_migration_path = os.path.join(root, file_name)
|
||||
app_name, migration_name = split_migration_path(full_migration_path)
|
||||
migrations.append((app_name, migration_name))
|
||||
migrations.append(Migration(full_migration_path))
|
||||
return migrations
|
||||
|
||||
def should_ignore_migration(self, app_name, migration_name):
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
import re
|
||||
import logging
|
||||
|
||||
from django_migration_linter.operations import IGNORE_MIGRATION_SQL
|
||||
from .operations import IGNORE_MIGRATION_SQL
|
||||
|
||||
IGNORED_MIGRATION = "IGNORED_MIGRATION"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ from __future__ import print_function
|
||||
import os
|
||||
import sys
|
||||
|
||||
from django_migration_linter.constants import MIGRATION_FOLDER_NAME
|
||||
from .constants import MIGRATION_FOLDER_NAME
|
||||
|
||||
|
||||
def is_django_project(path):
|
||||
|
||||
Reference in new issue
Block a user