Skip to content

Instantly share code, notes, and snippets.

@tkanmae
Last active December 18, 2015 03:39
Show Gist options
  • Save tkanmae/5719957 to your computer and use it in GitHub Desktop.
Save tkanmae/5719957 to your computer and use it in GitHub Desktop.
Initialize a data analysis project.
#!/usr/bin/env bash
\unalias -a
# Print the usage and exit..
#
print_usage_and_exit() {
cat <<EOF
Usage: init_data_analysis_project [-h|--help] PROJECT_NAME
EOF
exit 1
} >&2
# Print an error message and exit.
#
print_error_and_exit() {
echo "$@"
exit 1
} >&2
# Create .ipython/ in the current directory and initalize the default profile.
# This function does not check if for existing ipython directory.
#
init_ipython_dir() {
local ipython_dir="$1/.ipython"
mkdir $ipython_dir
cat <<EOF >"$ipython_dir/.gitignore"
README
Crash_report_ipython.txt
cython/
*.sqlite
profile_*/db/
profile_*/log/
profile_*/pid/
profile_*/security/
profile_*/startup/
profile_*/static/mathjax/
EOF
# TODO: Return a status code.
export IPYTOHNDIR="$ipython_dir"
command ipython profile create --ipython-dir="$ipython_dir"
export IPYTHONDIR=
# Insert the project's top directory to sys.path.
echo <<EOF >"$ipython_dir/profile_default/startup/00_sys_path.py"
"""Insert the project's top directory to sys.path."""
import os
import sys
sys.path.insert(0, os.path.dirname(get_ipython().ipython_dir))
del os, sys
EOF
}
# Create and initialize a directory tree:
#
# .ipython/
# data/
# notetooks/
# results/
# scripts/
#
init_dirtrees() {
local project_dir="$1"
mkdir "$project_dir/data"
cat <<EOF >"$project_dir/data/.gitignore"
*
!*/
!.gitignore
!**/*README
EOF
mkdir "$project_dir/results"
cat <<EOF >"$project_dir/results/.gitignore"
*
!*/
!.gitignore
!**/*README
EOF
mkdir "$project_dir/lib"
touch "$project_dir/lib/__init__.py"
mkdir "$project_dir/scripts"
touch "$project_dir/scripts/.gitkeep"
mkdir "$project_dir/notebooks"
touch "$project_dir/notebooks/.gitkeep"
init_ipython_dir "$project_dir"
}
init_project() {
local project_dir="$1"
# Check if an existing directory named as `project_dir`.
if [[ -d "$project_dir" ]]; then
print_error_and_exit "Unable to create a project:"\
"./$project_dir already exists."
fi
# Check if the current directory is under git control.
git rev-parse 2>/dev/null
if [[ $? == 0 ]]; then
print_error_and_exit "Unable to create a project:"\
"the current directory is part of a git repository."
fi
mkdir "$project_dir" && cd "$project_dir"
cat <<EOF >".gitignore"
# C/C++
*.o
*.a
*.la
*.so
# Python
*.pyc
ipynb_checkpoints/
# Mac
.DS_Store
.Spotlight-V100
.Trashes
._*
EOF
init_dirtrees "./"
git init && git add . && git commit -m "Initial import"
}
if [[ $# < 1 ]]; then
print_usage_and_exit
fi
case "$1" in
-h|--help)
print_usage_and_exit ;;
*)
init_project "$1" ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment