-
Notifications
You must be signed in to change notification settings - Fork 56
/
config.py
212 lines (189 loc) · 7.5 KB
/
config.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
import configparser
import getpass
import logging
import os
import re
from pathlib import Path
from typing import NamedTuple, Optional
import requests
import ghstack.logs
Config = NamedTuple(
"Config",
[
# Proxy to use when making connections to GitHub
("proxy", Optional[str]),
# OAuth token to authenticate to GitHub with
("github_oauth", Optional[str]),
# GitHub username; used to namespace branches we create
("github_username", str),
# Token to authenticate to CircleCI with
("circle_token", Optional[str]),
# These config parameters are not used by ghstack, but other
# tools that reuse this module
# Path to working fbsource checkout
("fbsource_path", str),
# Path to working git checkout (ghstack infers your git checkout
# based on CWD)
("github_path", str),
# Path to project directory inside fbsource, to default when
# autodetection fails
("default_project_dir", str),
# GitHub url. Defaults to github.com which is true for all non-enterprise github repos
("github_url", str),
# Name of the upstream remote
("remote_name", str),
],
)
def read_config(
*,
request_circle_token: bool = False,
request_github_token: bool = True,
) -> Config: # noqa: C901
config = configparser.ConfigParser()
config_path = None
current_dir = Path(os.getcwd())
while current_dir != current_dir.parent:
tentative_config_path = "/".join([str(current_dir), ".ghstackrc"])
if os.path.exists(tentative_config_path):
config_path = tentative_config_path
break
current_dir = current_dir.parent
write_back = False
if config_path is None:
config_path = os.path.expanduser("~/.ghstackrc")
write_back = True
logging.debug(f"config_path = {config_path}")
config.read([".ghstackrc", config_path])
if not config.has_section("ghstack"):
config.add_section("ghstack")
write_back = True
if config.has_option("ghstack", "github_url"):
github_url = config.get("ghstack", "github_url")
else:
github_url = input("GitHub enterprise domain (leave blank for OSS GitHub): ")
if not github_url:
github_url = "github.com"
if not re.match(r"[\w\.-]+\.\w+$", github_url):
raise RuntimeError(
f"{github_url} is not a valid domain name (do not include http:// scheme)"
)
config.set("ghstack", "github_url", github_url)
write_back = True
# Environment variable overrides config file
# This envvar is legacy from ghexport days
github_oauth = os.getenv("OAUTH_TOKEN")
if github_oauth is not None:
logging.warning(
"Deprecated OAUTH_TOKEN environment variable used to populate github_oauth--"
"this is probably not what you intended; unset OAUTH_TOKEN from your "
"environment to use the setting in .ghstackrc instead."
)
if github_oauth is None and config.has_option("ghstack", "github_oauth"):
github_oauth = config.get("ghstack", "github_oauth")
if github_oauth is None and request_github_token:
print("Generating GitHub access token...")
CLIENT_ID = "89cc88ca50efbe86907a"
res = requests.post(
f"https://{github_url}/login/device/code",
headers={"Accept": "application/json"},
data={"client_id": CLIENT_ID, "scope": "repo"},
)
data = res.json()
print(f"User verification code: {data['user_code']}")
print("Go to https://github.com/login/device and enter the code.")
print("Once you've authorized ghstack, press any key to continue...")
input()
res = requests.post(
f"https://{github_url}/login/oauth/access_token",
headers={"Accept": "application/json"},
data={
"client_id": CLIENT_ID,
"device_code": data["device_code"],
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
)
github_oauth = res.json()["access_token"]
config.set("ghstack", "github_oauth", github_oauth)
write_back = True
if github_oauth is not None:
ghstack.logs.formatter.redact(github_oauth, "<GITHUB_OAUTH>")
circle_token = None
if circle_token is None and config.has_option("ghstack", "circle_token"):
circle_token = config.get("ghstack", "circle_token")
if circle_token is None and request_circle_token:
circle_token = getpass.getpass(
"CircleCI Personal API token (make one at "
"https://circleci.com/account/api ): "
).strip()
config.set("ghstack", "circle_token", circle_token)
write_back = True
if circle_token is not None:
ghstack.logs.formatter.redact(circle_token, "<CIRCLE_TOKEN>")
github_username = None
if config.has_option("ghstack", "github_username"):
github_username = config.get("ghstack", "github_username")
if github_username is None and github_oauth is not None:
request_url: str
if github_url == "github.com":
request_url = f"https://api.{github_url}/user"
else:
request_url = f"https://{github_url}/api/v3/user"
res = requests.get(
request_url,
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {github_oauth}",
"X-GitHub-Api-Version": "2022-11-28",
},
)
res.raise_for_status()
github_username = res.json()["login"]
config.set("ghstack", "github_username", github_username)
write_back = True
if github_username is None:
github_username = input("GitHub username: ")
if not re.match(
r"^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$", github_username, re.I
):
raise RuntimeError(
"{} is not a valid GitHub username".format(github_username)
)
config.set("ghstack", "github_username", github_username)
write_back = True
proxy = None
if config.has_option("ghstack", "proxy"):
proxy = config.get("ghstack", "proxy")
if config.has_option("ghstack", "fbsource_path"):
fbsource_path = config.get("ghstack", "fbsource_path")
else:
fbsource_path = os.path.expanduser("~/local/fbsource")
if config.has_option("ghstack", "github_path"):
github_path = config.get("ghstack", "github_path")
else:
github_path = os.path.expanduser("~/local/ghstack-pytorch")
if config.has_option("ghstack", "default_project"):
default_project_dir = config.get("ghstack", "default_project_dir")
else:
default_project_dir = "fbcode/caffe2"
if config.has_option("ghstack", "remote_name"):
remote_name = config.get("ghstack", "remote_name")
else:
remote_name = "origin"
if write_back:
with open(config_path, "w") as f:
config.write(f)
logging.info("NB: configuration saved to {}".format(config_path))
conf = Config(
github_oauth=github_oauth,
circle_token=circle_token,
github_username=github_username,
proxy=proxy,
fbsource_path=fbsource_path,
github_path=github_path,
default_project_dir=default_project_dir,
github_url=github_url,
remote_name=remote_name,
)
logging.debug(f"conf = {conf}")
return conf