~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to lib/dbupgradeprogress.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Miro - an RSS based video player application
 
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
17
#
 
18
# In addition, as a special exception, the copyright holders give
 
19
# permission to link the code of portions of this program with the OpenSSL
 
20
# library.
 
21
#
 
22
# You must obey the GNU General Public License in all respects for all of
 
23
# the code used other than OpenSSL. If you modify file(s) with this
 
24
# exception, you may extend this exception to your version of the file(s),
 
25
# but you are not obligated to do so. If you do not wish to do so, delete
 
26
# this exception statement from your version. If you delete this exception
 
27
# statement from all source files in the program, then also delete it here.
 
28
 
 
29
"""dbupgradeprogress.py -- Send updates about progress upgrading the
 
30
database.
 
31
"""
 
32
 
 
33
from miro.gtcache import gettext as _
 
34
from miro import messages
 
35
 
 
36
_doing_20_upgrade = False
 
37
 
 
38
def doing_20_upgrade():
 
39
    """Call this if we are upgrading from a 2.0-style database.
 
40
 
 
41
    This will calibrate the progress bar to take into account the fact
 
42
    that we are running old-style upgrades and running the code in
 
43
    convert20database.py.
 
44
    """
 
45
    global _doing_20_upgrade
 
46
    _doing_20_upgrade = True
 
47
 
 
48
def upgrade_start():
 
49
    """Call at the begining of the database upgrades."""
 
50
    messages.DatabaseUpgradeStart().send_to_frontend()
 
51
 
 
52
def upgrade_end():
 
53
    """Call at the end of the database upgrades."""
 
54
    messages.DatabaseUpgradeEnd().send_to_frontend()
 
55
 
 
56
def old_style_progress(start_version, current_version, end_version):
 
57
    """Call while stepping through old-style upgrades"""
 
58
    progress = _calc_progress(start_version, current_version, end_version)
 
59
    total = 0.05 * progress # old style upgrades take us from 0% -> 5%
 
60
    _send_message(_('Upgrading Old Database'), progress, total)
 
61
 
 
62
def convert20_progress(current_step, total_step):
 
63
    """Call while stepping through 2.0 DB conversion code."""
 
64
    progress = _calc_progress(0, current_step, total_step)
 
65
    total = 0.05 + 0.80 * progress # conversion take us from 5% -> 85%
 
66
    _send_message(_('Converting Old Database'), progress, total)
 
67
 
 
68
def new_style_progress(start_version, current_version, end_version):
 
69
    """Call while stepping through new-style upgrades"""
 
70
    progress = _calc_progress(start_version, current_version, end_version)
 
71
    if _doing_20_upgrade:
 
72
        total = 0.85 + 0.15 * progress
 
73
        # new style upgrades take us from 85% -> 100%
 
74
    else:
 
75
        total = progress
 
76
        # We didn't do 2.0 conversion.  New style upgrades take us
 
77
        # from 0% to 100%.
 
78
    _send_message(_('Upgrading Database'), progress, total)
 
79
 
 
80
def _send_message(stage, stage_progress, total_progress):
 
81
    messages.DatabaseUpgradeProgress(stage, stage_progress,
 
82
            total_progress).send_to_frontend()
 
83
 
 
84
def _calc_progress(start, current, end):
 
85
    total = end - start
 
86
    # if total is 0.0 or less, then we return 0.0 to avoid the
 
87
    # ZeroDivisionError.  this happens in unit testing.
 
88
    if total <= 0.0:
 
89
        return 0.0
 
90
    return float(current - start) / (end - start)