Mercurial > cpython-lru_cache-weakref
annotate Lib/site.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 15949f34e08f |
| children | 6b74a93a2b8a |
| rev | line source |
|---|---|
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
1 """Append module search paths for third-party packages to sys.path. |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
2 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
3 **************************************************************** |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
4 * This module is automatically imported during initialization. * |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
5 **************************************************************** |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
6 |
| 30723 | 7 This will append site-specific paths to the module search path. On |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
8 Unix (including Mac OSX), it starts with sys.prefix and |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
9 sys.exec_prefix (if different) and appends |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
10 lib/python<version>/site-packages as well as lib/site-python. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
11 On other platforms (such as Windows), it tries each of the |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
12 prefixes directly, as well as with lib/site-packages appended. The |
|
6267
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
13 resulting directories, if they exist, are appended to sys.path, and |
|
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
14 also inspected for path configuration files. |
| 4010 | 15 |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
16 If a file named "pyvenv.cfg" exists one directory above sys.executable, |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
17 sys.prefix and sys.exec_prefix are set to that directory and |
|
77935
4a39ea2c1b11
Issue 15241: Improved site.py documentation relating to venvs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
77758
diff
changeset
|
18 it is also checked for site-packages and site-python (sys.base_prefix and |
|
4a39ea2c1b11
Issue 15241: Improved site.py documentation relating to venvs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
77758
diff
changeset
|
19 sys.base_exec_prefix will always be the "real" prefixes of the Python |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
20 installation). If "pyvenv.cfg" (a bootstrap configuration file) contains |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
21 the key "include-system-site-packages" set to anything other than "false" |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
22 (case-insensitive), the system-level prefixes will still also be |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
23 searched for site-packages; otherwise they won't. |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
24 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
25 All of the resulting site-specific directories, if they exist, are |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
26 appended to sys.path, and also inspected for path configuration |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
27 files. |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
28 |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
29 A path configuration file is a file whose name has the form |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
30 <package>.pth; its contents are additional directories (one per line) |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
31 to be added to sys.path. Non-existing directories (or |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
32 non-directories) are never added to sys.path; no directory is added to |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
33 sys.path more than once. Blank lines and lines beginning with |
|
21450
fa0bde3c86b2
Don't use Latex \code{...} in docstrings.
Guido van Rossum <guido@python.org>
parents:
21449
diff
changeset
|
34 '#' are skipped. Lines starting with 'import' are executed. |
| 4010 | 35 |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
36 For example, suppose sys.prefix and sys.exec_prefix are set to |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
37 /usr/local and there is a directory /usr/local/lib/python2.5/site-packages |
|
6267
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
38 with three subdirectories, foo, bar and spam, and two path |
|
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
39 configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
|
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
40 following: |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
41 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
42 # foo package configuration |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
43 foo |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
44 bar |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
45 bletch |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
46 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
47 and bar.pth contains: |
| 4010 | 48 |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
49 # bar package configuration |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
50 bar |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
51 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
52 Then the following directories are added to sys.path, in this order: |
| 4010 | 53 |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
54 /usr/local/lib/python2.5/site-packages/bar |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
55 /usr/local/lib/python2.5/site-packages/foo |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
56 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
57 Note that bletch is omitted because it doesn't exist; bar precedes foo |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
58 because bar.pth comes alphabetically before foo.pth; and spam is |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
59 omitted because it is not mentioned in either path configuration file. |
| 4010 | 60 |
| 61 After these path manipulations, an attempt is made to import a module | |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
62 named sitecustomize, which can perform arbitrary additional |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
63 site-specific customizations. If this import fails with an |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
64 ImportError exception, it is silently ignored. |
| 4010 | 65 |
| 66 """ | |
| 67 | |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
68 import sys |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
69 import os |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
70 import re |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
71 import builtins |
| 4010 | 72 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
73 # Prefixes for site-packages; add additional prefixes like /usr/local here |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
74 PREFIXES = [sys.prefix, sys.exec_prefix] |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
75 # Enable per user site-packages directory |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
76 # set it to False to disable the feature or True to force the feature |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
77 ENABLE_USER_SITE = None |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
78 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
79 # for distutils.commands.install |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
80 # These values are initialized by the getuserbase() and getusersitepackages() |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
81 # functions, through the main() function when Python starts. |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
82 USER_SITE = None |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
83 USER_BASE = None |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
84 |
|
16949
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16050
diff
changeset
|
85 |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
86 def makepath(*paths): |
|
65230
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
87 dir = os.path.join(*paths) |
|
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
88 try: |
|
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
89 dir = os.path.abspath(dir) |
|
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
90 except OSError: |
|
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
91 pass |
|
18364
2e1adbe89e4a
Avoid using os.path.normcase() on sys.path elements; doing so causes paths
Fred Drake <fdrake@acm.org>
parents:
18090
diff
changeset
|
92 return dir, os.path.normcase(dir) |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
93 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
94 |
| 60648 | 95 def abs_paths(): |
| 96 """Set all module __file__ and __cached__ attributes to an absolute path""" | |
|
41759
edb4c9028be0
Protect abs__file__() from changes to sys.modules while it's running.
Guido van Rossum <guido@python.org>
parents:
41645
diff
changeset
|
97 for m in set(sys.modules.values()): |
|
76303
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
75154
diff
changeset
|
98 if (getattr(getattr(m, '__loader__', None), '__module__', None) != |
|
2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents:
75154
diff
changeset
|
99 '_frozen_importlib'): |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37045
diff
changeset
|
100 continue # don't mess with a PEP 302-supplied __file__ |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
101 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
102 m.__file__ = os.path.abspath(m.__file__) |
|
65230
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
103 except (AttributeError, OSError): |
|
61731ec9a048
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59865
diff
changeset
|
104 pass |
| 60648 | 105 try: |
| 106 m.__cached__ = os.path.abspath(m.__cached__) | |
|
65226
ded61e26b841
Issue #6612: Fix site and sysconfig to catch os.getcwd() error, eg. if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64349
diff
changeset
|
107 except (AttributeError, OSError): |
| 60648 | 108 pass |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
109 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
110 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
111 def removeduppaths(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
112 """ Remove duplicate entries from sys.path along with making them |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
113 absolute""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
114 # This ensures that the initial path provided by the interpreter contains |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
115 # only absolute pathnames, even if we're running from the build directory. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
116 L = [] |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
117 known_paths = set() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
118 for dir in sys.path: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
119 # Filter out duplicate paths (on case-insensitive file systems also |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
120 # if they only differ in case); turn relative paths into absolute |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
121 # paths. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
122 dir, dircase = makepath(dir) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
123 if not dircase in known_paths: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
124 L.append(dir) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
125 known_paths.add(dircase) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
126 sys.path[:] = L |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
127 return known_paths |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
128 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
129 |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
130 def _init_pathinfo(): |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
131 """Return a set containing all existing directory entries from sys.path""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
132 d = set() |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
133 for dir in sys.path: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
134 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
135 if os.path.isdir(dir): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
136 dir, dircase = makepath(dir) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
137 d.add(dircase) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
138 except TypeError: |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
139 continue |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
140 return d |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
141 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
142 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
143 def addpackage(sitedir, name, known_paths): |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41759
diff
changeset
|
144 """Process a .pth file within the site-packages directory: |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41759
diff
changeset
|
145 For each line in the file, either combine it with sitedir to a path |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41759
diff
changeset
|
146 and add that to known_paths, or execute it if it starts with 'import '. |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41759
diff
changeset
|
147 """ |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
148 if known_paths is None: |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
149 _init_pathinfo() |
|
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
150 reset = 1 |
|
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
151 else: |
|
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
152 reset = 0 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
153 fullname = os.path.join(sitedir, name) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
154 try: |
|
69823
be5b8d1ded34
Replace open(filename, 'rU') by open(filename, 'r')
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68859
diff
changeset
|
155 f = open(fullname, "r") |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
156 except IOError: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
157 return |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
158 with f: |
|
67144
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
159 for n, line in enumerate(f): |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
160 if line.startswith("#"): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
161 continue |
|
67144
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
162 try: |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
163 if line.startswith(("import ", "import\t")): |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
164 exec(line) |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
165 continue |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
166 line = line.rstrip() |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
167 dir, dircase = makepath(sitedir, line) |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
168 if not dircase in known_paths and os.path.exists(dir): |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
169 sys.path.append(dir) |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
170 known_paths.add(dircase) |
|
73345
fa82071bb7e1
Remove redundant imports.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
69823
diff
changeset
|
171 except Exception: |
|
67144
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
172 print("Error processing line {:d} of {}:\n".format(n+1, fullname), |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
173 file=sys.stderr) |
|
75154
524a032c8e0f
site: don't import traceback at startup to speed up Python startup
Victor Stinner <victor.stinner@haypocalc.com>
parents:
73345
diff
changeset
|
174 import traceback |
|
67144
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
175 for record in traceback.format_exception(*sys.exc_info()): |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
176 for line in record.splitlines(): |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
177 print(' '+line, file=sys.stderr) |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
178 print("\nRemainder of file ignored", file=sys.stderr) |
|
e20a6436a927
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65230
diff
changeset
|
179 break |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
180 if reset: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
181 known_paths = None |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
182 return known_paths |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
183 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
184 |
|
32566
3aadfc3fc4e6
Change argument list for addsitedir() to not require a second argument and thus
Brett Cannon <bcannon@gmail.com>
parents:
32528
diff
changeset
|
185 def addsitedir(sitedir, known_paths=None): |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
186 """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
187 'sitedir'""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
188 if known_paths is None: |
|
32766
d15517f2abe8
Fix bug in addsitedir() to properly handle the lack of a second argument.
Brett Cannon <bcannon@gmail.com>
parents:
32566
diff
changeset
|
189 known_paths = _init_pathinfo() |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
190 reset = 1 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
191 else: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
192 reset = 0 |
|
18364
2e1adbe89e4a
Avoid using os.path.normcase() on sys.path elements; doing so causes paths
Fred Drake <fdrake@acm.org>
parents:
18090
diff
changeset
|
193 sitedir, sitedircase = makepath(sitedir) |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
194 if not sitedircase in known_paths: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7077
diff
changeset
|
195 sys.path.append(sitedir) # Add path component |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
196 known_paths.add(sitedircase) |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
197 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7077
diff
changeset
|
198 names = os.listdir(sitedir) |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
199 except os.error: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7077
diff
changeset
|
200 return |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
201 names = [name for name in names if name.endswith(".pth")] |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
202 for name in sorted(names): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
203 addpackage(sitedir, name, known_paths) |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
204 if reset: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
205 known_paths = None |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
206 return known_paths |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
207 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
208 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
209 def check_enableusersite(): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
210 """Check if user site directory is safe for inclusion |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
211 |
|
47066
31cc8e1a8fdf
Merged revisions 62954-62959,62961,62963-62967,62969-62970,62972-62973,62975-62976,62978-62982,62984,62987-62996 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47063
diff
changeset
|
212 The function tests for the command line flag (including environment var), |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
213 process uid/gid equal to effective uid/gid. |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
214 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
215 None: Disabled for security reasons |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
216 False: Disabled by user (command line option) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
217 True: Safe and enabled |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
218 """ |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
219 if sys.flags.no_user_site: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
220 return False |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
221 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
222 if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
223 # check process uid == effective uid |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
224 if os.geteuid() != os.getuid(): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
225 return None |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
226 if hasattr(os, "getgid") and hasattr(os, "getegid"): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
227 # check process gid == effective gid |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
228 if os.getegid() != os.getgid(): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
229 return None |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
230 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
231 return True |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
232 |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
233 def getuserbase(): |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
234 """Returns the `user base` directory path. |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
235 |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
236 The `user base` directory can be used to store data. If the global |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
237 variable ``USER_BASE`` is not initialized yet, this function will also set |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
238 it. |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
239 """ |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
240 global USER_BASE |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
241 if USER_BASE is not None: |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
242 return USER_BASE |
|
58558
5aa90248a99f
Merged revisions 77704,77752 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55708
diff
changeset
|
243 from sysconfig import get_config_var |
|
5aa90248a99f
Merged revisions 77704,77752 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55708
diff
changeset
|
244 USER_BASE = get_config_var('userbase') |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
245 return USER_BASE |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
246 |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
247 def getusersitepackages(): |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
248 """Returns the user-specific site-packages directory path. |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
249 |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
250 If the global variable ``USER_SITE`` is not initialized yet, this |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
251 function will also set it. |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
252 """ |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
253 global USER_SITE |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
254 user_base = getuserbase() # this will also set USER_BASE |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
255 |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
256 if USER_SITE is not None: |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
257 return USER_SITE |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
258 |
|
58558
5aa90248a99f
Merged revisions 77704,77752 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55708
diff
changeset
|
259 from sysconfig import get_path |
|
61403
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
260 |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
261 if sys.platform == 'darwin': |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
262 from sysconfig import get_config_var |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
263 if get_config_var('PYTHONFRAMEWORK'): |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
264 USER_SITE = get_path('purelib', 'osx_framework_user') |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
265 return USER_SITE |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
266 |
|
58558
5aa90248a99f
Merged revisions 77704,77752 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55708
diff
changeset
|
267 USER_SITE = get_path('purelib', '%s_user' % os.name) |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
268 return USER_SITE |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
269 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
270 def addusersitepackages(known_paths): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
271 """Add a per user site-package to sys.path |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
272 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
273 Each user has its own python directory with site-packages in the |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
274 home directory. |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
275 """ |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
276 # get the per user site-package path |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
277 # this call will also make sure USER_BASE and USER_SITE are set |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
278 user_site = getusersitepackages() |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
279 |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
280 if ENABLE_USER_SITE and os.path.isdir(user_site): |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
281 addsitedir(user_site, known_paths) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
282 return known_paths |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
283 |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
284 def getsitepackages(prefixes=None): |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
285 """Returns a list containing all global site-packages directories |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
286 (and possibly site-python). |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
287 |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
288 For each directory present in ``prefixes`` (or the global ``PREFIXES``), |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
289 this function will find its `site-packages` subdirectory depending on the |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
290 system environment, and will return a list of full paths. |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
291 """ |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
292 sitepackages = [] |
|
62566
504c499e8c95
Merged revisions 81465-81466,81468,81679,81735,81760,81868,82183 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61403
diff
changeset
|
293 seen = set() |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
294 |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
295 if prefixes is None: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
296 prefixes = PREFIXES |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
297 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
298 for prefix in prefixes: |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
299 if not prefix or prefix in seen: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
300 continue |
|
62566
504c499e8c95
Merged revisions 81465-81466,81468,81679,81735,81760,81868,82183 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61403
diff
changeset
|
301 seen.add(prefix) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
302 |
|
79480
15949f34e08f
#16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents:
77935
diff
changeset
|
303 if sys.platform == 'riscos': |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
304 sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
305 elif os.sep == '/': |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
306 sitepackages.append(os.path.join(prefix, "lib", |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
307 "python" + sys.version[:3], |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
308 "site-packages")) |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
309 sitepackages.append(os.path.join(prefix, "lib", "site-python")) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
310 else: |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
311 sitepackages.append(prefix) |
|
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
312 sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
313 if sys.platform == "darwin": |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
314 # for framework builds *only* we add the standard Apple |
|
52497
0d62cc5b48d5
Merged revisions 70778 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
47066
diff
changeset
|
315 # locations. |
|
61403
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
316 from sysconfig import get_config_var |
|
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
317 framework = get_config_var("PYTHONFRAMEWORK") |
|
63413
5ee3e2712d7d
Ensure that test_site actually passes with a framework build
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
62566
diff
changeset
|
318 if framework: |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
319 sitepackages.append( |
|
61403
3fae7cb49209
Merged revisions 80967 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60648
diff
changeset
|
320 os.path.join("/Library", framework, |
|
52497
0d62cc5b48d5
Merged revisions 70778 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
47066
diff
changeset
|
321 sys.version[:3], "site-packages")) |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
322 return sitepackages |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
323 |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
324 def addsitepackages(known_paths, prefixes=None): |
|
55708
86ef74c4ad97
Merged revisions 74526 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52497
diff
changeset
|
325 """Add site-packages (and possibly site-python) to sys.path""" |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
326 for sitedir in getsitepackages(prefixes): |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
327 if os.path.isdir(sitedir): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
328 addsitedir(sitedir, known_paths) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
329 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
330 return known_paths |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
331 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
332 def setquit(): |
|
60570
7a390eb50df1
Merged revisions 80022 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
60058
diff
changeset
|
333 """Define new builtins 'quit' and 'exit'. |
|
7a390eb50df1
Merged revisions 80022 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
60058
diff
changeset
|
334 |
|
7a390eb50df1
Merged revisions 80022 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
60058
diff
changeset
|
335 These are objects which make the interpreter exit when called. |
|
7a390eb50df1
Merged revisions 80022 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
60058
diff
changeset
|
336 The repr of each object contains a hint at how it works. |
|
8878
813f527f3bec
Add built-in string variables 'quit' and 'exit' that display a hint on
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
337 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
338 """ |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
339 if os.sep == ':': |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
340 eof = 'Cmd-Q' |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
341 elif os.sep == '\\': |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
342 eof = 'Ctrl-Z plus Return' |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
343 else: |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
344 eof = 'Ctrl-D (i.e. EOF)' |
|
36937
df43c04d2554
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36922
diff
changeset
|
345 |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
346 class Quitter(object): |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
347 def __init__(self, name): |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
348 self.name = name |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
349 def __repr__(self): |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
350 return 'Use %s() or %s to exit' % (self.name, eof) |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
351 def __call__(self, code=None): |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
352 # Shells like IDLE catch the SystemExit, but listen when their |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
353 # stdin wrapper is closed. |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
354 try: |
|
44390
86358b43c8bb
Don't close sys.stdin with quit() if sys.stdin wraps fd 0. Otherwise it will raise a warning: Lib/io.py:1221: RuntimeWarning: Trying to close unclosable fd
Christian Heimes <christian@cheimes.de>
parents:
44364
diff
changeset
|
355 fd = -1 |
|
86358b43c8bb
Don't close sys.stdin with quit() if sys.stdin wraps fd 0. Otherwise it will raise a warning: Lib/io.py:1221: RuntimeWarning: Trying to close unclosable fd
Christian Heimes <christian@cheimes.de>
parents:
44364
diff
changeset
|
356 if hasattr(sys.stdin, "fileno"): |
|
86358b43c8bb
Don't close sys.stdin with quit() if sys.stdin wraps fd 0. Otherwise it will raise a warning: Lib/io.py:1221: RuntimeWarning: Trying to close unclosable fd
Christian Heimes <christian@cheimes.de>
parents:
44364
diff
changeset
|
357 fd = sys.stdin.fileno() |
|
86358b43c8bb
Don't close sys.stdin with quit() if sys.stdin wraps fd 0. Otherwise it will raise a warning: Lib/io.py:1221: RuntimeWarning: Trying to close unclosable fd
Christian Heimes <christian@cheimes.de>
parents:
44364
diff
changeset
|
358 if fd != 0: |
|
86358b43c8bb
Don't close sys.stdin with quit() if sys.stdin wraps fd 0. Otherwise it will raise a warning: Lib/io.py:1221: RuntimeWarning: Trying to close unclosable fd
Christian Heimes <christian@cheimes.de>
parents:
44364
diff
changeset
|
359 # Don't close stdin if it wraps fd 0 |
|
86358b43c8bb
Don't close sys.stdin with quit() if sys.stdin wraps fd 0. Otherwise it will raise a warning: Lib/io.py:1221: RuntimeWarning: Trying to close unclosable fd
Christian Heimes <christian@cheimes.de>
parents:
44364
diff
changeset
|
360 sys.stdin.close() |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
361 except: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39576
diff
changeset
|
362 pass |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
363 raise SystemExit(code) |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
364 builtins.quit = Quitter('quit') |
|
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
365 builtins.exit = Quitter('exit') |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
366 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
367 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
368 class _Printer(object): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
369 """interactive prompt objects for printing the license text, a list of |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
370 contributors and the copyright notice.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
371 |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
372 MAXLINES = 23 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
373 |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
374 def __init__(self, name, data, files=(), dirs=()): |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
375 self.__name = name |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
376 self.__data = data |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
377 self.__files = files |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
378 self.__dirs = dirs |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
379 self.__lines = None |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
380 |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
381 def __setup(self): |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
382 if self.__lines: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
383 return |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
384 data = None |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
385 for dir in self.__dirs: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
386 for filename in self.__files: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
387 filename = os.path.join(dir, filename) |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
388 try: |
|
69823
be5b8d1ded34
Replace open(filename, 'rU') by open(filename, 'r')
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68859
diff
changeset
|
389 fp = open(filename, "r") |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
390 data = fp.read() |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
391 fp.close() |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
392 break |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
393 except IOError: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
394 pass |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
395 if data: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
396 break |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
397 if not data: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
398 data = self.__data |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
399 self.__lines = data.split('\n') |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
400 self.__linecnt = len(self.__lines) |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
401 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
402 def __repr__(self): |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
403 self.__setup() |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
404 if len(self.__lines) <= self.MAXLINES: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
405 return "\n".join(self.__lines) |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
406 else: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
407 return "Type %s() to see the full %s text" % ((self.__name,)*2) |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
408 |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
409 def __call__(self): |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
410 self.__setup() |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
411 prompt = 'Hit Return for more, or q (and Return) to quit: ' |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
412 lineno = 0 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
413 while 1: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
414 try: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
415 for i in range(lineno, lineno + self.MAXLINES): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
416 print(self.__lines[i]) |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
417 except IndexError: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
418 break |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
419 else: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
420 lineno += self.MAXLINES |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
421 key = None |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
422 while key is None: |
|
44364
6ea0e15ce573
Fix issue #1667. The _Printer() class was using sys.stdin.readline()
Guido van Rossum <guido@python.org>
parents:
44098
diff
changeset
|
423 key = input(prompt) |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
424 if key not in ('', 'q'): |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
425 key = None |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
426 if key == 'q': |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
427 break |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
428 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
429 def setcopyright(): |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
430 """Set 'copyright' and 'credits' in builtins""" |
|
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
431 builtins.copyright = _Printer("copyright", sys.copyright) |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
432 if sys.platform[:4] == 'java': |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
433 builtins.credits = _Printer( |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
434 "credits", |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
435 "Jython is maintained by the Jython developers (www.jython.org).") |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
436 else: |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
437 builtins.credits = _Printer("credits", """\ |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
438 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
439 for supporting Python development. See www.python.org for more information.""") |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
440 here = os.path.dirname(os.__file__) |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
441 builtins.license = _Printer( |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
442 "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
443 ["LICENSE.txt", "LICENSE"], |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
444 [os.path.join(here, os.pardir), here, os.curdir]) |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
445 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
446 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
447 class _Helper(object): |
|
60570
7a390eb50df1
Merged revisions 80022 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
60058
diff
changeset
|
448 """Define the builtin 'help'. |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
449 This is a wrapper around pydoc.help (with a twist). |
|
18090
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
450 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
451 """ |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
452 |
|
18090
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
453 def __repr__(self): |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
454 return "Type help() for interactive help, " \ |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
455 "or help(object) for help about object." |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
456 def __call__(self, *args, **kwds): |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
457 import pydoc |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
458 return pydoc.help(*args, **kwds) |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
459 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
460 def sethelper(): |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
43605
diff
changeset
|
461 builtins.help = _Helper() |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
462 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
463 def aliasmbcs(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
464 """On Windows, some default encodings are not provided by Python, |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
465 while they are always available as "mbcs" in each locale. Make |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
466 them usable by aliasing to "mbcs" in such a case.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
467 if sys.platform == 'win32': |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
468 import locale, codecs |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
469 enc = locale.getdefaultlocale()[1] |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
470 if enc.startswith('cp'): # "cp***" ? |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
471 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
472 codecs.lookup(enc) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
473 except LookupError: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
474 import encodings |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
475 encodings._cache[enc] = encodings._unknown |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
476 encodings.aliases.aliases[enc] = 'mbcs' |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
477 |
|
18090
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
478 |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
479 CONFIG_LINE = re.compile(r'^(?P<key>(\w|[-_])+)\s*=\s*(?P<value>.*)\s*$') |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
480 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
481 def venv(known_paths): |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
482 global PREFIXES, ENABLE_USER_SITE |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
483 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
484 env = os.environ |
|
77758
c96dd645825e
Closes #15173: Tidied up copyright statements and removed pythonv references.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
77154
diff
changeset
|
485 if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: |
|
c96dd645825e
Closes #15173: Tidied up copyright statements and removed pythonv references.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
77154
diff
changeset
|
486 executable = os.environ['__PYVENV_LAUNCHER__'] |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
487 else: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
488 executable = sys.executable |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
489 executable_dir, executable_name = os.path.split(executable) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
490 site_prefix = os.path.dirname(executable_dir) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
491 sys._home = None |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
492 if sys.platform == 'win32': |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
493 executable_name = os.path.splitext(executable_name)[0] |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
494 conf_basename = 'pyvenv.cfg' |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
495 candidate_confs = [ |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
496 conffile for conffile in ( |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
497 os.path.join(executable_dir, conf_basename), |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
498 os.path.join(site_prefix, conf_basename) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
499 ) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
500 if os.path.isfile(conffile) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
501 ] |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
502 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
503 if candidate_confs: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
504 virtual_conf = candidate_confs[0] |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
505 system_site = "true" |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
506 with open(virtual_conf) as f: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
507 for line in f: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
508 line = line.strip() |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
509 m = CONFIG_LINE.match(line) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
510 if m: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
511 d = m.groupdict() |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
512 key, value = d['key'].lower(), d['value'] |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
513 if key == 'include-system-site-packages': |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
514 system_site = value.lower() |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
515 elif key == 'home': |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
516 sys._home = value |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
517 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
518 sys.prefix = sys.exec_prefix = site_prefix |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
519 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
520 # Doing this here ensures venv takes precedence over user-site |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
521 addsitepackages(known_paths, [sys.prefix]) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
522 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
523 # addsitepackages will process site_prefix again if its in PREFIXES, |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
524 # but that's ok; known_paths will prevent anything being added twice |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
525 if system_site == "true": |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
526 PREFIXES.insert(0, sys.prefix) |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
527 else: |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
528 PREFIXES = [sys.prefix] |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
529 ENABLE_USER_SITE = False |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
530 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
531 return known_paths |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
532 |
|
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
533 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
534 def execsitecustomize(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
535 """Run custom site specific code, if available.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
536 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
537 import sitecustomize |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
538 except ImportError: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
539 pass |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
540 except Exception as err: |
|
39576
d23b7cb1700b
When sitecustomize.py fails, don't make all of site.py fail,
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
541 if os.environ.get("PYTHONVERBOSE"): |
|
59865
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
542 sys.excepthook(*sys.exc_info()) |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
543 else: |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
544 sys.stderr.write( |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
545 "Error in sitecustomize; set PYTHONVERBOSE for traceback:\n" |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
546 "%s: %s\n" % |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
547 (err.__class__.__name__, err)) |
|
28141
a3cf07bac373
Patch #671666: Alias ANSI code page to "mbcs".
Martin v. Löwis <martin@v.loewis.de>
parents:
26885
diff
changeset
|
548 |
|
13612
f3401ed132a1
-- changed default encoding to "ascii". you can still change
Fredrik Lundh <fredrik@pythonware.com>
parents:
13523
diff
changeset
|
549 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
550 def execusercustomize(): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
551 """Run custom user specific code, if available.""" |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
552 try: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
553 import usercustomize |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
554 except ImportError: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
555 pass |
|
59865
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
556 except Exception as err: |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
557 if os.environ.get("PYTHONVERBOSE"): |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
558 sys.excepthook(*sys.exc_info()) |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
559 else: |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
560 sys.stderr.write( |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
561 "Error in usercustomize; set PYTHONVERBOSE for traceback:\n" |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
562 "%s: %s\n" % |
|
c2d2e26bcf7f
Merged revisions 78872 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
52497
diff
changeset
|
563 (err.__class__.__name__, err)) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
564 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
565 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
566 def main(): |
|
68859
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
567 """Add standard site-specific directories to the module search path. |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
568 |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
569 This function is called automatically when this module is imported, |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
570 unless the python interpreter was started with the -S flag. |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
571 """ |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
572 global ENABLE_USER_SITE |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
573 |
| 60648 | 574 abs_paths() |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
575 known_paths = removeduppaths() |
|
77154
294f8aeb4d4b
Implemented PEP 405 (Python virtual environments).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
76303
diff
changeset
|
576 known_paths = venv(known_paths) |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
577 if ENABLE_USER_SITE is None: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
578 ENABLE_USER_SITE = check_enableusersite() |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
579 known_paths = addusersitepackages(known_paths) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
580 known_paths = addsitepackages(known_paths) |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
581 setquit() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
582 setcopyright() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
583 sethelper() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
584 aliasmbcs() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
585 execsitecustomize() |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
586 if ENABLE_USER_SITE: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
587 execusercustomize() |
|
12873
efc6d7789d9e
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
9520
diff
changeset
|
588 |
|
68859
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
589 # Prevent edition of sys.path when python was started with -S and |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
590 # site is imported later. |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
591 if not sys.flags.no_site: |
|
a364719e400a
Do not touch sys.path when site is imported and python was started with -S.
Éric Araujo <merwok@netwok.org>
parents:
68257
diff
changeset
|
592 main() |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
593 |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
594 def _script(): |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
595 help = """\ |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
596 %s [--user-base] [--user-site] |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
597 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
598 Without arguments print some useful information |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
599 With arguments print the value of USER_BASE and/or USER_SITE separated |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
600 by '%s'. |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
601 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
602 Exit codes with --user-base or --user-site: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
603 0 - user site directory is enabled |
|
47063
081c15806e28
Merged revisions 62914-62916,62918-62919,62921-62922,62924-62942,62944-62945,62947-62949 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46708
diff
changeset
|
604 1 - user site directory is disabled by user |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
605 2 - uses site directory is disabled by super user |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
606 or for security reasons |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
607 >2 - unknown error |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
608 """ |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
609 args = sys.argv[1:] |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
610 if not args: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
611 print("sys.path = [") |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
612 for dir in sys.path: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
613 print(" %r," % (dir,)) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
614 print("]") |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
615 print("USER_BASE: %r (%s)" % (USER_BASE, |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
616 "exists" if os.path.isdir(USER_BASE) else "doesn't exist")) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
617 print("USER_SITE: %r (%s)" % (USER_SITE, |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
618 "exists" if os.path.isdir(USER_SITE) else "doesn't exist")) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
619 print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
620 sys.exit(0) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
621 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
622 buffer = [] |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
623 if '--user-base' in args: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
624 buffer.append(USER_BASE) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
625 if '--user-site' in args: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
626 buffer.append(USER_SITE) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
627 |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
628 if buffer: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
629 print(os.pathsep.join(buffer)) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
630 if ENABLE_USER_SITE: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
631 sys.exit(0) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
632 elif ENABLE_USER_SITE is False: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
633 sys.exit(1) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
634 elif ENABLE_USER_SITE is None: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
635 sys.exit(2) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
636 else: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
637 sys.exit(3) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
638 else: |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
639 import textwrap |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
640 print(textwrap.dedent(help % (sys.argv[0], os.pathsep))) |
|
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
641 sys.exit(10) |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
642 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
643 if __name__ == '__main__': |
|
46708
74b9c85361f8
Merged revisions 62774-62775,62785,62787-62788 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
644 _script() |
