~cytrogen/srht-deploy

srht-deploy/todo-custom/compat_oauth.py -rw-r--r-- 2.5 KiB
6b7b1351 — Cytrogen 修复 README 里的 Markdown 格式错误 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Compatibility patches for core.sr.ht 0.78.6 + todo.sr.ht 0.77.5.

core.sr.ht 0.78.6 refactored the OAuth API:
- AbstractOAuthService removed, replaced by OAuthService (different signature)
- DelegatedScope removed, replaced by OAuthScope (different signature)
- SrhtFlask no longer accepts oauth_service=, uses user_class= instead
"""
import glob

# 1. Patch srht/oauth/__init__.py: add compat aliases
OAUTH_PATCH = '''

# --- compat shim for todo.sr.ht 0.77.5 ---
class AbstractOAuthService(OAuthService):
    """Compat wrapper: old API accepted (client_id, client_secret, delegated_scopes=, token_class=, user_class=)"""
    def __init__(self, client_id=None, client_secret=None,
                 delegated_scopes=None, token_class=None, user_class=None, **kwargs):
        super().__init__("todo.sr.ht",
                         user_class=user_class,
                         oauthtoken_class=token_class)
        self.delegated_scopes = delegated_scopes or []

class DelegatedScope:
    def __init__(self, name, description=None, write=False):
        self.name = name
        self.description = description
        self.write = write
# --- end compat shim ---
'''

for f in glob.glob("/usr/lib/python3.*/site-packages/srht/oauth/__init__.py"):
    with open(f) as fh:
        content = fh.read()
    if "DelegatedScope" not in content:
        with open(f, "a") as fh:
            fh.write(OAUTH_PATCH)
        print(f"Patched {f}: added AbstractOAuthService + DelegatedScope compat")
    else:
        print(f"Already patched: {f}")

# 2. Patch todosrht/flask.py: use new SrhtFlask API
#    Old: super().__init__("todo.sr.ht", __name__, oauth_service=TodoOAuthService())
#    New: super().__init__("todo.sr.ht", __name__, user_class=User, legacy_oauthtoken_class=OAuthToken)
for f in glob.glob("/usr/lib/python3.*/site-packages/todosrht/flask.py"):
    with open(f) as fh:
        content = fh.read()
    if "oauth_service=TodoOAuthService()" in content:
        content = content.replace(
            "oauth_service=TodoOAuthService())",
            "user_class=User, legacy_oauthtoken_class=OAuthToken)"
        )
        # Add OAuthToken import
        content = content.replace(
            "from todosrht.types import TicketAccess, TicketStatus, TicketResolution, User",
            "from todosrht.types import TicketAccess, TicketStatus, TicketResolution, User, OAuthToken"
        )
        with open(f, "w") as fh:
            fh.write(content)
        print(f"Patched {f}: switched to new SrhtFlask API")
    else:
        print(f"Already patched: {f}")