Passed
Pull Request — 2.x (#1933)
by Ramon
05:34
created

senaite.core.scripts._upgrade_sites   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 58
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 3
A upgrade() 0 22 5
A get_site_ids() 0 5 1
A run_upgrades_until_stuck() 0 15 4
1
# -*- coding: utf-8 -*-
2
3
import transaction
4
from AccessControl.SecurityManagement import newSecurityManager
5
from bika.lims import api
6
from senaite.core import logger
7
from senaite.core.decorators import retriable
8
from senaite.core.scripts import parser
9
from senaite.core.scripts.utils import setup_site
10
11
__doc__ = """Run upgrade profiles on sites
12
"""
13
14
parser.description = __doc__
15
16
17
def get_site_ids(app):
18
    """Returns a list of available site ids
19
    """
20
    sites = app.objectValues("Plone Site")
21
    return map(api.get_id, sites)
22
23
24
def run_upgrades_until_stuck(portal_setup,
25
                             profile_id,
26
                             original_steps_to_run=None,
27
                             first_iteration=False):
28
    steps_to_run = portal_setup.listUpgrades(profile_id)
29
    if steps_to_run:
30
        if first_iteration:
31
            logger.debug("Running profile upgrades for {}".format(profile_id))
32
        elif steps_to_run == original_steps_to_run:
33
            return steps_to_run
34
        portal_setup.upgradeProfile(profile_id)
35
        return run_upgrades_until_stuck(
36
            portal_setup,
37
            profile_id,
38
            steps_to_run,
39
        )
40
41
42
@retriable(sync=True)
43
def upgrade(site):
44
    setup_site(site)
45
46
    # attempt to upgrade plone first
47
    pm = site.portal_migration
48
    report = pm.upgrade(dry_run=False)
49
    if report:
50
        logger.debug(report)
51
    ps = site.portal_setup
52
    # go through all profiles that need upgrading
53
    for profile_id in ps.listProfilesWithUpgrades():
54
        if not profile_id.startswith("senaite"):
55
            continue
56
        remaining = run_upgrades_until_stuck(
57
            ps, profile_id, first_iteration=True)
58
        if remaining:
59
            raise Exception(
60
                '[{}] Running upgrades did not finish all upgrade steps: {}'
61
                .format(profile_id, remaining))
62
63
    transaction.commit()
64
65
66
def run(app):
67
    args, _ = parser.parse_known_args()
68
    user = app.acl_users.getUser("admin")
69
    newSecurityManager(None, user.__of__(app.acl_users))
70
71
    if args.site_id:
72
        site = app[args.site_id]
73
        upgrade(site)
74
    else:
75
        for sid in get_site_ids(app):
76
            site = app[sid]
77
            upgrade(site)
78
79
80
if __name__ == "__main__":
81
    run(app)  # noqa
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable app does not seem to be defined.
Loading history...
82