diff options
| author | Claude Paroz <claude@2xlibre.net> | 2015-07-21 23:24:32 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2015-07-24 18:37:55 +0200 |
| commit | c296e55dc6a697c7e4d4be92b954633cda4a79b1 (patch) | |
| tree | e6c242384036dd8cf4360a3ff66241e2f6762fb9 /django/core | |
| parent | 03aec35a12ea522ab9a60d6229b53e3ec3a871a3 (diff) | |
Fixed #22258 -- Added progress status for dumpdata when outputting to file
Thanks Gwildor Sok for the report and Tim Graham for the review.
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/management/base.py | 5 | ||||
| -rw-r--r-- | django/core/management/commands/dumpdata.py | 23 | ||||
| -rw-r--r-- | django/core/serializers/base.py | 30 |
3 files changed, 51 insertions, 7 deletions
diff --git a/django/core/management/base.py b/django/core/management/base.py index 7b31a69478..665b7da1fb 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -89,7 +89,7 @@ class OutputWrapper(object): @style_func.setter def style_func(self, style_func): - if style_func and hasattr(self._out, 'isatty') and self._out.isatty(): + if style_func and self.isatty(): self._style_func = style_func else: self._style_func = lambda x: x @@ -102,6 +102,9 @@ class OutputWrapper(object): def __getattr__(self, name): return getattr(self._out, name) + def isatty(self): + return hasattr(self._out, 'isatty') and self._out.isatty() + def write(self, msg, style_func=None, ending=None): ending = self.ending if ending is None else ending if ending and not msg.endswith(ending): diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 01b229ce36..1468a92e6c 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -127,8 +127,11 @@ class Command(BaseCommand): raise CommandError("Unknown serialization format: %s" % format) - def get_objects(): - # Collate the objects to be serialized. + def get_objects(count_only=False): + """ + Collate the objects to be serialized. If count_only is True, just + count the number of objects to be serialized. + """ for model in serializers.sort_dependencies(app_list.items()): if model in excluded_models: continue @@ -141,17 +144,27 @@ class Command(BaseCommand): queryset = objects.using(using).order_by(model._meta.pk.name) if primary_keys: queryset = queryset.filter(pk__in=primary_keys) - for obj in queryset.iterator(): - yield obj + if count_only: + yield queryset.order_by().count() + else: + for obj in queryset.iterator(): + yield obj try: self.stdout.ending = None + progress_output = None + object_count = 0 + # If dumpdata is outputting to stdout, there is no way to display progress + if (output and self.stdout.isatty() and options['verbosity'] > 0): + progress_output = self.stdout + object_count = sum(get_objects(count_only=True)) stream = open(output, 'w') if output else None try: serializers.serialize(format, get_objects(), indent=indent, use_natural_foreign_keys=use_natural_foreign_keys, use_natural_primary_keys=use_natural_primary_keys, - stream=stream or self.stdout) + stream=stream or self.stdout, progress_output=progress_output, + object_count=object_count) finally: if stream: stream.close() diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index 23a0c7b72f..0e2e25d946 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -27,6 +27,29 @@ class DeserializationError(Exception): return cls("%s: (%s:pk=%s) field_value was '%s'" % (original_exc, model, fk, field_value)) +class ProgressBar(object): + progress_width = 75 + + def __init__(self, output, total_count): + self.output = output + self.total_count = total_count + self.prev_done = 0 + + def update(self, count): + if not self.output: + return + perc = count * 100 // self.total_count + done = perc * self.progress_width // 100 + if self.prev_done >= done: + return + self.prev_done = done + cr = '' if self.total_count == 1 else '\r' + self.output.write(cr + '[' + '.' * done + ' ' * (self.progress_width - done) + ']') + if done == self.progress_width: + self.output.write('\n') + self.output.flush() + + class Serializer(object): """ Abstract serializer base class. @@ -35,6 +58,7 @@ class Serializer(object): # Indicates if the implemented serializer is only available for # internal Django use. internal_use_only = False + progress_class = ProgressBar def serialize(self, queryset, **options): """ @@ -46,10 +70,13 @@ class Serializer(object): self.selected_fields = options.pop("fields", None) self.use_natural_foreign_keys = options.pop('use_natural_foreign_keys', False) self.use_natural_primary_keys = options.pop('use_natural_primary_keys', False) + progress_bar = self.progress_class( + options.pop('progress_output', None), options.pop('object_count', 0) + ) self.start_serialization() self.first = True - for obj in queryset: + for count, obj in enumerate(queryset, start=1): self.start_object(obj) # Use the concrete parent class' _meta instead of the object's _meta # This is to avoid local_fields problems for proxy models. Refs #17717. @@ -67,6 +94,7 @@ class Serializer(object): if self.selected_fields is None or field.attname in self.selected_fields: self.handle_m2m_field(obj, field) self.end_object(obj) + progress_bar.update(count) if self.first: self.first = False self.end_serialization() |
