1 | from ..api import * |
---|
2 | |
---|
3 | from trac.util.translation import _ |
---|
4 | |
---|
5 | from ConfigParser import ConfigParser |
---|
6 | |
---|
7 | import hglib |
---|
8 | import os |
---|
9 | |
---|
10 | class MercurialConnector(Component): |
---|
11 | """Add support for creating and managing HG repositories.""" |
---|
12 | |
---|
13 | implements(IAdministrativeRepositoryConnector) |
---|
14 | |
---|
15 | def get_supported_types(self): |
---|
16 | yield ('hg', 0) |
---|
17 | |
---|
18 | def can_fork(self, type): |
---|
19 | return True |
---|
20 | |
---|
21 | def can_delete_changesets(self, type): |
---|
22 | return True |
---|
23 | |
---|
24 | def can_ban_changesets(self, type): |
---|
25 | try: |
---|
26 | import hgban |
---|
27 | return True |
---|
28 | except: |
---|
29 | return False |
---|
30 | |
---|
31 | def create(self, repo): |
---|
32 | try: |
---|
33 | hglib.init(repo['dir']) |
---|
34 | except Exception, e: |
---|
35 | raise TracError(_("Failed to initialize repository: ") + str(e)) |
---|
36 | |
---|
37 | def fork(self, repo): |
---|
38 | try: |
---|
39 | hglib.clone(repo['origin_url'], repo['dir'], |
---|
40 | updaterev='null', pull=True) |
---|
41 | except Exception, e: |
---|
42 | raise TracError(_("Failed to clone repository: ") + str(e)) |
---|
43 | |
---|
44 | def delete_changeset(self, repo, rev, ban): |
---|
45 | try: |
---|
46 | from mercurial import ui, hg, repair |
---|
47 | hg_repo = hg.repository(ui.ui(), repo.directory) |
---|
48 | repair.strip(ui.ui(), hg_repo, [ hg_repo[rev].node() ], None) |
---|
49 | except Exception, e: |
---|
50 | raise TracError(_("Failed to strip changesets from repository: ") + str(e)) |
---|
51 | |
---|
52 | if ban: |
---|
53 | try: |
---|
54 | import hgban |
---|
55 | except: |
---|
56 | raise TracError(_("Could not import the hgban extension")) |
---|
57 | hgrc_path = os.path.join(repo.directory, '.hg/hgrc') |
---|
58 | |
---|
59 | hgrc = ConfigParser() |
---|
60 | hgrc.read(hgrc_path) |
---|
61 | |
---|
62 | if not hgrc.has_section('extensions'): |
---|
63 | hgrc.add_section('extensions') |
---|
64 | hgrc.set('extensions', 'hgban', '') |
---|
65 | |
---|
66 | revsets = '' |
---|
67 | if hgrc.has_section('hgban'): |
---|
68 | revsets = hgrc.get('hgban', 'revsets') |
---|
69 | else: |
---|
70 | hgrc.add_section('hgban') |
---|
71 | hgrc.set('hgban', 'revsets', revsets + "\n" + rev) |
---|
72 | |
---|
73 | with open(hgrc_path, 'wb') as hgrc_file: |
---|
74 | hgrc.write(hgrc_file) |
---|
75 | try: |
---|
76 | modes = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP |
---|
77 | os.chmod(hgrc_path, modes) |
---|
78 | except: |
---|
79 | pass |
---|
80 | |
---|
81 | def update_auth_files(self, repositories): |
---|
82 | for repo in repositories: |
---|
83 | writers = repo.maintainers() | repo.writers() |
---|
84 | writers = expand_user_set(self.env, writers) |
---|
85 | readers = expand_user_set(self.env, writers | repo.readers()) |
---|
86 | |
---|
87 | hgrc_path = os.path.join(repo.directory, '.hg/hgrc') |
---|
88 | |
---|
89 | hgrc = ConfigParser() |
---|
90 | hgrc.read(hgrc_path) |
---|
91 | |
---|
92 | options = ('deny_read', 'deny_push', |
---|
93 | 'allow_read', 'allow_push', 'allow_write') |
---|
94 | if hgrc.has_section('web'): |
---|
95 | for option in options: |
---|
96 | if hgrc.has_option('web', option): |
---|
97 | hgrc.remove_option('web', option) |
---|
98 | else: |
---|
99 | hgrc.add_section('web') |
---|
100 | |
---|
101 | if repo.description: |
---|
102 | hgrc.set('web', 'description', repo.description) |
---|
103 | |
---|
104 | def apply_user_list(users, action): |
---|
105 | if not users: |
---|
106 | hgrc.set('web', 'deny_' + action, '*') |
---|
107 | return |
---|
108 | if 'anonymous' in users: |
---|
109 | return |
---|
110 | if 'authenticated' in users: |
---|
111 | hgrc.set('web', 'deny_' + action, 'anonymous') |
---|
112 | return |
---|
113 | hgrc.set('web', 'allow_' + action, ', '.join(sorted(users))) |
---|
114 | |
---|
115 | apply_user_list(readers, 'read') |
---|
116 | if repo.maintainers(): |
---|
117 | apply_user_list(writers, 'write') |
---|
118 | else: |
---|
119 | apply_user_list(writers, 'push') |
---|
120 | |
---|
121 | with open(hgrc_path, 'wb') as hgrc_file: |
---|
122 | hgrc.write(hgrc_file) |
---|
123 | try: |
---|
124 | modes = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP |
---|
125 | os.chmod(hgrc_path, modes) |
---|
126 | except: |
---|
127 | pass |
---|