1
0
mirror of https://github.com/oskar456/dzonegit.git synced 2024-05-11 05:55:41 +00:00

Add pre-commit and fix coding style

This commit is contained in:
Ondřej Caletka
2018-07-07 22:36:35 +02:00
parent b04985c12a
commit c30b1d72cd
3 changed files with 101 additions and 67 deletions

View File

@@ -37,11 +37,11 @@ class HookException(ValueError):
def get_head():
r = subprocess.run(
["git", "rev-parse", "--verify", "HEAD"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8"
)
["git", "rev-parse", "--verify", "HEAD"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8",
)
if r.returncode == 0:
return r.stdout.strip()
else:
@@ -51,11 +51,11 @@ def get_head():
def check_whitespace_errors(against):
r = subprocess.run(
["git", "diff-index", "--check", "--cached", against],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)
["git", "diff-index", "--check", "--cached", against],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)
if r.returncode != 0:
raise HookException("Whitespace errors", stderr=r.stdout)
@@ -63,26 +63,27 @@ def check_whitespace_errors(against):
def get_file_contents(path, revision=""):
""" Return contents of a file in staged env or in some revision. """
r = subprocess.run(
["git", "show", f"{revision}:{path}"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8",
check=True
)
["git", "show", f"{revision}:{path}"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8",
check=True,
)
return r.stdout
def compile_zone(zonename, zonedata):
""" Compile the zone. Return tuple with results."""
CompileResults = namedtuple("CompileResults", "success, serial, "
"zonehash, stderr")
CompileResults = namedtuple(
"CompileResults", "success, serial, zonehash, stderr",
)
r = subprocess.run(
["/usr/sbin/named-compilezone", "-o", "-", zonename, "/dev/stdin"],
input=zonedata,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
["/usr/sbin/named-compilezone", "-o", "-", zonename, "/dev/stdin"],
input=zonedata,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
m = re.search(r"^zone.*loaded serial ([0-9]*)$", r.stderr, re.MULTILINE)
if r.returncode == 0 and m:
serial = m.group(1)
@@ -106,12 +107,12 @@ def get_altered_files(against, diff_filter=None):
cmd.append(f"--diff-filter={diff_filter}")
cmd.append(against)
r = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8",
check=True
)
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding="utf-8",
check=True,
)
if r.stdout:
return (Path(p) for p in r.stdout.rstrip("\0").split("\0"))
else:
@@ -143,8 +144,10 @@ def get_zone_name(path, zonedata):
tt = str.maketrans("", "", "/_,:-+*%^&#$")
sn, on = [s.translate(tt) for s in [stemname, originname]]
if sn != on:
raise HookException(f"Zone origin {originname} differs from "
"zone file.", fname=path)
raise HookException(
f"Zone origin {originname} differs from zone file.",
fname=path,
)
return originname
else:
return stemname
@@ -160,8 +163,10 @@ def check_updated_zones(against):
zname = get_zone_name(f, zonedata)
rnew = compile_zone(zname, zonedata)
if not rnew.success:
raise HookException("New zone version does not compile",
f, rnew.stderr)
raise HookException(
"New zone version does not compile",
f, rnew.stderr,
)
try:
zonedata = get_file_contents(f, against)
zname = get_zone_name(f, zonedata)
@@ -169,8 +174,10 @@ def check_updated_zones(against):
if (rold.success and rold.zonehash != rnew.zonehash and not
is_serial_increased(rold.serial, rnew.serial)):
raise HookException("Zone contents changed without "
"increasing serial", fname=f)
raise HookException(
"Zone contents changed without increasing serial",
fname=f,
)
except subprocess.CalledProcessError:
pass # Old version of zone did not exist