-
Notifications
You must be signed in to change notification settings - Fork 63
/
deploy.py
128 lines (105 loc) · 4.87 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
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
#!/usr/bin/env python
# coding:utf-8
import os
import sys
import glob
import locale
import shutil
import argparse
import subprocess
__author__ = 'coderzh'
GIT_REPO = [
['origin', 'gh-pages', '[email protected]:coderzh/hugo-blog-deployed.git'],
# ['gitcafe', 'gh-pages', '[email protected]:coderzh/coderzh-hugo-blog.git'],
# ['coding', 'gh-pages', '[email protected]:coderzh/hugo-blog-deployed.git'],
]
DEPLOY_DIR = 'gh-pages'
class ChDir:
"""Context manager for changing the current working directory"""
def __init__(self, new_path):
self.newPath = os.path.expanduser(new_path)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, exception_type, exception_value, traceback):
os.chdir(self.savedPath)
def deploy(args):
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
public_dir = os.path.join(current_dir, 'public')
commit_msg = ''
with ChDir(current_dir):
# step1 clean
if os.path.exists(public_dir):
shutil.rmtree(public_dir)
if args.type == 'auto':
subprocess.call('git fetch origin', shell=True)
subprocess.call('git checkout master', shell=True)
subprocess.call('git reset --hard origin/master', shell=True)
subprocess.call('git clean -fdx', shell=True)
subprocess.call('git submodule init', shell=True)
subprocess.call('git submodule update', shell=True)
subprocess.call('git submodule foreach git pull --rebase origin master', shell=True)
# on windows set TERM=msys
s = subprocess.Popen('git log -1 --pretty=format:"%s"',
shell=True, stdout=subprocess.PIPE)
commit_msg = s.communicate()[0].decode('utf-8').encode(locale.getpreferredencoding())
# step2 build
subprocess.call('hugo -v', shell=True)
deploy_dir = os.path.join(parent_dir, DEPLOY_DIR)
# step3 clone if not exists
if not os.path.exists(deploy_dir):
os.makedirs(deploy_dir)
with ChDir(deploy_dir):
subprocess.call('git init', shell=True)
for repo in GIT_REPO:
subprocess.call('git remote add {0} {1}'.format(repo[0], repo[2]), shell=True)
elif args.type == 'first':
with ChDir(deploy_dir):
subprocess.call('git init', shell=True)
for repo in GIT_REPO:
subprocess.call('git remote add {0} {1}'.format(repo[0], repo[2]), shell=True)
with ChDir(deploy_dir):
# step4 clean and pull
if len(GIT_REPO) > 0:
subprocess.call('git fetch {0}'.format(GIT_REPO[0][0]), shell=True)
if args.type == 'first':
subprocess.call('git checkout --orphan temp', shell=True)
subprocess.call('git rm --cached -r .', shell=True)
subprocess.call('git clean -fdx', shell=True)
subprocess.call('git branch -D {0}'.format(GIT_REPO[0][1]), shell=True)
subprocess.call('git checkout -b {0}'.format(GIT_REPO[0][1]), shell=True)
else:
subprocess.call('git checkout {0}'.format(GIT_REPO[0][1]), shell=True)
subprocess.call('git reset --hard {0}/{1}'.format(GIT_REPO[0][0], GIT_REPO[0][1]), shell=True)
subprocess.call('git clean -fdx', shell=True)
# step5 remove all files
for f in os.listdir('.'):
if f != '.git':
if os.path.isfile(f):
os.remove(f)
elif os.path.isdir(f):
shutil.rmtree(f)
# step6 copy new files
for f in os.listdir(public_dir):
file_path = os.path.join(public_dir, f)
if os.path.isfile(file_path):
shutil.copy(file_path, '.')
elif os.path.isdir(file_path):
shutil.copytree(file_path, f)
# step7 commit and push
if len(GIT_REPO) > 0:
subprocess.call('git add --all', shell=True)
subprocess.call('git commit -a -m "{0}"'.format(commit_msg), shell=True)
for repo in GIT_REPO:
if args.test:
print('git push {0} {1}:{2} -u'.format(repo[0], GIT_REPO[0][1], repo[1]))
else:
subprocess.call('git push {0} {1}:{2} -u'.format(repo[0], GIT_REPO[0][1], repo[1]), shell=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='deploy hugo')
parser.add_argument('type', help='auto or manual')
parser.add_argument('-t', dest='test', action='store_true', help='for test')
args = parser.parse_args()
if args.type in ['auto', 'manual', 'first']:
deploy(args)