/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/brz_postinstall.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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
 
"""bzr postinstall helper for win32 installation
 
17
"""brz 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 = """Bzr postinstall helper for win32 installation
 
33
USAGE = """Brz 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-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
 
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
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_bzr = False
 
100
    start_brz = 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-bzr",
 
112
                                    "start-brz",
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-bzr":
134
 
                start_bzr = True
 
133
            elif o == "--start-brz":
 
134
                start_brz = 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, msg:
147
 
        print str(msg)
148
 
        print USAGE
 
146
    except getopt.GetoptError as 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
 
    bzr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
 
157
    brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
158
158
 
159
 
    if start_bzr:
160
 
        fname = os.path.join(bzr_dir, "start_bzr.bat")
 
159
    if start_brz:
 
160
        fname = os.path.join(brz_dir, "start_brz.bat")
161
161
        if os.path.isfile(fname):
162
 
            f = file(fname, "r")
163
 
            content = f.readlines()
164
 
            f.close()
 
162
            with open(fname, "r") as f:
 
163
                content = f.readlines()
165
164
        else:
166
 
            content = ["bzr.exe help\n"]
 
165
            content = ["brz.exe help\n"]
167
166
 
168
167
        for ix in xrange(len(content)):
169
168
            s = content[ix]
170
 
            if re.match(r'.*(?<!\\)bzr\.exe([ "].*)?$',
 
169
            if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
171
170
                        s.rstrip('\r\n'),
172
171
                        re.IGNORECASE):
173
 
                content[ix] = s.replace('bzr.exe',
174
 
                                        '"%s"' % os.path.join(bzr_dir,
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)
 
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)
179
178
 
180
179
        if dry_run:
181
 
            print "*** Write file: start_bzr.bat"
182
 
            print "*** File content:"
183
 
            print ''.join(content)
 
180
            print("*** Write file: start_brz.bat")
 
181
            print("*** File content:")
 
182
            print(''.join(content))
184
183
        else:
185
 
            f = file(fname, 'w')
186
 
            f.write(''.join(content))
187
 
            f.close()
 
184
            with open(fname, 'w') as f:
 
185
                f.write(''.join(content))
188
186
 
189
187
    if (add_path or delete_path) and winver == 'Windows NT':
190
188
        # find appropriate registry key:
193
191
        keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
194
192
                                              r'\Session Manager\Environment')),
195
193
                (_winreg.HKEY_CURRENT_USER, r'Environment'),
196
 
               )
 
194
                )
197
195
 
198
196
        hkey = None
199
197
        for key, subkey in keys:
214
212
            break
215
213
 
216
214
        if hkey is None:
217
 
            print "Cannot find appropriate registry key for PATH"
 
215
            print("Cannot find appropriate registry key for PATH")
218
216
        else:
219
217
            path_list = [i for i in path_u.split(os.pathsep) if i != '']
220
218
            f_change = False
221
219
            for ix, item in enumerate(path_list[:]):
222
 
                if item == bzr_dir:
 
220
                if item == brz_dir:
223
221
                    if delete_path:
224
222
                        del path_list[ix]
225
223
                        f_change = True
226
224
                    elif add_path:
227
 
                        print "*** Bzr already in PATH"
 
225
                        print("*** brz already in PATH")
228
226
                    break
229
227
            else:
230
228
                if add_path and not delete_path:
231
 
                    path_list.append(bzr_dir.decode(user_encoding))
 
229
                    path_list.append(brz_dir.decode(user_encoding))
232
230
                    f_change = True
233
231
 
234
232
            if f_change:
235
233
                path_u = os.pathsep.join(path_list)
236
234
                if dry_run:
237
 
                    print "*** Registry key %s\\%s" % (hkey_str[key], subkey)
238
 
                    print "*** Modify PATH variable. New value:"
239
 
                    print path_u
 
235
                    print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
 
236
                    print("*** Modify PATH variable. New value:")
 
237
                    print(path_u)
240
238
                else:
241
239
                    _winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
242
240
                    _winreg.FlushKey(hkey)
243
241
 
244
 
        if not hkey is None:
 
242
        if hkey is not None:
245
243
            _winreg.CloseKey(hkey)
246
244
 
247
245
    if (add_path or delete_path) and winver == 'Windows 98':
248
246
        # mutating autoexec.bat
249
247
        # adding or delete string:
250
 
        # SET PATH=%PATH%;C:\PROGRA~1\Bazaar
 
248
        # SET PATH=%PATH%;C:\PROGRA~1\Breezy
251
249
        abat = 'C:\\autoexec.bat'
252
250
        abak = 'C:\\autoexec.bak'
253
251
 
257
255
                if not dry_run:
258
256
                    shutil.copyfile(name, backupname)
259
257
                else:
260
 
                    print '*** backup copy of autoexec.bat created'
 
258
                    print('*** backup copy of autoexec.bat created')
261
259
 
262
260
        GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
263
261
        buf = ctypes.create_string_buffer(260)
264
 
        if GetShortPathName(bzr_dir, buf, 260):
265
 
            bzr_dir_8_3 = buf.value
 
262
        if GetShortPathName(brz_dir, buf, 260):
 
263
            brz_dir_8_3 = buf.value
266
264
        else:
267
 
            bzr_dir_8_3 = bzr_dir
268
 
        pattern = 'SET PATH=%PATH%;' + bzr_dir_8_3
 
265
            brz_dir_8_3 = brz_dir
 
266
        pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
269
267
 
270
268
        # search pattern
271
 
        f = file(abat, 'r')
272
 
        lines = f.readlines()
273
 
        f.close()
 
269
        with open(abat, 'r') as f:
 
270
            lines = f.readlines()
274
271
        found = False
275
272
        for i in lines:
276
273
            if i.rstrip('\r\n') == pattern:
280
277
        if delete_path and found:
281
278
            backup_autoexec_bat(abat, abak, dry_run)
282
279
            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()
 
280
                with open(abat, 'w') as f:
 
281
                    for i in lines:
 
282
                        if i.rstrip('\r\n') != pattern:
 
283
                            f.write(i)
288
284
            else:
289
 
                print '*** Remove line <%s> from autoexec.bat' % pattern
290
 
                    
 
285
                print('*** Remove line <%s> from autoexec.bat' % pattern)
 
286
 
291
287
        elif add_path and not found:
292
288
            backup_autoexec_bat(abat, abak, dry_run)
293
289
            if not dry_run:
294
 
                f = file(abat, 'a')
295
 
                f.write(pattern)
296
 
                f.write('\n')
297
 
                f.close()
 
290
                with open(abat, 'a') as f:
 
291
                    f.write(pattern)
 
292
                    f.write('\n')
298
293
            else:
299
 
                print '*** Add line <%s> to autoexec.bat' % pattern
 
294
                print('*** Add line <%s> to autoexec.bat' % pattern)
300
295
 
301
296
    if add_shell_menu and not delete_shell_menu:
302
297
        hkey = None
303
298
        try:
304
299
            hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
305
 
                                     r'Folder\shell\bzr')
 
300
                                     r'Folder\shell\brz')
306
301
        except EnvironmentError:
307
302
            if not silent:
308
303
                MessageBoxA(None,
310
305
                            'EnvironmentError',
311
306
                            MB_OK | MB_ICONERROR)
312
307
 
313
 
        if not hkey is None:
314
 
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Bzr Here')
 
308
        if hkey is not None:
 
309
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
315
310
            hkey2 = _winreg.CreateKey(hkey, 'command')
316
311
            _winreg.SetValue(hkey2, '', _winreg.REG_SZ,
317
312
                             '%s /K "%s"' % (
318
313
                                    os.environ.get('COMSPEC', '%COMSPEC%'),
319
 
                                    os.path.join(bzr_dir, 'start_bzr.bat')))
 
314
                                    os.path.join(brz_dir, 'start_brz.bat')))
320
315
            _winreg.CloseKey(hkey2)
321
316
            _winreg.CloseKey(hkey)
322
317
 
323
318
    if delete_shell_menu:
324
319
        try:
325
320
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
326
 
                              r'Folder\shell\bzr\command')
 
321
                              r'Folder\shell\brz\command')
327
322
        except EnvironmentError:
328
323
            pass
329
324
 
330
325
        try:
331
326
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
332
 
                              r'Folder\shell\bzr')
 
327
                              r'Folder\shell\brz')
333
328
        except EnvironmentError:
334
329
            pass
335
330
 
342
337
                         "This library needed for SFTP transport.\n"
343
338
                         "If you need to work via SFTP you should download\n"
344
339
                         "this library manually and put it to directory\n"
345
 
                         "where Bzr installed.\n"
 
340
                         "where Brz installed.\n"
346
341
                         "For detailed instructions see:\n"
347
342
                         "http://wiki.bazaar.canonical.com/BzrOnPureWindows"
348
 
                        ),
 
343
                         ),
349
344
                        "Warning",
350
345
                        MB_OK | MB_ICONEXCLAMATION)
351
346