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 generate_data(repo): |
23 command = ['hg', '-R', authRepo, 'archive', '-r', 'default', '-t', 'tar', '-p'
, '.', '-'] | 23 command = ['hg', '-R', repo, '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 with tarfile.open(mode='r:', fileobj=StringIO(data)) as archive: |
29 for fileInfo in tarFile: | 29 for fileinfo in archive: |
30 name = fileInfo.name | 30 name = fileinfo.name |
31 if name.startswith('./'): | 31 if name.startswith('./'): |
32 name = name[2:] | 32 name = name[2:] |
33 | 33 |
34 if fileInfo.type == tarfile.REGTYPE and name.startswith('users/'): | 34 if fileinfo.type == tarfile.REGTYPE and name.startswith('users/'): |
35 name = os.path.basename(name).lower() | 35 name = os.path.basename(name).lower() |
36 options = [] | 36 options = [] |
37 match = re.search(r'^(.*)\[(.*)\]$', name) | 37 match = re.search(r'^(.*)\[(.*)\]$', name) |
38 if match: | 38 if match: |
39 name = match.group(1) | 39 name = match.group(1) |
40 options = match.group(2).split(',') | 40 options = match.group(2).split(',') |
41 | 41 |
42 user = { | 42 user = { |
43 'name': name, | 43 'name': name, |
44 'keytype': 'rsa', | 44 'keytype': 'rsa', |
45 'disabled': False, | 45 'disabled': False, |
46 'trusted': False, | 46 'trusted': False, |
47 'repos': [] | 47 'repos': [] |
48 } | 48 } |
49 for option in options: | 49 for option in options: |
50 if option == 'dsa': | 50 if option == 'dsa': |
51 user['keytype'] = 'dsa' | 51 user['keytype'] = 'dsa' |
52 elif option == 'disabled': | 52 elif option == 'disabled': |
53 user['disabled'] = True | 53 user['disabled'] = True |
54 elif option == 'trusted': | 54 elif option == 'trusted': |
55 user['trusted'] = True | 55 user['trusted'] = True |
| 56 else: |
| 57 print >>sys.stderr, 'Unknown user option: %s' % option |
| 58 user['key'] = re.sub(r'\s', '', archive.extractfile(fileinfo).read()) |
| 59 users[name] = user |
| 60 elif fileinfo.type == tarfile.REGTYPE and name.startswith('repos/'): |
| 61 repos.append(fileinfo) |
| 62 elif fileinfo.type == tarfile.REGTYPE and not name.startswith('.'): |
| 63 print >>sys.stderr, 'Unrecognized file in the repository: %s' % name |
| 64 |
| 65 for fileinfo in repos: |
| 66 name = os.path.basename(fileinfo.name).lower() |
| 67 repousers = archive.extractfile(fileinfo).readlines() |
| 68 for user in repousers: |
| 69 user = user.strip() |
| 70 if user == '' or user.startswith('#'): |
| 71 continue |
| 72 if user in users: |
| 73 users[user]['repos'].append(name) |
56 else: | 74 else: |
57 print >>sys.stderr, 'Unknown user option: %s' % option | 75 print >>sys.stderr, 'Unknown user listed for repository %s: %s' % (nam
e, user) |
58 user['key'] = re.sub(r'\s', '', tarFile.extractfile(fileInfo).read()) | |
59 users[name] = user | |
60 elif fileInfo.type == tarfile.REGTYPE and name.startswith('repos/'): | |
61 repos.append(fileInfo) | |
62 elif fileInfo.type == tarfile.REGTYPE and not name.startswith('.'): | |
63 print >>sys.stderr, 'Unrecognized file in the repository: %s' % name | |
64 | |
65 for fileInfo in repos: | |
66 name = os.path.basename(fileInfo.name).lower() | |
67 repoUsers = tarFile.extractfile(fileInfo).readlines() | |
68 for user in repoUsers: | |
69 user = user.strip() | |
70 if user == '' or user.startswith('#'): | |
71 continue | |
72 if user in users: | |
73 users[user]['repos'].append(name) | |
74 else: | |
75 print >>sys.stderr, 'Unknown user listed for repository %s: %s' % (name,
user) | |
76 | 76 |
77 for user in users.itervalues(): | 77 for user in users.itervalues(): |
78 if user['disabled']: | 78 if user['disabled']: |
79 continue | 79 continue |
80 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' % ( |
81 user['name'] if not user['trusted'] else '', | 81 user['name'] if not user['trusted'] else '', |
82 ' '.join(user['repos']), | 82 ' '.join(user['repos']), |
83 'ssh-rsa' if user['keytype'] == 'rsa' else 'ssh-dss', | 83 'ssh-rsa' if user['keytype'] == 'rsa' else 'ssh-dss', |
84 user['key'] | 84 user['key'] |
85 ) | 85 ) |
86 tarFile.close() | |
87 | 86 |
88 def hook(ui=None, repo=None, **kwargs): | 87 def hook(ui=None, repo=None, **kwargs): |
89 setupStderr() | 88 setupStderr() |
90 | 89 |
91 root = repo.root if repo != None else get_config().get('hg', 'auth_repository'
) | 90 root = repo.root if repo != None else get_config().get('hg', 'auth_repository'
) |
92 result = generateData(root) | 91 result = generate_data(root) |
93 | 92 |
94 file = open(get_config().get('hg', 'auth_file'), 'wb') | 93 with open(get_config().get('hg', 'auth_file'), 'wb') as file: |
95 for s in result: | 94 for s in result: |
96 file.write(s) | 95 file.write(s) |
97 file.close() | |
98 | 96 |
99 if __name__ == '__main__': | 97 if __name__ == '__main__': |
100 hook() | 98 hook() |
OLD | NEW |