-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.py
298 lines (258 loc) · 10.4 KB
/
submit.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# coding=utf-8
"""
This script try to submit the zipped WSL app to Windows Store
Using the [API](https://learn.microsoft.com/en-us/windows/uwp/monetize/manage-app-submissions)
"""
import argparse
import datetime
import json
import logging
import os
import sys
import time
from string import Template
from typing import Optional
import humanize
import requests
from azure.storage.blob import BlobClient
# setup logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
fmter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s: %(message)s")
ch.setFormatter(fmt=fmter)
logger.addHandler(ch)
REQUEST_TIMEOUT = 60
class SubmitPackage:
"""
submit the wsl app
"""
def __init__(self, tenantId: str, clientId: str, clientSecret: str) -> None:
self.baseurl = tokenResource = "https://manage.devcenter.microsoft.com"
tokenRequestBody = (
f"grant_type=client_credentials&client_id={clientId}&"
f"client_secret={clientSecret}&resource={tokenResource}"
)
tokenHeaders = {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
}
tokenResponse = requests.post(
f"https://login.microsoftonline.com/{tenantId}/oauth2/token",
data=tokenRequestBody,
headers=tokenHeaders,
timeout=REQUEST_TIMEOUT,
)
logger.info(
f"get access token status: {tokenResponse.status_code} {tokenResponse.reason}"
)
tokenJson = tokenResponse.json()
self._acess_token = tokenJson["access_token"]
self._init = True
def make_submit_body(self, metapath: str, templatepath: str):
with open(metapath, encoding="utf-8") as meta:
metadata = json.loads(meta.read())
metadata["year"] = datetime.date.today().year
with open(templatepath, encoding="utf-8") as template:
content = "".join(template.readlines())
t = Template(content)
config = t.substitute(metadata)
return json.loads(config)
def get_app_info(self, release: str):
if not self._init:
sys.exit(1)
headers = {
"Authorization": "Bearer " + self._acess_token,
"Content-type": "application/json",
"User-Agent": "Python",
}
api = f"/v1.0/my/applications"
appResponse = requests.get(
f"{self.baseurl}{api}", headers=headers, timeout=REQUEST_TIMEOUT
)
logger.info(
f"get {api} to get app info, status: {appResponse.status_code} {appResponse.reason}"
)
# Log correlation ID
logger.info(appResponse.headers["MS-CorrelationId"])
data = appResponse.json()
for app in data["value"]:
if app["primaryName"] == f"openEuler {release}":
return app
raise ValueError(f"no {release} found")
def delete_exist_submission(self, submissionToRemove: str):
if not self._init:
sys.exit(1)
headers = {
"Authorization": "Bearer " + self._acess_token,
"Content-type": "application/json",
"User-Agent": "Python",
}
api = f"/v1.0/my/{submissionToRemove}"
deleteSubmissionResponse = requests.delete(
f"{self.baseurl}{api}", headers=headers, timeout=REQUEST_TIMEOUT
)
logger.info(
f"delete {api} to delete pending submit, status: {deleteSubmissionResponse.status_code} {deleteSubmissionResponse.reason}",
)
# Log correlation ID
logger.info(deleteSubmissionResponse.headers["MS-CorrelationId"])
def create_submit(
self, applicationId: str, appSubmissionRequestJson, zipFilePath: str
):
"""
Do the submit process:
1. create a submission
2. fill the submission metadata
3. upload blob file
4. commit the submission
5. wait the submission accepted/denied by server
"""
headers = {
"Authorization": "Bearer " + self._acess_token,
"Content-type": "application/json",
"User-Agent": "Python",
}
api = f"/v1.0/my/applications/{applicationId}/submissions"
# Create submission
createSubmissionResponse = requests.post(
f"{self.baseurl}{api}", headers=headers, timeout=REQUEST_TIMEOUT
)
logger.info(
f"post {api} to create submission, status: {createSubmissionResponse.status_code} {createSubmissionResponse.reason}"
)
# Log correlation ID
logger.info(createSubmissionResponse.headers["MS-CorrelationId"])
submissionJsonObject = createSubmissionResponse.json()
if createSubmissionResponse.status_code >= 400:
logger.error(submissionJsonObject)
sys.exit(1)
submissionId = submissionJsonObject["id"]
fileUploadUrl = submissionJsonObject["fileUploadUrl"]
if len(submissionJsonObject["applicationPackages"]) > 0:
for package in submissionJsonObject["applicationPackages"]:
logger.info(
f"package {package['fileName']} {package['fileStatus']} {package['version']} will be replaced"
)
package["fileStatus"] = "PendingDelete"
appSubmissionRequestJson["applicationPackages"].extend(
submissionJsonObject["applicationPackages"]
)
api = f"/v1.0/my/applications/{applicationId}/submissions/{submissionId}"
# Update submission
updateSubmissionResponse = requests.put(
f"{self.baseurl}{api}",
json.dumps(appSubmissionRequestJson).encode("utf-8"),
headers=headers,
timeout=REQUEST_TIMEOUT,
)
logger.info(
f"put {api} to update submission, status: {updateSubmissionResponse.status_code} {updateSubmissionResponse.reason}"
)
# Log correlation ID
logger.info(updateSubmissionResponse.headers["MS-CorrelationId"])
updateSubmissionObject = updateSubmissionResponse.json()
if updateSubmissionResponse.status_code >= 400:
logger.error(updateSubmissionObject)
sys.exit(1)
size = os.stat(zipFilePath).st_size
logger.info(
f"begin to upload zip file({humanize.naturalsize(size)}): {zipFilePath} to {fileUploadUrl}"
)
# Upload images and packages in a zip file.
blob_client = BlobClient.from_blob_url(fileUploadUrl)
with open(zipFilePath, "rb") as data:
blob_client.upload_blob(
data, blob_type="BlockBlob", progress_hook=progress, length=size
)
logger.info("upload end")
api = f"/v1.0/my/applications/{applicationId}/submissions/{submissionId}/commit"
# Commit submission
commitResponse = requests.post(
f"{self.baseurl}{api}", headers=headers, timeout=REQUEST_TIMEOUT
)
logger.info(
f"post {api} to commit submission, status: {commitResponse.status_code} {commitResponse.reason}"
)
# Log correlation ID
logger.info(commitResponse.headers["MS-CorrelationId"])
commitResponseObject = commitResponse.json()
if commitResponse.status_code >= 400:
logger.error(commitResponseObject)
sys.exit(1)
api = f"/v1.0/my/applications/{applicationId}/submissions/{submissionId}/status"
# Pull submission status until commit process is completed
getSubmissionStatusResponse = requests.get(
f"{self.baseurl}{api}", headers=headers, timeout=REQUEST_TIMEOUT
)
submissionJsonObject = getSubmissionStatusResponse.json()
while submissionJsonObject["status"] == "CommitStarted":
time.sleep(60)
getSubmissionStatusResponse = requests.get(
f"{self.baseurl}{api}", headers=headers, timeout=REQUEST_TIMEOUT
)
submissionJsonObject = getSubmissionStatusResponse.json()
logger.info(
f"get {api} to grab commit status: {submissionJsonObject['status']}"
)
exitCode = 0
logger.info(
f"get commit status finished, final status: {submissionJsonObject['status']}"
)
if submissionJsonObject["statusDetails"]["errors"]:
logger.error(submissionJsonObject)
exitCode = 1
return exitCode
def progress(current: int, total: Optional[int]):
t = total if total else "0"
logger.info(f"{humanize.naturalsize(current)}/{humanize.naturalsize(t)}")
def init_parser():
new_parser = argparse.ArgumentParser(
prog="submit.py",
description="automate create a UWP app submission",
)
new_parser.add_argument("-c", "--client_id", help="azure AD applicaion client id")
new_parser.add_argument("-t", "--tenant_id", help="azure AD user id")
new_parser.add_argument(
"-k", "--client_secret", help="azure AD application key secret"
)
new_parser.add_argument("-r", "--release", help="release number")
new_parser.add_argument(
"-m",
"--meta",
help="submission request template file, default to ./meta/{RELEASE}/meta.json",
)
new_parser.add_argument(
"--template",
default="template.json",
help="submission request template, default to ./template.json",
)
new_parser.add_argument(
"-f",
"--zipfile",
help="zip file path which contains intro images name as `release` and a appxupload bundle",
)
return new_parser
if __name__ == "__main__":
parser = init_parser()
args = parser.parse_args()
if (
not args.client_id
or not args.tenant_id
or not args.client_secret
or not args.release
or not args.zipfile
):
parser.print_help()
sys.exit(1)
if not args.meta:
args.meta = f"meta/{args.release}/meta.json"
sp = SubmitPackage(args.tenant_id, args.client_id, args.client_secret)
release_data = sp.get_app_info(args.release)
if release_data and "pendingApplicationSubmission" in release_data:
submissionToRemove = release_data["pendingApplicationSubmission"][
"resourceLocation"
]
sp.delete_exist_submission(submissionToRemove)
requestBody = sp.make_submit_body(args.meta, args.template)
sys.exit(sp.create_submit(release_data["id"], requestBody, args.zipfile))