Repository navigation
Expand file tree
/
Copy pathgen_cloud_client.py
More file actions
64 lines (53 loc) · 2.45 KB
/
Copy pathgen_cloud_client.py
File metadata and controls
64 lines (53 loc) · 2.45 KB
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
62
63
64
#!/usr/bin/env python3
"""Generate .cloud-deps/include/wle_cloud_client.h for native Google Drive.
Official builds embed Launchstation's own Google OAuth client ("TVs and Limited
Input devices" type) so users can link Drive with only a device code, without
creating a Google Cloud project. The values come from the environment
(GitHub Actions secrets) and are never committed:
WLE_GOOGLE_CLIENT_ID e.g. 1234-abc.apps.googleusercontent.com
WLE_GOOGLE_CLIENT_SECRET e.g. GOCSPX-...
Google does not treat installed/TV-app client secrets as confidential, but
keeping them out of git keeps forks from silently reusing the project's quota.
When the variables are absent the header defines nothing and users must set a
custom client in WLE-CLOUD.CNF.
"""
import os
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / ".cloud-deps" / "include" / "wle_cloud_client.h"
SAFE = re.compile(r"^[A-Za-z0-9._-]{1,383}$")
def main() -> int:
client_id = os.environ.get("WLE_GOOGLE_CLIENT_ID", "").strip()
client_secret = os.environ.get("WLE_GOOGLE_CLIENT_SECRET", "").strip()
if bool(client_id) != bool(client_secret):
print("WLE_GOOGLE_CLIENT_ID and WLE_GOOGLE_CLIENT_SECRET must be set together "
"(Google's device flow requires the secret).", file=sys.stderr)
return 1
for name, value in (("WLE_GOOGLE_CLIENT_ID", client_id),
("WLE_GOOGLE_CLIENT_SECRET", client_secret)):
if value and not SAFE.match(value):
print(f"{name} contains unexpected characters.", file=sys.stderr)
return 1
if client_id and not client_id.endswith(".apps.googleusercontent.com"):
print("WLE_GOOGLE_CLIENT_ID is not a Google OAuth client ID.", file=sys.stderr)
return 1
lines = [
"/* Generated by scripts/gen_cloud_client.py. Do not commit. */",
"#ifndef WLE_CLOUD_CLIENT_H",
"#define WLE_CLOUD_CLIENT_H",
]
if client_id:
lines += [
f'#define WLE_GOOGLE_CLIENT_ID "{client_id}"',
f'#define WLE_GOOGLE_CLIENT_SECRET "{client_secret}"',
]
lines += ["#endif", ""]
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_text("\n".join(lines), newline="\n")
print("Built-in Google Drive client: " + ("embedded" if client_id else
"NOT configured (set WLE_GOOGLE_CLIENT_ID/SECRET secrets)"))
return 0
if __name__ == "__main__":
sys.exit(main())