/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2006, 2007, 2009, 2010 by Canonical Ltd
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
16
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
17
"""brz postinstall helper for win32 installation
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
18
Written by Alexander Belchenko
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
19
20
Dependency: ctypes
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
21
"""
22
23
import os
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
24
import shutil
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
25
import sys
26
27
28
##
29
# CONSTANTS
30
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
31
VERSION = "1.5.20070131"
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
32
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
33
USAGE = """Brz postinstall helper for win32 installation
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
34
Usage: %s [options]
35
36
OPTIONS:
37
    -h, --help                  - help message
38
    -v, --version               - version info
39
40
    -n, --dry-run               - print actions rather than execute them
41
    -q, --silent                - no messages for user
42
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
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
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
47
    --delete-shell-menu         - delete context menu from shell
48
    --check-mfc71               - check if MFC71.DLL present in system
49
""" % os.path.basename(sys.argv[0])
50
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
51
# Windows version
7143.16.19 by Jelmer Vernooij
Fix E231.
52
_major, _minor, _build, _platform, _text = sys.getwindowsversion()
2245.4.11 by Alexander Belchenko
Small fixes after John's review; added NEWS entry
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
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
72
    winver = 'Windows 98'
73
74
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
75
##
76
# INTERNAL VARIABLES
77
78
(OK, ERROR) = range(2)
79
VERSION_FORMAT = "%-50s%s"
80
81
82
def main():
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
83
    import ctypes
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
84
    import getopt
85
    import re
86
    import _winreg
87
88
    import locale
89
    user_encoding = locale.getpreferredencoding() or 'ascii'
90
91
    import ctypes
92
93
    hkey_str = {_winreg.HKEY_LOCAL_MACHINE: 'HKEY_LOCAL_MACHINE',
94
                _winreg.HKEY_CURRENT_USER: 'HKEY_CURRENT_USER',
95
                _winreg.HKEY_CLASSES_ROOT: 'HKEY_CLASSES_ROOT',
7143.16.6 by Jelmer Vernooij
Fix E124.
96
                }
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
97
98
    dry_run = False
99
    silent = False
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
100
    start_brz = False
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
101
    add_path = False
102
    delete_path = False
103
    add_shell_menu = False
104
    delete_shell_menu = False
105
    check_mfc71 = False
106
107
    try:
108
        opts, args = getopt.getopt(sys.argv[1:], "hvnq",
109
                                   ["help", "version",
110
                                    "dry-run",
111
                                    "silent",
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
112
                                    "start-brz",
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
113
                                    "add-path",
114
                                    "delete-path",
115
                                    "add-shell-menu",
116
                                    "delete-shell-menu",
117
                                    "check-mfc71",
7143.16.6 by Jelmer Vernooij
Fix E124.
118
                                    ])
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
119
120
        for o, a in opts:
121
            if o in ("-h", "--help"):
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
122
                print(USAGE)
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
123
                return OK
124
            elif o in ("-v", "--version"):
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
125
                print(VERSION_FORMAT % (USAGE.splitlines()[0], VERSION))
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
126
                return OK
127
128
            elif o in ('-n', "--dry-run"):
129
                dry_run = True
130
            elif o in ('-q', '--silent'):
131
                silent = True
132
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
133
            elif o == "--start-brz":
134
                start_brz = True
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
135
            elif o == "--add-path":
136
                add_path = True
137
            elif o == "--delete-path":
138
                delete_path = True
139
            elif o == "--add-shell-menu":
140
                add_shell_menu = True
141
            elif o == "--delete-shell-menu":
142
                delete_shell_menu = True
143
            elif o == "--check-mfc71":
144
                check_mfc71 = True
145
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
146
    except getopt.GetoptError as msg:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
147
        print(str(msg))
148
        print(USAGE)
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
149
        return ERROR
150
151
    # message box from Win32API
152
    MessageBoxA = ctypes.windll.user32.MessageBoxA
153
    MB_OK = 0
154
    MB_ICONERROR = 16
155
    MB_ICONEXCLAMATION = 48
156
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
157
    brz_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
158
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
159
    if start_brz:
160
        fname = os.path.join(brz_dir, "start_brz.bat")
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
161
        if os.path.isfile(fname):
6973.7.5 by Jelmer Vernooij
s/file/open.
162
            with open(fname, "r") as f:
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
163
                content = f.readlines()
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
164
        else:
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
165
            content = ["brz.exe help\n"]
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
166
167
        for ix in xrange(len(content)):
168
            s = content[ix]
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
169
            if re.match(r'.*(?<!\\)brz\.exe([ "].*)?$',
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
170
                        s.rstrip('\r\n'),
171
                        re.IGNORECASE):
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
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)
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
178
179
        if dry_run:
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
180
            print("*** Write file: start_brz.bat")
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
181
            print("*** File content:")
182
            print(''.join(content))
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
183
        else:
6973.7.5 by Jelmer Vernooij
s/file/open.
184
            with open(fname, 'w') as f:
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
185
                f.write(''.join(content))
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
186
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
187
    if (add_path or delete_path) and winver == 'Windows NT':
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
188
        # find appropriate registry key:
189
        # 1. HKLM\System\CurrentControlSet\Control\SessionManager\Environment
190
        # 2. HKCU\Environment
191
        keys = ((_winreg.HKEY_LOCAL_MACHINE, (r'System\CurrentControlSet\Control'
192
                                              r'\Session Manager\Environment')),
193
                (_winreg.HKEY_CURRENT_USER, r'Environment'),
7143.16.6 by Jelmer Vernooij
Fix E124.
194
                )
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
195
196
        hkey = None
197
        for key, subkey in keys:
198
            try:
199
                hkey = _winreg.OpenKey(key, subkey, 0, _winreg.KEY_ALL_ACCESS)
200
                try:
201
                    path_u, type_ = _winreg.QueryValueEx(hkey, 'Path')
202
                except WindowsError:
203
                    if key != _winreg.HKEY_CURRENT_USER:
204
                        _winreg.CloseKey(hkey)
205
                        hkey = None
206
                        continue
207
                    else:
208
                        path_u = u''
209
                        type_ = _winreg.REG_SZ
210
            except EnvironmentError:
211
                continue
212
            break
213
214
        if hkey is None:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
215
            print("Cannot find appropriate registry key for PATH")
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
216
        else:
217
            path_list = [i for i in path_u.split(os.pathsep) if i != '']
218
            f_change = False
219
            for ix, item in enumerate(path_list[:]):
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
220
                if item == brz_dir:
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
221
                    if delete_path:
222
                        del path_list[ix]
223
                        f_change = True
224
                    elif add_path:
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
225
                        print("*** brz already in PATH")
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
226
                    break
227
            else:
228
                if add_path and not delete_path:
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
229
                    path_list.append(brz_dir.decode(user_encoding))
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
230
                    f_change = True
231
232
            if f_change:
233
                path_u = os.pathsep.join(path_list)
234
                if dry_run:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
235
                    print("*** Registry key %s\\%s" % (hkey_str[key], subkey))
236
                    print("*** Modify PATH variable. New value:")
237
                    print(path_u)
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
238
                else:
239
                    _winreg.SetValueEx(hkey, 'Path', 0, type_, path_u)
240
                    _winreg.FlushKey(hkey)
241
7186.1.1 by Martin
Fix E27* lint errors
242
        if hkey is not None:
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
243
            _winreg.CloseKey(hkey)
244
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
245
    if (add_path or delete_path) and winver == 'Windows 98':
246
        # mutating autoexec.bat
247
        # adding or delete string:
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
248
        # SET PATH=%PATH%;C:\PROGRA~1\Breezy
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
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:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
258
                    print('*** backup copy of autoexec.bat created')
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
259
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
260
        GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
261
        buf = ctypes.create_string_buffer(260)
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
262
        if GetShortPathName(brz_dir, buf, 260):
263
            brz_dir_8_3 = buf.value
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
264
        else:
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
265
            brz_dir_8_3 = brz_dir
266
        pattern = 'SET PATH=%PATH%;' + brz_dir_8_3
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
267
268
        # search pattern
6973.7.5 by Jelmer Vernooij
s/file/open.
269
        with open(abat, 'r') as f:
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
270
            lines = f.readlines()
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
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:
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
278
            backup_autoexec_bat(abat, abak, dry_run)
279
            if not dry_run:
6973.7.5 by Jelmer Vernooij
s/file/open.
280
                with open(abat, 'w') as f:
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
281
                    for i in lines:
282
                        if i.rstrip('\r\n') != pattern:
283
                            f.write(i)
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
284
            else:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
285
                print('*** Remove line <%s> from autoexec.bat' % pattern)
7195.5.1 by Martin
Fix remaining whitespace lint in codebase
286
2245.4.8 by Alexander Belchenko
bzr_postinstall.py: on win98 path added to autoexec.bat should have 8.3 form
287
        elif add_path and not found:
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
288
            backup_autoexec_bat(abat, abak, dry_run)
289
            if not dry_run:
6973.7.5 by Jelmer Vernooij
s/file/open.
290
                with open(abat, 'a') as f:
6855.4.5 by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files.
291
                    f.write(pattern)
292
                    f.write('\n')
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
293
            else:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
294
                print('*** Add line <%s> to autoexec.bat' % pattern)
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
295
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
296
    if add_shell_menu and not delete_shell_menu:
297
        hkey = None
298
        try:
299
            hkey = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
300
                                     r'Folder\shell\brz')
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
301
        except EnvironmentError:
302
            if not silent:
303
                MessageBoxA(None,
304
                            'Unable to create registry key for context menu',
305
                            'EnvironmentError',
306
                            MB_OK | MB_ICONERROR)
307
7192.5.2 by Jelmer Vernooij
Fixes.
308
        if hkey is not None:
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
309
            _winreg.SetValue(hkey, '', _winreg.REG_SZ, 'Brz Here')
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
310
            hkey2 = _winreg.CreateKey(hkey, 'command')
311
            _winreg.SetValue(hkey2, '', _winreg.REG_SZ,
2245.4.7 by Alexander Belchenko
standalone installer: win98 support
312
                             '%s /K "%s"' % (
313
                                    os.environ.get('COMSPEC', '%COMSPEC%'),
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
314
                                    os.path.join(brz_dir, 'start_brz.bat')))
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
315
            _winreg.CloseKey(hkey2)
316
            _winreg.CloseKey(hkey)
317
318
    if delete_shell_menu:
319
        try:
320
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
321
                              r'Folder\shell\brz\command')
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
322
        except EnvironmentError:
323
            pass
324
325
        try:
326
            _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
6622.1.18 by Jelmer Vernooij
Rename some windows stuff.
327
                              r'Folder\shell\brz')
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
328
        except EnvironmentError:
329
            pass
330
331
    if check_mfc71:
332
        try:
333
            ctypes.windll.LoadLibrary('mfc71.dll')
334
        except WindowsError:
335
            MessageBoxA(None,
336
                        ("Library MFC71.DLL is not found on your system.\n"
337
                         "This library needed for SFTP transport.\n"
338
                         "If you need to work via SFTP you should download\n"
339
                         "this library manually and put it to directory\n"
7528 by Gustav Hartvigsson
Clean up, clarification.
340
                         "where Brz installed."
7143.16.6 by Jelmer Vernooij
Fix E124.
341
                         ),
1821.1.1 by Alexander Belchenko
win32 installer for bzr.dev.0.9
342
                        "Warning",
343
                        MB_OK | MB_ICONEXCLAMATION)
344
345
    return OK
346
347
348
if __name__ == "__main__":
349
    sys.exit(main())