/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: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
17
 
"""brz postinstall helper for win32 installation
 
17
"""bzr postinstall helper for win32 installation
18
18
Written by Alexander Belchenko
19
19
 
20
20
Dependency: ctypes
30
30
 
31
31
VERSION = "1.5.20070131"
32
32
 
33
 
USAGE = """Brz postinstall helper for win32 installation
 
33
USAGE = """Bzr postinstall helper for win32 installation
34
34
Usage: %s [options]
35
35
 
36
36
OPTIONS:
40
40
    -n, --dry-run               - print actions rather than execute them
41
41
    -q, --silent                - no messages for user
42
42
 
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])
50
50
 
51
51
# Windows version
52
 
_major, _minor, _build, _platform, _text = sys.getwindowsversion()
 
52
_major,_minor,_build,_platform,_text = sys.getwindowsversion()
53
53
# from MSDN:
54
54
# dwPlatformId
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',
96
 
                }
 
96
               }
97
97
 
98
98
    dry_run = False
99
99
    silent = False
100
 
    start_brz = False
 
100
    start_bzr = False
101
101
    add_path = False
102
102
    delete_path = False
103
103
    add_shell_menu = False
109
109
                                   ["help", "version",
110
110
                                    "dry-run",
111
111
                                    "silent",
112
 
                                    "start-brz",
 
112
                                    "start-bzr",
113
113
                                    "add-path",
114
114
                                    "delete-path",
115
115
                                    "add-shell-menu",
116
116
                                    "delete-shell-menu",
117
117
                                    "check-mfc71",
118
 
                                    ])
 
118
                                   ])
119
119
 
120
120
        for o, a in opts:
121
121
            if o in ("-h", "--help"):
122
 
                print(USAGE)
 
122
                print USAGE
123
123
                return OK
124
124
            elif o in ("-v", "--version"):
125
 
                print(VERSION_FORMAT % (USAGE.splitlines()[0], VERSION))
 
125
                print VERSION_FORMAT % (USAGE.splitlines()[0], VERSION)
126
126
                return OK
127
127
 
128
128
            elif o in ('-n', "--dry-run"):
130
130
            elif o in ('-q', '--silent'):
131
131
                silent = True
132
132
 
133
 
            elif o == "--start-brz":
134
 
                start_brz = True
 
133
            elif o == "--start-bzr":
 
134
                start_bzr = True
135
135
            elif o == "--add-path":
136
136
                add_path = True
137
137
            elif o == "--delete-path":
143
143
            elif o == "--check-mfc71":
144
144
                check_mfc71 = True
145
145
 
146
 
    except getopt.GetoptError as msg:
147
 
        print(str(msg))
148
 
        print(USAGE)
 
146
    except getopt.GetoptError, msg:
 
147
        print str(msg)
 
148
        print USAGE
149
149
        return ERROR
150
150
 
151
151
    # message box from Win32API
154
154
    MB_ICONERROR = 16
155
155
    MB_ICONEXCLAMATION = 48
156
156
 
157
 
    brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
 
157
    bzr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
158
158
 
159
 
    if start_brz:
160
 
        fname = os.path.join(brz_dir, "start_brz.bat")
 
159
    if start_bzr:
 
160
        fname = os.path.join(bzr_dir, "start_bzr.bat")
161
161
        if os.path.isfile(fname):
162
 
            with open(fname, "r") as f:
163
 
                content = f.readlines()
 
162
            f = file(fname, "r")
 
163
            content = f.readlines()
 
164
            f.close()
164
165
        else:
165
 
            content = ["brz.exe help\n"]
 
166
            content = ["bzr.exe help\n"]
166
167
 
167
168
        for ix in xrange(len(content)):
168
169
            s = content[ix]
169
 
            if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
 
170
            if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
170
171
                        s.rstrip('\r\n'),
171
172
                        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)
 
173
                content[ix] = s.replace('bzr.exe',
 
174
                                        '"%s"' % os.path.join(bzr_dir,
 
175
                                                              'bzr.exe'))
178
176
 
179
177
        if dry_run:
180
 
            print("*** Write file: start_brz.bat")
181
 
            print("*** File content:")
182
 
            print(''.join(content))
 
178
            print "*** Write file: start_bzr.bat"
 
179
            print "*** File content:"
 
180
            print ''.join(content)
183
181
        else:
184
 
            with open(fname, 'w') as f:
185
 
                f.write(''.join(content))
 
182
            f = file(fname, 'w')
 
183
            f.write(''.join(content))
 
184
            f.close()
186
185
 
187
186
    if (add_path or delete_path) and winver == 'Windows NT':
188
187
        # find appropriate registry key:
191
190
        keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
192
191
                                              r'\Session Manager\Environment')),
193
192
                (_winreg.HKEY_CURRENT_USER, r'Environment'),
194
 
                )
 
193
               )
195
194
 
196
195
        hkey = None
197
196
        for key, subkey in keys:
212
211
            break
213
212
 
214
213
        if hkey is None:
215
 
            print("Cannot find appropriate registry key for PATH")
 
214
            print "Cannot find appropriate registry key for PATH"
216
215
        else:
217
216
            path_list = [i for i in path_u.split(os.pathsep) if i != '']
218
217
            f_change = False
219
218
            for ix, item in enumerate(path_list[:]):
220
 
                if item == brz_dir:
 
219
                if item == bzr_dir:
221
220
                    if delete_path:
222
221
                        del path_list[ix]
223
222
                        f_change = True
224
223
                    elif add_path:
225
 
                        print("*** brz already in PATH")
 
224
                        print "*** Bzr already in PATH"
226
225
                    break
227
226
            else:
228
227
                if add_path and not delete_path:
229
 
                    path_list.append(brz_dir.decode(user_encoding))
 
228
                    path_list.append(bzr_dir.decode(user_encoding))
230
229
                    f_change = True
231
230
 
232
231
            if f_change:
233
232
                path_u = os.pathsep.join(path_list)
234
233
                if dry_run:
235
 
                    print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
236
 
                    print("*** Modify PATH variable. New value:")
237
 
                    print(path_u)
 
234
                    print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
 
235
                    print "*** Modify PATH variable. New value:"
 
236
                    print path_u
238
237
                else:
239
238
                    _winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
240
239
                    _winreg.FlushKey(hkey)
241
240
 
242
 
        if hkey is not None:
 
241
        if not hkey is None:
243
242
            _winreg.CloseKey(hkey)
244
243
 
245
244
    if (add_path or delete_path) and winver == 'Windows 98':
246
245
        # mutating autoexec.bat
247
246
        # adding or delete string:
248
 
        # SET PATH=%PATH%;C:\PROGRA~1\Breezy
 
247
        # SET PATH=%PATH%;C:\PROGRA~1\Bazaar
249
248
        abat = 'C:\\autoexec.bat'
250
249
        abak = 'C:\\autoexec.bak'
251
250
 
255
254
                if not dry_run:
256
255
                    shutil.copyfile(name, backupname)
257
256
                else:
258
 
                    print('*** backup copy of autoexec.bat created')
 
257
                    print '*** backup copy of autoexec.bat created'
259
258
 
260
259
        GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
261
260
        buf = ctypes.create_string_buffer(260)
262
 
        if GetShortPathName(brz_dir, buf, 260):
263
 
            brz_dir_8_3 = buf.value
 
261
        if GetShortPathName(bzr_dir, buf, 260):
 
262
            bzr_dir_8_3 = buf.value
264
263
        else:
265
 
            brz_dir_8_3 = brz_dir
266
 
        pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
 
264
            bzr_dir_8_3 = bzr_dir
 
265
        pattern = 'SET PATH=%PATH%;' + bzr_dir_8_3
267
266
 
268
267
        # search pattern
269
 
        with open(abat, 'r') as f:
270
 
            lines = f.readlines()
 
268
        f = file(abat, 'r')
 
269
        lines = f.readlines()
 
270
        f.close()
271
271
        found = False
272
272
        for i in lines:
273
273
            if i.rstrip('\r\n') == pattern:
277
277
        if delete_path and found:
278
278
            backup_autoexec_bat(abat, abak, dry_run)
279
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)
 
280
                f = file(abat, 'w')
 
281
                for i in lines:
 
282
                    if i.rstrip('\r\n') != pattern:
 
283
                        f.write(i)
 
284
                f.close()
284
285
            else:
285
 
                print('*** Remove line <%s> from autoexec.bat' % pattern)
286
 
 
 
286
                print '*** Remove line <%s> from autoexec.bat' % pattern
 
287
                    
287
288
        elif add_path and not found:
288
289
            backup_autoexec_bat(abat, abak, dry_run)
289
290
            if not dry_run:
290
 
                with open(abat, 'a') as f:
291
 
                    f.write(pattern)
292
 
                    f.write('\n')
 
291
                f = file(abat, 'a')
 
292
                f.write(pattern)
 
293
                f.write('\n')
 
294
                f.close()
293
295
            else:
294
 
                print('*** Add line <%s> to autoexec.bat' % pattern)
 
296
                print '*** Add line <%s> to autoexec.bat' % pattern
295
297
 
296
298
    if add_shell_menu and not delete_shell_menu:
297
299
        hkey = None
298
300
        try:
299
301
            hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
300
 
                                     r'Folder\shell\brz')
 
302
                                     r'Folder\shell\bzr')
301
303
        except EnvironmentError:
302
304
            if not silent:
303
305
                MessageBoxA(None,
305
307
                            'EnvironmentError',
306
308
                            MB_OK | MB_ICONERROR)
307
309
 
308
 
        if hkey is not None:
309
 
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
 
310
        if not hkey is None:
 
311
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
310
312
            hkey2 = _winreg.CreateKey(hkey, 'command')
311
313
            _winreg.SetValue(hkey2, '', _winreg.REG_SZ,
312
314
                             '%s /K "%s"' % (
313
315
                                    os.environ.get('COMSPEC', '%COMSPEC%'),
314
 
                                    os.path.join(brz_dir, 'start_brz.bat')))
 
316
                                    os.path.join(bzr_dir, 'start_bzr.bat')))
315
317
            _winreg.CloseKey(hkey2)
316
318
            _winreg.CloseKey(hkey)
317
319
 
318
320
    if delete_shell_menu:
319
321
        try:
320
322
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
321
 
                              r'Folder\shell\brz\command')
 
323
                              r'Folder\shell\bzr\command')
322
324
        except EnvironmentError:
323
325
            pass
324
326
 
325
327
        try:
326
328
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
327
 
                              r'Folder\shell\brz')
 
329
                              r'Folder\shell\bzr')
328
330
        except EnvironmentError:
329
331
            pass
330
332
 
337
339
                         "This library needed for SFTP transport.\n"
338
340
                         "If you need to work via SFTP you should download\n"
339
341
                         "this library manually and put it to directory\n"
340
 
                         "where Brz installed.\n"
 
342
                         "where Bzr installed.\n"
341
343
                         "For detailed instructions see:\n"
342
344
                         "http://wiki.bazaar.canonical.com/BzrOnPureWindows"
343
 
                         ),
 
345
                        ),
344
346
                        "Warning",
345
347
                        MB_OK | MB_ICONEXCLAMATION)
346
348