167
179
names = os.listdir(sitedir)
172
if name.endswith(os.extsep + "pth"):
173
addpackage(sitedir, name, known_paths)
182
dotpth = os.extsep + "pth"
183
names = [name for name in names if name.endswith(dotpth)]
184
for name in sorted(names):
185
addpackage(sitedir, name, known_paths)
175
187
known_paths = None
176
188
return known_paths
191
def check_enableusersite():
192
"""Check if user site directory is safe for inclusion
194
The function tests for the command line flag (including environment var),
195
process uid/gid equal to effective uid/gid.
197
None: Disabled for security reasons
198
False: Disabled by user (command line option)
199
True: Safe and enabled
201
if sys.flags.no_user_site:
204
if hasattr(os, "getuid") and hasattr(os, "geteuid"):
205
# check process uid == effective uid
206
if os.geteuid() != os.getuid():
208
if hasattr(os, "getgid") and hasattr(os, "getegid"):
209
# check process gid == effective gid
210
if os.getegid() != os.getgid():
216
def addusersitepackages(known_paths):
217
"""Add a per user site-package to sys.path
219
Each user has its own python directory with site-packages in the
222
USER_BASE is the root directory for all Python versions
224
USER_SITE is the user specific site-packages directory
226
USER_SITE/.. can be used for data.
228
global USER_BASE, USER_SITE, ENABLE_USER_SITE
229
env_base = os.environ.get("PYTHONUSERBASE", None)
232
return os.path.expanduser(os.path.join(*args))
234
#if sys.platform in ('os2emx', 'riscos'):
235
# # Don't know what to put here
239
base = os.environ.get("APPDATA") or "~"
240
USER_BASE = env_base if env_base else joinuser(base, "Python")
241
USER_SITE = os.path.join(USER_BASE,
242
"Python" + sys.version[0] + sys.version[2],
245
USER_BASE = env_base if env_base else joinuser("~", ".local")
246
USER_SITE = os.path.join(USER_BASE, "lib",
247
"python" + sys.version[:3],
250
if ENABLE_USER_SITE and os.path.isdir(USER_SITE):
251
addsitedir(USER_SITE, known_paths)
178
255
def addsitepackages(known_paths):
179
256
"""Add site-packages (and possibly site-python) to sys.path"""
180
prefixes = [sys.prefix]
181
if sys.exec_prefix != sys.prefix:
182
prefixes.append(sys.exec_prefix)
183
for prefix in prefixes:
185
if sys.platform in ('os2emx', 'riscos'):
186
sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
188
sitedirs = [os.path.join(prefix,
190
"python" + sys.version[:3],
192
os.path.join(prefix, "lib", "site-python")]
194
sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
195
if sys.platform == 'darwin':
196
# for framework builds *only* we add the standard Apple
197
# locations. Currently only per-user, but /Library and
198
# /Network/Library could be added too
199
if 'Python.framework' in prefix:
200
home = os.environ.get('HOME')
208
for sitedir in sitedirs:
209
if os.path.isdir(sitedir):
210
addsitedir(sitedir, known_paths)
260
for prefix in PREFIXES:
261
if not prefix or prefix in seen:
265
if sys.platform in ('os2emx', 'riscos'):
266
sitedirs.append(os.path.join(prefix, "Lib", "site-packages"))
268
sitedirs.append(os.path.join(prefix, "lib",
269
"python" + sys.version[:3],
271
sitedirs.append(os.path.join(prefix, "lib", "site-python"))
273
sitedirs.append(prefix)
274
sitedirs.append(os.path.join(prefix, "lib", "site-packages"))
276
if sys.platform == "darwin":
277
# for framework builds *only* we add the standard Apple
278
# locations. Currently only per-user, but /Library and
279
# /Network/Library could be added too
280
if 'Python.framework' in prefix:
283
os.path.join("~", "Library", "Python",
284
sys.version[:3], "site-packages")))
286
for sitedir in sitedirs:
287
if os.path.isdir(sitedir):
288
addsitedir(sitedir, known_paths)
214
293
def setBEGINLIBPATH():
424
print " %r," % (dir,)
517
%s [--user-base] [--user-site]
519
Without arguments print some useful information
520
With arguments print the value of USER_BASE and/or USER_SITE separated
523
Exit codes with --user-base or --user-site:
524
0 - user site directory is enabled
525
1 - user site directory is disabled by user
526
2 - uses site directory is disabled by super user
527
or for security reasons
534
print " %r," % (dir,)
536
print "USER_BASE: %r (%s)" % (USER_BASE,
537
"exists" if os.path.isdir(USER_BASE) else "doesn't exist")
538
print "USER_SITE: %r (%s)" % (USER_SITE,
539
"exists" if os.path.isdir(USER_SITE) else "doesn't exist")
540
print "ENABLE_USER_SITE: %r" % ENABLE_USER_SITE
544
if '--user-base' in args:
545
buffer.append(USER_BASE)
546
if '--user-site' in args:
547
buffer.append(USER_SITE)
550
print os.pathsep.join(buffer)
553
elif ENABLE_USER_SITE is False:
555
elif ENABLE_USER_SITE is None:
561
print textwrap.dedent(help % (sys.argv[0], os.pathsep))
427
564
if __name__ == '__main__':