0

I'm working on a website using Django and I have Fabric as well, which is very useful for scripting some chunks of code that me and other developers use. I'm pretty new to all of these (and linux in general, tbh) so I have ideas, but I don't know how (or if) they are possible. Specifically, I wanted to write a script to start the server on a specific port that we use for testing. Manually, I would just run

python ~/project/manage.py runserver 0.0.0.0:8080

but that gets old. To manually implement that specific command, I have the following code in my fabfile:

def start8080():
    local("python ~/project/manage.py runserver 0.0.0.0:8080")

which works, but I'm not the only one using the port for testing, and ~/project/ is not the only project which would need to use a similar script. Is there a way to search down the tree from the directory you are working in for the first manage.py and then to run it the same command from there?

Kapura
  • 107
  • 6

2 Answers2

1

Farbic functions allow you to use arguments:

@task   #not bad to use once your fabfile is big by using helper functions
def runserver(project_path, port=8000):
    run("python %s/manage.py runserver 0.0.0.0:%s" % (project_path,port))

and you would use it like this:

fab runserver:/home/project,8080

you could also simplify it by creating a task that selects a port per project, although all available projects and their paths would have to be defined there. Then it could be as easy as:

fab runserver:myprojectname

Of course you could additionally implement @morgan's answer making the script check if the port is open and automatically assign a free one.

ashwoods
  • 2,209
  • 1
  • 22
  • 36
  • Is arguments the best way to handle it? I was hoping there was a solution in which no matter what project you're in, you would run the same command (no arguments required) and Fabric would search for the requisite manage.py automatically. Is that wishful thinking? – Kapura Jul 18 '12 at 21:23
  • no, of course its not wishful thinking. you can of course write code to intropect your project (git url, django project name, directory, you can use whatever you want) and use that information to its stuff. You still need a mapping, or a standard in how you name and where you put your code. imo there are better ways of handling testing servers: take a look at vagrant: http://klewel.com/conferences/djangocon-2012/index.php?talkID=17 Not the best talk given ;) – ashwoods Jul 19 '12 at 10:50
0

You could use the socket module, as shown here and have the os figure out your port, and then fabric just let you know which it chose.

Community
  • 1
  • 1
Morgan
  • 4,143
  • 27
  • 35
  • The port we've designated for testing is always 8080, and we generally only keep things open there for enough time to run a quick unit test. I'm more concerned with the fabric script being extensible to any project we are working on, ideally without having to specify the project I am in. The crux of my problem is whether or not it is possible to check the directory tree for the project's manage.py automatically. – Kapura Jul 18 '12 at 21:21