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

I am developing an application using Flask .

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

Development:

 app.run(debug=True) 

Production:

 app.run() 

For security reasons, since I can publish confidential / confidential information about the application, if I leave the debugging mode to "in the wild."

I thought of using sed or awk to automate this in a git hook (the production version is stored in a bare remote repo that I click on), or by including it in a shell script, I'm going to write to run uwsgi and some other β€œmaintenance” tasks that allow The application is normally served.

What do you think?

+6
source share
5 answers

You probably shouldn't use app.run in production (and you definitely don't need this if you use uwsgi ). Instead, use one of several deployment options discussed in the deployment section of the excellent Flask documentation. ( app.run simply calls werkzeug.serving.run_simple , which runs Python enabled wsgiref server.)

At the same time, the right way to do this is not with editing the post-deployment into the source code, but with a server-specific configuration file that changes your settings as @brandizzi indicated in his answer .

You can do this in several different ways (Flask has documentation about this too - see Armin's suggestions for configuring from files and handling the development-development switch ):

  • Include both your designs and your server configuration in your repository. To switch between them, use the environment variable:

     # your_app.config.develop DEBUG = True # your_app.config.production DEBUG = False # your_app.app from flask import Flask from os import environ mode = environ.get("YOURAPP_MODE") mode = "production" if mode is None else "develop" config = __import__("your_app.config." + mode) app = Flask("your_app") app.config.from_object(config) 
  • Save the configuration of your products in a separate repository along with any other server configurations you may need. Download the configuration if the environment variable is set.

+4
source

This is not the way! My recommendation is to create some Python configuration module (say config.py ) with some content, for example:

 DEBUG = True 

Now in our current code write:

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

Now that you start production, just change DEBUG from True to False . Or you can leave this file unversioned, so the development copy is different from the production copy. This is not uncommon, because, for example, the same database connection parameters are not used both in development and in production.

Even if you want to update it automatically, just call sed in the configuration file with the -i flag. It is safer to update only one file:

 $ sed -i.bkp 's/^ *DEBUG *=.*$/DEBUG = False/' config.py 
+12
source

You must configure some environment variable on the server. Your script can detect the presence of this variable and disable debugging.

+5
source

I would use sed :

 sed 's/debug=True//' 

portable, scriptable, ubiquitous.

+4
source

You can also use the NOCOMMIT hook (from gitty ):

Set it like a pre-hook

 if git diff --cached | grep NOCOMMIT > /dev/null; then echo "You tried to commit a line containing NOCOMMIT" exit 1 fi exit 0 

This will prevent commit if it contains NOCOMMIT .

You can, of course, directly replace NOCOMMIT with Debug=True with a hook.

+3
source

Source: https://habr.com/ru/post/917446/


All Articles