blob: 019f3e945c49ffbff5322dd85f3e49736f6e42a2 [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
Éric Araujo3a9f58f2011-06-01 20:42:49 +020093.. function:: execute(func, args[, msg=None, verbose=0, dry_run=0])
94
95 Perform some action that affects the outside world (for instance, writing to
96 the filesystem). Such actions are special because they are disabled by the
97 *dry_run* flag. This method takes care of all that bureaucracy for you;
98 all you have to do is supply the function to call and an argument tuple for
99 it (to embody the "external action" being performed), and an optional message
100 to print.
101
102
103.. function:: newer(source, target)
104
105 Return true if *source* exists and is more recently modified than *target*,
106 or if *source* exists and *target* doesn't. Return false if both exist and
107 *target* is the same age or newer than *source*. Raise
108 :exc:`PackagingFileError` if *source* does not exist.
109
110
111.. function:: strtobool(val)
112
113 Convert a string representation of truth to true (1) or false (0).
114
115 True values are ``y``, ``yes``, ``t``, ``true``, ``on`` and ``1``; false
116 values are ``n``, ``no``, ``f``, ``false``, ``off`` and ``0``. Raises
117 :exc:`ValueError` if *val* is anything else.
118
119.. TODO Add :term: markup to bytecode when merging into the stdlib
120
121.. function:: byte_compile(py_files[, optimize=0, force=0, prefix=None, base_dir=None, verbose=1, dry_run=0, direct=None])
122
123 Byte-compile a collection of Python source files to either :file:`.pyc` or
124 :file:`.pyo` files in the same directory. *py_files* is a list of files to
125 compile; any files that don't end in :file:`.py` are silently skipped.
126 *optimize* must be one of the following:
127
128 * ``0`` - don't optimize (generate :file:`.pyc`)
129 * ``1`` - normal optimization (like ``python -O``)
130 * ``2`` - extra optimization (like ``python -OO``)
131
132 If *force* is true, all files are recompiled regardless of timestamps.
133
134 The source filename encoded in each bytecode file defaults to the filenames
135 listed in *py_files*; you can modify these with *prefix* and *basedir*.
136 *prefix* is a string that will be stripped off of each source filename, and
137 *base_dir* is a directory name that will be prepended (after *prefix* is
138 stripped). You can supply either or both (or neither) of *prefix* and
139 *base_dir*, as you wish.
140
141 If *dry_run* is true, doesn't actually do anything that would affect the
142 filesystem.
143
144 Byte-compilation is either done directly in this interpreter process with the
145 standard :mod:`py_compile` module, or indirectly by writing a temporary
146 script and executing it. Normally, you should let :func:`byte_compile`
147 figure out to use direct compilation or not (see the source for details).
148 The *direct* flag is used by the script generated in indirect mode; unless
149 you know what you're doing, leave it set to ``None``.