File: __init__.py

package info (click to toggle)
freedombox 26.2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 82,976 kB
  • sloc: python: 48,504; javascript: 1,736; xml: 481; makefile: 290; sh: 167; php: 32
file content (244 lines) | stat: -rw-r--r-- 8,531 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# SPDX-License-Identifier: AGPL-3.0-or-later
"""FreedomBox app to configure Gitweb."""

from django.utils.translation import gettext_lazy as _

from plinth import app as app_module
from plinth import frontpage, menu
from plinth.config import DropinConfigs
from plinth.modules.apache.components import Webserver
from plinth.modules.backups.components import BackupRestore
from plinth.modules.firewall.components import Firewall
from plinth.modules.users.components import UsersAndGroups
from plinth.package import Packages

from . import manifest, privileged
from .forms import is_repo_url
from .manifest import GIT_REPO_PATH

_description = [
    _('Git is a distributed version-control system for tracking changes in '
      'source code during software development. Gitweb provides a web '
      'interface to Git repositories. You can browse history and content of '
      'source code, use search to find relevant commits and code. '
      'You can also clone repositories and upload code changes with a '
      'command-line Git client or with multiple available graphical clients. '
      'And you can share your code with people around the world.'),
    _('To learn more on how to use Git visit '
      '<a href="https://git-scm.com/docs/gittutorial">Git tutorial</a>.')
]


class GitwebApp(app_module.App):
    """FreedomBox app for Gitweb."""

    app_id = 'gitweb'

    _version = 4

    def __init__(self) -> None:
        """Create components for the app."""
        super().__init__()

        groups = {'git-access': _('Read-write access to Git repositories')}

        info = app_module.Info(app_id=self.app_id, version=self._version,
                               name=_('Gitweb'), icon_filename='gitweb',
                               description=_description, manual_page='GitWeb',
                               clients=manifest.clients, tags=manifest.tags)
        self.add(info)

        menu_item = menu.Menu('menu-gitweb', info.name, info.icon_filename,
                              info.tags, 'gitweb:index',
                              parent_url_name='apps')
        self.add(menu_item)

        shortcut = frontpage.Shortcut('shortcut-gitweb', info.name,
                                      icon=info.icon_filename, url='/gitweb/',
                                      clients=info.clients, tags=info.tags,
                                      login_required=True,
                                      allowed_groups=list(groups))
        self.add(shortcut)

        packages = Packages('packages-gitweb', ['gitweb', 'highlight'])
        self.add(packages)

        dropin_configs = DropinConfigs('dropin-configs-gitweb', [
            '/etc/gitweb-freedombox.conf',
            '/etc/apache2/conf-available/gitweb-freedombox.conf',
            '/etc/apache2/conf-available/gitweb-freedombox-auth.conf'
        ])
        self.add(dropin_configs)

        firewall = Firewall('firewall-gitweb', info.name,
                            ports=['http', 'https'], is_external=True)
        self.add(firewall)

        webserver = Webserver('webserver-gitweb', 'gitweb-freedombox',
                              urls=['https://{host}/gitweb/'])
        self.add(webserver)

        self.auth_webserver = GitwebWebserverAuth('webserver-gitweb-auth',
                                                  'gitweb-freedombox-auth')
        self.add(self.auth_webserver)

        users_and_groups = UsersAndGroups('users-and-groups-gitweb',
                                          groups=groups)
        self.add(users_and_groups)

        backup_restore = GitwebBackupRestore('backup-restore-gitweb',
                                             **manifest.backup)
        self.add(backup_restore)

    def post_init(self):
        """Perform post initialization operations."""
        if not self.needs_setup():
            self.update_service_access()

    def set_shortcut_login_required(self, login_required):
        """Change the login_required property of shortcut."""
        self.get_component('shortcut-gitweb').login_required = login_required

    def update_service_access(self):
        """Update the frontpage shortcut and webserver auth requirement."""
        repos = get_repo_list()
        if have_public_repos(repos):
            self._enable_public_access()
        else:
            self._disable_public_access()

    def _enable_public_access(self):
        """Allow Gitweb app to be accessed by anyone with access."""
        if self.is_enabled():
            self.auth_webserver.disable()

        self.set_shortcut_login_required(False)

    def _disable_public_access(self):
        """Allow Gitweb app to be accessed by logged-in users only."""
        if self.is_enabled():
            self.auth_webserver.enable()

        self.set_shortcut_login_required(True)

    def setup(self, old_version):
        """Install and configure the app."""
        super().setup(old_version)
        privileged.setup()
        if not old_version:
            self.enable()

    def uninstall(self):
        """De-configure and uninstall the app."""
        super().uninstall()
        privileged.uninstall()


class GitwebWebserverAuth(Webserver):
    """Component to handle Gitweb authentication webserver configuration."""

    is_leader = False

    def enable(self):
        """Enable apache configuration only if no public repository exists."""
        repos = get_repo_list()
        if not have_public_repos(repos):
            super().enable()


class GitwebBackupRestore(BackupRestore):
    """Component to handle backup/restore for Gitweb."""

    def restore_post(self, packet):
        """Update access after restoration of backups."""
        super().restore_post(packet)
        self.app.update_service_access()


def have_public_repos(repos):
    """Check for public repositories."""
    return any((repo['access'] == 'public' for repo in repos))


def create_repo(repo, repo_description, owner, is_private):
    """Create a new repository or clone a remote repository."""
    kwargs = {
        'url': None,
        'name': None,
        'description': repo_description,
        'owner': owner,
        'is_private': is_private
    }

    if is_repo_url(repo):
        kwargs['url'] = repo
        # create a repo directory and set correct access rights
        privileged.create_repo(prepare_only=True, **kwargs)
        # start cloning in background
        privileged.create_repo(skip_prepare=True, _run_in_background=True,
                               **kwargs)
    else:
        kwargs['name'] = repo
        privileged.create_repo(**kwargs)


def get_repo_list():
    """List all git repositories."""
    repos = []
    if GIT_REPO_PATH.exists():
        for repo in GIT_REPO_PATH.iterdir():
            if not repo.name.endswith('.git') or repo.name.startswith('.'):
                continue

            repo_info = {}
            repo_info['name'] = repo.name[:-4]

            private_file = repo / 'private'
            if private_file.exists():
                repo_info['access'] = 'private'
            else:
                repo_info['access'] = 'public'

            progress_file = repo / 'clone_progress'
            if progress_file.exists():
                repo_info['clone_progress'] = progress_file.read_text()

            repos.append(repo_info)

    return sorted(repos, key=lambda repo: repo['name'])


def repo_info(repo):
    """Get information about repository."""
    info = privileged.repo_info(repo)
    if info['access'] == 'private':
        info['is_private'] = True
    else:
        info['is_private'] = False
    del info['access']

    return info


def edit_repo(form_initial, form_cleaned):
    """Edit repository data."""
    repo = form_initial['name']

    if form_cleaned['name'] != repo:
        privileged.rename_repo(repo, form_cleaned['name'])
        repo = form_cleaned['name']

    if form_cleaned['description'] != form_initial['description']:
        privileged.set_repo_description(repo, form_cleaned['description'])

    if form_cleaned['owner'] != form_initial['owner']:
        privileged.set_repo_owner(repo, form_cleaned['owner'])

    if form_cleaned['is_private'] != form_initial['is_private']:
        if form_cleaned['is_private']:
            privileged.set_repo_access(repo, 'private')
        else:
            privileged.set_repo_access(repo, 'public')

    if form_cleaned['default_branch'] != form_initial['default_branch']:
        privileged.set_default_branch(repo, form_cleaned['default_branch'])