-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.py
149 lines (122 loc) · 5.04 KB
/
upload.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
import os
import pathlib
import sys
from urllib.parse import urljoin
import requests
MY_DIR = os.path.dirname(os.path.abspath(__file__))
WSGI_FILE_TEMPLATE = """
import sys
project_home = {project_home!r}
if project_home not in sys.path:
sys.path = [project_home] + sys.path
from flask_app import app as application # noqa
"""
def main():
username = input("Please enter the PythonAnywhere username: ")
api_token = input("Please enter the API token: ")
region = None
while region not in ("eu", "www"):
region = input("Please enter the region (eu or www): ")
base_api_url = f"https://{region}.pythonanywhere.com/api/v0/user/{username}/"
if region == "eu":
site_hostname = f"{username}.eu.pythonanywhere.com"
else:
site_hostname = f"{username}.pythonanywhere.com"
project_home = f"/home/{username}/mysite"
webapps_url = urljoin(base_api_url, "webapps/")
print(f"Checking if website already exists with GET from {webapps_url}")
resp = requests.get(
webapps_url,
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code != 200:
print(f"Error getting website list: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
sites = [site["domain_name"] for site in resp.json()]
print(f"Found these sites: {sites}")
if site_hostname not in sites:
print(f"Creating website at {site_hostname} with POST to {webapps_url}")
resp = requests.post(
webapps_url,
data={
"domain_name": site_hostname,
"python_version": "python37",
},
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code not in (200, 201):
print(f"Error creating site: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
paths_to_upload = [
"src/flask_app.py",
"src/static/style.css",
"src/templates/index.html"
]
file_upload_url = urljoin(base_api_url, f"files/path")
for path in paths_to_upload:
print(f"Reading {path}")
with open(os.path.join(MY_DIR, path)) as f:
content = f.read()
relative_path = str(pathlib.Path(*pathlib.Path(path).parts[1:]))
remote_path = f"{project_home}/{relative_path}"
upload_url = file_upload_url + remote_path
print(f"Uploading {path} via {upload_url}")
resp = requests.post(
upload_url,
files={"content": content},
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code not in (200, 201):
print(f"Error uploading {path}: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
wsgi_file_filename = site_hostname.replace(".", "_").lower() + "_wsgi.py"
wsgi_file_remote_path = f"/var/www/{wsgi_file_filename}"
wsgi_file_upload_url = file_upload_url + wsgi_file_remote_path
wsgi_file_content = WSGI_FILE_TEMPLATE.format(project_home=project_home)
print(f"Uploading WSGI file via {wsgi_file_upload_url}")
resp = requests.post(
wsgi_file_upload_url,
files={"content": wsgi_file_content},
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code not in (200, 201):
print(f"Error uploading WSGI file: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
our_webapp_url = urljoin(webapps_url, f"{site_hostname}/")
static_file_route_url = urljoin(our_webapp_url, "static_files/")
print(f"Getting existing static file routes with get to {static_file_route_url}")
resp = requests.get(
static_file_route_url,
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code != 200:
print(f"Error getting static file route list: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
static_route_urls = [route["url"] for route in resp.json()]
print(f"Found these route URLs: {static_route_urls}")
if "/static" not in static_route_urls:
print(f"Configuring static file route with post to {static_file_route_url}")
resp = requests.post(
static_file_route_url,
data={
"url": "/static",
"path": f"{project_home}/static",
},
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code not in (200, 201):
print(f"Error creating static file route: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
reload_website_url = urljoin(our_webapp_url, "reload/")
print(f"Reloading website with post to {reload_website_url}")
resp = requests.post(
reload_website_url,
headers={"Authorization": f"Token {api_token}"}
)
if resp.status_code not in (200, 201):
print(f"Error reloading website: status was {resp.status_code}\n{resp.content}")
sys.exit(-1)
site_url = f"https://{site_hostname}/"
print(f"All done! The site is now live at {site_url}")
if __name__ == "__main__":
main()