~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/win32/bzr-win32-bdist-postinstall.py

[merge] bialix: add a win32 installer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (c) Canonical Ltd, 2006
 
2
# written by Alexander Belchenko for bzr project
 
3
#
 
4
# This script will be executed after installation of bzrlib package
 
5
# and before installer exits.
 
6
# All printed data will appear on the last screen of installation
 
7
# procedure.
 
8
# The main goal of this script is to create special batch file
 
9
# launcher for bzr. Typical content of this batch file is:
 
10
#  @python bzr %*
 
11
#
 
12
# This file works only on Windows 2000/XP. For win98 there is
 
13
# should be "%1 %2 %3 %4 %5 %6 %7 %8 %9" instead of "%*".
 
14
# Or even more complex thing.
 
15
#
 
16
# [bialix]: bzr de-facto does not support win98.
 
17
#           Although it seems to work on. Sometimes.
 
18
# 2006/07/30    added minimal support of win98.
 
19
 
 
20
import os
 
21
import sys
 
22
import _winreg
 
23
 
 
24
 
 
25
def _quoted_path(path):
 
26
    if ' ' in path:
 
27
        return '"' + path + '"'
 
28
    else:
 
29
        return path
 
30
 
 
31
def _win_batch_args():
 
32
    if os.name == 'nt':
 
33
        return '%*'
 
34
    else:
 
35
        return '%1 %2 %3 %4 %5 %6 %7 %8 %9'
 
36
 
 
37
 
 
38
if "-install" in sys.argv[1:]:
 
39
    # try to detect version number automatically
 
40
    try:
 
41
        import bzrlib
 
42
    except ImportError:
 
43
        ver = ''
 
44
    else:
 
45
        ver = bzrlib.__version__
 
46
 
 
47
    ##
 
48
    # XXX change message for something more appropriate
 
49
    print """Bazaar %s
 
50
 
 
51
Congratulation! Bzr successfully installed.
 
52
 
 
53
""" % ver
 
54
 
 
55
    batch_path = "bzr.bat"
 
56
    prefix = sys.exec_prefix
 
57
    try:
 
58
        ##
 
59
        # try to create
 
60
        scripts_dir = os.path.join(prefix, "Scripts")
 
61
        script_path = _quoted_path(os.path.join(scripts_dir, "bzr"))
 
62
        python_path = _quoted_path(os.path.join(prefix, "python.exe"))
 
63
        args = _win_batch_args()
 
64
        batch_str = "@%s %s %s" % (python_path, script_path, args)
 
65
        # minimal support of win98
 
66
        # if there is no HOME in system then set it for Bazaar manually
 
67
        homes = ('BZR_HOME', 'APPDATA', 'HOME')
 
68
        for home in homes:
 
69
            bzr_home = os.environ.get(home, None)
 
70
            if bzr_home is not None:
 
71
                break
 
72
        else:
 
73
            try:
 
74
                bzr_home = get_special_folder('CSIDL_APPDATA')
 
75
            except OSError:
 
76
                # no Application Data
 
77
                bzr_home = ''
 
78
 
 
79
            if not bzr_home:
 
80
                bzr_home = os.path.splitdrive(sys.prefix)[0] + '\\'
 
81
 
 
82
            batch_str = ("@SET BZR_HOME=" + _quoted_path(bzr_home) + "\n" +
 
83
                         batch_str)
 
84
 
 
85
        batch_path = script_path + ".bat"
 
86
        f = file(batch_path, "w")
 
87
        f.write(batch_str)
 
88
        f.close()
 
89
        file_created(batch_path)        # registering manually created files for
 
90
                                        # auto-deinstallation procedure
 
91
        ##
 
92
        # inform user where batch launcher is.
 
93
        print "Created:", batch_path
 
94
        print "Use this batch file to run bzr"
 
95
    except Exception, e:
 
96
        print "ERROR: Unable to create %s: %s" % (batch_path, e)
 
97
 
 
98
    # make entry in bzr home directory
 
99
    dst = os.path.join(bzr_home, "bazaar", "2.0")
 
100
    if not os.path.isdir(dst):
 
101
        os.makedirs(dst)
 
102
        import locale
 
103
        print "Configuration files stored in %s" % \
 
104
              dst.encode(locale.getpreferredencoding(), 'replace')
 
105
        # create dummy bazaar.conf
 
106
        f = file(os.path.join(dst,'bazaar.conf'), 'w')
 
107
        f.write("# main configuration file of Bazaar\n"
 
108
                "[DEFAULT]\n"
 
109
                "#email=Your Name <you@domain.com>\n")
 
110
        f.close()
 
111
 
 
112
    ## this hunk borrowed from pywin32_postinstall.py
 
113
    # use bdist_wininst builtins to create a shortcut.
 
114
    # CSIDL_COMMON_PROGRAMS only available works on NT/2000/XP, and
 
115
    # will fail there if the user has no admin rights.
 
116
    if get_root_hkey()==_winreg.HKEY_LOCAL_MACHINE:
 
117
        try:
 
118
            fldr = get_special_folder_path("CSIDL_COMMON_PROGRAMS")
 
119
        except OSError:
 
120
            # No CSIDL_COMMON_PROGRAMS on this platform
 
121
            fldr = get_special_folder_path("CSIDL_PROGRAMS")
 
122
    else:
 
123
        # non-admin install - always goes in this user's start menu.
 
124
        fldr = get_special_folder_path("CSIDL_PROGRAMS")
 
125
 
 
126
    # make Bazaar entry
 
127
    fldr = os.path.join(fldr, 'Bazaar')
 
128
    if not os.path.isdir(fldr):
 
129
        os.mkdir(fldr)
 
130
        directory_created(fldr)
 
131
 
 
132
    # link to documentation
 
133
    docs = os.path.join(sys.exec_prefix, 'Doc', 'Bazaar', 'index.htm')
 
134
    dst = os.path.join(fldr, 'Documentation.lnk')
 
135
    create_shortcut(docs, 'Bazaar Documentation', dst)
 
136
    file_created(dst)
 
137
    print 'Documentation for Bazaar: Start => Programs => Bazaar'
 
138
 
 
139
    # bzr in cmd shell
 
140
    if os.name == 'nt':
 
141
        cmd = os.environ.get('COMSPEC', 'cmd.exe')
 
142
        args = "/K bzr help"
 
143
    else:
 
144
        # minimal support of win98
 
145
        cmd = os.environ.get('COMSPEC', 'command.com')
 
146
        args = "bzr help"
 
147
    dst = os.path.join(fldr, 'Start bzr.lnk')
 
148
    create_shortcut(cmd,
 
149
                    'Start bzr in cmd shell',
 
150
                    dst,
 
151
                    args,
 
152
                    os.path.join(sys.exec_prefix, 'Scripts'))
 
153
    file_created(dst)