• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-python / 4182445342

pending completion
4182445342

Pull #3320

github-actions

GitHub
Merge f135d5076 into 0e5906373
Pull Request #3320: chore: fix coveralls comments

12864 of 23835 relevant lines covered (53.97%)

1.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.86
/renku/command/command_builder/database.py
1
# -*- coding: utf-8 -*-
2
#
3
# Copyright 2018-2022 - Swiss Data Science Center (SDSC)
4
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
5
# Eidgenössische Technische Hochschule Zürich (ETHZ).
6
#
7
# Licensed under the Apache License, Version 2.0 (the "License");
8
# you may not use this file except in compliance with the License.
9
# You may obtain a copy of the License at
10
#
11
#     http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS,
15
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
# See the License for the specific language governing permissions and
17
# limitations under the License.
18
"""Command builder for local object database."""
3✔
19

20

21
import json
3✔
22
import os
3✔
23
from typing import Optional
3✔
24

25
from packaging.version import Version
3✔
26

27
from renku.command.command_builder.command import Command, CommandResult, check_finalized
3✔
28
from renku.core import errors
3✔
29
from renku.core.interface.activity_gateway import IActivityGateway
3✔
30
from renku.core.interface.database_gateway import IDatabaseGateway
3✔
31
from renku.core.interface.dataset_gateway import IDatasetGateway
3✔
32
from renku.core.interface.plan_gateway import IPlanGateway
3✔
33
from renku.core.interface.project_gateway import IProjectGateway
3✔
34
from renku.core.interface.storage import IStorageFactory
3✔
35
from renku.domain_model.project_context import project_context
3✔
36
from renku.infrastructure.gateway.activity_gateway import ActivityGateway
3✔
37
from renku.infrastructure.gateway.database_gateway import DatabaseGateway
3✔
38
from renku.infrastructure.gateway.dataset_gateway import DatasetGateway
3✔
39
from renku.infrastructure.gateway.plan_gateway import PlanGateway
3✔
40
from renku.infrastructure.gateway.project_gateway import ProjectGateway
3✔
41
from renku.infrastructure.storage.factory import StorageFactory
3✔
42

43

44
class DatabaseCommand(Command):
3✔
45
    """Builder to get a database connection."""
46

47
    PRE_ORDER = 4
3✔
48
    POST_ORDER = 5
3✔
49

50
    def __init__(self, builder: Command, write: bool = False, path: Optional[str] = None, create: bool = False) -> None:
3✔
51
        self._builder = builder
3✔
52
        self._write = write
3✔
53
        self._path = path
3✔
54
        self._create = create
3✔
55
        self.project_found: bool = False
3✔
56

57
    def _injection_pre_hook(self, builder: Command, context: dict, *args, **kwargs) -> None:
3✔
58
        """Create a Database singleton."""
59
        from renku.version import __version__
3✔
60

61
        if not project_context.has_context():
3✔
62
            raise ValueError("Database builder needs a ProjectContext to be set.")
×
63

64
        project_context.push_path(path=self._path or project_context.path, save_changes=self._write)
3✔
65

66
        project_gateway = ProjectGateway()
3✔
67

68
        context["constructor_bindings"][IPlanGateway] = lambda: PlanGateway()
3✔
69
        context["constructor_bindings"][IActivityGateway] = lambda: ActivityGateway()
3✔
70
        context["constructor_bindings"][IDatabaseGateway] = lambda: DatabaseGateway()
3✔
71
        context["constructor_bindings"][IDatasetGateway] = lambda: DatasetGateway()
3✔
72
        context["constructor_bindings"][IProjectGateway] = lambda: project_gateway
3✔
73
        context["constructor_bindings"][IStorageFactory] = lambda: StorageFactory
3✔
74

75
        if int(os.environ.get("RENKU_SKIP_MIN_VERSION_CHECK", "0")) == 1:
3✔
76
            # NOTE: Used for unit tests
77
            return
3✔
78

79
        try:
×
80
            project = project_gateway.get_project()
×
81
            minimum_renku_version = Version(project.minimum_renku_version)
×
82
            self.project_found = True
×
83
        except (KeyError, ImportError, ValueError):
×
84
            try:
×
85
                with open(project_context.database_path / "project", "r") as f:
×
86
                    project = json.load(f)
×
87
                    min_version = project.get("minimum_renku_version")
×
88
                    if min_version is None:
×
89
                        return
×
90
                    minimum_renku_version = Version(min_version)
×
91
            except (KeyError, OSError, json.JSONDecodeError):
×
92
                # NOTE: We don't check minimum version if there's no project metadata available
93
                return
×
94

95
        current_version = Version(__version__)
×
96

97
        if current_version < minimum_renku_version:
×
98
            raise errors.MinimumVersionError(current_version, minimum_renku_version)
×
99

100
    def _post_hook(self, builder: Command, context: dict, result: CommandResult, *args, **kwargs) -> None:
3✔
101
        from renku.domain_model.project import Project
3✔
102

103
        if self._write and self.project_found:
3✔
104
            # NOTE: Fetch project again in case it was updated (the current reference would be put of date)
105
            project_gateway = ProjectGateway()
×
106
            project = project_gateway.get_project()
×
107

108
            if Version(project.minimum_renku_version) < Version(Project.minimum_renku_version):
×
109
                # NOTE: update minimum renku version on write as migrations might happen on the fly
110
                project.minimum_renku_version = Project.minimum_renku_version
×
111

112
        project_context.pop_context()
3✔
113

114
    @check_finalized
3✔
115
    def build(self) -> Command:
3✔
116
        """Build the command."""
117
        self._builder.add_injection_pre_hook(self.PRE_ORDER, self._injection_pre_hook)
3✔
118
        self._builder.add_post_hook(self.POST_ORDER, self._post_hook)
3✔
119

120
        return self._builder.build()
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc