~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/win32/bzr_postinstall.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-05-04 12:10:51 UTC
  • mfrom: (5819.1.4 777007-developer-doc)
  • Revision ID: pqm@pqm.ubuntu.com-20110504121051-aovlsmqiivjmc4fc
(jelmer) Small fixes to developer documentation. (Jonathan Riddell)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009, 2010 by Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""bzr postinstall helper for win32 installation
18
18
Written by Alexander Belchenko
 
19
 
 
20
Dependency: ctypes
19
21
"""
20
22
 
21
23
import os
 
24
import shutil
22
25
import sys
23
26
 
24
27
 
25
28
##
26
29
# CONSTANTS
27
30
 
28
 
VERSION = "1.3.20060513"
 
31
VERSION = "1.5.20070131"
29
32
 
30
33
USAGE = """Bzr postinstall helper for win32 installation
31
34
Usage: %s [options]
45
48
    --check-mfc71               - check if MFC71.DLL present in system
46
49
""" % os.path.basename(sys.argv[0])
47
50
 
 
51
# Windows version
 
52
_major,_minor,_build,_platform,_text = sys.getwindowsversion()
 
53
# from MSDN:
 
54
# dwPlatformId
 
55
#   The operating system platform.
 
56
#   This member can be one of the following values.
 
57
#   ==========================  ======================================
 
58
#   Value                       Meaning
 
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.
 
64
#
 
65
#   VER_PLATFORM_WIN32_WINDOWS  The operating system is Windows Me,
 
66
#   1                           Windows 98, or Windows 95.
 
67
#   ==========================  ======================================
 
68
if _platform == 2:
 
69
    winver = 'Windows NT'
 
70
else:
 
71
    # don't care about real Windows name, just to force safe operations
 
72
    winver = 'Windows 98'
 
73
 
 
74
 
48
75
##
49
76
# INTERNAL VARIABLES
50
77
 
53
80
 
54
81
 
55
82
def main():
 
83
    import ctypes
56
84
    import getopt
57
85
    import re
58
86
    import _winreg
126
154
    MB_ICONERROR = 16
127
155
    MB_ICONEXCLAMATION = 48
128
156
 
129
 
    bzr_dir = os.path.dirname(sys.argv[0])
 
157
    bzr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
130
158
 
131
159
    if start_bzr:
132
160
        fname = os.path.join(bzr_dir, "start_bzr.bat")
145
173
                content[ix] = s.replace('bzr.exe',
146
174
                                        '"%s"' % os.path.join(bzr_dir,
147
175
                                                              'bzr.exe'))
 
176
            elif s.find(r'C:\Program Files\Bazaar') != -1:
 
177
                content[ix] = s.replace(r'C:\Program Files\Bazaar',
 
178
                                        bzr_dir)
148
179
 
149
180
        if dry_run:
150
181
            print "*** Write file: start_bzr.bat"
155
186
            f.write(''.join(content))
156
187
            f.close()
157
188
 
158
 
    if add_path or delete_path:
 
189
    if (add_path or delete_path) and winver == 'Windows NT':
159
190
        # find appropriate registry key:
160
191
        # 1. HKLM\System\CurrentControlSet\Control\SessionManager\Environment
161
192
        # 2. HKCU\Environment
213
244
        if not hkey is None:
214
245
            _winreg.CloseKey(hkey)
215
246
 
 
247
    if (add_path or delete_path) and winver == 'Windows 98':
 
248
        # mutating autoexec.bat
 
249
        # adding or delete string:
 
250
        # SET PATH=%PATH%;C:\PROGRA~1\Bazaar
 
251
        abat = 'C:\\autoexec.bat'
 
252
        abak = 'C:\\autoexec.bak'
 
253
 
 
254
        def backup_autoexec_bat(name, backupname, dry_run):
 
255
            # backup autoexec.bat
 
256
            if os.path.isfile(name):
 
257
                if not dry_run:
 
258
                    shutil.copyfile(name, backupname)
 
259
                else:
 
260
                    print '*** backup copy of autoexec.bat created'
 
261
 
 
262
        GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
 
263
        buf = ctypes.create_string_buffer(260)
 
264
        if GetShortPathName(bzr_dir, buf, 260):
 
265
            bzr_dir_8_3 = buf.value
 
266
        else:
 
267
            bzr_dir_8_3 = bzr_dir
 
268
        pattern = 'SET PATH=%PATH%;' + bzr_dir_8_3
 
269
 
 
270
        # search pattern
 
271
        f = file(abat, 'r')
 
272
        lines = f.readlines()
 
273
        f.close()
 
274
        found = False
 
275
        for i in lines:
 
276
            if i.rstrip('\r\n') == pattern:
 
277
                found = True
 
278
                break
 
279
 
 
280
        if delete_path and found:
 
281
            backup_autoexec_bat(abat, abak, dry_run)
 
282
            if not dry_run:
 
283
                f = file(abat, 'w')
 
284
                for i in lines:
 
285
                    if i.rstrip('\r\n') != pattern:
 
286
                        f.write(i)
 
287
                f.close()
 
288
            else:
 
289
                print '*** Remove line <%s> from autoexec.bat' % pattern
 
290
                    
 
291
        elif add_path and not found:
 
292
            backup_autoexec_bat(abat, abak, dry_run)
 
293
            if not dry_run:
 
294
                f = file(abat, 'a')
 
295
                f.write(pattern)
 
296
                f.write('\n')
 
297
                f.close()
 
298
            else:
 
299
                print '*** Add line <%s> to autoexec.bat' % pattern
 
300
 
216
301
    if add_shell_menu and not delete_shell_menu:
217
302
        hkey = None
218
303
        try:
229
314
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
230
315
            hkey2 = _winreg.CreateKey(hkey, 'command')
231
316
            _winreg.SetValue(hkey2, '', _winreg.REG_SZ,
232
 
                             'cmd /K "%s"' % os.path.join(bzr_dir,
233
 
                                                          'start_bzr.bat'))
 
317
                             '%s /K "%s"' % (
 
318
                                    os.environ.get('COMSPEC', '%COMSPEC%'),
 
319
                                    os.path.join(bzr_dir, 'start_bzr.bat')))
234
320
            _winreg.CloseKey(hkey2)
235
321
            _winreg.CloseKey(hkey)
236
322
 
258
344
                         "this library manually and put it to directory\n"
259
345
                         "where Bzr installed.\n"
260
346
                         "For detailed instructions see:\n"
261
 
                         "http://bazaar-vcs.org/BzrOnPureWindows"
 
347
                         "http://wiki.bazaar.canonical.com/BzrOnPureWindows"
262
348
                        ),
263
349
                        "Warning",
264
350
                        MB_OK | MB_ICONEXCLAMATION)