forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appveyor_env.py
203 lines (170 loc) · 5.52 KB
/
appveyor_env.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
# -*- coding: utf-8 -*-
import sys
import re
import os
def getURLJoinFunc():
# for python 2
try:
import urlparse
def joinFunc2(base, path):
if not base.endswith('/'):
base = base + '/'
return urlparse.urljoin(base, path)
return joinFunc2
except ImportError:
pass
# for python 3
try:
import urllib.parse
def joinFunc3(base, path):
if not base.endswith('/'):
base = base + '/'
return urllib.parse.urljoin(base, path)
return joinFunc3
except ImportError:
pass
raise Exception
class AppveyorEnv():
keysEnv = [
"APPVEYOR_BUILD_FOLDER",
"APPVEYOR_REPO_PROVIDER", # "gitHub" or etc
"APPVEYOR_REPO_NAME", # ex. "sakura-editor/sakura"
"APPVEYOR_REPO_COMMIT",
"APPVEYOR_REPO_TAG_NAME",
"APPVEYOR_PULL_REQUEST_NUMBER",
"APPVEYOR_PULL_REQUEST_HEAD_COMMIT",
"APPVEYOR_URL",
"APPVEYOR_ACCOUNT_NAME",
"APPVEYOR_PROJECT_SLUG",
"APPVEYOR_BUILD_VERSION",
"APPVEYOR_BUILD_NUMBER",
]
def __init__(self):
self.prefixGitHub = "https://github.com"
self.env = {}
self.var = {}
self.gitHubCommitURL = ""
self.gitHubCommitURLPRHead = ""
self.joinFunc = getURLJoinFunc()
# 必要な環境変数をクラスの変数に保存
for key in self.keysEnv:
if key in os.environ:
self.env[key] = os.environ[key]
# set APPVEYOR_SHORTHASH_PR_HEAD=%APPVEYOR_PULL_REQUEST_HEAD_COMMIT:~0,8%
if "APPVEYOR_PULL_REQUEST_HEAD_COMMIT" in self.env:
commitHashPRHead = self.env["APPVEYOR_PULL_REQUEST_HEAD_COMMIT"]
if "APPVEYOR_URL" in self.env and "APPVEYOR_ACCOUNT_NAME" in self.env:
if "APPVEYOR_PROJECT_SLUG" in self.env and "APPVEYOR_BUILD_VERSION" in self.env:
temp = [
self.env["APPVEYOR_URL"],
"project",
self.env["APPVEYOR_ACCOUNT_NAME"],
self.env["APPVEYOR_PROJECT_SLUG"],
"build",
self.env["APPVEYOR_BUILD_VERSION"],
]
self.var["APPVEYOR_BUILD_URL"] = '/'.join(temp)
if "APPVEYOR_REPO_PROVIDER" in self.env:
if self.env["APPVEYOR_REPO_PROVIDER"] == "gitHub":
if "APPVEYOR_REPO_NAME" in self.env:
# GitHub のプロジェクト URL 作成
temp = [
self.prefixGitHub,
self.env["APPVEYOR_REPO_NAME"],
]
repoURL = '/'.join(temp)
# appveyor の commit Hash
if "APPVEYOR_REPO_COMMIT" in self.env:
# GITHUB_COMMIT_URL
temp = [
repoURL,
"commit",
self.env["APPVEYOR_REPO_COMMIT"],
]
self.var["GITHUB_COMMIT_URL"] = '/'.join(temp)
# GITHUB_BLOB_ROOT_URL
temp = [
repoURL,
"blob",
self.env["APPVEYOR_REPO_COMMIT"],
]
self.var["GITHUB_BLOB_ROOT_URL"] = '/'.join(temp)
# appveyor の PR の最新の commit Hash
if "APPVEYOR_PULL_REQUEST_NUMBER" in self.env and "APPVEYOR_PULL_REQUEST_HEAD_COMMIT" in self.env:
# GITHUB_PR_HEAD_URL
temp = [
repoURL,
"pull",
self.env["APPVEYOR_PULL_REQUEST_NUMBER"],
"commits",
self.env["APPVEYOR_PULL_REQUEST_HEAD_COMMIT"],
]
self.var["GITHUB_PR_HEAD_URL"] = '/'.join(temp)
# msbuild のログに現れる小文字に変換されてしまったファイルパスを
# もともとの大文字小文字を保存したパスに変換する。
# os.walk() でファイルシステムを走査して、大文字小文字を区別しないで
# 比較して一致したらそのパスを返す。
def convertRealPath(self, path):
pathLower = path.lower()
dirname = os.path.dirname(path)
for rootdir, dirs, files in os.walk(dirname):
for fileName in files:
work_path = os.path.join(rootdir, fileName)
if work_path.lower() == pathLower:
return work_path
return path
def getBlobURL(self, path):
if "APPVEYOR_BUILD_FOLDER" in self.env:
relpath = os.path.relpath(path, self.env["APPVEYOR_BUILD_FOLDER"])
else:
relpath = path
relpath = self.convertRealPath(relpath)
relpath = relpath.replace('\\', '/')
blobURL = ""
if "GITHUB_BLOB_ROOT_URL" in self.var:
blobURL = self.joinFunc(self.var["GITHUB_BLOB_ROOT_URL"], relpath)
return (blobURL, relpath)
def getBlobURLWithLine(self, path, startLine):
(blobURL, relpath) = self.getBlobURL(path)
if blobURL:
targetURL = blobURL + "#" + "L" + str(startLine)
return (targetURL, relpath)
else:
return ("", relpath)
def getBlobURLWithLines(self, path, startLine, endLine):
(blobURL, relpath) = self.getBlobURL(path)
if blobURL:
targetURL = blobURL + "#" + "L" + str(startLine) + "-" + "L" + str(endLine)
return (targetURL, relpath)
else:
return ("", relpath)
def printAll(self):
for key in self.env.keys():
print (key, self.env[key])
for key in self.var.keys():
print (key, self.var[key])
# Appveyor 関連の環境変数をバッチファイルの形で保存する
def saveEnvAsBat(self, fileName):
with open(fileName, "w") as fout:
for key in self.keysEnv:
fout.write("set " + key + "=" + os.environ.get(key, "") + "\n")
print ("wrote: " + fileName)
def main(fileBat):
appveyor = AppveyorEnv()
appveyor.printAll()
fileName = r"appveyor_env.py"
if os.path.exists(fileName):
print (appveyor.getBlobURL(fileName))
print (appveyor.getBlobURLWithLine(fileName, 1))
print (appveyor.getBlobURLWithLines(fileName, 9, 15))
fileName = r"sakura\preBuild.bat"
if os.path.exists(fileName):
print (appveyor.getBlobURL(fileName))
print (appveyor.getBlobURLWithLine(fileName, 1))
print (appveyor.getBlobURLWithLines(fileName, 9, 15))
appveyor.saveEnvAsBat(fileBat)
if __name__ == '__main__':
fileBat = "set_appveyor_env.bat"
if len(sys.argv) > 1:
fileBat = sys.argv[1]
main(fileBat)