1
# Copyright (C) 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""bzr postinstall helper for win32 installation
18
Written by Alexander Belchenko
28
VERSION = "1.3.20060513"
30
USAGE = """Bzr postinstall helper for win32 installation
34
-h, --help - help message
35
-v, --version - version info
37
-n, --dry-run - print actions rather than execute them
38
-q, --silent - no messages for user
40
--start-bzr - update start_bzr.bat
41
--add-path - add bzr directory to environment PATH
42
--delete-path - delete bzr directory to environment PATH
43
--add-shell-menu - add shell context menu to start bzr session
44
--delete-shell-menu - delete context menu from shell
45
--check-mfc71 - check if MFC71.DLL present in system
46
""" % os.path.basename(sys.argv[0])
51
(OK, ERROR) = range(2)
52
VERSION_FORMAT = "%-50s%s"
61
user_encoding = locale.getpreferredencoding() or 'ascii'
65
hkey_str = {_winreg.HKEY_LOCAL_MACHINE: 'HKEY_LOCAL_MACHINE',
66
_winreg.HKEY_CURRENT_USER: 'HKEY_CURRENT_USER',
67
_winreg.HKEY_CLASSES_ROOT: 'HKEY_CLASSES_ROOT',
75
add_shell_menu = False
76
delete_shell_menu = False
80
opts, args = getopt.getopt(sys.argv[1:], "hvnq",
93
if o in ("-h", "--help"):
96
elif o in ("-v", "--version"):
97
print VERSION_FORMAT % (USAGE.splitlines()[0], VERSION)
100
elif o in ('-n', "--dry-run"):
102
elif o in ('-q', '--silent'):
105
elif o == "--start-bzr":
107
elif o == "--add-path":
109
elif o == "--delete-path":
111
elif o == "--add-shell-menu":
112
add_shell_menu = True
113
elif o == "--delete-shell-menu":
114
delete_shell_menu = True
115
elif o == "--check-mfc71":
118
except getopt.GetoptError, msg:
123
# message box from Win32API
124
MessageBoxA = ctypes.windll.user32.MessageBoxA
127
MB_ICONEXCLAMATION = 48
129
bzr_dir = os.path.dirname(sys.argv[0])
132
fname = os.path.join(bzr_dir, "start_bzr.bat")
133
if os.path.isfile(fname):
135
content = f.readlines()
138
content = ["bzr.exe help\n"]
140
for ix in xrange(len(content)):
142
if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
145
content[ix] = s.replace('bzr.exe',
146
'"%s"' % os.path.join(bzr_dir,
150
print "*** Write file: start_bzr.bat"
151
print "*** File content:"
152
print ''.join(content)
155
f.write(''.join(content))
158
if add_path or delete_path:
159
# find appropriate registry key:
160
# 1. HKLM\System\CurrentControlSet\Control\SessionManager\Environment
161
# 2. HKCU\Environment
162
keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
163
r'\Session Manager\Environment')),
164
(_winreg.HKEY_CURRENT_USER, r'Environment'),
168
for key, subkey in keys:
170
hkey = _winreg.OpenKey(key, subkey, 0, _winreg.KEY_ALL_ACCESS)
172
path_u, type_ = _winreg.QueryValueEx(hkey, 'Path')
174
if key != _winreg.HKEY_CURRENT_USER:
175
_winreg.CloseKey(hkey)
180
type_ = _winreg.REG_SZ
181
except EnvironmentError:
186
print "Cannot find appropriate registry key for PATH"
188
path_list = [i for i in path_u.split(os.pathsep) if i != '']
190
for ix, item in enumerate(path_list[:]):
196
print "*** Bzr already in PATH"
199
if add_path and not delete_path:
200
path_list.append(bzr_dir.decode(user_encoding))
204
path_u = os.pathsep.join(path_list)
206
print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
207
print "*** Modify PATH variable. New value:"
210
_winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
211
_winreg.FlushKey(hkey)
214
_winreg.CloseKey(hkey)
216
if add_shell_menu and not delete_shell_menu:
219
hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
221
except EnvironmentError:
224
'Unable to create registry key for context menu',
226
MB_OK | MB_ICONERROR)
229
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
230
hkey2 = _winreg.CreateKey(hkey, 'command')
231
_winreg.SetValue(hkey2, '', _winreg.REG_SZ,
232
'cmd /K "%s"' % os.path.join(bzr_dir,
234
_winreg.CloseKey(hkey2)
235
_winreg.CloseKey(hkey)
237
if delete_shell_menu:
239
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
240
r'Folder\shell\bzr\command')
241
except EnvironmentError:
245
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
247
except EnvironmentError:
252
ctypes.windll.LoadLibrary('mfc71.dll')
255
("Library MFC71.DLL is not found on your system.\n"
256
"This library needed for SFTP transport.\n"
257
"If you need to work via SFTP you should download\n"
258
"this library manually and put it to directory\n"
259
"where Bzr installed.\n"
260
"For detailed instructions see:\n"
261
"http://bazaar-vcs.org/BzrOnPureWindows"
264
MB_OK | MB_ICONEXCLAMATION)
269
if __name__ == "__main__":