blob: b95d5b528eceb68764afba6f43d88498b6af549b [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001:mod:`packaging.util` --- Miscellaneous utility functions
2=========================================================
3
4.. module:: packaging.util
5 :synopsis: Miscellaneous utility functions.
6
7
8This module contains various helpers for the other modules.
9
10.. XXX a number of functions are missing, but the module may be split first
11 (it's ginormous right now, some things could go to compat for example)
12
13.. function:: get_platform()
14
15 Return a string that identifies the current platform. This is used mainly to
16 distinguish platform-specific build directories and platform-specific built
17 distributions. Typically includes the OS name and version and the
18 architecture (as supplied by 'os.uname()'), although the exact information
19 included depends on the OS; e.g. for IRIX the architecture isn't particularly
20 important (IRIX only runs on SGI hardware), but for Linux the kernel version
21 isn't particularly important.
22
23 Examples of returned values:
24
25 * ``linux-i586``
26 * ``linux-alpha``
27 * ``solaris-2.6-sun4u``
28 * ``irix-5.3``
29 * ``irix64-6.2``
30
31 For non-POSIX platforms, currently just returns ``sys.platform``.
32
33 For Mac OS X systems the OS version reflects the minimal version on which
34 binaries will run (that is, the value of ``MACOSX_DEPLOYMENT_TARGET``
35 during the build of Python), not the OS version of the current system.
36
37 For universal binary builds on Mac OS X the architecture value reflects
38 the univeral binary status instead of the architecture of the current
39 processor. For 32-bit universal binaries the architecture is ``fat``,
40 for 64-bit universal binaries the architecture is ``fat64``, and
41 for 4-way universal binaries the architecture is ``universal``. Starting
42 from Python 2.7 and Python 3.2 the architecture ``fat3`` is used for
43 a 3-way universal build (ppc, i386, x86_64) and ``intel`` is used for
44 a univeral build with the i386 and x86_64 architectures
45
46 Examples of returned values on Mac OS X:
47
48 * ``macosx-10.3-ppc``
49
50 * ``macosx-10.3-fat``
51
52 * ``macosx-10.5-universal``
53
54 * ``macosx-10.6-intel``
55
56 .. XXX reinvention of platform module?
57
58
59.. function:: convert_path(pathname)
60
61 Return 'pathname' as a name that will work on the native filesystem, i.e.
62 split it on '/' and put it back together again using the current directory
63 separator. Needed because filenames in the setup script are always supplied
64 in Unix style, and have to be converted to the local convention before we
65 can actually use them in the filesystem. Raises :exc:`ValueError` on
66 non-Unix-ish systems if *pathname* either starts or ends with a slash.
67
68
69.. function:: change_root(new_root, pathname)
70
71 Return *pathname* with *new_root* prepended. If *pathname* is relative, this
72 is equivalent to ``os.path.join(new_root,pathname)`` Otherwise, it requires
73 making *pathname* relative and then joining the two, which is tricky on
74 DOS/Windows.
75
76
77.. function:: check_environ()
78
79 Ensure that 'os.environ' has all the environment variables we guarantee that
80 users can use in config files, command-line options, etc. Currently this
81 includes:
82
83 * :envvar:`HOME` - user's home directory (Unix only)
84 * :envvar:`PLAT` - description of the current platform, including hardware
85 and OS (see :func:`get_platform`)
86
87
88.. function:: find_executable(executable, path=None)
89
90 Search the path for a given executable name.
91
92
93.. function:: subst_vars(s, local_vars)
94
95 Perform shell/Perl-style variable substitution on *s*. Every occurrence of
96 ``$`` followed by a name is considered a variable, and variable is
97 substituted by the value found in the *local_vars* dictionary, or in
98 ``os.environ`` if it's not in *local_vars*. *os.environ* is first
99 checked/augmented to guarantee that it contains certain values: see
100 :func:`check_environ`. Raise :exc:`ValueError` for any variables not found
101 in either *local_vars* or ``os.environ``.
102
103 Note that this is not a fully-fledged string interpolation function. A valid
104 ``$variable`` can consist only of upper and lower case letters, numbers and
105 an underscore. No { } or ( ) style quoting is available.
106
107
108.. function:: split_quoted(s)
109
110 Split a string up according to Unix shell-like rules for quotes and
111 backslashes. In short: words are delimited by spaces, as long as those spaces
112 are not escaped by a backslash, or inside a quoted string. Single and double
113 quotes are equivalent, and the quote characters can be backslash-escaped.
114 The backslash is stripped from any two-character escape sequence, leaving
115 only the escaped character. The quote characters are stripped from any
116 quoted string. Returns a list of words.
117
118 .. TODO Should probably be moved into the standard library.
119
120
121.. function:: execute(func, args[, msg=None, verbose=0, dry_run=0])
122
123 Perform some action that affects the outside world (for instance, writing to
124 the filesystem). Such actions are special because they are disabled by the
125 *dry_run* flag. This method takes care of all that bureaucracy for you;
126 all you have to do is supply the function to call and an argument tuple for
127 it (to embody the "external action" being performed), and an optional message
128 to print.
129
130
131.. function:: newer(source, target)
132
133 Return true if *source* exists and is more recently modified than *target*,
134 or if *source* exists and *target* doesn't. Return false if both exist and
135 *target* is the same age or newer than *source*. Raise
136 :exc:`PackagingFileError` if *source* does not exist.
137
138
139.. function:: strtobool(val)
140
141 Convert a string representation of truth to true (1) or false (0).
142
143 True values are ``y``, ``yes``, ``t``, ``true``, ``on`` and ``1``; false
144 values are ``n``, ``no``, ``f``, ``false``, ``off`` and ``0``. Raises
145 :exc:`ValueError` if *val* is anything else.
146
147.. TODO Add :term: markup to bytecode when merging into the stdlib
148
149.. function:: byte_compile(py_files[, optimize=0, force=0, prefix=None, base_dir=None, verbose=1, dry_run=0, direct=None])
150
151 Byte-compile a collection of Python source files to either :file:`.pyc` or
152 :file:`.pyo` files in the same directory. *py_files* is a list of files to
153 compile; any files that don't end in :file:`.py` are silently skipped.
154 *optimize* must be one of the following:
155
156 * ``0`` - don't optimize (generate :file:`.pyc`)
157 * ``1`` - normal optimization (like ``python -O``)
158 * ``2`` - extra optimization (like ``python -OO``)
159
160 If *force* is true, all files are recompiled regardless of timestamps.
161
162 The source filename encoded in each bytecode file defaults to the filenames
163 listed in *py_files*; you can modify these with *prefix* and *basedir*.
164 *prefix* is a string that will be stripped off of each source filename, and
165 *base_dir* is a directory name that will be prepended (after *prefix* is
166 stripped). You can supply either or both (or neither) of *prefix* and
167 *base_dir*, as you wish.
168
169 If *dry_run* is true, doesn't actually do anything that would affect the
170 filesystem.
171
172 Byte-compilation is either done directly in this interpreter process with the
173 standard :mod:`py_compile` module, or indirectly by writing a temporary
174 script and executing it. Normally, you should let :func:`byte_compile`
175 figure out to use direct compilation or not (see the source for details).
176 The *direct* flag is used by the script generated in indirect mode; unless
177 you know what you're doing, leave it set to ``None``.
178
179
180.. function:: rfc822_escape(header)
181
182 Return a version of *header* escaped for inclusion in an :rfc:`822` header, by
183 ensuring there are 8 spaces space after each newline. Note that it does no
184 other modification of the string.
185
186 .. TODO this _can_ be replaced