Skip to content

Commit b10e2ec

Browse files
committed
More robust subprocess invocation.
1 parent cc9db04 commit b10e2ec

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

build_docs.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def _file_unchanged(old, new):
7272
return True
7373

7474

75-
def shell_out(cmd):
75+
def shell_out(cmd, shell=False):
7676
logging.debug("Running command %r", cmd)
7777
try:
78-
return subprocess.check_output(cmd, shell=True,
78+
return subprocess.check_output(cmd, shell=shell,
7979
stdin=subprocess.PIPE,
8080
stderr=subprocess.STDOUT,
8181
universal_newlines=True)
@@ -108,17 +108,17 @@ def git_clone(repository, directory, branch=None):
108108
logging.info("Updating repository %s in %s", repository, directory)
109109
try:
110110
if branch:
111-
shell_out("git -C {} checkout {}".format(directory, branch))
112-
shell_out("git -C {} pull --ff-only".format(directory))
111+
shell_out(['git', '-C', directory, 'checkout', branch])
112+
shell_out(['git', '-C', directory, 'pull', '--ff-only'])
113113
except subprocess.CalledProcessError:
114114
if os.path.exists(directory):
115115
shutil.rmtree(directory)
116116
logging.info("Cloning %s into %s", repository, directory)
117117
os.makedirs(directory, mode=0o775)
118-
shell_out("git clone --depth 1 --no-single-branch {} {}".format(
119-
repository, directory))
118+
shell_out(['git', 'clone', '--depth=1', '--no-single-branch',
119+
repository, directory])
120120
if branch:
121-
shell_out("git -C {} checkout {}".format(directory, branch))
121+
shell_out(['git', '-C', directory, 'checkout', branch])
122122

123123

124124
def pep_545_tag_to_gettext_tag(tag):
@@ -140,8 +140,7 @@ def translation_branch(locale_repo, locale_clone_dir, needed_version):
140140
returns the name of the nearest existing branch.
141141
"""
142142
git_clone(locale_repo, locale_clone_dir)
143-
remote_branches = shell_out(
144-
"git -C {} branch -r".format(locale_clone_dir))
143+
remote_branches = shell_out(['git', '-C', locale_clone_dir, 'branch', '-r'])
145144
translated_branches = []
146145
for translated_branch in remote_branches.split('\n'):
147146
if not translated_branch:
@@ -150,7 +149,8 @@ def translation_branch(locale_repo, locale_clone_dir, needed_version):
150149
translated_branches.append(float(translated_branch.split('/')[1]))
151150
except ValueError:
152151
pass # Skip non-version branches like 'master' if they exists.
153-
return sorted(translated_branches, key=lambda x: abs(needed_version - x))[0]
152+
return str(sorted(translated_branches,
153+
key=lambda x: abs(needed_version - x))[0])
154154

155155

156156
def build_one(version, git_branch, isdev, quick, venv, build_root, www_root,
@@ -187,7 +187,7 @@ def build_one(version, git_branch, isdev, quick, venv, build_root, www_root,
187187
os.makedirs(target, exist_ok=True)
188188
try:
189189
os.chmod(target, 0o775)
190-
shell_out("chgrp -R {group} {file}".format(group=group, file=target))
190+
shell_out(['chgrp', '-R', group, target])
191191
except (PermissionError, subprocess.CalledProcessError) as err:
192192
logging.warning("Can't change mod or group of %s: %s",
193193
target, str(err))
@@ -203,39 +203,42 @@ def build_one(version, git_branch, isdev, quick, venv, build_root, www_root,
203203
shell_out(
204204
"cd Doc; make PYTHON=%s SPHINXBUILD=%s BLURB=%s VENVDIR=%s SPHINXOPTS='%s' %s >> %s 2>&1" %
205205
(python, sphinxbuild, blurb, venv, sphinxopts, maketarget,
206-
os.path.join(log_directory, logname)))
207-
shell_out("chgrp -R {group} {file}".format(
208-
group=group, file=log_directory))
206+
os.path.join(log_directory, logname)), shell=True)
207+
shell_out(['chgrp', '-R', group, log_directory])
209208
changed = changed_files(os.path.join(checkout, "Doc/build/html"), target)
210209
logging.info("Copying HTML files to %s", target)
211-
shell_out("chown -R :{} Doc/build/html/".format(group))
212-
shell_out("chmod -R o+r Doc/build/html/")
213-
shell_out("find Doc/build/html/ -type d -exec chmod o+x {} ';'")
214-
shell_out("rsync -a {delete} Doc/build/html/ {target}".format(
215-
delete="" if quick else "--delete-delay",
216-
target=target))
210+
shell_out(['chown', '-R', ':' + group, 'Doc/build/html/'])
211+
shell_out(['chmod', '-R', 'o+r', 'Doc/build/html/'])
212+
shell_out(['find', 'Doc/build/html/', '-type', 'd',
213+
'-exec', 'chmod', 'o+x', '{}', ';'])
214+
if quick:
215+
shell_out(['rsync', '-a', 'Doc/build/html/', target])
216+
else:
217+
shell_out(['rsync', '-a', '--delete-delay', 'Doc/build/html/', target])
217218
if not quick:
218219
logging.debug("Copying dist files")
219-
shell_out("chown -R :{} Doc/dist/".format(group))
220-
shell_out("chmod -R o+r Doc/dist/")
221-
shell_out("mkdir -m o+rx -p %s/archives" % target)
222-
shell_out("chown :{} {}/archives".format(group, target))
223-
shell_out("cp -a Doc/dist/* %s/archives" % target)
220+
shell_out(['chown', '-R', ':' + group, 'Doc/dist/'])
221+
shell_out(['chmod', '-R', 'o+r', 'Doc/dist/'])
222+
shell_out(['mkdir', '-m', 'o+rx', '-p', target + '/archives'])
223+
shell_out(['chown', ':' + group, target + '/archives'])
224+
shell_out("cp -a Doc/dist/* %s/archives" % target, shell=True)
224225
changed.append("archives/")
225226
for fn in os.listdir(os.path.join(target, "archives")):
226227
changed.append("archives/" + fn)
227228

228229
logging.info("%s files changed", len(changed))
229230
if changed and not skip_cache_invalidation:
230231
targets_dir = www_root
231-
prefixes = shell_out('find -L {} -samefile {}'.format(
232-
targets_dir, target)).replace(targets_dir + '/', '')
232+
prefixes = shell_out(['find', '-L', targets_dir,
233+
'-samefile', target])
234+
prefixes = prefixes.replace(targets_dir + '/', '')
233235
prefixes = [prefix + '/' for prefix in prefixes.split('\n') if prefix]
234236
to_purge = prefixes[:]
235237
for prefix in prefixes:
236238
to_purge.extend(prefix + p for p in changed)
237239
logging.info("Running CDN purge")
238-
shell_out("curl -X PURGE \"https://docs.python.org/{%s}\"" % ",".join(to_purge))
240+
shell_out(['curl', '-XPURGE',
241+
'https://docs.python.org/{%s}' % ",".join(to_purge)])
239242

240243
logging.info("Finished %s", checkout)
241244

@@ -254,7 +257,7 @@ def parse_args():
254257
help="Make HTML files only (Makefile rules suffixed with -html).")
255258
parser.add_argument(
256259
"-b", "--branch",
257-
metavar=3.6,
260+
metavar='3.6',
258261
type=float,
259262
help="Version to build (defaults to all maintained branches).")
260263
parser.add_argument(

0 commit comments

Comments
 (0)