/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tools/win32/bzr_postinstall.py

  • Committer: Alexander Belchenko
  • Date: 2006-06-29 08:41:31 UTC
  • mto: (1860.1.1 win32.installer)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060629084131-3ea4d44e3204e36f
win32 installer for bzr.dev.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009, 2010 by Canonical Ltd
 
1
# Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""brz postinstall helper for win32 installation
 
17
"""bzr postinstall helper for win32 installation
18
18
Written by Alexander Belchenko
19
 
 
20
 
Dependency: ctypes
21
19
"""
22
20
 
23
21
import os
24
 
import shutil
25
22
import sys
26
23
 
27
24
 
28
25
##
29
26
# CONSTANTS
30
27
 
31
 
VERSION = "1.5.20070131"
 
28
VERSION = "1.3.20060513"
32
29
 
33
 
USAGE = """Brz postinstall helper for win32 installation
 
30
USAGE = """Bzr postinstall helper for win32 installation
34
31
Usage: %s [options]
35
32
 
36
33
OPTIONS:
40
37
    -n, --dry-run               - print actions rather than execute them
41
38
    -q, --silent                - no messages for user
42
39
 
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])
50
47
 
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
 
 
75
48
##
76
49
# INTERNAL VARIABLES
77
50
 
80
53
 
81
54
 
82
55
def main():
83
 
    import ctypes
84
56
    import getopt
85
57
    import re
86
58
    import _winreg
93
65
    hkey_str = {_winreg.HKEY_LOCAL_MACHINE: 'HKEY_LOCAL_MACHINE',
94
66
                _winreg.HKEY_CURRENT_USER: 'HKEY_CURRENT_USER',
95
67
                _winreg.HKEY_CLASSES_ROOT: 'HKEY_CLASSES_ROOT',
96
 
                }
 
68
               }
97
69
 
98
70
    dry_run = False
99
71
    silent = False
100
 
    start_brz = False
 
72
    start_bzr = False
101
73
    add_path = False
102
74
    delete_path = False
103
75
    add_shell_menu = False
109
81
                                   ["help", "version",
110
82
                                    "dry-run",
111
83
                                    "silent",
112
 
                                    "start-brz",
 
84
                                    "start-bzr",
113
85
                                    "add-path",
114
86
                                    "delete-path",
115
87
                                    "add-shell-menu",
116
88
                                    "delete-shell-menu",
117
89
                                    "check-mfc71",
118
 
                                    ])
 
90
                                   ])
119
91
 
120
92
        for o, a in opts:
121
93
            if o in ("-h", "--help"):
122
 
                print(USAGE)
 
94
                print USAGE
123
95
                return OK
124
96
            elif o in ("-v", "--version"):
125
 
                print(VERSION_FORMAT % (USAGE.splitlines()[0], VERSION))
 
97
                print VERSION_FORMAT % (USAGE.splitlines()[0], VERSION)
126
98
                return OK
127
99
 
128
100
            elif o in ('-n', "--dry-run"):
130
102
            elif o in ('-q', '--silent'):
131
103
                silent = True
132
104
 
133
 
            elif o == "--start-brz":
134
 
                start_brz = True
 
105
            elif o == "--start-bzr":
 
106
                start_bzr = True
135
107
            elif o == "--add-path":
136
108
                add_path = True
137
109
            elif o == "--delete-path":
143
115
            elif o == "--check-mfc71":
144
116
                check_mfc71 = True
145
117
 
146
 
    except getopt.GetoptError as msg:
147
 
        print(str(msg))
148
 
        print(USAGE)
 
118
    except getopt.GetoptError, msg:
 
119
        print str(msg)
 
120
        print USAGE
149
121
        return ERROR
150
122
 
151
123
    # message box from Win32API
154
126
    MB_ICONERROR = 16
155
127
    MB_ICONEXCLAMATION = 48
156
128
 
157
 
    brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
 
129
    bzr_dir = os.path.dirname(sys.argv[0])
158
130
 
159
 
    if start_brz:
160
 
        fname = os.path.join(brz_dir, "start_brz.bat")
 
131
    if start_bzr:
 
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()
 
134
            f = file(fname, "r")
 
135
            content = f.readlines()
 
136
            f.close()
164
137
        else:
165
 
            content = ["brz.exe help\n"]
 
138
            content = ["bzr.exe help\n"]
166
139
 
167
140
        for ix in xrange(len(content)):
168
141
            s = content[ix]
169
 
            if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
 
142
            if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
170
143
                        s.rstrip('\r\n'),
171
144
                        re.IGNORECASE):
172
 
                content[ix] = s.replace('brz.exe',
173
 
                                        '"%s"' % os.path.join(brz_dir,
174
 
                                                              'brz.exe'))
175
 
            elif s.find(r'C:\Program Files\Breezy') != -1:
176
 
                content[ix] = s.replace(r'C:\Program Files\Breezy',
177
 
                                        brz_dir)
 
145
                content[ix] = s.replace('bzr.exe',
 
146
                                        '"%s"' % os.path.join(bzr_dir,
 
147
                                                              'bzr.exe'))
178
148
 
179
149
        if dry_run:
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)
183
153
        else:
184
 
            with open(fname, 'w') as f:
185
 
                f.write(''.join(content))
 
154
            f = file(fname, 'w')
 
155
            f.write(''.join(content))
 
156
            f.close()
186
157
 
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'),
194
 
                )
 
165
               )
195
166
 
196
167
        hkey = None
197
168
        for key, subkey in keys:
212
183
            break
213
184
 
214
185
        if hkey is None:
215
 
            print("Cannot find appropriate registry key for PATH")
 
186
            print "Cannot find appropriate registry key for PATH"
216
187
        else:
217
188
            path_list = [i for i in path_u.split(os.pathsep) if i != '']
218
189
            f_change = False
219
190
            for ix, item in enumerate(path_list[:]):
220
 
                if item == brz_dir:
 
191
                if item == bzr_dir:
221
192
                    if delete_path:
222
193
                        del path_list[ix]
223
194
                        f_change = True
224
195
                    elif add_path:
225
 
                        print("*** brz already in PATH")
 
196
                        print "*** Bzr already in PATH"
226
197
                    break
227
198
            else:
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))
230
201
                    f_change = True
231
202
 
232
203
            if f_change:
233
204
                path_u = os.pathsep.join(path_list)
234
205
                if dry_run:
235
 
                    print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
236
 
                    print("*** Modify PATH variable. New value:")
237
 
                    print(path_u)
 
206
                    print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
 
207
                    print "*** Modify PATH variable. New value:"
 
208
                    print path_u
238
209
                else:
239
210
                    _winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
240
211
                    _winreg.FlushKey(hkey)
241
212
 
242
 
        if hkey is not None:
 
213
        if not hkey is None:
243
214
            _winreg.CloseKey(hkey)
244
215
 
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'
251
 
 
252
 
        def backup_autoexec_bat(name, backupname, dry_run):
253
 
            # backup autoexec.bat
254
 
            if os.path.isfile(name):
255
 
                if not dry_run:
256
 
                    shutil.copyfile(name, backupname)
257
 
                else:
258
 
                    print('*** backup copy of autoexec.bat created')
259
 
 
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
264
 
        else:
265
 
            brz_dir_8_3 = brz_dir
266
 
        pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
267
 
 
268
 
        # search pattern
269
 
        with open(abat, 'r') as f:
270
 
            lines = f.readlines()
271
 
        found = False
272
 
        for i in lines:
273
 
            if i.rstrip('\r\n') == pattern:
274
 
                found = True
275
 
                break
276
 
 
277
 
        if delete_path and found:
278
 
            backup_autoexec_bat(abat, abak, dry_run)
279
 
            if not dry_run:
280
 
                with open(abat, 'w') as f:
281
 
                    for i in lines:
282
 
                        if i.rstrip('\r\n') != pattern:
283
 
                            f.write(i)
284
 
            else:
285
 
                print('*** Remove line <%s> from autoexec.bat' % pattern)
286
 
 
287
 
        elif add_path and not found:
288
 
            backup_autoexec_bat(abat, abak, dry_run)
289
 
            if not dry_run:
290
 
                with open(abat, 'a') as f:
291
 
                    f.write(pattern)
292
 
                    f.write('\n')
293
 
            else:
294
 
                print('*** Add line <%s> to autoexec.bat' % pattern)
295
 
 
296
216
    if add_shell_menu and not delete_shell_menu:
297
217
        hkey = None
298
218
        try:
299
219
            hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
300
 
                                     r'Folder\shell\brz')
 
220
                                     r'Folder\shell\bzr')
301
221
        except EnvironmentError:
302
222
            if not silent:
303
223
                MessageBoxA(None,
305
225
                            'EnvironmentError',
306
226
                            MB_OK | MB_ICONERROR)
307
227
 
308
 
        if hkey is not None:
309
 
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
 
228
        if not hkey is None:
 
229
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
310
230
            hkey2 = _winreg.CreateKey(hkey, 'command')
311
231
            _winreg.SetValue(hkey2, '', _winreg.REG_SZ,
312
 
                             '%s /K "%s"' % (
313
 
                                    os.environ.get('COMSPEC', '%COMSPEC%'),
314
 
                                    os.path.join(brz_dir, 'start_brz.bat')))
 
232
                             'cmd /K "%s"' % os.path.join(bzr_dir,
 
233
                                                          'start_bzr.bat'))
315
234
            _winreg.CloseKey(hkey2)
316
235
            _winreg.CloseKey(hkey)
317
236
 
318
237
    if delete_shell_menu:
319
238
        try:
320
239
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
321
 
                              r'Folder\shell\brz\command')
 
240
                              r'Folder\shell\bzr\command')
322
241
        except EnvironmentError:
323
242
            pass
324
243
 
325
244
        try:
326
245
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
327
 
                              r'Folder\shell\brz')
 
246
                              r'Folder\shell\bzr')
328
247
        except EnvironmentError:
329
248
            pass
330
249
 
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"
343
 
                         ),
 
261
                         "http://bazaar-vcs.org/BzrOnPureWindows"
 
262
                        ),
344
263
                        "Warning",
345
264
                        MB_OK | MB_ICONEXCLAMATION)
346
265