diff --git a/layerindex/tools/import_otherdistro.py b/layerindex/tools/import_otherdistro.py index 68f91d2..1b43bb2 100755 --- a/layerindex/tools/import_otherdistro.py +++ b/layerindex/tools/import_otherdistro.py @@ -359,6 +359,11 @@ def import_pkgspec(args): return ret updateobj = get_update_obj(args) + logdir = getattr(settings, 'TASK_LOG_DIR') + if updateobj and updateobj.task_id and logdir: + pwriter = utils.ProgressWriter(logdir, updateobj.task_id, logger=logger) + else: + pwriter = None metapath = args.pkgdir @@ -367,7 +372,9 @@ def import_pkgspec(args): layerrecipes = ClassicRecipe.objects.filter(layerbranch=layerbranch) existing = list(layerrecipes.filter(deleted=False).values_list('filepath', 'filename')) - for entry in os.listdir(metapath): + dirlist = os.listdir(metapath) + total = len(dirlist) + for count, entry in enumerate(dirlist): if os.path.exists(os.path.join(metapath, entry, 'dead.package')): logger.info('Skipping dead package %s' % entry) continue @@ -398,6 +405,8 @@ def import_pkgspec(args): rupdate.save() else: logger.warn('Missing spec file in %s' % os.path.join(metapath, entry)) + if pwriter: + pwriter.write(int(count / total * 100)) if existing: fpaths = ['%s/%s' % (pth, fn) for pth, fn in existing] diff --git a/layerindex/utils.py b/layerindex/utils.py index b1544fb..04599a1 100644 --- a/layerindex/utils.py +++ b/layerindex/utils.py @@ -460,3 +460,43 @@ def timesince2(date, date2=None): if period: return '%d %s' % (period, singular if period == 1 else plural) return '0 seconds' + +class ProgressWriter(): + def __init__(self, logdir, task_id, logger=None): + self.logger = logger + self.fn = os.path.join(logdir, '%s.progress' % task_id) + self.last_value = None + self.write('') + + def write(self, value): + if self.fn is None: + return + if value == self.last_value: + return + try: + with open(self.fn + '.temp', 'w') as f: + f.write(str(value)) + os.rename(self.fn + '.temp', self.fn) + except Exception as e: + if self.logger is not None: + self.logger.warning('Failed to write to progress file %s: %s' % (self.fn, str(e))) + +class ProgressReader(): + def __init__(self, logdir, task_id, logger=None): + self.fn = os.path.join(logdir, '%s.progress' % task_id) + self.fd = None + self.logger = logger + self.last_mtime = None + self.last_value = None + + def read(self): + result = None + try: + mtime = os.path.getmtime(self.fn) + if mtime != self.last_mtime: + with open(self.fn, 'r') as f: + result = f.read() + except Exception as e: + if self.logger is not None: + self.logger.warning('Failed to read progress: %s' % str(e)) + return result diff --git a/layerindex/views.py b/layerindex/views.py index 3302054..8dc1b61 100644 --- a/layerindex/views.py +++ b/layerindex/views.py @@ -1400,8 +1400,11 @@ def task_log_view(request, task_id): response['Task-Done'] = '1' updateobj = get_object_or_404(Update, task_id=task_id) response['Task-Duration'] = utils.timesince2(updateobj.started, updateobj.finished) + response['Task-Progress'] = 100 else: response['Task-Done'] = '0' + preader = utils.ProgressReader(settings.TASK_LOG_DIR, task_id) + response['Task-Progress'] = preader.read() return response class ComparisonRecipeSelectView(ClassicRecipeSearchView): diff --git a/templates/layerindex/task.html b/templates/layerindex/task.html index c2e9c28..a57e730 100644 --- a/templates/layerindex/task.html +++ b/templates/layerindex/task.html @@ -23,6 +23,13 @@
{{ update.log }}
+{% if not update.finished %} +
+
+
+
+{% endif %} + {% if update.comparisonrecipeupdate_set.exists %}

Updated comparison recipes