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:
23
.pre-commit-config.yaml
Normal file
23
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v1.3.0
|
||||||
|
hooks:
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: autopep8-wrapper
|
||||||
|
- id: check-docstring-first
|
||||||
|
- id: check-json
|
||||||
|
- id: check-added-large-files
|
||||||
|
- id: check-yaml
|
||||||
|
- id: debug-statements
|
||||||
|
- id: name-tests-test
|
||||||
|
- id: requirements-txt-fixer
|
||||||
|
- id: flake8
|
||||||
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
|
rev: v1.2.0
|
||||||
|
hooks:
|
||||||
|
- id: pyupgrade
|
||||||
|
- repo: https://github.com/asottile/add-trailing-comma
|
||||||
|
rev: v0.6.4
|
||||||
|
hooks:
|
||||||
|
- id: add-trailing-comma
|
||||||
29
dzonegit.py
29
dzonegit.py
@@ -40,7 +40,7 @@ def get_head():
|
|||||||
["git", "rev-parse", "--verify", "HEAD"],
|
["git", "rev-parse", "--verify", "HEAD"],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
encoding="utf-8"
|
encoding="utf-8",
|
||||||
)
|
)
|
||||||
if r.returncode == 0:
|
if r.returncode == 0:
|
||||||
return r.stdout.strip()
|
return r.stdout.strip()
|
||||||
@@ -67,15 +67,16 @@ def get_file_contents(path, revision=""):
|
|||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
check=True
|
check=True,
|
||||||
)
|
)
|
||||||
return r.stdout
|
return r.stdout
|
||||||
|
|
||||||
|
|
||||||
def compile_zone(zonename, zonedata):
|
def compile_zone(zonename, zonedata):
|
||||||
""" Compile the zone. Return tuple with results."""
|
""" Compile the zone. Return tuple with results."""
|
||||||
CompileResults = namedtuple("CompileResults", "success, serial, "
|
CompileResults = namedtuple(
|
||||||
"zonehash, stderr")
|
"CompileResults", "success, serial, zonehash, stderr",
|
||||||
|
)
|
||||||
r = subprocess.run(
|
r = subprocess.run(
|
||||||
["/usr/sbin/named-compilezone", "-o", "-", zonename, "/dev/stdin"],
|
["/usr/sbin/named-compilezone", "-o", "-", zonename, "/dev/stdin"],
|
||||||
input=zonedata,
|
input=zonedata,
|
||||||
@@ -110,7 +111,7 @@ def get_altered_files(against, diff_filter=None):
|
|||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
check=True
|
check=True,
|
||||||
)
|
)
|
||||||
if r.stdout:
|
if r.stdout:
|
||||||
return (Path(p) for p in r.stdout.rstrip("\0").split("\0"))
|
return (Path(p) for p in r.stdout.rstrip("\0").split("\0"))
|
||||||
@@ -143,8 +144,10 @@ def get_zone_name(path, zonedata):
|
|||||||
tt = str.maketrans("", "", "/_,:-+*%^&#$")
|
tt = str.maketrans("", "", "/_,:-+*%^&#$")
|
||||||
sn, on = [s.translate(tt) for s in [stemname, originname]]
|
sn, on = [s.translate(tt) for s in [stemname, originname]]
|
||||||
if sn != on:
|
if sn != on:
|
||||||
raise HookException(f"Zone origin {originname} differs from "
|
raise HookException(
|
||||||
"zone file.", fname=path)
|
f"Zone origin {originname} differs from zone file.",
|
||||||
|
fname=path,
|
||||||
|
)
|
||||||
return originname
|
return originname
|
||||||
else:
|
else:
|
||||||
return stemname
|
return stemname
|
||||||
@@ -160,8 +163,10 @@ def check_updated_zones(against):
|
|||||||
zname = get_zone_name(f, zonedata)
|
zname = get_zone_name(f, zonedata)
|
||||||
rnew = compile_zone(zname, zonedata)
|
rnew = compile_zone(zname, zonedata)
|
||||||
if not rnew.success:
|
if not rnew.success:
|
||||||
raise HookException("New zone version does not compile",
|
raise HookException(
|
||||||
f, rnew.stderr)
|
"New zone version does not compile",
|
||||||
|
f, rnew.stderr,
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
zonedata = get_file_contents(f, against)
|
zonedata = get_file_contents(f, against)
|
||||||
zname = get_zone_name(f, zonedata)
|
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
|
if (rold.success and rold.zonehash != rnew.zonehash and not
|
||||||
is_serial_increased(rold.serial, rnew.serial)):
|
is_serial_increased(rold.serial, rnew.serial)):
|
||||||
raise HookException("Zone contents changed without "
|
raise HookException(
|
||||||
"increasing serial", fname=f)
|
"Zone contents changed without increasing serial",
|
||||||
|
fname=f,
|
||||||
|
)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
pass # Old version of zone did not exist
|
pass # Old version of zone did not exist
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import pytest
|
|||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from dzonegit import *
|
import dzonegit
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
@@ -16,11 +16,11 @@ def git_dir(tmpdir_factory):
|
|||||||
|
|
||||||
def test_get_head(git_dir):
|
def test_get_head(git_dir):
|
||||||
git_dir.chdir()
|
git_dir.chdir()
|
||||||
assert get_head() == "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
assert dzonegit.get_head() == "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
||||||
git_dir.join("dummy").write("dummy\n")
|
git_dir.join("dummy").write("dummy\n")
|
||||||
subprocess.call(["git", "add", "dummy"])
|
subprocess.call(["git", "add", "dummy"])
|
||||||
subprocess.call(["git", "commit", "-m", "dummy"])
|
subprocess.call(["git", "commit", "-m", "dummy"])
|
||||||
assert get_head() != "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
assert dzonegit.get_head() != "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
||||||
|
|
||||||
|
|
||||||
def test_check_whitespace_errors(git_dir):
|
def test_check_whitespace_errors(git_dir):
|
||||||
@@ -28,16 +28,16 @@ def test_check_whitespace_errors(git_dir):
|
|||||||
git_dir.join("whitespace").write(" ")
|
git_dir.join("whitespace").write(" ")
|
||||||
subprocess.call(["git", "add", "whitespace"])
|
subprocess.call(["git", "add", "whitespace"])
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
check_whitespace_errors(get_head())
|
dzonegit.check_whitespace_errors(dzonegit.get_head())
|
||||||
subprocess.call(["git", "rm", "-f", "whitespace"])
|
subprocess.call(["git", "rm", "-f", "whitespace"])
|
||||||
check_whitespace_errors(get_head())
|
dzonegit.check_whitespace_errors(dzonegit.get_head())
|
||||||
|
|
||||||
|
|
||||||
def test_get_file_contents(git_dir):
|
def test_get_file_contents(git_dir):
|
||||||
git_dir.chdir()
|
git_dir.chdir()
|
||||||
assert get_file_contents("dummy") == "dummy\n"
|
assert dzonegit.get_file_contents("dummy") == "dummy\n"
|
||||||
with pytest.raises(subprocess.CalledProcessError):
|
with pytest.raises(subprocess.CalledProcessError):
|
||||||
get_file_contents('nonexistent')
|
dzonegit.get_file_contents('nonexistent')
|
||||||
|
|
||||||
|
|
||||||
def test_compile_zone():
|
def test_compile_zone():
|
||||||
@@ -53,23 +53,23 @@ $ORIGIN example.com.
|
|||||||
60 IN NS ns
|
60 IN NS ns
|
||||||
ns.example.com. 60 IN A 192.0.2.1
|
ns.example.com. 60 IN A 192.0.2.1
|
||||||
"""
|
"""
|
||||||
r = compile_zone("example.org", testzone)
|
r = dzonegit.compile_zone("example.org", testzone)
|
||||||
assert not r.success
|
assert not r.success
|
||||||
assert r.zonehash is None
|
assert r.zonehash is None
|
||||||
assert r.stderr
|
assert r.stderr
|
||||||
r = compile_zone("example.com", testzone)
|
r = dzonegit.compile_zone("example.com", testzone)
|
||||||
assert r.success
|
assert r.success
|
||||||
assert r.serial == "1234567890"
|
assert r.serial == "1234567890"
|
||||||
assert r.zonehash
|
assert r.zonehash
|
||||||
r2 = compile_zone("example.com", testzone + "\n\n; some comment")
|
r2 = dzonegit.compile_zone("example.com", testzone + "\n\n; some comment")
|
||||||
assert r.zonehash == r2.zonehash
|
assert r.zonehash == r2.zonehash
|
||||||
|
|
||||||
|
|
||||||
def test_is_serial_increased():
|
def test_is_serial_increased():
|
||||||
assert is_serial_increased(1234567890, "2018010100")
|
assert dzonegit.is_serial_increased(1234567890, "2018010100")
|
||||||
assert is_serial_increased("2018010100", "4018010100")
|
assert dzonegit.is_serial_increased("2018010100", "4018010100")
|
||||||
assert is_serial_increased("4018010100", "1234567890")
|
assert dzonegit.is_serial_increased("4018010100", "1234567890")
|
||||||
assert not is_serial_increased(2018010100, "1234567890")
|
assert not dzonegit.is_serial_increased(2018010100, "1234567890")
|
||||||
|
|
||||||
|
|
||||||
def test_get_altered_files(git_dir):
|
def test_get_altered_files(git_dir):
|
||||||
@@ -77,10 +77,10 @@ def test_get_altered_files(git_dir):
|
|||||||
git_dir.join("dummy").write("dummy2\n")
|
git_dir.join("dummy").write("dummy2\n")
|
||||||
git_dir.join("new").write("newfile\n")
|
git_dir.join("new").write("newfile\n")
|
||||||
subprocess.call(["git", "add", "dummy", "new"])
|
subprocess.call(["git", "add", "dummy", "new"])
|
||||||
files = set(get_altered_files("HEAD", "AM"))
|
files = set(dzonegit.get_altered_files("HEAD", "AM"))
|
||||||
assert files == set([Path("dummy"), Path("new")])
|
assert files == {Path("dummy"), Path("new")}
|
||||||
subprocess.call(["git", "checkout", "-f", "HEAD"])
|
subprocess.call(["git", "checkout", "-f", "HEAD"])
|
||||||
assert set(get_altered_files("HEAD", "AM")) == set()
|
assert set(dzonegit.get_altered_files("HEAD", "AM")) == set()
|
||||||
|
|
||||||
|
|
||||||
def test_get_zone_origin():
|
def test_get_zone_origin():
|
||||||
@@ -93,14 +93,14 @@ $ORIGIN sub
|
|||||||
$ORIGIN subsub.example.com.
|
$ORIGIN subsub.example.com.
|
||||||
$ORIGIN example.com.
|
$ORIGIN example.com.
|
||||||
"""
|
"""
|
||||||
assert "example.com" == get_zone_origin(testzone)
|
assert "example.com" == dzonegit.get_zone_origin(testzone)
|
||||||
testzone = """
|
testzone = """
|
||||||
@ 60 IN SOA ns hostmaster 1 60 60 60 60
|
@ 60 IN SOA ns hostmaster 1 60 60 60 60
|
||||||
60 IN NS ns
|
60 IN NS ns
|
||||||
$ORIGIN example.com.
|
$ORIGIN example.com.
|
||||||
ns.example.com. 60 IN A 192.0.2.1
|
ns.example.com. 60 IN A 192.0.2.1
|
||||||
"""
|
"""
|
||||||
assert get_zone_origin(testzone) is None
|
assert dzonegit.get_zone_origin(testzone) is None
|
||||||
|
|
||||||
|
|
||||||
def test_get_zone_name():
|
def test_get_zone_name():
|
||||||
@@ -110,19 +110,23 @@ $ORIGIN eXample.com. ;coment
|
|||||||
60 IN NS ns
|
60 IN NS ns
|
||||||
ns.example.com. 60 IN A 192.0.2.1
|
ns.example.com. 60 IN A 192.0.2.1
|
||||||
"""
|
"""
|
||||||
assert "example.com" == get_zone_name("zones/example.com.zone", "")
|
assert "example.com" == dzonegit.get_zone_name(
|
||||||
assert "example.com" == get_zone_name("zones/example.com.zone", testzone)
|
"zones/example.com.zone", "",
|
||||||
|
)
|
||||||
|
assert "example.com" == dzonegit.get_zone_name(
|
||||||
|
"zones/example.com.zone", testzone,
|
||||||
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
get_zone_name("zones/example.org.zone", testzone)
|
dzonegit.get_zone_name("zones/example.org.zone", testzone)
|
||||||
testzone = """
|
testzone = """
|
||||||
$ORIGIN 240/28.2.0.192.in-addr.arpa.
|
$ORIGIN 240/28.2.0.192.in-addr.arpa.
|
||||||
@ 60 IN SOA ns hostmaster 1 60 60 60 60
|
@ 60 IN SOA ns hostmaster 1 60 60 60 60
|
||||||
60 IN NS ns
|
60 IN NS ns
|
||||||
ns 60 IN A 192.0.2.1
|
ns 60 IN A 192.0.2.1
|
||||||
"""
|
"""
|
||||||
assert "240/28.2.0.192.in-addr.arpa" == get_zone_name(
|
assert "240/28.2.0.192.in-addr.arpa" == dzonegit.get_zone_name(
|
||||||
"zones/240-28.2.0.192.in-addr.arpa.zone",
|
"zones/240-28.2.0.192.in-addr.arpa.zone",
|
||||||
testzone
|
testzone,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -131,13 +135,13 @@ def test_check_updated_zones(git_dir):
|
|||||||
git_dir.join("dummy.zone").write("")
|
git_dir.join("dummy.zone").write("")
|
||||||
subprocess.call(["git", "add", "dummy.zone"])
|
subprocess.call(["git", "add", "dummy.zone"])
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
check_updated_zones(get_head())
|
dzonegit.check_updated_zones(dzonegit.get_head())
|
||||||
git_dir.join("dummy.zone").write("""
|
git_dir.join("dummy.zone").write("""
|
||||||
@ 60 IN SOA ns hm 1 60 60 60 60
|
@ 60 IN SOA ns hm 1 60 60 60 60
|
||||||
60 NS ns.example.com.
|
60 NS ns.example.com.
|
||||||
""")
|
""")
|
||||||
subprocess.call(["git", "add", "dummy.zone"])
|
subprocess.call(["git", "add", "dummy.zone"])
|
||||||
check_updated_zones(get_head())
|
dzonegit.check_updated_zones(dzonegit.get_head())
|
||||||
subprocess.call(["git", "commit", "-m", "dummy.zone"])
|
subprocess.call(["git", "commit", "-m", "dummy.zone"])
|
||||||
git_dir.join("dummy.zone").write("""
|
git_dir.join("dummy.zone").write("""
|
||||||
@ 60 IN SOA ns hm 1 60 60 60 60
|
@ 60 IN SOA ns hm 1 60 60 60 60
|
||||||
@@ -145,7 +149,7 @@ def test_check_updated_zones(git_dir):
|
|||||||
""")
|
""")
|
||||||
subprocess.call(["git", "add", "dummy.zone"])
|
subprocess.call(["git", "add", "dummy.zone"])
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
check_updated_zones(get_head())
|
dzonegit.check_updated_zones(dzonegit.get_head())
|
||||||
git_dir.join("dummy.zone").write("""
|
git_dir.join("dummy.zone").write("""
|
||||||
$ORIGIN other.
|
$ORIGIN other.
|
||||||
@ 60 IN SOA ns hm 1 60 60 60 60
|
@ 60 IN SOA ns hm 1 60 60 60 60
|
||||||
@@ -153,11 +157,11 @@ $ORIGIN other.
|
|||||||
""")
|
""")
|
||||||
subprocess.call(["git", "add", "dummy.zone"])
|
subprocess.call(["git", "add", "dummy.zone"])
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
check_updated_zones(get_head())
|
dzonegit.check_updated_zones(dzonegit.get_head())
|
||||||
git_dir.join("dummy.zone").write("""
|
git_dir.join("dummy.zone").write("""
|
||||||
$ORIGIN dummy.
|
$ORIGIN dummy.
|
||||||
@ 60 IN SOA ns hm 2 60 60 60 60
|
@ 60 IN SOA ns hm 2 60 60 60 60
|
||||||
60 NS ns.example.org.
|
60 NS ns.example.org.
|
||||||
""")
|
""")
|
||||||
subprocess.call(["git", "add", "dummy.zone"])
|
subprocess.call(["git", "add", "dummy.zone"])
|
||||||
check_updated_zones(get_head())
|
dzonegit.check_updated_zones(dzonegit.get_head())
|
||||||
|
|||||||
Reference in New Issue
Block a user