Skip to content

Cheatsheet | Virtual Environment and Build Flow

Lenny edited this page Apr 12, 2019 · 2 revisions

Overview

This page was created to explain the build process in detail and to shed light on the environment setup. During session #1, you:

  1. Cloned the repository to get access to the code
  2. Added several aliases to the .bashrc to get access to some new commands on your Git Bash terminal
  3. Downloaded Python 3.7 and an Integrated Development Environment (IDE) for writing code
  4. Ran the build environment script
  5. Downloaded Slack and got on GitHub

The following sections serve as Q/A to clarify any questions that came up during session 1.

1 | Cloning Repository

  1. What does it mean to clone a repository?
    1. You are downloading a copy of some source code and linking your code to the remote code that is hosted on GitHub.
    2. You also needed to be added to the NiaStemGroup so that you would have permission to push code back up to the repository

2 | Bashrc

  1. What is a .bashrc file?
    1. It is a script that is run every time that you open your interactive terminal (e.g. Git Bash). More details: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work
  2. Why did we make one?
    1. So that you get some commands already available to you by default on your Git Bash command line to make it easier to work with PyStockAnalyze.

3 | Downloaded Python 3.7

  1. Why did I have to specifically download Python 3.7.2?
    1. Because Lennard sucks at provisioning virtual environments--I setup the virtual environment to have overly specific requirements, which broke down if the user had lower versions of Python. In future runs of our Python sessions this will be addressed.

4 | Build Environment

  1. What did the buildenv command do?
    1. It called a script inside of your repository, tools/buildenv.sh, which did the following:
      1. Setup environment variables to provide ease of use aliases:
        1. activate - Enables the built virtual environment, called only after environment has been built
        2. manage - Shortcut for running "python manage.py"; For instance python manage.py runserver is replaced with manage runserver
        3. gits - Shortcut for running "git flow feature start"
        4. gite - Shortcut for running "git flow feature finish"
      2. The first time that it is run for a repository:
        1. Creates virtual environment in build/pydjango-build-env
        2. Activates the virtual environment
        3. Upgrades pip, the python package manager
        4. Installs the list of library dependencies specified in the tools/requirements.txt files

Build Environment Detailed Inline Comments

#!/bin/bash -e

echo "[build] Creating project environment variables"
PROJ_ROOT_DIR=$(git rev-parse --show-toplevel) # Finds repository root directory
PROJ_BUILD_PATH=$PROJ_ROOT_DIR/build # Path to build folder
PROJ_SRC_PATH=$PROJ_ROOT_DIR/src # Path to source folder
PROJ_TOOL_PATH=$PROJ_ROOT_DIR/tools # Path to tools folder

PROJ_VENV_NAME='pydjango-build-env' # Name of virtual environment
PROJ_VENV_PATH=$PROJ_BUILD_PATH/$PROJ_VENV_NAME # Path to virtual environment
PROJ_ACTENV_PATH=$PROJ_VENV_PATH/Scripts/activate # Path to the activate script

# Pointer to manage python script
PROJ_DJNAME='pysdjango' # Django project folder name
PROJ_DJANGO_PATH=$PROJ_SRC_PATH/$PROJ_DJNAME # Path to django project
PROJ_MANAGE_PATH=$PROJ_DJANGO_PATH/manage.py # Path to the Django manage.py script
alias manage='python $PROJ_MANAGE_PATH' # Shortcut to run python manage.py <command>

# Local environment (not-to-be-commited) directory
PROJ_LOC_PATH=$PROJ_DJANGO_PATH/$PROJ_DJNAME/localenv


# Create activate script pointer
alias activate='source $PROJ_ACTENV_PATH' # Alias to make it possible to run activate from any folder from within the project

# Section only ran if the build environment hasn't been run
# if there are any environment changes, the user must run cleanenv followed by rerunning buildenv
if [ ! -d $PROJ_VENV_PATH ]; then
	if [ ! -d $PROJ_BUILD_PATH ]; then
		mkdir $PROJ_BUILD_PATH # Create build folder if it doesn't exist
	fi

	echo "[build] Creating python virtual environment"
	python -m venv "$PROJ_VENV_PATH" # Create virtual environment (Python 3.x+ only)
	# Install dependencies into environment
	echo "[build] Activate virtual environment"
	activate # Jump into the virtual environment so that packages can be installed there

	echo "[build] Upgrading pip installation"
	python -m pip install --upgrade pip $PROXY_OPT # Upgrade pip

	echo "[build] Instsalling dependencies"
	pip install -r $PROJ_TOOL_PATH/requirements.txt $PROXY_OPT # Install all files listed in the requirements.txt file
else
	echo "[build] Python virtual environment already exists"
fi

alias gits='git flow feature start'
alias gite='git flow feature finish'

5 | Slack + GitHub

Clone this wiki locally