-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.py
More file actions
98 lines (75 loc) · 2.77 KB
/
Copy pathupdate.py
File metadata and controls
98 lines (75 loc) · 2.77 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
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
import glob
import json
import os
from os.path import join
import requests
from pathlib import Path
_ = join
def get_file_infos(project_id, secret, base_url="https://paratranz.cn"):
# https://paratranz.cn/api/projects/76/files
# GET
url = "{}/api/projects/{}/files".format(base_url, project_id)
headers = {'Authorization': secret}
response = json.loads(requests.get(url, headers=headers).text)
result = {}
for record in response:
result[record["name"]] = record["id"]
return result
def update_old_file(file_id, source_path: Path, project_id, secret, base_url="https://paratranz.cn"):
# https://paratranz.cn/api/projects/76/files
# POST
# arg1 : file : binary
# arg2 : path: /clausewitz/text_utils
# Content-Type: multipart/form-data;
# Content-Length: 751
file_name = source_path.name
file_data_binary = open(source_path, 'rb').read()
files = {
'file': (file_name, file_data_binary, 'application/json; charset=utf-8')
}
url = "{}/api/projects/{}/files/{}".format(base_url, project_id, file_id)
headers = {'Authorization': secret}
response = requests.post(url, files=files, headers=headers)
print("update.yml")
print(response)
def update_new_file(base_path: Path, source_path: Path, project_id, secret, base_url="https://paratranz.cn"):
# https://paratranz.cn/api/projects/76/files
# POST
# arg1 : file : binary
# arg2 : path: /clausewitz/text_utils
# Content-Type: multipart/form-data;
# Content-Length: 751
file_name = source_path.name
file_data_binary = open(source_path, 'rb').read()
data = {'path': str(Path("/").joinpath(source_path.relative_to(base_path).parent)).replace("\\", "/")}
files = {
'file': (file_name, file_data_binary, 'application/yaml; charset=utf-8')
}
url = "{}/api/projects/{}/files".format(base_url, project_id)
headers = {'Authorization': secret}
response = requests.post(url, files=files, data=data, headers=headers)
print("new")
print(response)
def main():
secret = os.environ.get("PARATRANZ_SECRET")
project_id = 76
name2id = get_file_infos(secret=secret, project_id=project_id)
ext_path = Path("extract")
for f in ext_path.glob("**/*.yml"):
print(f)
pure = str(f.relative_to(ext_path)).replace("\\", "/")
if pure not in name2id:
update_new_file(
base_path=ext_path,
source_path=f,
secret=secret,
project_id=project_id)
else:
update_old_file(
file_id=name2id[pure],
source_path=f,
secret=secret,
project_id=project_id)
if __name__ == "__main__":
# execute only if run as a script
main()