forked from astropy/old-astropy-website-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_script.py
executable file
·225 lines (181 loc) · 7.31 KB
/
upload_script.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
import subprocess
from sys import exit
from os import path
from argparse import ArgumentParser
from shutil import rmtree
_instructions_description = """
This script will perform all the steps required to update the astropy
github page to reflect the current build of the repository this script is
executed from. This should thus only be run in the root directory of a
clone of the astropy-website repository.
To make this work correctly, a few things must be true:
* You must have git installed and on your path
* You must have a clone of the astropy github site present - the default
assumes it's in the directory ../astropy.github.com, so this would mean
having this clone of astropy-website in $SOMETHING/astropy-website and the
clone of the github page in $SOMETHING/astropy.github.com. You run this
script, then, in $SOMETHING/astropy-website.
* You must have push rights to the astropy.github.com repo. Normally this
means the astropy/astropy.github.com repo, and by default it assumes you
are pushing to the "master" branch on the remote named "origin". You can
change this with options, but that's what git will do automatically if
you clone directly from astropy/astropy.github.com.
"""[1:-1]
DRYRUN = False
FORCE = False
def safe_syscall(cmd):
if DRYRUN:
print 'DRY RUN! Command would have been:', cmd
else:
res = subprocess.call(cmd, shell=True)
if res != 0:
print 'Command line call failed! Exiting...'
exit(res)
def continue_check(msg, default='y'):
if default not in 'ynq':
raise ValueError('invalid continue_check default ' + str(default))
if FORCE:
if default == 'y':
print msg + ' <y forced>'
return True
elif default == 'n':
print msg + ' <n forced>'
return False
else:
print msg + ' <q forced>'
print 'You chose to quit... exiting!'
exit(1)
if default == 'y':
opstr = '[y]/n/q'
elif default == 'n':
opstr = 'y/[n]/q'
elif default == 'q':
opstr = 'y/n/[q]'
res = raw_input(msg + ' ' + opstr + ':')
if res == '':
res = default
elif res not in 'ynq':
print 'Invalid entry',res,'...exiting!'
exit(1)
if res == 'y':
return True
elif res == 'n':
return False
else:
print 'You chose to quit... exiting!'
exit(1)
def check_for_uncommited_changes():
p = subprocess.Popen('git status -s -u no', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
print out
print err
print 'Couldn\'t get current repo\'s status! Exiting...'
exit(-3)
elif out == '':
return False
else:
return out
def get_current_sha():
p = subprocess.Popen('git rev-parse HEAD', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
print out
print err
print 'Couldn\'t get current repo\'s SHA! Exiting...'
exit(-3)
else:
return out.strip()
def remake_page():
builddir = path.abspath('_build')
if path.exists(builddir):
if continue_check('Wipe old _build dir {0} before rebuilding?'.format(builddir)):
if DRYRUN:
print 'DRY RUN! Would have deleted directory',builddir
else:
rmtree(builddir)
print 'Rebuilding page'
safe_syscall('make html')
else:
print 'Building new page'
safe_syscall('make html')
if not path.isdir(builddir):
print 'Build could not be found in', builddir, 'Exiting...'
exit(-5)
def copy_html_to_page_repo(repodir):
htmldir = path.abspath('_build/html')
#pull h
if continue_check('Update git repo at {0}?'.format(repodir)):
syscall = 'cd {0};git pull'.format(repodir)
safe_syscall(syscall)
#copy file
if continue_check('Copy contents of {0} to {1}?'.format(htmldir, repodir)):
syscall = 'rsync -a --delete {0} {1}'.format(path.join(htmldir, '*'), repodir)
safe_syscall(syscall)
def commit_to_page_repo(repodir, apywebsha, updatereason=''):
if updatereason:
commitmsg = 'Updated for {0} \n(commit {1} in astropy-website)'.format(updatereason, apywebsha)
else:
commitmsg = 'Updated page \n(commit {0} in astropy-website)'.format(apywebsha)
if continue_check('Want to commit to local page repo with message "{0}"?'.format(commitmsg)):
syscall1 = 'cd ' + repodir + ';git add *'
syscall2 = 'cd ' + repodir + ';git commit -m "{0}"'.format(commitmsg)
print 'Adding to page repo'
safe_syscall(syscall1)
print 'Comitting to page repo with message: "{0}"'.format(commitmsg)
safe_syscall(syscall2)
def push_page_repo(repodir, remotename, branchname):
if continue_check('Want to push to page repo?'):
syscall = 'cd ' + repodir + ';git push {0} {1}'.format(remotename, branchname)
safe_syscall(syscall)
if __name__ == '__main__':
descr = _instructions_description[:_instructions_description.index('.') + 1]
ap = ArgumentParser(description=descr)
ap.add_argument('-l', '--long-help', default=False, dest='longhelp',
help='Print the installation/usage instructions', action='store_true')
ap.add_argument('--dry-run', '-y', default=False, dest='dryrun',
help='Don\'t actually do anything - just show what would have happened',
action='store_true')
ap.add_argument('--reason', '-r', default='',
help='The reason for the commit (used in the commit message)')
ap.add_argument('--repo-dir', '-d', default='../astropy.github.com',
help='The directory holding the page itself (astropy.github.com)',
dest='repo')
ap.add_argument('--remote', '-e', default='origin',
help='The name of the remote to push the final page repo to')
ap.add_argument('--branch', '-b', default='master',
help='The name of the branch to use on the page repo')
ap.add_argument('--skip-rebuild-page', '-s', default=False,
help='Skip the step of cleaning the current build of the page and '
're-building it', dest='nobuild', action='store_true')
ap.add_argument('--force', '-f', default=False,
help='Do all the actions without prompting. Basically the '
'equivalent of hitting enter at each prompt.',action='store_true')
args = ap.parse_args()
DRYRUN = args.dryrun
FORCE = args.force
if args.longhelp:
print _instructions_description
exit(0)
repodir = path.abspath(args.repo)
if not path.isdir(repodir):
print 'Requested repository directory', repodir, 'is not a directory! Exiting...'
exit(-1)
elif not path.isdir(path.join(repodir, '.git')):
print 'Requested repository', repodir, 'is not a git repo! Exiting...'
exit(-2)
apywebsha = get_current_sha()
stat = check_for_uncommited_changes()
if stat:
print 'Uncommited changes found:'
print stat
print 'Exiting!'
exit(-4)
if not args.nobuild:
remake_page()
copy_html_to_page_repo(repodir)
commit_to_page_repo(repodir, apywebsha, args.reason)
push_page_repo(repodir, args.remote, args.branch)