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
31
VERSION = "1.5.20070131"
33
USAGE = """Bzr postinstall helper for win32 installation
37
-h, --help - help message
38
-v, --version - version info
40
-n, --dry-run - print actions rather than execute them
41
-q, --silent - no messages for user
43
--start-bzr - update start_bzr.bat
44
--add-path - add bzr directory to environment PATH
45
--delete-path - delete bzr directory to environment PATH
46
--add-shell-menu - add shell context menu to start bzr session
47
--delete-shell-menu - delete context menu from shell
48
--check-mfc71 - check if MFC71.DLL present in system
49
""" % os.path.basename(sys.argv[0])
52
_major,_minor,_build,_platform,_text = sys.getwindowsversion()
54
raise Exception('This platform does not supported!')
64
(OK, ERROR) = range(2)
65
VERSION_FORMAT = "%-50s%s"
75
user_encoding = locale.getpreferredencoding() or 'ascii'
79
hkey_str = {_winreg.HKEY_LOCAL_MACHINE: 'HKEY_LOCAL_MACHINE',
80
_winreg.HKEY_CURRENT_USER: 'HKEY_CURRENT_USER',
81
_winreg.HKEY_CLASSES_ROOT: 'HKEY_CLASSES_ROOT',
89
add_shell_menu = False
90
delete_shell_menu = False
94
opts, args = getopt.getopt(sys.argv[1:], "hvnq",
107
if o in ("-h", "--help"):
110
elif o in ("-v", "--version"):
111
print VERSION_FORMAT % (USAGE.splitlines()[0], VERSION)
114
elif o in ('-n', "--dry-run"):
116
elif o in ('-q', '--silent'):
119
elif o == "--start-bzr":
121
elif o == "--add-path":
123
elif o == "--delete-path":
125
elif o == "--add-shell-menu":
126
add_shell_menu = True
127
elif o == "--delete-shell-menu":
128
delete_shell_menu = True
129
elif o == "--check-mfc71":
132
except getopt.GetoptError, msg:
137
# message box from Win32API
138
MessageBoxA = ctypes.windll.user32.MessageBoxA
141
MB_ICONEXCLAMATION = 48
143
bzr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
146
fname = os.path.join(bzr_dir, "start_bzr.bat")
147
if os.path.isfile(fname):
149
content = f.readlines()
152
content = ["bzr.exe help\n"]
154
for ix in xrange(len(content)):
156
if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
159
content[ix] = s.replace('bzr.exe',
160
'"%s"' % os.path.join(bzr_dir,
164
print "*** Write file: start_bzr.bat"
165
print "*** File content:"
166
print ''.join(content)
169
f.write(''.join(content))
172
if (add_path or delete_path) and winver == 'Windows NT':
173
# find appropriate registry key:
174
# 1. HKLM\System\CurrentControlSet\Control\SessionManager\Environment
175
# 2. HKCU\Environment
176
keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
177
r'\Session Manager\Environment')),
178
(_winreg.HKEY_CURRENT_USER, r'Environment'),
182
for key, subkey in keys:
184
hkey = _winreg.OpenKey(key, subkey, 0, _winreg.KEY_ALL_ACCESS)
186
path_u, type_ = _winreg.QueryValueEx(hkey, 'Path')
188
if key != _winreg.HKEY_CURRENT_USER:
189
_winreg.CloseKey(hkey)
194
type_ = _winreg.REG_SZ
195
except EnvironmentError:
200
print "Cannot find appropriate registry key for PATH"
202
path_list = [i for i in path_u.split(os.pathsep) if i != '']
204
for ix, item in enumerate(path_list[:]):
210
print "*** Bzr already in PATH"
213
if add_path and not delete_path:
214
path_list.append(bzr_dir.decode(user_encoding))
218
path_u = os.pathsep.join(path_list)
220
print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
221
print "*** Modify PATH variable. New value:"
224
_winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
225
_winreg.FlushKey(hkey)
228
_winreg.CloseKey(hkey)
230
if (add_path or delete_path) and winver == 'Windows 98':
231
# mutating autoexec.bat
232
# adding or delete string:
233
# SET PATH=%PATH%;C:\PROGRA~1\Bazaar
234
abat = 'C:\\autoexec.bat'
235
abak = 'C:\\autoexec.bak'
237
def backup_autoexec_bat(name, backupname, dry_run):
238
# backup autoexec.bat
239
if os.path.isfile(name):
241
shutil.copyfile(name, backupname)
243
print '*** backup copy of autoexec.bat created'
245
GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
246
buf = ctypes.create_string_buffer(260)
247
if GetShortPathName(bzr_dir, buf, 260):
248
bzr_dir_8_3 = buf.value
250
bzr_dir_8_3 = bzr_dir
251
pattern = 'SET PATH=%PATH%;' + bzr_dir_8_3
255
lines = f.readlines()
259
if i.rstrip('\r\n') == pattern:
263
if delete_path and found:
264
backup_autoexec_bat(abat, abak, dry_run)
268
if i.rstrip('\r\n') != pattern:
272
print '*** Remove line <%s> from autoexec.bat' % pattern
274
elif add_path and not found:
275
backup_autoexec_bat(abat, abak, dry_run)
282
print '*** Add line <%s> to autoexec.bat' % pattern
284
if add_shell_menu and not delete_shell_menu:
287
hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
289
except EnvironmentError:
292
'Unable to create registry key for context menu',
294
MB_OK | MB_ICONERROR)
297
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
298
hkey2 = _winreg.CreateKey(hkey, 'command')
299
_winreg.SetValue(hkey2, '', _winreg.REG_SZ,
301
os.environ.get('COMSPEC', '%COMSPEC%'),
302
os.path.join(bzr_dir, 'start_bzr.bat')))
303
_winreg.CloseKey(hkey2)
304
_winreg.CloseKey(hkey)
306
if delete_shell_menu:
308
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
309
r'Folder\shell\bzr\command')
310
except EnvironmentError:
314
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
316
except EnvironmentError:
321
ctypes.windll.LoadLibrary('mfc71.dll')
324
("Library MFC71.DLL is not found on your system.\n"
325
"This library needed for SFTP transport.\n"
326
"If you need to work via SFTP you should download\n"
327
"this library manually and put it to directory\n"
328
"where Bzr installed.\n"
329
"For detailed instructions see:\n"
330
"http://bazaar-vcs.org/BzrOnPureWindows"
333
MB_OK | MB_ICONEXCLAMATION)
338
if __name__ == "__main__":