コード例 #1
0
def serialize_to_response(app_labels=[], exclude=[], response=None,
                          format=SMUGGLER_FORMAT, indent=SMUGGLER_INDENT):
    response = response or HttpResponse(mimetype='text/plain')
    # There's some funky output redirecting going on as Django >= 1.5 writes
    # to a wrapped output stream, instead of just returning the dumped output.
    stream = StringIO()  # this is going to be our stdout
    # We need to fake an OutputWrapper as it's only introduced in Django 1.5
    out = lambda: None
    out.write = lambda s: stream.write(s)  # this seems to be sufficient.
    try:
        # Now make sys.stdout our wrapped StringIO instance and start the dump.
        sys.stdout = out
        dumpdata = DumpData()
        dumpdata.stdout = sys.stdout
        dumpdata.stderr = sys.stderr
        output = dumpdata.handle(*app_labels, **{
            'exclude': exclude,
            'format': format,
            'indent': indent,
            'show_traceback': True,
            'use_natural_keys': True
        })
    except CommandError:
        # We expect and re-raise CommandErrors, these contain "user friendly"
        # error messages.
        raise
    else:
        if output:
            response.write(output)
        else:
            response.write(stream.getvalue())
        return response
    finally:
        # Be nice and cleanup!
        sys.stdout = sys.__stdout__
コード例 #2
0
ファイル: routing.py プロジェクト: DjangoBD/django-synctool
 def view(request):
     cmd = Command()
     cmd.stdout = StringIO()
     cmd.handle(label, format="json", exclude=[])
     return HttpResponse(
         content=cmd.stdout.getvalue(),
         content_type="application/json",
     )
コード例 #3
0
ファイル: backup.py プロジェクト: jeonghanlee/md_inventory
    def handle(self, *args, **options):

        pdict = {'origin': 'django app backup'}
        outfile = options.get('output')
        verb = int(options.get('verbosity', '1'))

        bnum = int(options.get('backup'))
        if bnum and os.path.isfile(outfile):
            # backup file names
            backups = ['%s.%d' % (outfile, n) for n in range(bnum)]
            backups.reverse()  # .2, .1, .0
            if verb > 1:
                print 'backups', backups

            # rotate
            for n in range(1, len(backups)):
                TO, FROM = backups[n - 1], backups[n]
                if verb > 1:
                    print 'rotate', FROM, TO
                if os.path.isfile(FROM):
                    # eg. Replace .1 with .0
                    if os.path.isfile(TO):
                        os.remove(TO)
                    os.rename(FROM, TO)

            # .0 should not exist
            assert not os.path.isfile(backups[-1])

            os.rename(outfile, backups[-1])

        output = tarfile.open(name=outfile,
                              mode='w:gz',
                              format=tarfile.PAX_FORMAT,
                              pax_headers=pdict)
        try:
            with tempfile.SpooledTemporaryFile(mode='w+') as dout:

                dumpcmd = DumpCommand()

                dumpcmd.stdout = dout
                dumpcmd.stderr = self.stderr
                dumpcmd.handle(*args, **options)
                if verb > 1:
                    print 'raw DB dump size', dout.tell()

                dname = output.tarinfo('db.json')
                dname.type = tarfile.REGTYPE
                dname.size = dout.tell()
                dname.mtime = time.time()
                dname.uid = os.geteuid()
                dname.gid = os.getegid()

                dout.seek(0)
                output.addfile(dname, dout)

            if options.get('media') and os.path.isdir(settings.MEDIA_ROOT):
                if verb > 1:
                    print 'backup MEDIA_ROOT =', settings.MEDIA_ROOT
                output.add(settings.MEDIA_ROOT, 'media')

            if options.get('static') and os.path.isdir(settings.STATIC_ROOT):
                if verb > 1:
                    print 'backup STATIC_ROOT =', settings.STATIC_ROOT
                output.add(settings.STATIC_ROOT, 'static')

            if verb > 2:
                output.list()
            output.close()
        except:
            # delete partial output on error
            output.close()
            os.remove(outfile)
            raise