Gestion de contact et suivi de conversion pour les groupement d'agriculture biologique
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

72 lines
1.9 KiB

#! /usr/bin/env python
import argparse
import os
import sys
from importlib import import_module
import django
from django.db import IntegrityError, transaction
from gaby.settings import DJANGO_SETTINGS_MODULE
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
# following from Python cookbook, #475186
def has_colours(stream):
if not hasattr(stream, "isatty"):
return False
if not stream.isatty():
return False # auto color only on TTYs
try:
import curses
curses.setupterm()
return curses.tigetnum("colors") > 2
except Exception:
# guess false in case of error
return False
HAS_COL = has_colours(sys.stdout)
def p_col(text, color=WHITE):
if HAS_COL:
f_str = "\x1b[1;{0:d}m".format(30 + color) + text + "\x1b[0m"
else:
f_str = text
sys.stdout.write(f_str)
# Entry point #
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="quick launch python script in django context"
)
parser.add_argument(
"py_script", help="python script, (as a python module, w/o .py)"
)
parser.add_argument(
"-c",
"--commit",
help="commit changes in database (default: rollback)",
action="store_true",
default=False,
)
args = parser.parse_args()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DJANGO_SETTINGS_MODULE)
django.setup()
try:
with transaction.atomic():
import_module(args.py_script)
if not args.commit:
raise IntegrityError("dry_run")
except IntegrityError as err:
yolo = err
if "dry_run" not in err.args:
raise
else:
p_col("# DRY RUN - changes in database were rollbacked #\n", GREEN)
else:
p_col("# REAL RUN - changes in database were committed #\n", YELLOW)
Map all the world