forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappveyor_env.py
178 lines (148 loc) · 4.56 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
# -*- 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_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=%APPVEYOR_REPO_COMMIT:~0,8%
if "APPVEYOR_REPO_COMMIT" in self.env:
commitHash = self.env["APPVEYOR_REPO_COMMIT"]
if len(commitHash) >= 8:
self.var["APPVEYOR_SHORTHASH"] = commitHash[0:8]
# 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 len(commitHashPRHead) >= 8:
self.var["APPVEYOR_SHORTHASH_PR_HEAD"] = commitHashPRHead[0:8]
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_COMMIT_URL_PR_HEAD
temp = [
repoURL,
"pull",
self.env["APPVEYOR_PULL_REQUEST_NUMBER"],
"commits",
self.env["APPVEYOR_PULL_REQUEST_HEAD_COMMIT"],
]
self.var["GITHUB_COMMIT_URL_PR_HEAD"] = '/'.join(temp)
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 = relpath.replace('\\', '/')
blobURL = ""
if "GITHUB_BLOB_ROOT_URL" in self.var:
blobURL = self.joinFunc(self.var["GITHUB_BLOB_ROOT_URL"], relpath)
return blobURL
def getBlobURLWithLine(self, path, startLine):
blobURL = self.getBlobURL(path)
if blobURL:
return blobURL + "#" + "L" + str(startLine)
else:
return ""
def getBlobURLWithLines(self, path, startLine, endLine):
blobURL = self.getBlobURL(path)
if blobURL:
return blobURL + "#" + "L" + str(startLine) + "-" + "L" + str(endLine)
else:
return ""
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])
def main():
appveyor = AppveyorEnv()
appveyor.printAll()
file = r"appveyor_env.py"
print (appveyor.getBlobURL(file))
print (appveyor.getBlobURLWithLine(file, 1))
print (appveyor.getBlobURLWithLines(file, 9, 15))
file = r"sakura\preBuild.bat"
print (appveyor.getBlobURL(file))
print (appveyor.getBlobURLWithLine(file, 1))
print (appveyor.getBlobURLWithLines(file, 9, 15))
if __name__ == '__main__':
main()