mirror of
https://github.com/oskar456/dzonegit.git
synced 2024-05-11 05:55:41 +00:00
Support zone black and whitelists
This commit is contained in:
33
dzonegit.py
33
dzonegit.py
@ -267,7 +267,7 @@ def replace_serial(path, oldserial, newserial):
|
|||||||
path.write_text(updated)
|
path.write_text(updated)
|
||||||
|
|
||||||
|
|
||||||
def template_config(checkoutpath, template):
|
def template_config(checkoutpath, template, blacklist=set(), whitelist=set()):
|
||||||
""" Recursively find all *.zone files and template config file using
|
""" Recursively find all *.zone files and template config file using
|
||||||
a simple JSON based template like this:
|
a simple JSON based template like this:
|
||||||
|
|
||||||
@ -300,6 +300,18 @@ def template_config(checkoutpath, template):
|
|||||||
out.append(headertpl.substitute(mapping))
|
out.append(headertpl.substitute(mapping))
|
||||||
for f in sorted(Path(checkoutpath).glob("**/*.zone")):
|
for f in sorted(Path(checkoutpath).glob("**/*.zone")):
|
||||||
zonename = get_zone_name(f, f.read_bytes())
|
zonename = get_zone_name(f, f.read_bytes())
|
||||||
|
if whitelist and zonename not in whitelist:
|
||||||
|
print(
|
||||||
|
"WARNING: Ignoring zone {} - not whitelisted for "
|
||||||
|
"this repository.".format(zonename),
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
if zonename in blacklist:
|
||||||
|
print(
|
||||||
|
"WARNING: Ignoring zone {} - blacklisted for "
|
||||||
|
"this repository.".format(zonename),
|
||||||
|
)
|
||||||
|
continue
|
||||||
if zonename in zones:
|
if zonename in zones:
|
||||||
print(
|
print(
|
||||||
"WARNING: Duplicate zone file found for zone {}. "
|
"WARNING: Duplicate zone file found for zone {}. "
|
||||||
@ -319,6 +331,16 @@ def template_config(checkoutpath, template):
|
|||||||
return "\n".join(out)
|
return "\n".join(out)
|
||||||
|
|
||||||
|
|
||||||
|
def load_set_file(path):
|
||||||
|
if path is None:
|
||||||
|
return set()
|
||||||
|
with open(path) as inf:
|
||||||
|
return {
|
||||||
|
l.strip() for l in inf
|
||||||
|
if not l.strip().startswith("#") and len(l) > 1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def do_commit_checks(against, revision=None, autoupdate_serial=False):
|
def do_commit_checks(against, revision=None, autoupdate_serial=False):
|
||||||
try:
|
try:
|
||||||
if not get_config("dzonegit.ignorewhitespaceerrors", bool):
|
if not get_config("dzonegit.ignorewhitespaceerrors", bool):
|
||||||
@ -378,6 +400,8 @@ def post_receive(stdin=sys.stdin):
|
|||||||
"""
|
"""
|
||||||
suffixes = list(str(n) if n else "" for n in range(10))
|
suffixes = list(str(n) if n else "" for n in range(10))
|
||||||
checkoutpath = get_config("dzonegit.checkoutpath")
|
checkoutpath = get_config("dzonegit.checkoutpath")
|
||||||
|
blacklist = load_set_file(get_config("dzonegit.zoneblacklist"))
|
||||||
|
whitelist = load_set_file(get_config("dzonegit.zonewhitelist"))
|
||||||
if checkoutpath:
|
if checkoutpath:
|
||||||
print("Checking out repository into {}…".format(checkoutpath))
|
print("Checking out repository into {}…".format(checkoutpath))
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
@ -392,7 +416,12 @@ def post_receive(stdin=sys.stdin):
|
|||||||
continue
|
continue
|
||||||
print("Templating config file {}…".format(cfpath))
|
print("Templating config file {}…".format(cfpath))
|
||||||
Path(cfpath).write_text(
|
Path(cfpath).write_text(
|
||||||
template_config(checkoutpath, Path(tplpath).read_text()),
|
template_config(
|
||||||
|
checkoutpath,
|
||||||
|
Path(tplpath).read_text(),
|
||||||
|
blacklist=blacklist,
|
||||||
|
whitelist=whitelist,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
if stdin.isatty():
|
if stdin.isatty():
|
||||||
|
@ -293,3 +293,15 @@ def test_template_config(git_dir):
|
|||||||
assert output.startswith("# Managed by dzonegit")
|
assert output.startswith("# Managed by dzonegit")
|
||||||
assert " - zone: \"dummy\"\n file: \"" in output
|
assert " - zone: \"dummy\"\n file: \"" in output
|
||||||
assert output.endswith("# This is the end")
|
assert output.endswith("# This is the end")
|
||||||
|
output = dzonegit.template_config(
|
||||||
|
str(git_dir),
|
||||||
|
template,
|
||||||
|
whitelist=set("a"),
|
||||||
|
)
|
||||||
|
assert " - zone: \"dummy\"\n file: \"" not in output
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_set_file(git_dir):
|
||||||
|
git_dir.join("dummy").write("dummy\n\n # Comment")
|
||||||
|
s = dzonegit.load_set_file("dummy")
|
||||||
|
assert s == {"dummy"}
|
||||||
|
Reference in New Issue
Block a user