diff --git a/.travis.yml b/.travis.yml index 94e13f47..40863080 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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; diff --git a/doctr/__init__.py b/doctr/__init__.py index 5245b4e6..38687811 100644 --- a/doctr/__init__.py +++ b/doctr/__init__.py @@ -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 diff --git a/doctr/__main__.py b/doctr/__main__.py index 22871182..e734bc18 100644 --- a/doctr/__main__.py +++ b/doctr/__main__.py @@ -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): @@ -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") @@ -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)) @@ -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") @@ -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.") diff --git a/doctr/travis.py b/doctr/travis.py index bf073c27..af31bfce 100644 --- a/doctr/travis.py +++ b/doctr/travis.py @@ -9,6 +9,8 @@ import sys import glob import re +import pathlib +import tempfile from cryptography.fernet import Fernet @@ -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)) @@ -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``. @@ -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``.