Deprecate $WIN32 variables name in place of $WINDOWS* variables names, and eliminate...
[scons.git] / doc / user / tasks.sgml
1 <!--
2
3   __COPYRIGHT__
4
5   Permission is hereby granted, free of charge, to any person obtaining
6   a copy of this software and associated documentation files (the
7   "Software"), to deal in the Software without restriction, including
8   without limitation the rights to use, copy, modify, merge, publish,
9   distribute, sublicense, and/or sell copies of the Software, and to
10   permit persons to whom the Software is furnished to do so, subject to
11   the following conditions:
12
13   The above copyright notice and this permission notice shall be included
14   in all copies or substantial portions of the Software.
15
16   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17   KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18   WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 -->
25
26 <para>
27 There is a common set of simple tasks that many build configurations rely
28 on as they become more complex. Most build tools have special
29 purpose constructs for performing these tasks, but since &SConscript;
30 files are &Python; scripts, you can use more flexible built-in &Python;
31 services to perform these tasks. This appendix lists a number of these
32 tasks and how to implement them in &Python;.
33 </para>
34
35 <example>
36 <title>Wildcard globbing to create a list of filenames</title>
37 <programlisting>
38 import glob
39 files = glob.glob(wildcard)
40 </programlisting>
41 </example>
42
43 <example>
44 <title>Filename extension substitution</title>
45 <programlisting>
46 import os.path
47 filename = os.path.splitext(filename)[0]+extension
48 </programlisting>
49 </example>
50
51 <example>
52 <title>Appending a path prefix to a list of filenames</title>
53 <programlisting>
54 import os.path
55 filenames = [os.path.join(prefix, x) for x in filenames]
56 </programlisting>
57
58 <simpara>or in Python 1.5.2:</simpara>
59
60 <programlisting>
61 import os.path
62 new_filenames = [] 
63 for x in filenames:
64     new_filenames.append(os.path.join(prefix, x))
65 </programlisting>
66 </example>
67
68 <example>
69 <title>Substituting a path prefix with another one</title>
70 <programlisting>
71 if filename.find(old_prefix) == 0:
72     filename = filename.replace(old_prefix, new_prefix)
73 </programlisting>
74
75 <simpara>or in Python 1.5.2:</simpara>
76
77 <programlisting>
78 import string
79 if string.find(filename, old_prefix) == 0:
80     filename = string.replace(filename, old_prefix, new_prefix)      
81 </programlisting>
82 </example>
83
84 <example>
85 <title>Filtering a filename list to exclude/retain only a specific set
86 of extensions</title>
87 <programlisting>
88 import os.path
89 filenames = [x for x in filenames if os.path.splitext(x)[1] in extensions]
90 </programlisting>
91
92 <simpara>or in Python 1.5.2:</simpara>
93
94 <programlisting>
95 import os.path
96 new_filenames = []
97 for x in filenames:
98     if os.path.splitext(x)[1] in extensions:
99         new_filenames.append(x)
100 </programlisting>
101 </example>
102
103 <example>
104 <title>The "backtick function": run a shell command and capture the
105 output</title>
106 <programlisting>import os
107 output = os.popen(command).read()
108 </programlisting>
109 </example>