125 lines
3.4 KiB
Python
125 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import os
|
|
import shlex
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def nginx_v_output(nginx_bin):
|
|
proc = subprocess.run(
|
|
[nginx_bin, "-V"],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
check=True,
|
|
)
|
|
return proc.stdout
|
|
|
|
|
|
def parse_nginx_v(output):
|
|
version = ""
|
|
configure_line = ""
|
|
|
|
for line in output.splitlines():
|
|
if line.startswith("nginx version:"):
|
|
version = line.split(":", 1)[1].strip()
|
|
elif line.startswith("configure arguments:"):
|
|
configure_line = line.split(":", 1)[1].strip()
|
|
|
|
if not configure_line:
|
|
raise RuntimeError("nginx -V did not include configure arguments")
|
|
|
|
return version, configure_line, shlex.split(configure_line)
|
|
|
|
|
|
def option_value(args, option):
|
|
prefix = option + "="
|
|
for arg in args:
|
|
if arg.startswith(prefix):
|
|
return arg[len(prefix) :]
|
|
return ""
|
|
|
|
|
|
def rewrite_module_path(arg, nginx_src):
|
|
for option in ("--add-module=", "--add-dynamic-module="):
|
|
if not arg.startswith(option):
|
|
continue
|
|
|
|
module_path = arg[len(option) :]
|
|
if os.path.exists(module_path):
|
|
return arg
|
|
|
|
marker = "/debian/modules/"
|
|
if marker in module_path:
|
|
suffix = module_path.split(marker, 1)[1]
|
|
candidate = os.path.join(nginx_src, "debian", "modules", suffix)
|
|
if os.path.exists(candidate):
|
|
return option + candidate
|
|
|
|
marker = "/debian/"
|
|
if marker in module_path:
|
|
suffix = module_path.split(marker, 1)[1]
|
|
candidate = os.path.join(nginx_src, "debian", suffix)
|
|
if os.path.exists(candidate):
|
|
return option + candidate
|
|
|
|
return arg
|
|
|
|
|
|
def rewrite_configure_args(args, nginx_src):
|
|
if not nginx_src:
|
|
return args
|
|
|
|
return [rewrite_module_path(arg, nginx_src) for arg in args]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--nginx-bin", default="nginx")
|
|
parser.add_argument("--nginx-src", default="")
|
|
parser.add_argument("--json", action="store_true")
|
|
parser.add_argument("--configure-line", action="store_true")
|
|
parser.add_argument("--modules-path", action="store_true")
|
|
args = parser.parse_args()
|
|
|
|
output = nginx_v_output(args.nginx_bin)
|
|
version, configure_line, configure_args = parse_nginx_v(output)
|
|
rewritten_args = rewrite_configure_args(configure_args, args.nginx_src)
|
|
|
|
modules_path = option_value(configure_args, "--modules-path")
|
|
if not modules_path:
|
|
prefix = option_value(configure_args, "--prefix") or "/usr/share/nginx"
|
|
modules_path = os.path.join(prefix, "modules")
|
|
|
|
if args.configure_line:
|
|
print(shlex.join(rewritten_args))
|
|
return
|
|
|
|
if args.modules_path:
|
|
print(modules_path)
|
|
return
|
|
|
|
if args.json:
|
|
json.dump(
|
|
{
|
|
"version": version,
|
|
"configure_line": configure_line,
|
|
"configure_args": configure_args,
|
|
"rewritten_configure_args": rewritten_args,
|
|
"modules_path": modules_path,
|
|
},
|
|
sys.stdout,
|
|
indent=2,
|
|
sort_keys=True,
|
|
)
|
|
sys.stdout.write("\n")
|
|
return
|
|
|
|
parser.error("choose one of --json, --configure-line, or --modules-path")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|