forked from luogu-dev/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.deploy.py
55 lines (41 loc) · 1.75 KB
/
.deploy.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
#!/usr/bin/env python3
import os
import oss2
import zipfile
import argparse
import tempfile
import re
def build_parser():
parser = argparse.ArgumentParser(description='Luogu deploy script')
parser.add_argument('-p', '--prefix', help='Aliyun OSS Path Prefix')
parser.add_argument('path', help='built path')
return parser
def main():
args = build_parser().parse_args()
oss_auth = oss2.Auth(os.environ['ALIOSS_AK'], os.environ['ALIOSS_SK'])
oss_bucket = oss2.Bucket(oss_auth, os.environ['ALIOSS_ENDPOINT'], os.environ['ALIOSS_BUCKET'])
prefix = (args.prefix.replace('\\', '/').rstrip('/') + '/').lstrip('/')
built_dir = os.path.join(os.getcwd(), args.path)
built_files = set([os.path.join(base[len(built_dir):], file).replace('\\', '/').lstrip('/\\')
for base, _, files in os.walk(built_dir)
for file in files
])
oss_file_set = set([obj.key for obj in oss2.ObjectIterator(oss_bucket, prefix=prefix) if obj.key])
files_to_remove = oss_file_set - set([prefix + file for file in built_files])
print(f'Files to remove: {files_to_remove}')
for file in built_files:
oss_key = prefix + file
if re.search(r'([0-9a-f]{16,}|\.[0-9a-f]{8}\.(js|css)$)', file) and oss_key in oss_file_set:
print(f'Skipping file {file} (hashed)')
continue
full_path = os.path.join(built_dir, *file.split('/'))
print(f'Putting file {file}')
result = oss_bucket.put_object(oss_key, open(full_path, 'rb').read())
if result.status != 200:
raise RuntimeError(result)
print("Removing old files")
for file in files_to_remove:
print("Removing file {}".format(file))
oss_bucket.delete_object(file)
if __name__ == '__main__':
main()