API¶
This is the Python API. We recommend that most users use doctr from the command line.
Local¶
The code that should be run locally
-
doctr.local.GitHub_login(client_id, *, headers=None, scope='repo')[source]¶ Login to GitHub.
This uses the device authorization flow. client_id should be the client id for your GitHub application. See https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#device-flow.
‘scope’ should be the scope for the access token (‘repo’ by default). See https://docs.github.com/en/free-pro-team@latest/developers/apps/scopes-for-oauth-apps#available-scopes.
Returns an access token.
-
doctr.local.GitHub_post(data, url, *, headers)[source]¶ POST the data
datato GitHub.Returns the json response from the server, or raises on error status.
-
doctr.local.GitHub_raise_for_status(r)[source]¶ Call instead of r.raise_for_status() for GitHub requests
Checks for common GitHub response issues and prints messages for them.
-
doctr.local.check_repo_exists(deploy_repo, service='github', *, headers=None, ask=False)[source]¶ Checks that the repository exists on GitHub.
This should be done before attempting generate a key to deploy to that repo.
Raises
RuntimeErrorif the repo is not valid.Returns a dictionary with the following keys:
‘private’: Indicates whether or not the repo requires authorization to access. Private repos require authorization.
‘service’: For service=’travis’, is ‘travis-ci.com’ or ‘travis-ci.org’, depending on which should be used. Otherwise it is just equal to
service.
For service=’travis’, if ask=True, it will ask at the command line if both travis-ci.org and travis-ci.com exist. If ask=False, service=’travis’ will check travis-ci.com first and only check travis-ci.org if it doesn’t exist. ask=True does nothing for service=’github’, service=’travis-ci.com’, service=’travis-ci.org’.
-
doctr.local.encrypt_to_file(contents, filename)[source]¶ Encrypts
contentsand writes it tofilename.contentsshould be a bytes string.filenameshould end with.enc.Returns the secret key used for the encryption.
Decrypt the file with
doctr.travis.decrypt_file().
-
doctr.local.encrypt_variable(variable, build_repo, *, tld='.org', public_key=None, travis_token=None, **login_kwargs)[source]¶ Encrypt an environment variable for
build_repofor Travisvariableshould be a bytes object, of the formb'ENV=value'.build_repois the repo thatdoctr deploywill be run from. It should be like ‘drdoctr/doctr’.tldshould be'.org'for travis-ci.org and'.com'for travis-ci.com.public_keyshould be a pem format public key, obtained from Travis if not provided.If the repo is private, travis_token should be as returned by
get_temporary_token(**login_kwargs). A token being present automatically impliestld='.com'.
-
doctr.local.generate_GitHub_token(*, note='Doctr token for pushing to gh-pages from Travis', scopes=None, **login_kwargs)[source]¶ Generate a GitHub token for pushing from Travis
The scope requested is public_repo.
If no password or OTP are provided, they will be requested from the command line.
The token created here can be revoked at https://github.com/settings/tokens.
-
doctr.local.generate_ssh_key()[source]¶ Generates an SSH deploy public and private key.
Returns (private key, public key), a tuple of byte strings.
-
doctr.local.get_travis_token(*, GitHub_token=None, **login_kwargs)[source]¶ Generate a temporary token for authenticating with Travis
The GitHub token can be passed in to the
GitHub_tokenkeyword argument. If no token is passed in, a GitHub token is generated temporarily, and then immediately deleted.This is needed to activate a private repo
Returns the secret token. It should be added to the headers like
headers[‘Authorization’] = “token {}”.format(token)
Travis¶
The code that should be run on Travis
-
doctr.travis.checkout_deploy_branch(deploy_branch, canpush=True)[source]¶ Checkout the deploy branch, creating it if it doesn’t exist.
-
doctr.travis.commit_docs(*, added, removed)[source]¶ Commit the docs to the current branch
Assumes that
setup_GitHub_push(), which sets up thedoctr_remoteremote, has been run.Returns True if changes were committed and False if no changes were committed.
-
doctr.travis.copy_to_tmp(source)[source]¶ Copies
sourceto a temporary directory, and returns the copied location.If source is a file, the copied location is also a file.
-
doctr.travis.create_deploy_branch(deploy_branch, push=True)[source]¶ If there is no remote branch with name specified in
deploy_branch, create one.Note that default
deploy_branchisgh-pagesfor regular repos andmasterforgithub.iorepos.Return True if
deploy_branchwas created, False if not.
-
doctr.travis.decrypt_file(file, key)[source]¶ Decrypts the file
file.The encrypted file is assumed to end with the
.encextension. The decrypted file is saved to the same location without the.encextension.The permissions on the decrypted file are automatically set to 0o600.
See also
doctr.local.encrypt_file().
-
doctr.travis.deploy_branch_exists(deploy_branch)[source]¶ Check if there is a remote branch with name specified in
deploy_branch.Note that default
deploy_branchisgh-pagesfor regular repos andmasterforgithub.iorepos.This isn’t completely robust. If there are multiple remotes and you have a
deploy_branchbranch on the non-default remote, this won’t see it.
-
doctr.travis.determine_push_rights(*, branch_whitelist, TRAVIS_BRANCH, TRAVIS_PULL_REQUEST, TRAVIS_TAG, build_tags, fork)[source]¶ Check if Travis is running on
master(or a whitelisted branch) to determine if we can/should push the docs to the deploy repo
-
doctr.travis.find_sphinx_build_dir()[source]¶ Find build subfolder within sphinx docs directory.
This is called by
commit_docs()if keyword argbuilt_docsis not specified on the command line.
-
doctr.travis.get_current_repo()[source]¶ Get the GitHub repo name for the current directory.
Assumes that the repo is in the
originremote.
-
doctr.travis.get_token()[source]¶ Get the encrypted GitHub token in Travis.
Make sure the contents this variable do not leak. The
run()function will remove this from the output, so always use it.
-
doctr.travis.get_travis_branch()[source]¶ Get the name of the branch that the PR is from.
Note that this is not simply
$TRAVIS_BRANCH. thepushbuild will use the correct branch (the branch that the PR is from) but theprbuild will use the _target_ of the PR (usually master). So instead, we ask for$TRAVIS_PULL_REQUEST_BRANCHif it’s a PR build, and$TRAVIS_BRANCHif it’s a push build.
-
doctr.travis.last_commit_by_doctr()[source]¶ Check whether the author of HEAD is doctr to avoid starting an infinite loop
-
doctr.travis.push_docs(deploy_branch='gh-pages', retries=5)[source]¶ Push the changes to the branch named
deploy_branch.Assumes that
setup_GitHub_push()has been run and returned True, and thatcommit_docs()has been run. Does not push anything if no changes were made.
-
doctr.travis.run(args, shell=False, exit=True)[source]¶ Run the command
args.Automatically hides the secret GitHub token from the output.
If shell=False (recommended for most commands), args should be a list of strings. If shell=True, args should be a string of the command to run.
If exit=True, it exits on nonzero returncode. Otherwise it returns the returncode.
-
doctr.travis.set_git_user_email()[source]¶ Set global user and email for git user if not already present on system
-
doctr.travis.setup_GitHub_push(deploy_repo, *, auth_type='deploy_key', full_key_path='github_deploy_key.enc', require_master=None, branch_whitelist=None, deploy_branch='gh-pages', env_name='DOCTR_DEPLOY_ENCRYPTION_KEY', build_tags=False)[source]¶ Setup the remote to push to GitHub (to be run on Travis).
auth_typeshould be either'deploy_key'or'token'.For
auth_type='token', this sets up the remote with the token and checks out the gh-pages branch. The token to push to GitHub is assumed to be in theGH_TOKENenvironment variable.For
auth_type='deploy_key', this sets up the remote with ssh access.
-
doctr.travis.setup_deploy_key(keypath='github_deploy_key', key_ext='.enc', env_name='DOCTR_DEPLOY_ENCRYPTION_KEY')[source]¶ Decrypts the deploy key and configures it with ssh
The key is assumed to be encrypted as keypath + key_ext, and the encryption key is assumed to be set in the environment variable
env_name. Ifenv_nameis not set, it falls back toDOCTR_DEPLOY_ENCRYPTION_KEYfor backwards compatibility.If keypath + key_ext does not exist, it falls back to
github_deploy_key.encfor backwards compatibility.
-
doctr.travis.sync_from_log(src, dst, log_file, exclude=())[source]¶ Sync the files in
srctodst.The files that are synced are logged to
log_file. Iflog_fileexists, the files inlog_fileare removed first.Returns
(added, removed), where added is a list of all files synced fromsrc(even if it already existed indst), andremovedis every file fromlog_filethat was removed fromdstbecause it wasn’t insrc.addedalso includes the log file.excludemay be a list of paths fromsrcthat should be ignored. Such paths are neither added nor removed, even if they are in the logfile.