-
Notifications
You must be signed in to change notification settings - Fork 1
/
ci_update_session_record_cs.py
74 lines (57 loc) · 2.52 KB
/
ci_update_session_record_cs.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
import base64
import os
import re
import logging
# SDK_REPO = 'c:/github/azure-sdk-for-java/sdk/resourcemanager'
SDK_REPO = 'c:/github/azure-libraries-for-net'
RESOURCE_PROVIDER = 'Microsoft.Resources'
VERSION_CHANGES = {
'2019-08-01': '2021-01-01'
}
def main():
logging.basicConfig(level=logging.INFO)
process_session_records()
def process_session_records():
for root, dirs, files in os.walk(SDK_REPO):
for name in files:
filepath = os.path.join(root, name)
if os.path.splitext(filepath)[1] == '.json' \
and 'SessionRecords' in filepath and 'netcoreapp2.0' not in filepath:
update(filepath)
def update(filepath: str):
with open(filepath, encoding='utf-8') as f:
lines = f.readlines()
modified = False
uri = None
out_lines = []
for line in lines:
if '"RequestUri"' in line and f'/providers/{RESOURCE_PROVIDER}/' in line:
for before, after in VERSION_CHANGES.items():
if f'api-version={before}' in line:
modified = True
line = line.replace(f'api-version={before}', f'api-version={after}')
elif RESOURCE_PROVIDER == 'Microsoft.Resources':
# resource groups
if re.match(r' {6}"RequestUri": "/subscriptions/[-a-z0-9]*/resource[Gg]roups/[-\w._()]+\?api-version=[-0-9]+",', line) \
or re.match(r' {6}"RequestUri": "/subscriptions/[-a-z0-9]*/resource[Gg]roups\?api-version=[-0-9]+",', line)\
or re.match(r' {6}"RequestUri": "/subscriptions/[-a-z0-9]*/resource[Gg]roups/[-\w._()]+/resources\?api-version=[-0-9]+",', line):
for before, after in VERSION_CHANGES.items():
if f'api-version={before}' in line:
modified = True
line = line.replace(f'api-version={before}', f'api-version={after}')
if '"RequestUri"' in line:
m = re.search(' {6}"RequestUri": "(.+?)",\n', line)
if m:
uri = m.group(1)
else:
if uri and '"EncodedRequestUri"' in line:
encoded_uri = str(base64.b64encode(uri.encode('utf-8')), 'utf-8')
line = f' "EncodedRequestUri": "{encoded_uri}",\n'
modified = True
uri = None
out_lines.append(line)
if modified:
logging.info(f'update session record {filepath}')
with open(filepath, 'w', encoding='utf-8') as f:
f.write(''.join(out_lines))
main()