Wednesday, June 7, 2017

Dealing with outdated or abandoned pypi modules with VCS install

So I was using a small, simple but working Paypal module which unfortunately was forgotten or abandon by the original author. It was working on my Django 1.5 site up until I needed to upgrade to Django 1.10 because of the other modules. Also for security and stability reasons but that's another story for another time.

Fortunately, this Paypal module wasn't "super" broken. The errors were just a couple of lines in the templates and a function named patterns() from django.url.conf -

This leads into a couple of solutions:

1. I could copy-pasta the code and turn it into a internal django app with the fixes.

2. I could fork it, fix and then setup by own pypi server.

3. I could find out if someone fixed this already and take it from there.

Of course, I'd pick #3 - Programmer. So, lazy. Look it up.

I was lucky to found out that someone has fixed all the issues and got it running on Django 1.10. All that's left is to fork his repo and install it into my virtual environment.

It's simple because pip already supports installing modules from a VCS.

$ pip install git+

If you need install a certain branch do this instead

$ pip install git+

This also works with other VCS like Mercurial (hg+https), Bazaar (bzr+https) or for-fuck-sakes why! - SVN (svn+https).

Wednesday, May 31, 2017

Windows Python 3.6, VS2017 and the case of the missing basetsd.h

So you decided to join the Python 3 bandwagon on Windows with your new shiny Visual Studio. And then you start work on a fresh virtual environment and then you do a pip install or something. Sometimes, you'll end up with a compile error just like this:
WTF is this missing 'basetsd.h' file
You'll google this and end up with one of many stackoverflow topics stating you must install Visual Studio 2015 or some SDK.

Fuck that and save your time. You can do better because a programmer named Dimitri Janczak found a solution that works and I can confirm it works. 

The solution is as follows:

1. Install or update setuptools (> 34.0). Do note that setuptools is sort of tied with your version of Pip. So you might have to do: python -m pip install -U pip setuptools

2. The next step is to pick the correct command-line environment. What?! I know right, Visual Studio comes with multiple command prompts. 

Select the appropriate Native Tools Command prompt. Navigate to the project folder. Activate the virtual environment and do what you did last before you encountered the compile error.

If you goes to plan then you should get a successful result. 

Thursday, May 11, 2017

Using git push to deploy code to DigitalOcean

There'll be times using a continue integration server or stack is a big pain in the butt. Sometimes we just want to type git push from the terminal and the live app is updated. This is that tutorial although specific to DigitalOcean. I'll bet it will roughly work the same for other cloud services.

  • You have a Digital Ocean server (or droplet) running and configured
  • You have the ssh to said droplet working
So ssh to the droplet and then navigate to /var/www (or anywhere else you want to put your code) and make two directories:

$ mkdir && mkdir appcode.git
$ cd appcode.git

Inside the appcode.git folder we will create a "bare" git repository: git init --bare
A bare git repository doesn't contain our code but rather the internals of the .git folder. The whole point of this is to access the hooks folder within.

The hooks folder have scripts that can be run at certain steps within the git repo's lifecycle. We want a hook script to run after we "receive" a push to this repo. Hence we create a post-receive file inside the hooks folder.

$ vi post-receive

# Type this in

git --work-tree=/var/www/ --git-dir=/var/www/appcode.git checkout -f

# save and exit
# make the post-receive hook executable

$ chmod +x post-receive

There! We are done for the server side. We move to the our local machines.

All we actually need is need is to add a remote target in your working git repo. Assuming you added the droplet in your local ssh config then the command should be look like:

$ git remote add droplet ssh://user@ip-of-droplet/var/www/appcode.git

# add a new commit - this will not work even if you existing commits
# you must have a new commit
$ git add .
$ git commit -m "new commit"

# push to repo - could be github or bitbucket
$ git push

# push to droplet
$ git push droplet master

If all goes to plan, then our code will show in our new "git work tree" which is the folder inside our droplet.

A couple more thoughts:
  1. It's fairly easy to setup a sort of staging or testing folder/area with this.
  2. The post-receive script can do more things like a "restart app" or send mail to admin for notification. It's just a bash script.