• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Zope2/utilities/mkwsgiinstance.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE
11
#
12
##############################################################################
13

14
"""%(program)s:  Create a Zope WSGI instance home.
×
15

16
usage:  %(program)s [options]
17

18
Options:
19
-h/--help -- print this help text
20
-d/--dir  -- the dir in which the instance home should be created
21
-u/--user NAME:PASSWORD -- set the user name and password of the initial user
22
-s/--skelsrc -- the dir from which skeleton files should be copied
23
-p/--python -- the Python interpreter to use
24

25
When run without arguments, this script will ask for the information
26
necessary to create a Zope WSGI instance home.
27
"""
28

29
import getopt
×
30
import os
×
31
import subprocess
×
32
import sys
×
33
from configparser import ParsingError
×
34
from configparser import RawConfigParser
×
35

36
from . import copyzopeskel
×
37

38

39
def main():
×
40
    try:
×
41
        opts, args = getopt.getopt(
×
42
            sys.argv[1:],
43
            "hu:d:s:p:",
44
            ["help", "user=", "dir=", "skelsrc=", "python="]
45
        )
46
    except getopt.GetoptError as msg:
×
47
        usage(sys.stderr, msg)
×
48
        sys.exit(2)
×
49

50
    script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
×
51
    user = None
×
52
    password = None
×
53
    skeltarget = None
×
54
    skelsrc = None
×
55
    python = None
×
56

57
    if check_buildout(script_path):
×
58
        python = os.path.join(script_path, 'zopepy')
×
59

60
    for opt, arg in opts:
×
61
        if opt in ("-d", "--dir"):
×
62
            skeltarget = os.path.abspath(os.path.expanduser(arg))
×
63
            if not skeltarget:
×
64
                usage(sys.stderr, "dir must not be empty")
×
65
                sys.exit(2)
×
66
        if opt in ("-s", "--skelsrc"):
×
67
            skelsrc = os.path.abspath(os.path.expanduser(arg))
×
68
            if not skelsrc:
×
69
                usage(sys.stderr, "skelsrc must not be empty")
×
70
                sys.exit(2)
×
71
        if opt in ("-p", "--python"):
×
72
            python = os.path.abspath(os.path.expanduser(arg))
×
73
            if not os.path.exists(python) and os.path.isfile(python):
×
74
                usage(sys.stderr, "The Python interpreter does not exist.")
×
75
                sys.exit(2)
×
76
        if opt in ("-h", "--help"):
×
77
            usage(sys.stdout)
×
78
            sys.exit()
×
79
        if opt in ("-u", "--user"):
×
80
            if not arg:
×
81
                usage(sys.stderr, "user must not be empty")
×
82
                sys.exit(2)
×
83
            if ":" not in arg:
×
84
                usage(sys.stderr, "user must be specified as name:password")
×
85
                sys.exit(2)
×
86
            user, password = arg.split(":", 1)
×
87

88
    if not skeltarget:
×
89
        # interactively ask for skeltarget and initial user name/passwd.
90
        # cant set custom instancehome in interactive mode, we default
91
        # to skeltarget.
92
        skeltarget = instancehome = os.path.abspath(
×
93
            os.path.expanduser(get_skeltarget())
94
        )
95

96
    instancehome = skeltarget
×
97
    if skelsrc is None:
×
98
        # default to using stock Zope skeleton source
99
        skelsrc = os.path.join(os.path.dirname(__file__), "skel")
×
100

101
    inituser = os.path.join(instancehome, "inituser")
×
102
    if not (user or os.path.exists(inituser)):
×
103
        user, password = get_inituser()
×
104

105
    # we need to distinguish between python.exe and pythonw.exe under
106
    # Windows.  Zope is always run using 'python.exe' (even for services),
107
    # however, it may be installed via pythonw.exe (as a sub-process of an
108
    # installer).  Thus, sys.executable may not be the executable we use.
109
    # We still provide both PYTHON and PYTHONW, but PYTHONW should never
110
    # need be used.
111
    if python is None:
×
112
        python = sys.executable
×
113

114
    psplit = os.path.split(python)
×
115
    exedir = os.path.join(*psplit[:-1])
×
116
    pythonexe = os.path.join(exedir, 'python.exe')
×
117
    pythonwexe = os.path.join(exedir, 'pythonw.exe')
×
118

119
    if os.path.isfile(pythonwexe) and \
×
120
       os.path.isfile(pythonexe) and \
121
       python in [pythonwexe, pythonexe]:
122
        # we're using a Windows build with both python.exe and pythonw.exe
123
        # in the same directory
124
        PYTHON = pythonexe
×
125
        PYTHONW = pythonwexe
×
126
    else:
127
        # we're on UNIX or we have a nonstandard Windows setup
128
        PYTHON = PYTHONW = python
×
129

130
    zope2path = get_zope2path(PYTHON)
×
131

132
    kw = {
×
133
        "PYTHON": PYTHON,
134
        "PYTHONW": PYTHONW,
135
        "INSTANCE_HOME": instancehome,
136
        "ZOPE_SCRIPTS": script_path,
137
        "ZOPE2PATH": zope2path,
138
    }
139

140
    copyzopeskel.copyskel(skelsrc, skeltarget, None, None, **kw)
×
141
    if user and password:
×
142
        write_inituser(inituser, user, password)
×
143

144

145
def usage(stream, msg=None):
×
146
    if msg:
×
147
        stream.write(msg)
×
148
        stream.write('\n')
×
149

150
    program = os.path.basename(sys.argv[0])
×
151
    stream.write(__doc__ % {"program": program})
×
152

153

154
def get_skeltarget():
×
155
    print('Please choose a directory in which you\'d like to install')
×
156
    print('Zope "instance home" files such as database files, configuration')
×
157
    print('files, etc.\n')
×
158
    while 1:
159
        skeltarget = input("Directory: ").strip()
×
160
        if skeltarget == '':
×
161
            print('You must specify a directory')
×
162
            continue
×
163
        else:
164
            break
×
165
    return skeltarget
×
166

167

168
def get_inituser():
×
169
    import getpass
×
170
    print('Please choose a username and password for the initial user.')
×
171
    print('These will be the credentials you use to initially manage')
×
172
    print('your new Zope instance.\n')
×
173
    user = input("Username: ").strip()
×
174
    if user == '':
×
175
        return None, None
×
176
    while 1:
177
        passwd = getpass.getpass("Password: ")
×
178
        verify = getpass.getpass("Verify password: ")
×
179
        if verify == passwd:
×
180
            break
×
181
        else:
182
            passwd = verify = ''
×
183
            print("Password mismatch, please try again...")
×
184
    return user, passwd
×
185

186

187
def write_inituser(fn, user, password):
×
188
    import binascii
×
189
    from hashlib import sha1 as sha
×
190
    pw = binascii.b2a_base64(sha(password.encode('utf-8')).digest())[:-1]
×
191
    with open(fn, "wb") as fp:
×
192
        fp.write(user.encode('utf-8') + b':{SHA}' + pw + b'\n')
×
193
    os.chmod(fn, 0o644)
×
194

195

196
def check_buildout(script_path):
×
197
    """ Are we running from within a buildout which supplies 'zopepy'?
198
    """
199
    buildout_cfg = os.path.join(os.path.dirname(script_path), 'buildout.cfg')
×
200
    if os.path.exists(buildout_cfg):
×
201
        parser = RawConfigParser()
×
202
        try:
×
203
            parser.read(buildout_cfg)
×
204
            return 'zopepy' in parser.sections()
×
205
        except ParsingError:
×
206
            # zc.buildout uses its own parser and it allows syntax that
207
            # ConfigParser does not like. Here's one really stupid workaround.
208
            # The alternative is using the zc.buildout parser, which would
209
            # introduce a hard dependency.
210
            zope_py = os.path.join(os.path.dirname(script_path),
×
211
                                   'bin', 'zopepy')
212
            if os.path.isfile(zope_py) and os.access(zope_py, os.X_OK):
×
213
                return True
×
214

215

216
def get_zope2path(python):
×
217
    """ Get Zope2 path from selected Python interpreter.
218
    """
219
    zope2file = ''
×
220
    try:
×
221
        output = subprocess.check_output(
×
222
            [python, '-c', 'import Zope2; print(Zope2.__file__)'],
223
            universal_newlines=True,  # makes Python 3 return text, not bytes
224
            stderr=subprocess.PIPE)
225
        zope2file = output.strip()
×
226
    except subprocess.CalledProcessError:
×
227
        # fall back to current Python interpreter
228
        import Zope2
×
229
        zope2file = Zope2.__file__
×
230
    return os.path.abspath(os.path.dirname(os.path.dirname(zope2file)))
×
231

232

233
if __name__ == "__main__":
×
234
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc