13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""brz postinstall helper for win32 installation
17
"""bzr postinstall helper for win32 installation
18
18
Written by Alexander Belchenko
31
VERSION = "1.5.20070131"
28
VERSION = "1.3.20060513"
33
USAGE = """Brz postinstall helper for win32 installation
30
USAGE = """Bzr postinstall helper for win32 installation
34
31
Usage: %s [options]
40
37
-n, --dry-run - print actions rather than execute them
41
38
-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
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
47
44
--delete-shell-menu - delete context menu from shell
48
45
--check-mfc71 - check if MFC71.DLL present in system
49
46
""" % os.path.basename(sys.argv[0])
52
_major, _minor, _build, _platform, _text = sys.getwindowsversion()
55
# The operating system platform.
56
# This member can be one of the following values.
57
# ========================== ======================================
59
# -------------------------- --------------------------------------
60
# VER_PLATFORM_WIN32_NT The operating system is Windows Vista,
61
# 2 Windows Server "Longhorn",
62
# Windows Server 2003, Windows XP,
63
# Windows 2000, or Windows NT.
65
# VER_PLATFORM_WIN32_WINDOWS The operating system is Windows Me,
66
# 1 Windows 98, or Windows 95.
67
# ========================== ======================================
71
# don't care about real Windows name, just to force safe operations
76
49
# INTERNAL VARIABLES
109
81
["help", "version",
116
88
"delete-shell-menu",
121
93
if o in ("-h", "--help"):
124
96
elif o in ("-v", "--version"):
125
print(VERSION_FORMAT % (USAGE.splitlines()[0], VERSION))
97
print VERSION_FORMAT % (USAGE.splitlines()[0], VERSION)
128
100
elif o in ('-n', "--dry-run"):
154
126
MB_ICONERROR = 16
155
127
MB_ICONEXCLAMATION = 48
157
brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
129
bzr_dir = os.path.dirname(sys.argv[0])
160
fname = os.path.join(brz_dir, "start_brz.bat")
132
fname = os.path.join(bzr_dir, "start_bzr.bat")
161
133
if os.path.isfile(fname):
162
with open(fname, "r") as f:
163
content = f.readlines()
135
content = f.readlines()
165
content = ["brz.exe help\n"]
138
content = ["bzr.exe help\n"]
167
140
for ix in xrange(len(content)):
169
if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
142
if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
170
143
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',
145
content[ix] = s.replace('bzr.exe',
146
'"%s"' % os.path.join(bzr_dir,
180
print("*** Write file: start_brz.bat")
181
print("*** File content:")
182
print(''.join(content))
150
print "*** Write file: start_bzr.bat"
151
print "*** File content:"
152
print ''.join(content)
184
with open(fname, 'w') as f:
185
f.write(''.join(content))
155
f.write(''.join(content))
187
if (add_path or delete_path) and winver == 'Windows NT':
158
if add_path or delete_path:
188
159
# find appropriate registry key:
189
160
# 1. HKLM\System\CurrentControlSet\Control\SessionManager\Environment
190
161
# 2. HKCU\Environment
191
162
keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
192
163
r'\Session Manager\Environment')),
193
164
(_winreg.HKEY_CURRENT_USER, r'Environment'),
197
168
for key, subkey in keys:
215
print("Cannot find appropriate registry key for PATH")
186
print "Cannot find appropriate registry key for PATH"
217
188
path_list = [i for i in path_u.split(os.pathsep) if i != '']
219
190
for ix, item in enumerate(path_list[:]):
222
193
del path_list[ix]
225
print("*** brz already in PATH")
196
print "*** Bzr already in PATH"
228
199
if add_path and not delete_path:
229
path_list.append(brz_dir.decode(user_encoding))
200
path_list.append(bzr_dir.decode(user_encoding))
233
204
path_u = os.pathsep.join(path_list)
235
print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
236
print("*** Modify PATH variable. New value:")
206
print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
207
print "*** Modify PATH variable. New value:"
239
210
_winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
240
211
_winreg.FlushKey(hkey)
243
214
_winreg.CloseKey(hkey)
245
if (add_path or delete_path) and winver == 'Windows 98':
246
# mutating autoexec.bat
247
# adding or delete string:
248
# SET PATH=%PATH%;C:\PROGRA~1\Breezy
249
abat = 'C:\\autoexec.bat'
250
abak = 'C:\\autoexec.bak'
252
def backup_autoexec_bat(name, backupname, dry_run):
253
# backup autoexec.bat
254
if os.path.isfile(name):
256
shutil.copyfile(name, backupname)
258
print('*** backup copy of autoexec.bat created')
260
GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
261
buf = ctypes.create_string_buffer(260)
262
if GetShortPathName(brz_dir, buf, 260):
263
brz_dir_8_3 = buf.value
265
brz_dir_8_3 = brz_dir
266
pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
269
with open(abat, 'r') as f:
270
lines = f.readlines()
273
if i.rstrip('\r\n') == pattern:
277
if delete_path and found:
278
backup_autoexec_bat(abat, abak, dry_run)
280
with open(abat, 'w') as f:
282
if i.rstrip('\r\n') != pattern:
285
print('*** Remove line <%s> from autoexec.bat' % pattern)
287
elif add_path and not found:
288
backup_autoexec_bat(abat, abak, dry_run)
290
with open(abat, 'a') as f:
294
print('*** Add line <%s> to autoexec.bat' % pattern)
296
216
if add_shell_menu and not delete_shell_menu:
299
219
hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
301
221
except EnvironmentError:
303
223
MessageBoxA(None,
305
225
'EnvironmentError',
306
226
MB_OK | MB_ICONERROR)
309
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
229
_winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
310
230
hkey2 = _winreg.CreateKey(hkey, 'command')
311
231
_winreg.SetValue(hkey2, '', _winreg.REG_SZ,
313
os.environ.get('COMSPEC', '%COMSPEC%'),
314
os.path.join(brz_dir, 'start_brz.bat')))
232
'cmd /K "%s"' % os.path.join(bzr_dir,
315
234
_winreg.CloseKey(hkey2)
316
235
_winreg.CloseKey(hkey)
318
237
if delete_shell_menu:
320
239
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
321
r'Folder\shell\brz\command')
240
r'Folder\shell\bzr\command')
322
241
except EnvironmentError:
326
245
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
328
247
except EnvironmentError:
337
256
"This library needed for SFTP transport.\n"
338
257
"If you need to work via SFTP you should download\n"
339
258
"this library manually and put it to directory\n"
340
"where Brz installed.\n"
259
"where Bzr installed.\n"
341
260
"For detailed instructions see:\n"
342
"http://wiki.bazaar.canonical.com/BzrOnPureWindows"
261
"http://bazaar-vcs.org/BzrOnPureWindows"
345
264
MB_OK | MB_ICONEXCLAMATION)