Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This can easily be done with distutils by subclassing <strong>distutils.core.Command</strong> inside of setup.py.</p> <p>For example:</p> <pre><code>from distutils.core import setup, Command import os, sys class CleanCommand(Command): description = "custom clean command that forcefully removes dist/build directories" user_options = [] def initialize_options(self): self.cwd = None def finalize_options(self): self.cwd = os.getcwd() def run(self): assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd os.system('rm -rf ./build ./dist') </code></pre> <p>To enable the command you must reference it in setup():</p> <pre><code>setup( # stuff omitted for conciseness. cmdclass={ 'clean': CleanCommand } </code></pre> <p>Note that you can override built-in commands this way too, such as what I did with 'clean'. (I didn't like how the built-in version left behind the 'dist' and 'build' directories.)</p> <pre><code>% python setup.py --help-commands | grep clean clean custom clean command that forcefully removes dist/build dirs. </code></pre> <p>There are a number of conventions that are used:</p> <ul> <li>You specify any command-line arguments with <strong>user_options</strong>.</li> <li>You declare any variables you would use with the <strong>initialize_options()</strong> method, which is called after initialization to setup your custom namespace for the subclass. </li> <li>The <strong>finalize_options()</strong> method is called right before <strong>run()</strong>. </li> <li>The guts of the command itself will occur in <strong>run()</strong> so be sure to do any other prep work before that.</li> </ul> <p>The best example to use is just to look at the source code for one of the default commands found at <em>PYTHON_DIR/distutils/command</em> such as <em>install.py</em> or <em>build.py</em>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload