annotate Lib/pkgutil.py @ 38664:7619216bbc19 2.4

Bug #1502728: Correctly link against librt library on HP-UX. (backport from rev. 46743)
author Georg Brandl <georg@python.org>
date Thu, 08 Jun 2006 12:54:16 +0000
parents 7cb6745be525
children ccc0b5412799 f731859e9b3b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26753
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Utilities to support packages."""
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import os
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import sys
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 def extend_path(path, name):
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 """Extend a package's path.
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 Intended use is to place the following code in a package's __init__.py:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 from pkgutil import extend_path
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 __path__ = extend_path(__path__, __name__)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 This will add to the package's __path__ all subdirectories of
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 directories on sys.path named after the package. This is useful
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 if one wants to distribute different parts of a single logical
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 package as multiple directories.
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 It also looks for *.pkg files beginning where * matches the name
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 argument. This feature is similar to *.pth files (see site.py),
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 except that it doesn't special-case lines starting with 'import'.
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 A *.pkg file is trusted at face value: apart from checking for
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 duplicates, all entries found in a *.pkg file are added to the
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 path, regardless of whether they are exist the filesystem. (This
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 is a feature.)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 If the input path is not a list (as is the case for frozen
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 packages) it is returned unchanged. The input path is not
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 modified; an extended copy is returned. Items are only appended
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 to the copy at the end.
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 It is assumed that sys.path is a sequence. Items of sys.path that
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 are not (unicode or 8-bit) strings referring to existing
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 directories are ignored. Unicode items of sys.path that cause
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 errors when used as filenames may cause this function to raise an
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 exception (in line with os.path.isdir() behavior).
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 """
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 if not isinstance(path, list):
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # This could happen e.g. when this is called from inside a
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # frozen package. Return the path unchanged in that case.
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 return path
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 pname = os.path.join(*name.split('.')) # Reconstitute as relative path
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # Just in case os.extsep != '.'
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 sname = os.extsep.join(name.split('.'))
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 sname_pkg = sname + os.extsep + "pkg"
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 init_py = "__init__" + os.extsep + "py"
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 path = path[:] # Start with a copy of the existing path
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 for dir in sys.path:
30448
7cb6745be525 SF patch #806246: use basestring where possible
Raymond Hettinger <python@rcn.com>
parents: 26753
diff changeset
53 if not isinstance(dir, basestring) or not os.path.isdir(dir):
26753
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 continue
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 subdir = os.path.join(dir, pname)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # XXX This may still add duplicate entries to path on
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # case-insensitive filesystems
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 initfile = os.path.join(subdir, init_py)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 if subdir not in path and os.path.isfile(initfile):
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 path.append(subdir)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # XXX Is this the right thing for subpackages like zope.app?
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # It looks for a file named "zope.app.pkg"
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 pkgfile = os.path.join(dir, sname_pkg)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if os.path.isfile(pkgfile):
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 try:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 f = open(pkgfile)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 except IOError, msg:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 sys.stderr.write("Can't open %s: %s\n" %
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 (pkgfile, msg))
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 else:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 for line in f:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 line = line.rstrip('\n')
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 if not line or line.startswith('#'):
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 continue
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 path.append(line) # Don't check for existence!
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 f.close()
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return path