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

Python 3.5 compatibility

Handling file contents as bytestrings instead of strings.
This commit is contained in:
Ondřej Caletka
2018-07-12 14:24:44 +02:00
parent b73f2765ec
commit 6a63d7fdd0
2 changed files with 31 additions and 29 deletions

View File

@ -27,7 +27,7 @@ class HookException(ValueError):
def __str__(self): def __str__(self):
r = list() r = list()
if self.fname: if self.fname:
r.append(f"{self.fname}: ") r.append("{fname}: ".format(fname=self.fname))
r.append(self.message) r.append(self.message)
r.append("\n") r.append("\n")
if self.stderr: if self.stderr:
@ -42,10 +42,9 @@ 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",
) )
if r.returncode == 0: if r.returncode == 0:
return r.stdout.strip() return r.stdout.decode("utf-8").strip()
else: else:
# Initial commit: diff against an empty tree object # Initial commit: diff against an empty tree object
return "4b825dc642cb6eb9a060e54bf8d69288fbee4904" return "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
@ -60,10 +59,12 @@ def check_whitespace_errors(against, revision=None):
cmd, cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
encoding="utf-8",
) )
if r.returncode != 0: if r.returncode != 0:
raise HookException("Whitespace errors", stderr=r.stdout) raise HookException(
"Whitespace errors",
stderr=r.stdout.decode("utf-8"),
)
def check_tree_whitespace_errors(tree1, tree2): def check_tree_whitespace_errors(tree1, tree2):
@ -71,20 +72,21 @@ def check_tree_whitespace_errors(tree1, tree2):
["git", "diff-tree", "--check", tree1, tree2], ["git", "diff-tree", "--check", tree1, tree2],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
encoding="utf-8",
) )
if r.returncode != 0: if r.returncode != 0:
raise HookException("Whitespace errors", stderr=r.stdout) raise HookException(
"Whitespace errors",
stderr=r.stdout.decode("utf-8"),
)
def get_file_contents(path, revision=None): def get_file_contents(path, revision=None):
""" Return contents of a file in staged env or in some revision. """ """ Return contents of a file in staged env or in some revision. """
revision = "" if revision is None else revision revision = "" if revision is None else revision
r = subprocess.run( r = subprocess.run(
["git", "show", f"{revision}:{path}"], ["git", "show", "{r}:{p}".format(r=revision, p=path)],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
encoding="utf-8",
check=True, check=True,
) )
return r.stdout return r.stdout
@ -100,15 +102,15 @@ def compile_zone(zonename, zonedata):
input=zonedata, input=zonedata,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
encoding="utf-8",
) )
m = re.search(r"^zone.*loaded serial ([0-9]*)$", r.stderr, re.MULTILINE) stderr = r.stderr.decode("utf-8")
m = re.search(r"^zone.*loaded serial ([0-9]*)$", stderr, re.MULTILINE)
if r.returncode == 0 and m: if r.returncode == 0 and m:
serial = m.group(1) serial = m.group(1)
zonehash = sha256(r.stdout.encode("utf-8")).hexdigest() zonehash = sha256(r.stdout).hexdigest()
return CompileResults(True, serial, zonehash, r.stderr) return CompileResults(True, serial, zonehash, stderr)
else: else:
return CompileResults(False, None, None, r.stderr) return CompileResults(False, None, None, stderr)
def is_serial_increased(old, new): def is_serial_increased(old, new):
@ -143,7 +145,7 @@ def get_altered_files(against, diff_filter=None, revision=None):
""" """
cmd = ["git", "diff", "--name-only", "-z"] cmd = ["git", "diff", "--name-only", "-z"]
if diff_filter: if diff_filter:
cmd.append(f"--diff-filter={diff_filter}") cmd.append("--diff-filter={}".format(diff_filter))
if revision: if revision:
cmd.append(against) cmd.append(against)
cmd.append(revision) cmd.append(revision)
@ -155,11 +157,11 @@ def get_altered_files(against, diff_filter=None, revision=None):
cmd, cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
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.decode("utf-8").rstrip("\0").split("\0"))
else: else:
return list() return list()
@ -170,11 +172,11 @@ def get_zone_origin(zonedata):
Return zone name without the trailing dot. Return zone name without the trailing dot.
""" """
for line in zonedata.splitlines(): for line in zonedata.splitlines():
if re.match(r"^[^\s;]+\s+([0-9]+\s+)?(IN\s+)?SOA\s+", line, re.I): if re.match(br"^[^\s;]+\s+([0-9]+\s+)?(IN\s+)?SOA\s+", line, re.I):
break break
m = re.match(r"^\$ORIGIN\s+([^ ]+)\.\s*(;.*)?$", line, re.I) m = re.match(br"^\$ORIGIN\s+([^ ]+)\.\s*(;.*)?$", line, re.I)
if m: if m:
return m.group(1).lower() return m.group(1).decode("utf-8").lower()
def get_zone_name(path, zonedata): def get_zone_name(path, zonedata):
@ -190,7 +192,7 @@ def get_zone_name(path, zonedata):
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( raise HookException(
f"Zone origin {originname} differs from zone file.", "Zone origin {o} differs from zone file.".format(o=originname),
fname=path, fname=path,
) )
return originname return originname
@ -203,7 +205,7 @@ def check_updated_zones(against, revision=None):
for f in get_altered_files(against, "AM", revision): for f in get_altered_files(against, "AM", revision):
if not f.suffix == ".zone": if not f.suffix == ".zone":
continue continue
print(f"Checking file {f}") print("Checking file {f}".format(f=f))
zonedata = get_file_contents(f, revision) zonedata = get_file_contents(f, revision)
zname = get_zone_name(f, zonedata) zname = get_zone_name(f, zonedata)
rnew = compile_zone(zname, zonedata) rnew = compile_zone(zname, zonedata)

View File

@ -37,13 +37,13 @@ def test_check_whitespace_errors(git_dir):
def test_get_file_contents(git_dir): def test_get_file_contents(git_dir):
git_dir.chdir() git_dir.chdir()
assert dzonegit.get_file_contents("dummy") == "dummy\n" assert dzonegit.get_file_contents("dummy") == b"dummy\n"
with pytest.raises(subprocess.CalledProcessError): with pytest.raises(subprocess.CalledProcessError):
dzonegit.get_file_contents('nonexistent') dzonegit.get_file_contents('nonexistent')
def test_compile_zone(): def test_compile_zone():
testzone = """ testzone = b"""
$ORIGIN example.com. $ORIGIN example.com.
@ 60 IN SOA ns hostmaster ( @ 60 IN SOA ns hostmaster (
1234567890 ; serial 1234567890 ; serial
@ -63,7 +63,7 @@ ns.example.com. 60 IN A 192.0.2.1
assert r.success assert r.success
assert r.serial == "1234567890" assert r.serial == "1234567890"
assert r.zonehash assert r.zonehash
r2 = dzonegit.compile_zone("example.com", testzone + "\n\n; some comment") r2 = dzonegit.compile_zone("example.com", testzone + b"\n\n; some comment")
assert r.zonehash == r2.zonehash assert r.zonehash == r2.zonehash
@ -86,7 +86,7 @@ def test_get_altered_files(git_dir):
def test_get_zone_origin(): def test_get_zone_origin():
testzone = """ testzone = b"""
$ORIGIN examPle.com. ;coment $ORIGIN examPle.com. ;coment
@ 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
@ -96,7 +96,7 @@ $ORIGIN subsub.example.com.
$ORIGIN example.com. $ORIGIN example.com.
""" """
assert "example.com" == dzonegit.get_zone_origin(testzone) assert "example.com" == dzonegit.get_zone_origin(testzone)
testzone = """ testzone = b"""
@ 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.
@ -106,7 +106,7 @@ ns.example.com. 60 IN A 192.0.2.1
def test_get_zone_name(): def test_get_zone_name():
testzone = """ testzone = b"""
$ORIGIN eXample.com. ;coment $ORIGIN eXample.com. ;coment
@ 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
@ -120,7 +120,7 @@ ns.example.com. 60 IN A 192.0.2.1
) )
with pytest.raises(ValueError): with pytest.raises(ValueError):
dzonegit.get_zone_name("zones/example.org.zone", testzone) dzonegit.get_zone_name("zones/example.org.zone", testzone)
testzone = """ testzone = b"""
$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