Last active
March 8, 2017 09:48
-
-
Save logc/2793a4e35d663ba1baf278d7a2e15a39 to your computer and use it in GitHub Desktop.
Python packages with Sphinx & Github pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
pkg_name=$1 | |
pkg_root=$PWD/${pkg_name} | |
doc_root=${pkg_root}/docs/_build/html | |
hostname=`hostname` | |
year=`date '+%Y'` | |
version='0.1.0' | |
mkdir -p ${pkg_root}/${pkg_name} | |
mkdir -p ${pkg_root}/test | |
cat << EOF > ${pkg_root}/setup.py | |
from setuptools import setup | |
setup(name='$pkg_name', | |
version='$version', | |
author='$USER', | |
author_email='$USER@$hostname', | |
entry_points={ | |
'console_scripts': [ | |
'${pkg_name} = ${pkg_name}.main:run' | |
] | |
}, | |
test_suite='nose.collector' | |
) | |
EOF | |
touch ${pkg_root}/${pkg_name}/__init__.py | |
cat <<EOF > ${pkg_root}/${pkg_name}/main.py | |
""" | |
This module holds entry points of the ${pkg_name} package | |
""" | |
def run(): | |
"""Main console entry point""" | |
print "Hello, ${pkg_name}" | |
if __name__ == '__main__': | |
main() | |
EOF | |
cat <<EOF > ${pkg_root}/test/test_main.py | |
def test_main(): | |
assert 1 == 2, "Fix this test" | |
EOF | |
cat << EOF > ${pkg_root}/README.md | |
$pkg_name | |
========= | |
# Installation | |
This is a Python setuptools package. Use this command: | |
$ python setup.py install | |
# Usage | |
Please refer to the documentation | |
# License | |
See LICENSE.txt | |
EOF | |
cat << EOF > ${pkg_root}/LICENSE.txt | |
MIT License | |
Copyright (c) $year $USER | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
EOF | |
cat << EOF > ${pkg_root}/requirements.txt | |
nose | |
sphinx | |
EOF | |
cat << EOF > ${pkg_root}/.gitignore | |
.Python | |
/bin/ | |
/include/ | |
/lib/ | |
pip-selfcheck.json | |
EOF | |
pip --quiet install virtualenv | |
virtualenv --quiet ${pkg_root} | |
source ${pkg_root}/bin/activate | |
pip --quiet install -r ${pkg_root}/requirements.txt | |
git init --quiet ${pkg_root} | |
sphinx-quickstart --quiet \ | |
--project=${pkg_name} \ | |
--author=$USER \ | |
-v $version \ | |
--ext-autodoc \ | |
--no-batchfile \ | |
${pkg_root}/docs | |
cd ${pkg_root} | |
git add --all . | |
git commit --quiet -m "Initial commit" | |
git checkout --orphan gh-pages | |
git checkout master | |
mkdir -p ${doc_root} | |
git worktree add -b gh-pages ${doc_root} | |
cd ${doc_root} | |
git rm -rf . | |
git commit --quiet -m "Remove sources from documentation root" | |
cd ${pkg_root} | |
sed -i -e "s/# import os/import os/" docs/conf.py | |
sed -i -e "s/# import sys/import sys/" docs/conf.py | |
# Notice we insert the **parent directory** | |
sed -i -e "s/# sys.path.insert(0, os.path.abspath('.'))/sys.path.insert(0, os.path.abspath('..'))/" docs/conf.py | |
sphinx-apidoc -o docs/ ${pkg_name} | |
sed -i -e '12 a\ | |
\ \ \ modules' docs/index.rst | |
cd docs/ | |
make html | |
cd ${doc_root} | |
touch .nojekyll | |
git add --all . | |
git commit --quiet -m "Start HTML documentation" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example
Package creation
Now there are 2 working trees in the Git repo:
master
. Push toorigin
to publish source (or test) changes.docs/_build/html
. This points togh-pages
. Push toorigin
to publish documentation changes.Develop the package
Test the package
Document the package