| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="dist.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="dist.html" title='Distributing Python Modules' /> |
| 8 | <link rel='index' href='genindex.html' title='Index' /> |
| 9 | <link rel='last' href='about.html' title='About this document...' /> |
| 10 | <link rel='help' href='about.html' title='About this document...' /> |
| 11 | <link rel="prev" href="module-distutils.command.register.html" /> |
| 12 | <link rel="parent" href="api-reference.html" /> |
| 13 | <link rel="next" href="modindex.html" /> |
| 14 | <meta name='aesop' content='information' /> |
| 15 | <title>10.46 Creating a new Distutils command</title> |
| 16 | </head> |
| 17 | <body> |
| 18 | <DIV CLASS="navigation"> |
| 19 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 20 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 21 | <tr> |
| 22 | <td class='online-navigation'><a rel="prev" title="10.45 distutils.command.register " |
| 23 | href="module-distutils.command.register.html"><img src='../icons/previous.png' |
| 24 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 25 | <td class='online-navigation'><a rel="parent" title="10. API Reference" |
| 26 | href="api-reference.html"><img src='../icons/up.png' |
| 27 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 28 | <td class='online-navigation'><a rel="next" title="Module Index" |
| 29 | href="modindex.html"><img src='../icons/next.png' |
| 30 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 31 | <td align="center" width="100%">Distributing Python Modules</td> |
| 32 | <td class='online-navigation'><img src='../icons/blank.png' |
| 33 | border='0' height='32' alt='' width='32' /></td> |
| 34 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 35 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 36 | <td class='online-navigation'><a rel="index" title="Index" |
| 37 | href="genindex.html"><img src='../icons/index.png' |
| 38 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 39 | </tr></table> |
| 40 | <div class='online-navigation'> |
| 41 | <b class="navlabel">Previous:</b> |
| 42 | <a class="sectref" rel="prev" href="module-distutils.command.register.html">10.45 distutils.command.register </A> |
| 43 | <b class="navlabel">Up:</b> |
| 44 | <a class="sectref" rel="parent" href="api-reference.html">10. API Reference</A> |
| 45 | <b class="navlabel">Next:</b> |
| 46 | <a class="sectref" rel="next" href="modindex.html">Module Index</A> |
| 47 | </div> |
| 48 | <hr /></div> |
| 49 | </DIV> |
| 50 | <!--End of Navigation Panel--> |
| 51 | |
| 52 | <H1><A NAME="SECTION00104600000000000000000"> |
| 53 | 10.46 Creating a new Distutils command</A> |
| 54 | </H1> |
| 55 | |
| 56 | <P> |
| 57 | This section outlines the steps to create a new Distutils command. |
| 58 | |
| 59 | <P> |
| 60 | A new command lives in a module in the <tt class="module">distutils.command</tt> |
| 61 | package. There is a sample template in that directory called |
| 62 | <span class="file">command_template</span>. Copy this file to a new module with the |
| 63 | same name as the new command you're implementing. This module should |
| 64 | implement a class with the same name as the module (and the command). |
| 65 | So, for instance, to create the command <code>peel_banana</code> (so that users |
| 66 | can run "<tt class="samp">setup.py peel_banana</tt>"), you'd copy <span class="file">command_template</span> |
| 67 | to <span class="file">distutils/command/peel_banana.py</span>, then edit it so that it's |
| 68 | implementing the class <tt class="class">peel_banana</tt>, a subclass of |
| 69 | <tt class="class">distutils.cmd.Command</tt>. |
| 70 | |
| 71 | <P> |
| 72 | Subclasses of <tt class="class">Command</tt> must define the following methods. |
| 73 | |
| 74 | <P> |
| 75 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 76 | <td><nobr><b><tt id='l2h-148' xml:id='l2h-148' class="method">initialize_options()</tt></b>(</nobr></td> |
| 77 | <td><var>S</var>)</td></tr></table></dt> |
| 78 | <dd>et default values for all the options that this command |
| 79 | supports. Note that these defaults may be overridden by other |
| 80 | commands, by the setup script, by config files, or by the |
| 81 | command-line. Thus, this is not the place to code dependencies |
| 82 | between options; generally, <tt class="method">initialize_options()</tt> implementations |
| 83 | are just a bunch of "<tt class="samp">self.foo = None</tt>" assignments. |
| 84 | </dl> |
| 85 | |
| 86 | <P> |
| 87 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 88 | <td><nobr><b><tt id='l2h-149' xml:id='l2h-149' class="method">finalize_options</tt></b>(</nobr></td> |
| 89 | <td><var></var>)</td></tr></table></dt> |
| 90 | <dd> |
| 91 | Set final values for all the options that this command supports. |
| 92 | This is always called as late as possible, ie. after any option |
| 93 | assignments from the command-line or from other commands have been |
| 94 | done. Thus, this is the place to to code option dependencies: if |
| 95 | <var>foo</var> depends on <var>bar</var>, then it is safe to set <var>foo</var> from |
| 96 | <var>bar</var> as long as <var>foo</var> still has the same value it was assigned in |
| 97 | <tt class="method">initialize_options()</tt>. |
| 98 | </dl> |
| 99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 100 | <td><nobr><b><tt id='l2h-150' xml:id='l2h-150' class="method">run</tt></b>(</nobr></td> |
| 101 | <td><var></var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | A command's raison d'etre: carry out the action it exists to |
| 104 | perform, controlled by the options initialized in |
| 105 | <tt class="method">initialize_options()</tt>, customized by other commands, the setup |
| 106 | script, the command-line, and config files, and finalized in |
| 107 | <tt class="method">finalize_options()</tt>. All terminal output and filesystem |
| 108 | interaction should be done by <tt class="method">run()</tt>. |
| 109 | </dl> |
| 110 | |
| 111 | <P> |
| 112 | <var>sub_commands</var> formalizes the notion of a ``family'' of commands, |
| 113 | eg. <code>install</code> as the parent with sub-commands <code>install_lib</code>, |
| 114 | <code>install_headers</code>, etc. The parent of a family of commands |
| 115 | defines <var>sub_commands</var> as a class attribute; it's a list of |
| 116 | 2-tuples "<tt class="samp">(command_name, predicate)</tt>", with <var>command_name</var> a string |
| 117 | and <var>predicate</var> an unbound method, a string or None. |
| 118 | <var>predicate</var> is a method of the parent command that |
| 119 | determines whether the corresponding command is applicable in the |
| 120 | current situation. (Eg. we <code>install_headers</code> is only applicable if |
| 121 | we have any C header files to install.) If <var>predicate</var> is None, |
| 122 | that command is always applicable. |
| 123 | |
| 124 | <P> |
| 125 | <var>sub_commands</var> is usually defined at the *end* of a class, because |
| 126 | predicates can be unbound methods, so they must already have been |
| 127 | defined. The canonical example is the <code class="du-command">install</code> command. |
| 128 | |
| 129 | <P> |
| 130 | |
| 131 | <DIV CLASS="navigation"> |
| 132 | <div class='online-navigation'> |
| 133 | <p></p><hr /> |
| 134 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 135 | <tr> |
| 136 | <td class='online-navigation'><a rel="prev" title="10.45 distutils.command.register " |
| 137 | href="module-distutils.command.register.html"><img src='../icons/previous.png' |
| 138 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 139 | <td class='online-navigation'><a rel="parent" title="10. API Reference" |
| 140 | href="api-reference.html"><img src='../icons/up.png' |
| 141 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 142 | <td class='online-navigation'><a rel="next" title="Module Index" |
| 143 | href="modindex.html"><img src='../icons/next.png' |
| 144 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 145 | <td align="center" width="100%">Distributing Python Modules</td> |
| 146 | <td class='online-navigation'><img src='../icons/blank.png' |
| 147 | border='0' height='32' alt='' width='32' /></td> |
| 148 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 149 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 150 | <td class='online-navigation'><a rel="index" title="Index" |
| 151 | href="genindex.html"><img src='../icons/index.png' |
| 152 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 153 | </tr></table> |
| 154 | <div class='online-navigation'> |
| 155 | <b class="navlabel">Previous:</b> |
| 156 | <a class="sectref" rel="prev" href="module-distutils.command.register.html">10.45 distutils.command.register </A> |
| 157 | <b class="navlabel">Up:</b> |
| 158 | <a class="sectref" rel="parent" href="api-reference.html">10. API Reference</A> |
| 159 | <b class="navlabel">Next:</b> |
| 160 | <a class="sectref" rel="next" href="modindex.html">Module Index</A> |
| 161 | </div> |
| 162 | </div> |
| 163 | <hr /> |
| 164 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 165 | </DIV> |
| 166 | <!--End of Navigation Panel--> |
| 167 | <ADDRESS> |
| 168 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 169 | </ADDRESS> |
| 170 | </BODY> |
| 171 | </HTML> |