Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a608c0

Browse files
author
Tarek Ziadé
committed
Merged revisions 74526 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r74526 | tarek.ziade | 2009-08-20 23:23:13 +0200 (Thu, 20 Aug 2009) | 1 line #6693: New functions in site.py to get user/global site packages paths. ........
1 parent 21baf33 commit 4a608c0

4 files changed

Lines changed: 154 additions & 36 deletions

File tree

Doc/library/site.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ empty, and the path manipulations are skipped; however the import of
116116

117117
Adds a directory to sys.path and processes its pth files.
118118

119+
.. function:: getsitepackages()
120+
121+
Returns a list containing all global site-packages directories
122+
(and possibly site-python).
123+
124+
.. versionadded:: 2.7
125+
126+
.. function:: getuserbase()
127+
128+
Returns the `user base` directory path.
129+
130+
The `user base` directory can be used to store data. If the global
131+
variable ``USER_BASE`` is not initialized yet, this function will also set
132+
it.
133+
134+
.. versionadded:: 2.7
135+
136+
.. function:: getusersitepackages()
137+
138+
Returns the user-specific site-packages directory path.
139+
140+
If the global variable ``USER_SITE`` is not initialized yet, this
141+
function will also set it.
142+
143+
.. versionadded:: 2.7
119144

120145
XXX Update documentation
121146
XXX document python -m site --user-base --user-site
147+

Lib/site.py

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@
6161
# Enable per user site-packages directory
6262
# set it to False to disable the feature or True to force the feature
6363
ENABLE_USER_SITE = None
64+
6465
# for distutils.commands.install
66+
# These values are initialized by the getuserbase() and getusersitepackages()
67+
# functions, through the main() function when Python starts.
6568
USER_SITE = None
6669
USER_BASE = None
6770

@@ -205,49 +208,75 @@ def check_enableusersite():
205208

206209
return True
207210

211+
def getuserbase():
212+
"""Returns the `user base` directory path.
208213
209-
def addusersitepackages(known_paths):
210-
"""Add a per user site-package to sys.path
211-
212-
Each user has its own python directory with site-packages in the
213-
home directory.
214-
215-
USER_BASE is the root directory for all Python versions
216-
217-
USER_SITE is the user specific site-packages directory
218-
219-
USER_SITE/.. can be used for data.
214+
The `user base` directory can be used to store data. If the global
215+
variable ``USER_BASE`` is not initialized yet, this function will also set
216+
it.
220217
"""
221-
global USER_BASE, USER_SITE, ENABLE_USER_SITE
218+
global USER_BASE
219+
if USER_BASE is not None:
220+
return USER_BASE
221+
222222
env_base = os.environ.get("PYTHONUSERBASE", None)
223223

224224
def joinuser(*args):
225225
return os.path.expanduser(os.path.join(*args))
226226

227-
#if sys.platform in ('os2emx', 'riscos'):
228-
# # Don't know what to put here
229-
# USER_BASE = ''
230-
# USER_SITE = ''
227+
# what about 'os2emx', 'riscos' ?
231228
if os.name == "nt":
232229
base = os.environ.get("APPDATA") or "~"
233230
USER_BASE = env_base if env_base else joinuser(base, "Python")
234-
USER_SITE = os.path.join(USER_BASE,
235-
"Python" + sys.version[0] + sys.version[2],
236-
"site-packages")
237231
else:
238232
USER_BASE = env_base if env_base else joinuser("~", ".local")
239-
USER_SITE = os.path.join(USER_BASE, "lib",
240-
"python" + sys.version[:3],
233+
234+
return USER_BASE
235+
236+
def getusersitepackages():
237+
"""Returns the user-specific site-packages directory path.
238+
239+
If the global variable ``USER_SITE`` is not initialized yet, this
240+
function will also set it.
241+
"""
242+
global USER_SITE
243+
user_base = getuserbase() # this will also set USER_BASE
244+
245+
if USER_SITE is not None:
246+
return USER_SITE
247+
248+
if os.name == "nt":
249+
USER_SITE = os.path.join(user_base, "Python" + sys.version[0] +
250+
sys.version[2], "site-packages")
251+
else:
252+
USER_SITE = os.path.join(user_base, "lib", "python" + sys.version[:3],
241253
"site-packages")
242254

243-
if ENABLE_USER_SITE and os.path.isdir(USER_SITE):
244-
addsitedir(USER_SITE, known_paths)
255+
return USER_SITE
256+
257+
def addusersitepackages(known_paths):
258+
"""Add a per user site-package to sys.path
259+
260+
Each user has its own python directory with site-packages in the
261+
home directory.
262+
"""
263+
# get the per user site-package path
264+
# this call will also make sure USER_BASE and USER_SITE are set
265+
user_site = getusersitepackages()
266+
267+
if ENABLE_USER_SITE and os.path.isdir(user_site):
268+
addsitedir(user_site, known_paths)
245269
return known_paths
246270

271+
def getsitepackages():
272+
"""Returns a list containing all global site-packages directories
273+
(and possibly site-python).
247274
248-
def addsitepackages(known_paths):
249-
"""Add site-packages (and possibly site-python) to sys.path"""
250-
sitedirs = []
275+
For each directory present in the global ``PREFIXES``, this function
276+
will find its `site-packages` subdirectory depending on the system
277+
environment, and will return a list of full paths.
278+
"""
279+
sitepackages = []
251280
seen = []
252281

253282
for prefix in PREFIXES:
@@ -256,35 +285,36 @@ def addsitepackages(known_paths):
256285
seen.append(prefix)
257286

258287
if sys.platform in ('os2emx', 'riscos'):
259-
sitedirs.append(os.path.join(prefix, "Lib", "site-packages"))
288+
sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
260289
elif os.sep == '/':
261-
sitedirs.append(os.path.join(prefix, "lib",
290+
sitepackages.append(os.path.join(prefix, "lib",
262291
"python" + sys.version[:3],
263292
"site-packages"))
264-
sitedirs.append(os.path.join(prefix, "lib", "site-python"))
293+
sitepackages.append(os.path.join(prefix, "lib", "site-python"))
265294
else:
266-
sitedirs.append(prefix)
267-
sitedirs.append(os.path.join(prefix, "lib", "site-packages"))
268-
295+
sitepackages.append(prefix)
296+
sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
269297
if sys.platform == "darwin":
270298
# for framework builds *only* we add the standard Apple
271299
# locations.
272300
if 'Python.framework' in prefix:
273-
sitedirs.append(
301+
sitepackages.append(
274302
os.path.expanduser(
275303
os.path.join("~", "Library", "Python",
276304
sys.version[:3], "site-packages")))
277-
sitedirs.append(
305+
sitepackages.append(
278306
os.path.join("/Library", "Python",
279307
sys.version[:3], "site-packages"))
308+
return sitepackages
280309

281-
for sitedir in sitedirs:
310+
def addsitepackages(known_paths):
311+
"""Add site-packages (and possibly site-python) to sys.path"""
312+
for sitedir in getsitepackages():
282313
if os.path.isdir(sitedir):
283314
addsitedir(sitedir, known_paths)
284315

285316
return known_paths
286317

287-
288318
def setBEGINLIBPATH():
289319
"""The OS/2 EMX port has optional extension modules that do double duty
290320
as DLLs (and must use the .DLL file extension) for other extensions.

Lib/test/test_site.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ class HelperFunctionsTests(unittest.TestCase):
3535
def setUp(self):
3636
"""Save a copy of sys.path"""
3737
self.sys_path = sys.path[:]
38+
self.old_base = site.USER_BASE
39+
self.old_site = site.USER_SITE
40+
self.old_prefixes = site.PREFIXES
3841

3942
def tearDown(self):
4043
"""Restore sys.path"""
4144
sys.path = self.sys_path
45+
site.USER_BASE = self.old_base
46+
site.USER_SITE = self.old_site
47+
site.PREFIXES = self.old_prefixes
4248

4349
def test_makepath(self):
4450
# Test makepath() have an absolute path for its first return value
@@ -122,6 +128,60 @@ def test_s_option(self):
122128
env=env)
123129
self.assertEqual(rc, 1)
124130

131+
def test_getuserbase(self):
132+
site.USER_BASE = None
133+
user_base = site.getuserbase()
134+
135+
# the call sets site.USER_BASE
136+
self.assertEquals(site.USER_BASE, user_base)
137+
138+
# let's set PYTHONUSERBASE and see if it uses it
139+
site.USER_BASE = None
140+
with EnvironmentVarGuard() as environ:
141+
environ['PYTHONUSERBASE'] = 'xoxo'
142+
self.assertTrue(site.getuserbase().startswith('xoxo'))
143+
144+
def test_getusersitepackages(self):
145+
site.USER_SITE = None
146+
site.USER_BASE = None
147+
user_site = site.getusersitepackages()
148+
149+
# the call sets USER_BASE *and* USER_SITE
150+
self.assertEquals(site.USER_SITE, user_site)
151+
self.assertTrue(user_site.startswith(site.USER_BASE))
152+
153+
def test_getsitepackages(self):
154+
site.PREFIXES = ['xoxo']
155+
dirs = site.getsitepackages()
156+
157+
if sys.platform in ('os2emx', 'riscos'):
158+
self.assertTrue(len(dirs), 1)
159+
wanted = os.path.join('xoxo', 'Lib', 'site-packages')
160+
self.assertEquals(dirs[0], wanted)
161+
elif os.sep == '/':
162+
self.assertTrue(len(dirs), 2)
163+
wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
164+
'site-packages')
165+
self.assertEquals(dirs[0], wanted)
166+
wanted = os.path.join('xoxo', 'lib', 'site-python')
167+
self.assertEquals(dirs[1], wanted)
168+
else:
169+
self.assertTrue(len(dirs), 2)
170+
self.assertEquals(dirs[0], 'xoxo')
171+
wanted = os.path.join('xoxo', 'Lib', 'site-packages')
172+
self.assertEquals(dirs[1], wanted)
173+
174+
# let's try the specific Apple location
175+
if sys.platform == "darwin":
176+
site.PREFIXES = ['Python.framework']
177+
dirs = site.getsitepackages()
178+
self.assertTrue(len(dirs), 4)
179+
wanted = os.path.join('~', 'Library', 'Python',
180+
sys.version[:3], 'site-packages')
181+
self.assertEquals(dirs[2], os.path.expanduser(wanted))
182+
wanted = os.path.join('/Library', 'Python', sys.version[:3],
183+
'site-packages')
184+
self.assertEquals(dirs[3], wanted)
125185

126186
class PthFile(object):
127187
"""Helper class for handling testing of .pth files"""

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,8 @@ Core and Builtins
992992
Library
993993
-------
994994

995+
- Issue #6693: New functions in site.py to get user/global site packages paths.
996+
995997
- Issue #6511: ZipFile now raises BadZipfile (instead of an IOError) when
996998
opening an empty or very small file.
997999

0 commit comments

Comments
 (0)