Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Unix tool to quickly add/remove some text to a Python script?

I'm developing an application using Flask.

I want a quick, automated way to add and remove debug=True to the main function call:

Development:

app.run(debug=True)

Production:

app.run()

For security reasons, as I might expose private/sensitive information about the app if I leave debug mode on "in the wild".

I was thinking of using sed or awk to automate this in a git hook (production version is kept in a bare remote repo that I push to), or including it in a shell script I am going to write to fire up uwsgi and some other "maintenance"-ey tasks that allow the app to be served up properly.

What do you think?

like image 947
ZenLikeThat Avatar asked Jun 06 '12 13:06

ZenLikeThat


2 Answers

That is not the way to go! My recommendation is to create some configuration Python module (let us say, config.py) with some content such as:

DEBUG = True

Now, in our current code, write this:

import config
app.run(debug=config.DEBUG)

Now, when you run in production, just change DEBUG from True to False. Or you can leave this file unversioned, so the copy of development is different of the copy of production. This is not uncommon since, for example, one does not use the same database connection params both in development and production.

Even if you want to update it automatically, just call sed on the config file with the -i flag. It is way more secure to update just this one file:

$ sed -i.bkp 's/^ *DEBUG *=.*$/DEBUG = False/' config.py
like image 80
brandizzi Avatar answered Sep 28 '22 07:09

brandizzi


You should set up some environment variable on server. Your script can detect presense of this variable and disable debugging.

like image 31
Pavel Strakhov Avatar answered Sep 28 '22 07:09

Pavel Strakhov