Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ script:
python -m doctr deploy --sync .;
python -m doctr deploy --sync --gh-pages-docs docs;
python -m doctr deploy --sync --no-require-master --built-docs docs/_build/html "docs-$TRAVIS_BRANCH";
python -m doctr deploy --no-require-master --command "echo test; ls" docs;
python -m doctr deploy --no-require-master --command "echo test; ls; touch docs/_build/html/test" docs;
fi
- if [[ "${TESTS}" == "true" ]]; then
pyflakes doctr;
Expand Down
10 changes: 6 additions & 4 deletions doctr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
generate_GitHub_token, upload_GitHub_deploy_key, generate_ssh_key,
check_repo_exists)
from .travis import (decrypt_file, setup_deploy_key, get_token, run,
setup_GitHub_push, deploy_branch_exists, create_deploy_branch, sync_from_log,
commit_docs, push_docs, get_current_repo, find_sphinx_build_dir)
setup_GitHub_push, checkout_deploy_branch, deploy_branch_exists,
create_deploy_branch, copy_to_tmp, sync_from_log, commit_docs, push_docs,
get_current_repo, find_sphinx_build_dir)

__all__ = [
'encrypt_variable', 'encrypt_file', 'GitHub_post',
'generate_GitHub_token', 'upload_GitHub_deploy_key', 'generate_ssh_key',
'check_repo_exists',

'decrypt_file', 'setup_deploy_key', 'get_token', 'run', 'setup_GitHub_push', 'deploy_branch_exists',
'create_deploy_branch', 'sync_from_log', 'commit_docs', 'push_docs', 'get_current_repo', 'find_sphinx_build_dir'
'decrypt_file', 'setup_deploy_key', 'get_token', 'run',
'setup_GitHub_push', 'checkout_deploy_branch', 'deploy_branch_exists',
'create_deploy_branch', 'copy_to_tmp', 'sync_from_log', 'commit_docs', 'push_docs', 'get_current_repo', 'find_sphinx_build_dir'
]

from ._version import get_versions
Expand Down
27 changes: 19 additions & 8 deletions doctr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
from .local import (generate_GitHub_token, encrypt_variable, encrypt_file,
upload_GitHub_deploy_key, generate_ssh_key, check_repo_exists, GitHub_login)
from .travis import (setup_GitHub_push, commit_docs, push_docs,
get_current_repo, sync_from_log, find_sphinx_build_dir, run, get_travis_branch)
get_current_repo, sync_from_log, find_sphinx_build_dir, run,
get_travis_branch, copy_to_tmp, checkout_deploy_branch)
from . import __version__

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


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

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

can_push = setup_GitHub_push(deploy_repo, deploy_branch=deploy_branch,
canpush = setup_GitHub_push(deploy_repo, deploy_branch=deploy_branch,
auth_type='token' if args.token else 'deploy_key',
full_key_path=args.key_path,
branch_whitelist=branch_whitelist)

if args.command:
run(args.command, shell=True)

if args.sync:
built_docs = args.built_docs or find_sphinx_build_dir()
if args.temp_dir:
built_docs = copy_to_tmp(built_docs)

# Reset in case there are modified files that are tracked in the
# dpeloy branch.
run(['git', 'reset', '--hard'])
checkout_deploy_branch(deploy_branch, canpush=canpush)

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

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

if args.command:
run(['git', 'checkout', get_travis_branch()])
run(args.command, shell=True)
run(['git', 'checkout', deploy_branch])

changes = commit_docs(added=added, removed=removed)
if changes:
if can_push and args.push:
if canpush and args.push:
push_docs(deploy_branch)
else:
print("Don't have permission to push. Not trying.")
Expand Down
23 changes: 21 additions & 2 deletions doctr/travis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import sys
import glob
import re
import pathlib
import tempfile

from cryptography.fernet import Fernet

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

return canpush

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

return canpush

def deploy_branch_exists(deploy_branch):
"""
Check if there is a remote branch with name specified in ``deploy_branch``.
Expand Down Expand Up @@ -294,6 +300,19 @@ def find_sphinx_build_dir():
# TRAVIS_JOB_NUMBER = os.environ.get("TRAVIS_JOB_NUMBER", '')
# ACTUAL_TRAVIS_JOB_NUMBER = TRAVIS_JOB_NUMBER.split('.')[1]

def copy_to_tmp(directory):
"""
Copies the contents of directory to a temporary directory, and returns the
copied location.
"""
tmp_dir = tempfile.mkdtemp()
# Use pathlib because os.path.basename is different depending on whether
# the path ends in a /
p = pathlib.Path(directory)
new_dir = os.path.join(tmp_dir, p.name)
shutil.copytree(directory, new_dir)
return new_dir

def sync_from_log(src, dst, log_file):
"""
Sync the files in ``src`` to ``dst``.
Expand Down