Wednesday, May 30, 2012

Remote Repositories and GitHub

It's nice to be able to have version control by yourself, but what if you want to share your code with others? This is where remote repositories come in, and GitHub is one of the best.

Github Setup

There's a great tutorial here. Skip the first and last steps: "Download and Install Git" and "Set your GitHub Token"

Create a Repository

Make a remote repository on GitHub by following the directions here.


Git pull and push

There are now two additional commands that you will be using frequently:
git pull [options] [ [...]]
You can go to the link to see a full manual, but in essence this updates your local copy with the changes in the remote repository. Just running
$ git pull
Should suffice in most cases.
git push [options] [ [...]]
This pushes your local commits to the remote repository. After everything has been committed, just running
$ git push
Should suffice in most cases.

AITI Specific Instructions

It's now time to check out the labs. Go to your home directory
$ cd ~
And run
$ git clone git@github.com:theicfire/AITI-2012.git
All the labs from now on will be in
~/AITI-2012
Whenever there are new changes to the labs, or labs are added, run:
$ cd ~/AITI-2012
$ git pull

Introduction to Version Control and Git

Version control is one of the most useful tools that a programmer can have. It gives a very clear understanding of what has been programming, but more importantly, it gives the confidence to make radical changes. Simply put, version control allows a programmer to save a certain state of code, go back to that state of code, and compare different states of code. Git is the newest version control system (VCS), and it is understood to be better than all other types for various reasons that I will not go into.

So let's jump right into learning git.

Installation

Make sure you have git installed:
$ git --version
git version 1.7.9.5
$ sudo apt-get install git-core

Setup

Change your git config:
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@youremail.com"
$ git config --global color.ui true
These were just shorcuts to editing ~/.gitconfig. See this by running:
$ cat ~/.gitconfig
Make sure that git is ignoring files that don't need to be tracked. Edit ~/.gitignore
$ gedit ~/.gitignore
And put in:
*.pyc
This will make git ignore all files that have the file extension .pyc, which are files that speed up the loading of python (google for more info).

Learn Git!

Now start the git immersion tutorial on lab 3 by going here. This will hopefully teach you git. I've got a few ammendments that you should follow:
After Lab 5: Try out:
$ git diff
This will show you what has been added and removed in each file. Out of everything in git, I use this command the most.
Lab 8: This tells you how to interactively edit your commit messages. Don't worry about this. Just do:
$ git commit -m "Using ARGV"
$ git status
Lab 10: Just go over the first and second steps, that is:
$ git log
$ git log --pretty=oneline
The other parts are unnecessary
Lab 11: There are lots of shortcuts here. The only thing you need to do is the hist alias:
$ gedit ~/.gitconfig
Add this to the bottom to the bottom and save:
[alias]
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
Lab 13: Optional
Stop after lab 15 

Revised lab 16 and 17:
Make sure you are on master and have no changes
$ git checkout master
$ git status
There should be no changes in either Now edit hello.rb:
# oops, this was a bad comment
name = ARGV.first || "World"
puts "Hello, #{ARGV.first}!"
Check the difference:
$ git diff
Now commit:
$ git add hello.rb
$ git commit -m "Adding a bad commit"
$ git status
The status should be clean.
Look at the log:
$ git hist
To go back to another commit, do use git reset:
$ git reset <hash>
Where hash is the hash of the previous commit, as seen next to the commit in the hist or log. A hash is just random letters and numbers, like 72ba5ec.
Another way to do this is:
$ git reset HEAD~1
This means "reset to the parent of HEAD, that is one commit before HEAD" Notice that you are now at the point right before adding and committing:
$ git status
$ git diff
$ git hist
Go ahead and undo the changes
$ git checkout hello.rb
Your index and working directory should now have no changes:
$ git status
Congratulations! You should now know the basics of git. Remember, next time you want to initialize a repository, just do:
$ git init
$ git add .
$ git commit -m "Initializing directory"
Later on: pushing/pulling/remote repositories merging branching github

Sunday, May 27, 2012

Getting Started with Django on Heroku

There's a pretty good guide about how to set up django on Heroku here. However, the prerequisites that are mentioned can be a bit tricky to figure out, the sample application isn't all that good, and there are a few confusing steps. This is my attempt at fixing all of this.

Prerequisites (Modified)

To follow this tutorial, you will need

Local Notes Application Setup

Make a new folder called web in your home directory and go into it
$ cd ~
$ mkdir web
$ cd web
Checkout the notes application from git
$ git clone git@github.com:theicfire/heroku-django-notes.git
Go into the newly created directory and activate the environment:
$ cd heroku-django-notes
$ source venv/bin/activate
Change the database credentials by modifying the following line in main/settings.py to your Postgresql password
'PASSWORD': 'yourpass',                  # Not used with sqlite3.
Run the server:
$ foreman start
Go to localhost:5000 to make sure everything is working.

Deploy to Heroku

Go to the Heroku Django Tutorial and follow this instructions under "Deploy to Heroku"

Done

You should now be done with a fully functioning notes application, deployed on Heroku!

Notes and Troubleshooting

If you can't connect to database, make sure it's running and that the port in your settings.py file is correct:
$ grep postgresql /etc/services
postgresql      5432/tcp        postgres        # PostgreSQL Database
postgresql      5432/udp        postgres
The above output means that Posgresql is running on port 5432. Make sure this is consistent with that in your main/settings.py file.

Install Postgresql for Django

This should just be referenced for getting postgresql setup for testing purposes.
Install Postgres:
$ sudo apt-get install postgresql
This installs version 9.1 on my system. It automatically creates a user called postgres.
Set the password:
$ sudo passwd postgres
and enter a password twice.
Switch user:
$ su postgres
Create a user named django_login:
$ createuser -P django_login
and enter a password twice. This is the password you will be using in your settings.py file. Enter the Postgres shell:
$ psql template1
Create a database:
CREATE DATABASE django_db OWNER django_login ENCODING 'UTF8';
Get out of the db shell by typing:
$ \q
Edit the Postgres permissions in /etc/postgresql/9.1/main/pg_hba.conf by adding a line as follows after the postgres user line:
local      django_db   django_login   md5
Restart Postgres:
$ sudo /etc/init.d/postgresql restart
Download python-psycopg2 which lets Postgres talk to Python:
$ sudo apt-get install python-psycopg2
Edit your settings.py file in the django app directory:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_db',                      
        'USER': 'django_login',                   
        'PASSWORD': 'your_password',              
        'HOST': 'localhost',                      
        'PORT': '5432',           
    }
}
Make sure that this is the correct port by running:
$ grep postgresql /etc/services
Sync your db while in your django project directory:
$ python manage.py syncdb
If this works, you are now using Postgres with Django!
References: http://blog.iiilx.com/programming/how-to-install-postgres-on-ubuntu-for-django