From f1a4da7553aad14d2b80c756d637a3e00ed2020f Mon Sep 17 00:00:00 2001 From: Ronny Abraham Date: Wed, 7 Sep 2016 21:52:40 +0300 Subject: [PATCH] added check_app method, and fixtures method from debates_eu modified: modules/django.py --- modules/django.py | 125 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 104 insertions(+), 21 deletions(-) diff --git a/modules/django.py b/modules/django.py index 5e0cc35..23f7eca 100644 --- a/modules/django.py +++ b/modules/django.py @@ -626,42 +626,125 @@ def makemigrations_empty(param="help"): @task -def create_fixtures(param=None): +def fixtures(appname=None, backup=False): """ - param is the appname for this fixture + @appname django app name for this fixture + @backup default False, store in server backup dir? """ configuration = env.config - if param == "help": - print "print this help message" - return - - if param is None: - appname = None - else: - appname = param + # booleanize the backup parameter + backup = booleanize(backup) print "debug - appname: %s" % appname from fabric.api import * - path_root = configuration.paths.project.root - path_data = os.path.join(path_root, 'extras', 'data', 'fixtures') - path_backups = os.path.join(path_root, 'extras', 'backups', 'fixtures') + path_data = configuration.paths.django.fixtures + path_backup = configuration.paths.server.backups.fixtures + + print "debug - path_backup: %s" % path_backup if appname is not None: path_data = os.path.join(path_data, appname) - path_backups = os.path.join(path_backups, appname) - path_fixture = os.path.join(path_backups, "%s.json" % appname) + path_backup = os.path.join(path_backup, appname) + + fixture_name = "%s.json" % appname else: - path_fixture = os.path.join(path_backups, "all.json") + fixture_name = "all.json" + + if backup: + fix_split = os.path.splitext(fixture_name) + fixture_name = fix_split[0] + "_backup" + fix_split[1] + path_fixture = os.path.join(path_backup, fixture_name) + else: + path_fixture = os.path.join(path_data, fixture_name) + + print "debug - path_fixture: %s" % path_fixture from utils import ensure_dir - ensure_dir(path_data) - ensure_dir(path_backups) + ensure_dir(path_backup) - output = manage('dumpdata %s --indent 2' % appname) + from fabric.context_managers import hide + with hide('running', 'stdout', 'stderr'): + # get rid of things like "localhost: out" + output = manage('dumpdata %s --indent 2' % appname) - f = open(path_fixture, 'w') - f.write(output) + # Now go through the output and ignore all the extra + # information there. + import re + # first get rid of everything until the last time + # "[localhost] out:" is printed + + if configuration.project.host == 'localhost': + p = re.compile("\[localhost\] out:") + for match in p.finditer(output): + pass + pos = match.end() + output = output[pos:] + + # now find the first occurence of [ on a newline, e.g. + # [ + # { + # "model.auth": 40, + # etc. + + p = re.compile(r"\n\[") + m = p.search(output) + pos = m.end() - 1 + output = output[pos:] + + from StringIO import StringIO + fixture_iostring = StringIO(output) + + fabric_ops.put(fixture_iostring, path_fixture) + + +@task +def loaddata(param=None): + """ + loads fixture data to the specified app from extras/data + """ + + configuration = env.config + + if param is None: + print "you must specify an appname to load the fixture to" + return + else: + appname = param + + path_data = configuration.paths.django.fixtures + + if appname is not None: + path_data = os.path.join(path_data, appname) + path_fixture = os.path.join(path_data, "%s.json" % appname) + else: + path_fixture = os.path.join(path_data, "all.json") + + manage("loaddata %s" % path_fixture) + + +@task +def check_app(appname): + """ + check if is installed in settings + + @appname: the name of the app being checked for + + returns True/False + """ + + from fabric.context_managers import hide + with hide('running', 'stderr'): + output = manage('listapps') + + import re + p = re.compile(appname) + match = p.search(output) + + if match: + return True + else: + return False