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
"""brz postinstall helper for win32 installation
17
"""bzr 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-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
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
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])
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 as msg:
146
except getopt.GetoptError, msg:
151
151
# message box from Win32API
154
154
MB_ICONERROR = 16
155
155
MB_ICONEXCLAMATION = 48
157
brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
157
bzr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
160
fname = os.path.join(brz_dir, "start_brz.bat")
160
fname = os.path.join(bzr_dir, "start_bzr.bat")
161
161
if os.path.isfile(fname):
162
with file(fname, "r") as f:
163
content = f.readlines()
163
content = f.readlines()
165
content = ["brz.exe help\n"]
166
content = ["bzr.exe help\n"]
167
168
for ix in xrange(len(content)):
169
if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
170
if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
170
171
s.rstrip('\r\n'),
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',
173
content[ix] = s.replace('bzr.exe',
174
'"%s"' % os.path.join(bzr_dir,
176
elif s.find(r'C:\Program Files\Bazaar') != -1:
177
content[ix] = s.replace(r'C:\Program Files\Bazaar',
180
print("*** Write file: start_brz.bat")
181
print("*** File content:")
182
print(''.join(content))
181
print "*** Write file: start_bzr.bat"
182
print "*** File content:"
183
print ''.join(content)
184
with file(fname, 'w') as f:
185
f.write(''.join(content))
186
f.write(''.join(content))
187
189
if (add_path or delete_path) and winver == 'Windows NT':
188
190
# find appropriate registry key:
215
print("Cannot find appropriate registry key for PATH")
217
print "Cannot find appropriate registry key for PATH"
217
219
path_list = [i for i in path_u.split(os.pathsep) if i != '']
219
221
for ix, item in enumerate(path_list[:]):
222
224
del path_list[ix]
225
print("*** brz already in PATH")
227
print "*** Bzr already in PATH"
228
230
if add_path and not delete_path:
229
path_list.append(brz_dir.decode(user_encoding))
231
path_list.append(bzr_dir.decode(user_encoding))
233
235
path_u = os.pathsep.join(path_list)
235
print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
236
print("*** Modify PATH variable. New value:")
237
print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
238
print "*** Modify PATH variable. New value:"
239
241
_winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
240
242
_winreg.FlushKey(hkey)
245
247
if (add_path or delete_path) and winver == 'Windows 98':
246
248
# mutating autoexec.bat
247
249
# adding or delete string:
248
# SET PATH=%PATH%;C:\PROGRA~1\Breezy
250
# SET PATH=%PATH%;C:\PROGRA~1\Bazaar
249
251
abat = 'C:\\autoexec.bat'
250
252
abak = 'C:\\autoexec.bak'
256
258
shutil.copyfile(name, backupname)
258
print('*** backup copy of autoexec.bat created')
260
print '*** backup copy of autoexec.bat created'
260
262
GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
261
263
buf = ctypes.create_string_buffer(260)
262
if GetShortPathName(brz_dir, buf, 260):
263
brz_dir_8_3 = buf.value
264
if GetShortPathName(bzr_dir, buf, 260):
265
bzr_dir_8_3 = buf.value
265
brz_dir_8_3 = brz_dir
266
pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
267
bzr_dir_8_3 = bzr_dir
268
pattern = 'SET PATH=%PATH%;' + bzr_dir_8_3
269
with file(abat, 'r') as f:
270
lines = f.readlines()
272
lines = f.readlines()
273
276
if i.rstrip('\r\n') == pattern:
277
280
if delete_path and found:
278
281
backup_autoexec_bat(abat, abak, dry_run)
280
with file(abat, 'w') as f:
282
if i.rstrip('\r\n') != pattern:
285
if i.rstrip('\r\n') != pattern:
285
print('*** Remove line <%s> from autoexec.bat' % pattern)
289
print '*** Remove line <%s> from autoexec.bat' % pattern
287
291
elif add_path and not found:
288
292
backup_autoexec_bat(abat, abak, dry_run)
290
with file(abat, 'a') as f:
294
print('*** Add line <%s> to autoexec.bat' % pattern)
299
print '*** Add line <%s> to autoexec.bat' % pattern
296
301
if add_shell_menu and not delete_shell_menu:
299
304
hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
301
306
except EnvironmentError:
303
308
MessageBoxA(None,
306
311
MB_OK | MB_ICONERROR)
308
313
if not hkey is None:
309
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
314
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
310
315
hkey2 = _winreg.CreateKey(hkey, 'command')
311
316
_winreg.SetValue(hkey2, '', _winreg.REG_SZ,
313
318
os.environ.get('COMSPEC', '%COMSPEC%'),
314
os.path.join(brz_dir, 'start_brz.bat')))
319
os.path.join(bzr_dir, 'start_bzr.bat')))
315
320
_winreg.CloseKey(hkey2)
316
321
_winreg.CloseKey(hkey)
318
323
if delete_shell_menu:
320
325
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
321
r'Folder\shell\brz\command')
326
r'Folder\shell\bzr\command')
322
327
except EnvironmentError:
326
331
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
328
333
except EnvironmentError:
337
342
"This library needed for SFTP transport.\n"
338
343
"If you need to work via SFTP you should download\n"
339
344
"this library manually and put it to directory\n"
340
"where Brz installed.\n"
345
"where Bzr installed.\n"
341
346
"For detailed instructions see:\n"
342
347
"http://wiki.bazaar.canonical.com/BzrOnPureWindows"