2021-04-22 00:21:46 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import difflib
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2022-04-28 23:17:34 +02:00
|
|
|
from typing import Optional
|
2021-04-22 00:21:46 +02:00
|
|
|
|
2021-12-07 23:45:37 +01:00
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2021-04-22 00:21:46 +02:00
|
|
|
sys.path.append(BASE_DIR)
|
|
|
|
from scripts.lib.setup_path import setup_path
|
|
|
|
|
|
|
|
setup_path()
|
|
|
|
|
|
|
|
|
|
|
|
current_path = "/etc/zulip/settings.py"
|
|
|
|
if len(sys.argv) >= 2:
|
|
|
|
current_path = sys.argv[1]
|
|
|
|
|
|
|
|
print(f"Reading current configuration from {current_path}...")
|
2021-08-02 23:19:49 +02:00
|
|
|
with open(current_path) as f:
|
2021-04-22 00:21:46 +02:00
|
|
|
current_contents = f.read()
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
print("Fetching old versions of setting templates from Github...")
|
|
|
|
templ = {}
|
2022-04-28 23:25:41 +02:00
|
|
|
url: Optional[str] = "https://api.github.com/repos/zulip/zulip/tags?per_page=100"
|
2022-04-28 23:17:34 +02:00
|
|
|
|
|
|
|
while url is not None:
|
|
|
|
resp = requests.get(url)
|
|
|
|
if resp.status_code != 200:
|
|
|
|
print(resp.content)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
url = resp.links.get("next", {}).get("url")
|
|
|
|
for tag in [t["name"] for t in resp.json()]:
|
2022-04-28 23:18:43 +02:00
|
|
|
if not re.match(r"^\d+\.\d+(\.\d+)?$", tag):
|
|
|
|
continue
|
|
|
|
|
2022-04-28 23:19:18 +02:00
|
|
|
filename = "zproject/prod_settings_template.py"
|
|
|
|
if [int(x) for x in tag.split(".")] < [1, 4]:
|
|
|
|
filename = "zproject/local_settings_template.py"
|
|
|
|
|
2022-04-28 23:18:43 +02:00
|
|
|
print(f" - {tag}")
|
|
|
|
resp = requests.get(
|
2022-04-28 23:19:18 +02:00
|
|
|
f"https://raw.githubusercontent.com/zulip/zulip/{tag}/{filename}",
|
2022-04-28 23:18:43 +02:00
|
|
|
)
|
|
|
|
if resp.status_code == 200:
|
|
|
|
templ[tag] = resp.content.decode()
|
|
|
|
else:
|
|
|
|
print("Failure: ")
|
|
|
|
print(resp)
|
2021-04-22 00:21:46 +02:00
|
|
|
|
|
|
|
print("Computing minimal difference...")
|
|
|
|
sequence_matchers = {}
|
|
|
|
for version, contents in templ.items():
|
|
|
|
sm = difflib.SequenceMatcher(None, current_contents, contents)
|
|
|
|
sequence_matchers[version] = sm
|
|
|
|
|
|
|
|
likely = sorted(sequence_matchers.keys(), key=lambda v: sequence_matchers[v].ratio(), reverse=True)
|
|
|
|
print(f"Most likely version: {likely[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
diff = difflib.unified_diff(
|
|
|
|
templ[likely[0]].splitlines(keepends=True),
|
|
|
|
current_contents.splitlines(keepends=True),
|
|
|
|
fromfile="zulip-server-{version}/zproject/prod_settings_template.py",
|
|
|
|
tofile=current_path,
|
|
|
|
)
|
|
|
|
|
|
|
|
print()
|
|
|
|
print("".join(diff), end="")
|