14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""bzr postinstall helper for win32 installation
17
"""brz postinstall helper for win32 installation
18
18
Written by Alexander Belchenko
40
40
-n, --dry-run - print actions rather than execute them
41
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
43
--start-brz - update start_brz.bat
44
--add-path - add brz directory to environment PATH
45
--delete-path - delete brz directory to environment PATH
46
--add-shell-menu - add shell context menu to start brz session
47
47
--delete-shell-menu - delete context menu from shell
48
48
--check-mfc71 - check if MFC71.DLL present in system
49
49
""" % os.path.basename(sys.argv[0])
52
_major,_minor,_build,_platform,_text = sys.getwindowsversion()
52
_major, _minor, _build, _platform, _text = sys.getwindowsversion()
55
55
# The operating system platform.
93
93
hkey_str = {_winreg.HKEY_LOCAL_MACHINE: 'HKEY_LOCAL_MACHINE',
94
94
_winreg.HKEY_CURRENT_USER: 'HKEY_CURRENT_USER',
95
95
_winreg.HKEY_CLASSES_ROOT: 'HKEY_CLASSES_ROOT',
102
102
delete_path = False
103
103
add_shell_menu = False
109
109
["help", "version",
115
115
"add-shell-menu",
116
116
"delete-shell-menu",
120
120
for o, a in opts:
121
121
if o in ("-h", "--help"):
124
124
elif o in ("-v", "--version"):
125
print VERSION_FORMAT % (USAGE.splitlines()[0], VERSION)
125
print(VERSION_FORMAT % (USAGE.splitlines()[0], VERSION))
128
128
elif o in ('-n', "--dry-run"):
143
143
elif o == "--check-mfc71":
144
144
check_mfc71 = True
146
except getopt.GetoptError, msg:
146
except getopt.GetoptError as msg:
151
151
# message box from Win32API
154
154
MB_ICONERROR = 16
155
155
MB_ICONEXCLAMATION = 48
157
bzr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
157
brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
160
fname = os.path.join(bzr_dir, "start_bzr.bat")
160
fname = os.path.join(brz_dir, "start_brz.bat")
161
161
if os.path.isfile(fname):
163
content = f.readlines()
162
with open(fname, "r") as f:
163
content = f.readlines()
166
content = ["bzr.exe help\n"]
165
content = ["brz.exe help\n"]
168
167
for ix in xrange(len(content)):
170
if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
169
if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
171
170
s.rstrip('\r\n'),
173
content[ix] = s.replace('bzr.exe',
174
'"%s"' % os.path.join(bzr_dir,
172
content[ix] = s.replace('brz.exe',
173
'"%s"' % os.path.join(brz_dir,
175
elif s.find(r'C:\Program Files\Breezy') != -1:
176
content[ix] = s.replace(r'C:\Program Files\Breezy',
178
print "*** Write file: start_bzr.bat"
179
print "*** File content:"
180
print ''.join(content)
180
print("*** Write file: start_brz.bat")
181
print("*** File content:")
182
print(''.join(content))
183
f.write(''.join(content))
184
with open(fname, 'w') as f:
185
f.write(''.join(content))
186
187
if (add_path or delete_path) and winver == 'Windows NT':
187
188
# find appropriate registry key:
190
191
keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
191
192
r'\Session Manager\Environment')),
192
193
(_winreg.HKEY_CURRENT_USER, r'Environment'),
196
197
for key, subkey in keys:
214
print "Cannot find appropriate registry key for PATH"
215
print("Cannot find appropriate registry key for PATH")
216
217
path_list = [i for i in path_u.split(os.pathsep) if i != '']
218
219
for ix, item in enumerate(path_list[:]):
221
222
del path_list[ix]
224
print "*** Bzr already in PATH"
225
print("*** brz already in PATH")
227
228
if add_path and not delete_path:
228
path_list.append(bzr_dir.decode(user_encoding))
229
path_list.append(brz_dir.decode(user_encoding))
232
233
path_u = os.pathsep.join(path_list)
234
print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
235
print "*** Modify PATH variable. New value:"
235
print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
236
print("*** Modify PATH variable. New value:")
238
239
_winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
239
240
_winreg.FlushKey(hkey)
242
243
_winreg.CloseKey(hkey)
244
245
if (add_path or delete_path) and winver == 'Windows 98':
245
246
# mutating autoexec.bat
246
247
# adding or delete string:
247
# SET PATH=%PATH%;C:\PROGRA~1\Bazaar
248
# SET PATH=%PATH%;C:\PROGRA~1\Breezy
248
249
abat = 'C:\\autoexec.bat'
249
250
abak = 'C:\\autoexec.bak'
255
256
shutil.copyfile(name, backupname)
257
print '*** backup copy of autoexec.bat created'
258
print('*** backup copy of autoexec.bat created')
259
260
GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
260
261
buf = ctypes.create_string_buffer(260)
261
if GetShortPathName(bzr_dir, buf, 260):
262
bzr_dir_8_3 = buf.value
262
if GetShortPathName(brz_dir, buf, 260):
263
brz_dir_8_3 = buf.value
264
bzr_dir_8_3 = bzr_dir
265
pattern = 'SET PATH=%PATH%;' + bzr_dir_8_3
265
brz_dir_8_3 = brz_dir
266
pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
269
lines = f.readlines()
269
with open(abat, 'r') as f:
270
lines = f.readlines()
273
273
if i.rstrip('\r\n') == pattern:
277
277
if delete_path and found:
278
278
backup_autoexec_bat(abat, abak, dry_run)
282
if i.rstrip('\r\n') != pattern:
280
with open(abat, 'w') as f:
282
if i.rstrip('\r\n') != pattern:
286
print '*** Remove line <%s> from autoexec.bat' % pattern
285
print('*** Remove line <%s> from autoexec.bat' % pattern)
288
287
elif add_path and not found:
289
288
backup_autoexec_bat(abat, abak, dry_run)
290
with open(abat, 'a') as f:
296
print '*** Add line <%s> to autoexec.bat' % pattern
294
print('*** Add line <%s> to autoexec.bat' % pattern)
298
296
if add_shell_menu and not delete_shell_menu:
301
299
hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
303
301
except EnvironmentError:
305
303
MessageBoxA(None,
307
305
'EnvironmentError',
308
306
MB_OK | MB_ICONERROR)
311
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
309
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
312
310
hkey2 = _winreg.CreateKey(hkey, 'command')
313
311
_winreg.SetValue(hkey2, '', _winreg.REG_SZ,
315
313
os.environ.get('COMSPEC', '%COMSPEC%'),
316
os.path.join(bzr_dir, 'start_bzr.bat')))
314
os.path.join(brz_dir, 'start_brz.bat')))
317
315
_winreg.CloseKey(hkey2)
318
316
_winreg.CloseKey(hkey)
320
318
if delete_shell_menu:
322
320
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
323
r'Folder\shell\bzr\command')
321
r'Folder\shell\brz\command')
324
322
except EnvironmentError:
328
326
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
330
328
except EnvironmentError:
339
337
"This library needed for SFTP transport.\n"
340
338
"If you need to work via SFTP you should download\n"
341
339
"this library manually and put it to directory\n"
342
"where Bzr installed.\n"
340
"where Brz installed.\n"
343
341
"For detailed instructions see:\n"
344
342
"http://wiki.bazaar.canonical.com/BzrOnPureWindows"
347
345
MB_OK | MB_ICONEXCLAMATION)