OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2013 Eyeo GmbH | 4 # Copyright (C) 2006-2013 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import os, re, sys, subprocess, tarfile | 18 import os, re, sys, subprocess, tarfile |
19 from StringIO import StringIO | 19 from StringIO import StringIO |
20 from sitescripts.utils import get_config, setupStderr | 20 from sitescripts.utils import get_config, setupStderr |
21 | 21 |
22 def generateData(authRepo): | 22 def generateData(authRepo): |
23 command = ['hg', '-R', authRepo, 'archive', '-r', 'default', '-t', 'tar', '-p'
, '.', '-'] | 23 command = ['hg', '-R', authRepo, 'archive', '-r', 'default', '-t', 'tar', '-p'
, '.', '-'] |
24 data = subprocess.check_output(command) | 24 data = subprocess.check_output(command) |
25 | 25 |
26 users = {} | 26 users = {} |
27 repos = [] | 27 repos = [] |
28 tarFile = tarfile.open(mode='r:', fileobj=StringIO(data)) | 28 tarFile = tarfile.open(mode='r:', fileobj=StringIO(data)) |
29 fileInfo = tarFile.next() | 29 for fileInfo in tarFile: |
30 while fileInfo: | |
31 name = fileInfo.name | 30 name = fileInfo.name |
32 if name.startswith('./'): | 31 if name.startswith('./'): |
33 name = name[2:] | 32 name = name[2:] |
34 | 33 |
35 if fileInfo.type == tarfile.REGTYPE and name.startswith('users/'): | 34 if fileInfo.type == tarfile.REGTYPE and name.startswith('users/'): |
36 name = os.path.basename(name).lower() | 35 name = os.path.basename(name).lower() |
37 options = [] | 36 options = [] |
38 match = re.search(r'^(.*)\[(.*)\]$', name) | 37 match = re.search(r'^(.*)\[(.*)\]$', name) |
39 if match: | 38 if match: |
40 name = match.group(1) | 39 name = match.group(1) |
(...skipping 14 matching lines...) Expand all Loading... |
55 elif option == 'trusted': | 54 elif option == 'trusted': |
56 user['trusted'] = True | 55 user['trusted'] = True |
57 else: | 56 else: |
58 print >>sys.stderr, 'Unknown user option: %s' % option | 57 print >>sys.stderr, 'Unknown user option: %s' % option |
59 user['key'] = re.sub(r'\s', '', tarFile.extractfile(fileInfo).read()) | 58 user['key'] = re.sub(r'\s', '', tarFile.extractfile(fileInfo).read()) |
60 users[name] = user | 59 users[name] = user |
61 elif fileInfo.type == tarfile.REGTYPE and name.startswith('repos/'): | 60 elif fileInfo.type == tarfile.REGTYPE and name.startswith('repos/'): |
62 repos.append(fileInfo) | 61 repos.append(fileInfo) |
63 elif fileInfo.type == tarfile.REGTYPE and not name.startswith('.'): | 62 elif fileInfo.type == tarfile.REGTYPE and not name.startswith('.'): |
64 print >>sys.stderr, 'Unrecognized file in the repository: %s' % name | 63 print >>sys.stderr, 'Unrecognized file in the repository: %s' % name |
65 fileInfo = tarFile.next() | |
66 | 64 |
67 for fileInfo in repos: | 65 for fileInfo in repos: |
68 name = os.path.basename(fileInfo.name).lower() | 66 name = os.path.basename(fileInfo.name).lower() |
69 repoUsers = tarFile.extractfile(fileInfo).readlines() | 67 repoUsers = tarFile.extractfile(fileInfo).readlines() |
70 for user in repoUsers: | 68 for user in repoUsers: |
71 user = user.strip() | 69 user = user.strip() |
72 if user == '' or user.startswith('#'): | 70 if user == '' or user.startswith('#'): |
73 continue | 71 continue |
74 if user in users: | 72 if user in users: |
75 users[user]['repos'].append(name) | 73 users[user]['repos'].append(name) |
76 else: | 74 else: |
77 print >>sys.stderr, 'Unknown user listed for repository %s: %s' % (name,
user) | 75 print >>sys.stderr, 'Unknown user listed for repository %s: %s' % (name,
user) |
78 | 76 |
79 for user in users.itervalues(): | 77 for user in users.itervalues(): |
80 if user['disabled']: | 78 if user['disabled']: |
81 continue | 79 continue |
82 yield 'no-pty,environment="HGUSER=%s",environment="HGREPOS=%s" %s %s\n' % ( | 80 yield 'no-pty,environment="HGUSER=%s",environment="HGREPOS=%s" %s %s\n' % ( |
83 user['name'] if not user['trusted'] else '', | 81 user['name'] if not user['trusted'] else '', |
84 ' '.join(user['repos']), | 82 ' '.join(user['repos']), |
85 'ssh-rsa' if user['keytype'] == 'rsa' else 'ssh-dss', | 83 'ssh-rsa' if user['keytype'] == 'rsa' else 'ssh-dss', |
86 user['key'] | 84 user['key'] |
87 ) | 85 ) |
| 86 tarFile.close() |
88 | 87 |
89 def hook(ui, repo, **kwargs): | 88 def hook(ui, repo, **kwargs): |
90 setupStderr() | 89 setupStderr() |
91 | 90 |
92 result = generateData(get_config().get('hg', 'auth_repository')) | 91 result = generateData(repo) |
93 | 92 |
94 file = open(get_config().get('hg', 'auth_file'), 'wb') | 93 file = open(get_config().get('hg', 'auth_file'), 'wb') |
95 for s in result: | 94 for s in result: |
96 file.write(s) | 95 file.write(s) |
97 file.close() | 96 file.close() |
98 | 97 |
99 if __name__ == '__main__': | 98 if __name__ == '__main__': |
100 hook() | 99 hook(None, get_config().get('hg', 'auth_repository')) |
OLD | NEW |