-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgen.py
152 lines (118 loc) · 4.44 KB
/
gen.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
#Github Access Build On Github API v3
import requests
import os, yaml
import json
env_dist = os.environ
Gkey = env_dist.get('GithubKEY')
Owner = env_dist.get('Owner') # 当前组织名,请在TravisCI中定义
PusherAccount = env_dist.get('PusherAccount') # 推送者参数
PusherPassword = env_dist.get('PusherPassword')
PusherEmail = env_dist.get('PusherEmail')
Webroot = env_dist.get('Webroot') # 根域名(akioi.cn)
sess=requests.session()
headers = {
'Accept': 'application/vnd.github.machine-man-preview+json',
'User-Agent': 'DOT-CN Publisher/1.0',
'Authorization': 'token %s' % (Gkey)
}
headers_ForPagesOnly = {
'Accept': 'application/vnd.github.mister-fantastic-preview+json',
'User-Agent': 'DOT-CN Publisher/1.0',
'Authorization': 'token %s' % (Gkey)
}
global data
def mkdir(path):
if not os.path.exists(path):
print('%s Not Exist ,Creating......\n' % (path))
os.makedirs(path)
def read_file(path):
# mkdir(os.path.split(path)[0])
file = open(path, 'r+', encoding='utf8')
text = file.read()
file.close()
return text
def write_file(text, path):
#mkdir(os.path.split(path)[0])
file = open(path, 'w+', encoding='utf8')
file.write(text)
file.close()
def load(text, data):
for key, value in data.items():
text = text.replace('{{ %s }}' % key, value)
return text
def FromRequestsgetCloneURL(re):
Getdata = re.json()
CloneURL = Getdata['clone_url']
print('Clone URL %s\n'%(CloneURL))
return CloneURL
def RequestBuild(name):
print('Request Buildings for %s' % (name))
PostURL='https://api.github.com/repos/%s/%s/pages/builds' % (Owner,name)
res=requests.post(url=PostURL,headers=headers_ForPagesOnly)
print(res.text+'\n')
def getPagesURL(name):
if "cname" in data:
PagesURL=data['cname']
else:
PagesURL='%s.%s'%(name,Webroot)
return PagesURL
def CreateRepo(name):
print('Creating Repo %s'%(name))
Postdata={'name' : str(name),'description' : str(data['description']), 'homepage' : str('https://%s'%(getPagesURL(name)))}
PostURL='https://api.github.com/orgs/%s/repos' % (Owner)
res=requests.post(url=PostURL,data=json.dumps(Postdata),headers=headers)
#print(res.text+'\n')
return FromRequestsgetCloneURL(res)
def CheckIfRepoCreated(name):
print('Checking Repo %s\n'%(name))
# GET repo 信息 , 检测状态码 404 为未创建
r = requests.get(url = 'https://api.github.com/repos/%s/%s' % (Owner,name), headers = headers)
#print (r.status_code)
if(r.status_code==404): # Not Created
CloneURL = CreateRepo(name)
elif(r.status_code==200):# Created
CloneURL = FromRequestsgetCloneURL(r)
else:
print('Unknown Error!\n')
print(r.text+'\n')
return CloneURL
def CloneRepo(name):
CloneURL = CheckIfRepoCreated(name)
# print('Cloning %s \n'%(CloneURL))
mkdir(os.getcwd() + '/html/')
# os.system("cd {path} && git clone {CURL} && sed -i 's/github.com/{username}:{password}@github.com/g' {path}/{Reponame}/.git/config" .format( #Only Support Linux :(
# path = os.getcwd() + '/html/' ,
# CURL = CloneURL,
# Reponame = name,
# username = PusherAccount,
# password = PusherPassword
# ))
def deploy(name): # build pages at the moment?
print('Deploying %s\n'%(name))
os.system("git config --global push.default matching && cd {path} && mkdir ./temp/ && cd ./temp/ && git init && echo {pagesURL} > ./CNAME && git config --global user.email '{Email}' && git config --global user.name '{Account}' && git remote add origin https://{Account}:{password}@github.com/{owner}/{Reponame}.git && git pull origin gh-pages && rm -rf ./* && cp -f ../* ./ && git add --all . && git commit -m 'Update Pages' && git push --quiet --force origin HEAD:gh-pages ".format(
Reponame = name,
password = PusherPassword,
owner = Owner,
path = os.getcwd() + '/html/%s' % name,
Account = PusherAccount,
Email = PusherEmail,
pagesURL = getPagesURL(name)
))
RequestBuild(name)
def Cleanup():
print('Cleaning up......\n')
os.system("rm -rf html")
if __name__ == '__main__':
mkdir(os.getcwd() + '/html/')
#LoginGithub()
for filename in os.listdir('src'):
text = read_file('src/%s' % filename)
data = yaml.load(text)
data['short_name'] = filename.split('.')[0]
print('Processing %s , Shortname : %s'%(filename,data['short_name']))
CloneRepo(data['short_name'])
for filename in os.listdir('themes/%s' % data['theme']):
text = read_file('themes/%s/%s' % (data['theme'], filename))
write_file(load(text, data), 'html/%s/%s' % (data['short_name'], filename))
deploy(data['short_name'])
#CheckIfRepoCreated(input())