Cloning a django project from hg using buildout and using it for development - django

I have a django project sitting in a bitbucket repository. Is it possible to automate using buildout, the following process:
1. Install django
2. clone the django project from hg repository
3. install the dependency modules of the django project
Update: I have achieved what I wanted to, with the help of mr.developer extension as suggested by Ross.
While doing that I had another question popping up. Which is the best place to specify the dependencies - in the buildout.cfg or in the setup.py of the 'develop' modules? For now I have duplicated the specification.

Generally, you make your checkout the buildout itself, so you'd place buildout.cfg and bootstrap.py in your project root. That way when someone checks out/clones your project, they just do the bootstrap/buildout dannce and they're up and running.
If you have multiple checkouts, then look into mr.developer.

Related

Same Django project different GIT repositories

Which is the best way to have two different repositories on the same Django project?
I started developing a project months ago and I have the whole folder in the repository. I want to reuse some apps in the project and I would like to create a different repository for them since they will be spin-offs project. But I want to keep it updated.
Which is the best workflow, methodology, etc... to achieve this? Or is it a bad approuch?
Thanks!
Xavi
You can wrap each app as a python package, which has its own GIT repo. And save all your packages in some private (or public?) python packages repository (like Gemfury).
Then, in your projects, just use the app as you install django itself.. pip install myapp
This way the apps a reusable and decoupled from any project.
(This works very well for myself.. perhaps there is a better way)
You can use submodule,
$git submodule add git://github.com/yourusername/project2.git project2
$cat .gitmodules
.gitmodules output:
[submodule "project2"]
path = project2
url = git://github.com/yourusername/project2.git
If you want to Clone some git project like submodule,
git clone git://github.com/yourusername/project2.git
cd project2
git submodule init

Maintaining 3rd Party Django Apps as Git submodules

I know this is perfectly possible and a lot of people already doing it, but my problem is slightly different and I couldn't figure out the solution yet:
Let a 3rd party Django App's structure as below:
django-module
module
init.py
views.py
models.py
requirements.txt
setup.py
I want to bundle only the module directory as a submodule, because then I can access views.py file just by typing "module.views". If I import the django-module directory, I would have to write django-module.module.views to access the module files, which is not feasible.
My purpose is to modify the module and make pull requests occasionally to the original repository. Is there workflow that I can follow, or what are the best practices for this purpose?
Pip tips
Pip has support for editable packages and retrieving packages with git, so you could create a virtualenv, use pip to install the packages, and update them using pip when you want.
So you could add:
-e git://git.myproject.org/MyProject.git#da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject
To your requirements.txt to retrieve that exact commit.
Suggested workflow
I think that the best way to solve your issue is the following:
Make a private fork of the package
Edit the package in a specific development branch in the forked repository
Use the package from your fork's development branch in your requirements file.
When you feel like it, update the forked package where you're using it using pip.
When you're ready to make a pull request, pull origin, rebase your working branch to origin/master, and make the pull request from the master branch of your fork.
This means you have three places where the code is present:
The original repo (where you don't have access)
Your forked repo (where you work on your fork)
Wherever pip installed it (where you use your fork)
The best practice is to keep the submodules exactly as they are.
Once you've added a 3rd party module to your app as a submodule, the next step is to make sure you add that "django-module" directory to your python path. As long as django-module is on your python path, you will be able to use the submodule as you usually would by typing "module.views" as you wish.

Create a Django project in pydev from a mercurial repo

I like using the python editor pydev in Eclipse. I want to team up with some people who've a python project (actually a Django site) stored in a mercurial repo. Can one create a new Django project in pydev that grabs code from a Mercurial repo? If so, how?
Thanks
Arthur
Doing a google search on does eclipse support mercurial gives you:
MercurialEclipse - an Eclipse plugin for Mercurial support, and I would suggest to start from here. This should work regardless of what sort of project you're working on.
A discussion on whether it is better to use git or Mercurial with Eclipse, which can help you out.
Additionally, if you didn't want to install the MercurialEclipse plugin, you could use Mercurial independently of Eclipse, clone the repository and then start a project with those files. In this case I would just make sure that your project meta files are in Mercurial's ignore list so they wouldn't get pushed to the remote repository.
If there's anything you'd like me to elaborate on, please comment. Good luck.

Using virtualenv with legacy Django projects

I am finally going to start using virtualenv for my Django projects on my development machine. Before I start I want to know if there are any special considerations for dealing with my existing projects. My presumed workflow is something like:
make a new virtualenv
activate the new virtualenv
Install Django in there
pip install all the packages I know I need for my existing project
copy my Django project files, app files, and git files into the project folder within the virtualenv.
Edit
6. make requirements file for deployment
This is obviously very simplified but are there any steps or considerations I am fundamentally missing? Is git going to be happy about moving? Also is it best practice to have a separate virtualenv for each Django project?
I know this is not a typical code problem, but I hope those that know more than I do can point me in the right direction.
Many thanks.
I don't see any big issue on migrating your projects and I think your 5-steps plan is correct, in particular, for steps 3/4/5 (I'd merge them), you can handle project dependencies with pip, possibly using requirement files.
Requirement files are plain text files telling to pip which packages have to be installed in your virtualenv, included your git-tracked projects which eventually can be deployed in your virtual environment as development eggs (they bring with them version control infos).
Once you have a req file, it's a matter of:
pip install -r file.req
to have all needed packages installed in your env.
As you can see from virtualenv docs, a typical req file would contain something like:
django==1.3.0
-e git://git.myproject.org/MyProject.git#egg=MyProject
I usually keep each project in its own virtualenv, so I can deploy it to the production server the same way I do for local development.

Django + SVN + Deployment

I'm a strong proponent of version control, and am starting work on a Django project. I've done a few before, and have tried a few different approaches, but I haven't yet found a decent structure that I actually feel comfortable with.
Here's what I want:
a) Source code checked into version control
b) Preferably the environment is not checked into version control (something like buildout or pip requirements.txt is fine for setting up the environment)
c) A reasonable "get a new developer going" story
d) A reasonable deployment story - preferably the entire deployment environment could be generated by a script on the server
It seems to me like someone has to have done this before, but many hours of searching have all led to half-baked solutions that don't really address all of these.
Any thoughts on where I should look?
Look at fabric to manage deployments.
This is what I use to manage servers/deployments with fabric: louis (it is just a collection of fabric commands). I keep a louisconf.py file with each project.
I'd recommend using a distributed VCS (git, hg,...) instead of svn. The reason being that the ease of branching allows for several schemes for deployment. You can have, for example, production and staging branches. Then you enforce that the only merges into production happen from staging by convention.
As for getting developers started quickly you have it right with pip and requirements.txt. I think that also means that you are using virtualenv, but if not that's the third piece. I'd recommend getting a basic README in place. Have the first assignment of each developer that joins a project be to update the README.
The rough way to get someone on board is to have her checkout the code, create a virtualenv, and install the requirements.
I'd recommend having a settings.py file that works with sqlite3 and such that a new developer can use to just get going fast (ie after installing the requirements). However, how you manage the different settings files depends on your project layout. There should be some set of default settings for new developers to use, though.
I keep a projects/ directory in my home directory (on Linux). When I need to start a new project, I make a new, shortly-named (that sufficiently describes the project) dir in projects/; that becomes the root of a new virtualenv (with --no-site-packages) for that project.
Inside that dir (after I've installed the venv, sourced it, and installed the copy of django I'll be working with), I "django-admin.py startproject" a subdir, normally by the same short name. That dir becomes the root of my hg repo (with a quick hg init and ci), no matter how small the project.
If there's any chance of sharing the project with other developers (a project for work, for example), I include a pip requirements.txt at the repo root. Only project requirements go in there; django-debug-toolbar and django-extensions, staples for my dev workflow, are not project requirements, for example. South, when we use it, is.
As for the django project, I normally keep the default settings.py, possibly with a few changes, and add the local_settings convention to the end of it (try: from local_settings import *; except ImportError: pass). My and other devs' specific environment settings (adding django-extensions and django-debug-toolbar to installed apps, for example) go in local_settings.py, which is not checked in to version control. To help a new dev out, you could provide a template of that file as local_settings.py.temp, or some other name that won't be used for any other purpose, but I find that this unnecessarily clutters the repo.
For personal projects, I normally include a README if I plan on releasing it publicly. At work, we maintain Trac environments and good communication to get new devs up to speed on a project.
As for deployment, as rz mentioned, I hear fabric is really good for that kind of automated local/remote scripting, though I haven't really taken the chance myself to look into it.
For the uninitiated, a typical shell session for this might look like the following:
$ cd ~/projects/
$ mkdir newproj
$ cd newproj/
$ virtualenv --no-site-packages .
$ source bin/activate
(newproj)$ pip install django django-debug-toolbar django-extensions
... installing stuff ...
(newproj)$ django-admin.py startproject newproj
(newproj)$ cd newproj/
(newproj)$ hg init .; hg ci -A -m "Initial code"