Skip to content

Commit 853a5fa

Browse files
authored
Merge pull request #215 from drdoctr/temp-dir
Copy the --built-docs to a temporary directory
2 parents cc39c4e + e377b6d commit 853a5fa

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ script:
3434
python -m doctr deploy --sync .;
3535
python -m doctr deploy --sync --gh-pages-docs docs;
3636
python -m doctr deploy --sync --no-require-master --built-docs docs/_build/html "docs-$TRAVIS_BRANCH";
37-
python -m doctr deploy --no-require-master --command "echo test; ls" docs;
37+
python -m doctr deploy --no-require-master --command "echo test; ls; touch docs/_build/html/test" docs;
3838
fi
3939
- if [[ "${TESTS}" == "true" ]]; then
4040
pyflakes doctr;

doctr/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
generate_GitHub_token, upload_GitHub_deploy_key, generate_ssh_key,
33
check_repo_exists)
44
from .travis import (decrypt_file, setup_deploy_key, get_token, run,
5-
setup_GitHub_push, deploy_branch_exists, create_deploy_branch, sync_from_log,
6-
commit_docs, push_docs, get_current_repo, find_sphinx_build_dir)
5+
setup_GitHub_push, checkout_deploy_branch, deploy_branch_exists,
6+
create_deploy_branch, copy_to_tmp, sync_from_log, commit_docs, push_docs,
7+
get_current_repo, find_sphinx_build_dir)
78

89
__all__ = [
910
'encrypt_variable', 'encrypt_file', 'GitHub_post',
1011
'generate_GitHub_token', 'upload_GitHub_deploy_key', 'generate_ssh_key',
1112
'check_repo_exists',
1213

13-
'decrypt_file', 'setup_deploy_key', 'get_token', 'run', 'setup_GitHub_push', 'deploy_branch_exists',
14-
'create_deploy_branch', 'sync_from_log', 'commit_docs', 'push_docs', 'get_current_repo', 'find_sphinx_build_dir'
14+
'decrypt_file', 'setup_deploy_key', 'get_token', 'run',
15+
'setup_GitHub_push', 'checkout_deploy_branch', 'deploy_branch_exists',
16+
'create_deploy_branch', 'copy_to_tmp', 'sync_from_log', 'commit_docs', 'push_docs', 'get_current_repo', 'find_sphinx_build_dir'
1517
]
1618

1719
from ._version import get_versions

doctr/__main__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
from .local import (generate_GitHub_token, encrypt_variable, encrypt_file,
3737
upload_GitHub_deploy_key, generate_ssh_key, check_repo_exists, GitHub_login)
3838
from .travis import (setup_GitHub_push, commit_docs, push_docs,
39-
get_current_repo, sync_from_log, find_sphinx_build_dir, run, get_travis_branch)
39+
get_current_repo, sync_from_log, find_sphinx_build_dir, run,
40+
get_travis_branch, copy_to_tmp, checkout_deploy_branch)
4041
from . import __version__
4142

4243
def make_parser_with_config_adder(parser, config):
@@ -152,6 +153,9 @@ def get_parser(config=None):
152153
conjunction with the --command flag, for instance, if the command syncs
153154
the files for you. Any files you wish to commit should be added to the
154155
index.""")
156+
deploy_parser.add_argument('--no-temp-dir', dest='temp_dir',
157+
action='store_false', default=True, help="""Don't copy the
158+
--built-docs directory to a temporary directory.""")
155159
deploy_parser_add_argument('--no-push', dest='push', action='store_false',
156160
default=True, help="Run all the steps except the last push step. "
157161
"Useful for debugging")
@@ -161,6 +165,7 @@ def get_parser(config=None):
161165
the first argument to 'doctr deploy'. This flag is kept for backwards
162166
compatibility.""")
163167

168+
164169
if config:
165170
print('Warning, The following options in `.travis.yml` were not recognized:\n%s' % json.dumps(config, indent=2))
166171

@@ -249,14 +254,25 @@ def deploy(args, parser):
249254
branch_whitelist = {'master'} if args.require_master else set(get_travis_branch())
250255
branch_whitelist.update(set(config.get('branches',set({}))))
251256

252-
can_push = setup_GitHub_push(deploy_repo, deploy_branch=deploy_branch,
257+
canpush = setup_GitHub_push(deploy_repo, deploy_branch=deploy_branch,
253258
auth_type='token' if args.token else 'deploy_key',
254259
full_key_path=args.key_path,
255260
branch_whitelist=branch_whitelist)
256261

262+
if args.command:
263+
run(args.command, shell=True)
264+
257265
if args.sync:
258266
built_docs = args.built_docs or find_sphinx_build_dir()
267+
if args.temp_dir:
268+
built_docs = copy_to_tmp(built_docs)
269+
270+
# Reset in case there are modified files that are tracked in the
271+
# dpeloy branch.
272+
run(['git', 'reset', '--hard'])
273+
checkout_deploy_branch(deploy_branch, canpush=canpush)
259274

275+
if args.sync:
260276
log_file = os.path.join(deploy_dir, '.doctr-files')
261277

262278
print("Moving built docs into place")
@@ -266,14 +282,9 @@ def deploy(args, parser):
266282
else:
267283
added, removed = [], []
268284

269-
if args.command:
270-
run(['git', 'checkout', get_travis_branch()])
271-
run(args.command, shell=True)
272-
run(['git', 'checkout', deploy_branch])
273-
274285
changes = commit_docs(added=added, removed=removed)
275286
if changes:
276-
if can_push and args.push:
287+
if canpush and args.push:
277288
push_docs(deploy_branch)
278289
else:
279290
print("Don't have permission to push. Not trying.")

doctr/travis.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import sys
1010
import glob
1111
import re
12+
import pathlib
13+
import tempfile
1214

1315
from cryptography.fernet import Fernet
1416

@@ -218,6 +220,12 @@ def setup_GitHub_push(deploy_repo, auth_type='deploy_key', full_key_path='github
218220
print("Fetching doctr remote")
219221
run(['git', 'fetch', 'doctr_remote'])
220222

223+
return canpush
224+
225+
def checkout_deploy_branch(deploy_branch, canpush=True):
226+
"""
227+
Checkout the deploy branch, creating it if it doesn't exist.
228+
"""
221229
#create empty branch with .nojekyll if it doesn't already exist
222230
new_deploy_branch = create_deploy_branch(deploy_branch, push=canpush)
223231
print("Checking out {}".format(deploy_branch))
@@ -228,8 +236,6 @@ def setup_GitHub_push(deploy_repo, auth_type='deploy_key', full_key_path='github
228236
run(['git', 'checkout', '-b', deploy_branch, '--track', 'doctr_remote/{}'.format(deploy_branch)])
229237
print("Done")
230238

231-
return canpush
232-
233239
def deploy_branch_exists(deploy_branch):
234240
"""
235241
Check if there is a remote branch with name specified in ``deploy_branch``.
@@ -294,6 +300,19 @@ def find_sphinx_build_dir():
294300
# TRAVIS_JOB_NUMBER = os.environ.get("TRAVIS_JOB_NUMBER", '')
295301
# ACTUAL_TRAVIS_JOB_NUMBER = TRAVIS_JOB_NUMBER.split('.')[1]
296302

303+
def copy_to_tmp(directory):
304+
"""
305+
Copies the contents of directory to a temporary directory, and returns the
306+
copied location.
307+
"""
308+
tmp_dir = tempfile.mkdtemp()
309+
# Use pathlib because os.path.basename is different depending on whether
310+
# the path ends in a /
311+
p = pathlib.Path(directory)
312+
new_dir = os.path.join(tmp_dir, p.name)
313+
shutil.copytree(directory, new_dir)
314+
return new_dir
315+
297316
def sync_from_log(src, dst, log_file):
298317
"""
299318
Sync the files in ``src`` to ``dst``.

0 commit comments

Comments
 (0)