-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpublish.py
143 lines (125 loc) · 5.42 KB
/
publish.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
import pwd
import subprocess
import time
from pathlib import Path
import zipfile
import os
import oss2
import shutil
def path_is_parent(parent_path, child_path):
parent_path = os.path.abspath(parent_path)
child_path = os.path.abspath(child_path)
return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
def is_ignored(target_path, ignores):
for ignore in ignores:
if os.path.relpath(target_path, ignore) == '.' or path_is_parent(ignore, target_path):
return True
return False
def zip_file(workspace, eve_app):
os.chdir('%s/%s/src' % (workspace, eve_app))
if os.path.isfile('README.md'):
shutil.copy('README.md', 'code')
elif os.path.isfile('readme.md'):
shutil.copy('readme.md', 'code')
elif os.path.isfile('Readme.md'):
shutil.copy('Readme.md', 'code')
os.chdir('%s/%s/src/code' % (workspace, eve_app))
ignore_list = ['./.git', './.github',
'./.idea', './.DS_Store', './.vscode']
with zipfile.ZipFile('code.zip', mode="w") as f:
for dirpath, dirnames, filenames in os.walk('./'):
if dirpath != './' and is_ignored(dirpath, ignore_list):
continue
for filename in filenames:
absoult_file_path = os.path.join(dirpath, filename)
if not is_ignored(absoult_file_path, ignore_list) and "code.zip" not in filename:
f.write(os.path.join(dirpath, filename))
return 'code.zip'
def zip_golang_binary(workspace, eve_app):
os.chdir('%s/%s/src' % (workspace, eve_app))
if os.path.isfile('README.md'):
shutil.copy('README.md', 'code/target')
elif os.path.isfile('readme.md'):
shutil.copy('readme.md', 'code/target')
elif os.path.isfile('Readme.md'):
shutil.copy('Readme.md', 'code/target')
os.chdir('%s/%s/src/code/target' % (workspace, eve_app))
ignore_list = ['./.git', './.github',
'./.idea', './.DS_Store', './.vscode']
with zipfile.ZipFile('code.zip', mode="w") as f:
for dirpath, dirnames, filenames in os.walk('./'):
if dirpath != './' and is_ignored(dirpath, ignore_list):
continue
for filename in filenames:
absoult_file_path = os.path.join(dirpath, filename)
if not is_ignored(absoult_file_path, ignore_list) and "code.zip" not in filename:
f.write(os.path.join(dirpath, filename))
return 'code.zip'
def upload_oss(code_name, zip_file):
auth = oss2.Auth(os.environ.get('AccessKeyId'),
os.environ.get('AccessKeySecret'))
bucket = oss2.Bucket(auth, os.environ.get(
'ArtifactEndpoint'), os.environ.get('ArtifactBucket'))
with open(zip_file, 'rb') as fileobj:
object_name = '%s/code.zip' % (code_name)
bucket.put_object(object_name, fileobj)
workspace = os.getcwd()
with open('update.list') as f:
publish_list = [eve_app.strip() for eve_app in f.readlines()]
failed_registry = []
failed_oss = []
for eve_app in publish_list:
times = 1
os.chdir(workspace)
try:
while times <= 3:
print("----------------------: ", eve_app)
# publish app to registry
publish_script = 'https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py'
command = 'cd %s && wget %s && python hub-publish.py' % (
eve_app, publish_script)
child = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, )
stdout, stderr = child.communicate()
if child.returncode == 0:
print("stdout:", stdout.decode("utf-8"))
break
else:
print("stdout:", stdout.decode("utf-8"))
print("stderr:", stderr.decode("utf-8"))
time.sleep(3)
if times == 3:
raise ChildProcessError(stderr)
times = times + 1
except Exception as e:
print('Failed to publish registry, app %s, err: %s' % (eve_app, e))
failed_registry.append(eve_app)
# publish code.zip to oss
os.chdir(workspace)
try:
makefile = Path('%s/src/Makefile' % (eve_app))
if makefile.is_file():
print("----------------------Makefile: ", makefile)
command = 'cd %s/src && make release' % (eve_app)
print(command)
child = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, )
stdout, stderr = child.communicate()
print("stdout:", stdout.decode("utf-8"))
if child.returncode != 0:
print("stderr:", stderr.decode("utf-8"))
raise ChildProcessError(stderr)
jarPath = '%s/%s/src/code/target/code.jar' % (workspace, eve_app)
golangBinaryPath = '%s/%s/src/code/target/main' % (workspace, eve_app)
if os.path.isfile(jarPath):
code_zip = jarPath
elif os.path.isfile(golangBinaryPath):
code_zip = zip_golang_binary(workspace, eve_app)
else:
code_zip = zip_file(workspace, eve_app)
upload_oss(eve_app, code_zip)
except Exception as e:
print('Failed to publish oss, app %s, err: %s' % (eve_app, e))
failed_oss.append(eve_app)
print('Failed registry list: ', failed_registry)
print('Failed oss list: ', failed_oss)