| 1 | """ Copyright (c) 2002-2003 LOGILAB S.A. (Paris, FRANCE). |
|---|
| 2 | http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|---|
| 3 | |
|---|
| 4 | Distutils extensions for twisted framework. |
|---|
| 5 | |
|---|
| 6 | This module enables the installation of plugins.tml files using standard |
|---|
| 7 | distutils syntax. It adds the following commands to the standard |
|---|
| 8 | setup.py commands: |
|---|
| 9 | * build_twisted_plugins: build (i.e. copy) plugins |
|---|
| 10 | * install_twisted_plugins: install plugins |
|---|
| 11 | |
|---|
| 12 | Additionally, the following commands have been modified to deal with |
|---|
| 13 | plugins files: |
|---|
| 14 | * sdist |
|---|
| 15 | * build |
|---|
| 16 | * install |
|---|
| 17 | |
|---|
| 18 | To use these extenstion, you should import the setup fonction from this |
|---|
| 19 | module, and use it normally. To list the plugins.tml files, use the |
|---|
| 20 | twisted_plugins keyword argument to the setup function: |
|---|
| 21 | |
|---|
| 22 | from twisted_distutils import setup # you can also import Extension if needed |
|---|
| 23 | |
|---|
| 24 | if __name__ == '__main__': |
|---|
| 25 | setup(name='my_twisted_app', |
|---|
| 26 | version='1.0', |
|---|
| 27 | author='me', |
|---|
| 28 | packages=['my_package'], |
|---|
| 29 | twisted_plugins = ['my_package/plugins.tml']) |
|---|
| 30 | |
|---|
| 31 | Note that you can use this to install files that are not twisted plugins in any |
|---|
| 32 | package directory of your application. |
|---|
| 33 | """ |
|---|
| 34 | # |
|---|
| 35 | # (c) 2002 Alexandre Fayolle <alexandre.fayolle@free.fr> |
|---|
| 36 | # This module is heavily based on code copied from the python distutils |
|---|
| 37 | # framework, especially distutils.command.build_script, |
|---|
| 38 | # distutils.command.install_script. Many thanks to the authors of these |
|---|
| 39 | # modules. |
|---|
| 40 | # This module is provided as is, I'm not responsible if anything bad |
|---|
| 41 | # happens to you or your python library while using this module. You may |
|---|
| 42 | # freely copy it, distribute it and use it in your library or to distribute |
|---|
| 43 | # your applications. I'd appreciate if you could drop me an email if you plan |
|---|
| 44 | # to do so <wink>. |
|---|
| 45 | # |
|---|
| 46 | # Happy twisting! |
|---|
| 47 | # |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | from warnings import warn |
|---|
| 51 | warn('this module is deprecated and will disappear in a near release', |
|---|
| 52 | DeprecationWarning, stacklevel=1) |
|---|
| 53 | |
|---|
| 54 | __revision__ = "$Id: twisted_distutils.py,v 1.4 2003-09-12 11:54:48 syt Exp $" |
|---|
| 55 | |
|---|
| 56 | from distutils.core import Distribution, Command |
|---|
| 57 | from distutils.command.install import install |
|---|
| 58 | from distutils.command.build import build |
|---|
| 59 | from distutils.command.sdist import sdist |
|---|
| 60 | from distutils.dep_util import newer |
|---|
| 61 | from distutils.util import convert_path |
|---|
| 62 | import os |
|---|
| 63 | |
|---|
| 64 | class twisted_sdist(sdist): |
|---|
| 65 | def add_defaults(self): |
|---|
| 66 | sdist.add_defaults(self) |
|---|
| 67 | if self.distribution.has_twisted_plugins(): |
|---|
| 68 | plugins = self.get_finalized_command('build_twisted_plugins') |
|---|
| 69 | self.filelist.extend(plugins.get_source_files()) |
|---|
| 70 | |
|---|
| 71 | class twisted_install(install): |
|---|
| 72 | def initialize_options (self): |
|---|
| 73 | install.initialize_options(self) |
|---|
| 74 | self.twisted_plugins = None |
|---|
| 75 | |
|---|
| 76 | def has_twisted_plugins(self): |
|---|
| 77 | return self.distribution.has_twisted_plugins() |
|---|
| 78 | |
|---|
| 79 | sub_commands = [] |
|---|
| 80 | sub_commands.extend(install.sub_commands) |
|---|
| 81 | sub_commands.append(('install_twisted_plugins', has_twisted_plugins)) |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | class twisted_build(build): |
|---|
| 85 | def initialize_options (self): |
|---|
| 86 | build.initialize_options(self) |
|---|
| 87 | self.twisted_plugins = None |
|---|
| 88 | |
|---|
| 89 | def has_twisted_plugins(self): |
|---|
| 90 | return self.distribution.has_twisted_plugins() |
|---|
| 91 | |
|---|
| 92 | sub_commands = [] |
|---|
| 93 | sub_commands.extend(build.sub_commands) |
|---|
| 94 | sub_commands.append(('build_twisted_plugins', has_twisted_plugins)) |
|---|
| 95 | |
|---|
| 96 | class build_twisted_plugins (Command): |
|---|
| 97 | |
|---|
| 98 | description = "\"build\" twisted plugins (copy)" |
|---|
| 99 | |
|---|
| 100 | user_options = [ |
|---|
| 101 | ('build-dir=', 'd', "directory to \"build\" (copy) to"), |
|---|
| 102 | ('force', 'f', "forcibly build everything (ignore file timestamps"), |
|---|
| 103 | ] |
|---|
| 104 | |
|---|
| 105 | boolean_options = ['force'] |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | def initialize_options (self): |
|---|
| 109 | self.build_dir = None |
|---|
| 110 | self.twisted_plugins = None |
|---|
| 111 | self.force = None |
|---|
| 112 | self.outfiles = None |
|---|
| 113 | |
|---|
| 114 | def get_source_files(self): |
|---|
| 115 | return self.twisted_plugins |
|---|
| 116 | |
|---|
| 117 | def finalize_options (self): |
|---|
| 118 | self.set_undefined_options('build', |
|---|
| 119 | ('build_lib', 'build_dir'), |
|---|
| 120 | ('force', 'force')) |
|---|
| 121 | self.twisted_plugins = self.distribution.twisted_plugins |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | def run (self): |
|---|
| 125 | if not self.twisted_plugins: |
|---|
| 126 | return |
|---|
| 127 | self.copy_twisted_plugins() |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | def copy_twisted_plugins (self): |
|---|
| 131 | """Copy each plugin listed in 'self.twisted_plugins'. |
|---|
| 132 | """ |
|---|
| 133 | self.mkpath(self.build_dir) |
|---|
| 134 | for plugin in self.twisted_plugins: |
|---|
| 135 | adjust = 0 |
|---|
| 136 | plugin = convert_path(plugin) |
|---|
| 137 | outfile = os.path.join(self.build_dir, plugin) |
|---|
| 138 | if not self.force and not newer(plugin, outfile): |
|---|
| 139 | self.announce("not copying %s (up-to-date)" % plugin) |
|---|
| 140 | continue |
|---|
| 141 | |
|---|
| 142 | # Always open the file, but ignore failures in dry-run mode -- |
|---|
| 143 | # that way, we'll get accurate feedback if we can read the |
|---|
| 144 | # plugin. |
|---|
| 145 | try: |
|---|
| 146 | f = open(plugin, "r") |
|---|
| 147 | except IOError: |
|---|
| 148 | if not self.dry_run: |
|---|
| 149 | raise |
|---|
| 150 | f = None |
|---|
| 151 | else: |
|---|
| 152 | f.close() |
|---|
| 153 | self.copy_file(plugin, outfile) |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | class install_twisted_plugins(Command): |
|---|
| 157 | |
|---|
| 158 | description = "install twisted plugins" |
|---|
| 159 | |
|---|
| 160 | user_options = [ |
|---|
| 161 | ('install-dir=', 'd', "directory to install scripts to"), |
|---|
| 162 | ('build-dir=','b', "build directory (where to install from)"), |
|---|
| 163 | ('force', 'f', "force installation (overwrite existing files)"), |
|---|
| 164 | ('skip-build', None, "skip the build steps"), |
|---|
| 165 | ] |
|---|
| 166 | |
|---|
| 167 | boolean_options = ['force', 'skip-build'] |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | def initialize_options (self): |
|---|
| 171 | self.install_dir = None |
|---|
| 172 | self.force = 0 |
|---|
| 173 | self.build_dir = None |
|---|
| 174 | self.skip_build = None |
|---|
| 175 | |
|---|
| 176 | def finalize_options (self): |
|---|
| 177 | self.set_undefined_options('build', ('build_lib', 'build_dir')) |
|---|
| 178 | self.set_undefined_options('install', |
|---|
| 179 | ('install_lib', 'install_dir'), |
|---|
| 180 | ('force', 'force'), |
|---|
| 181 | ('skip_build', 'skip_build'), |
|---|
| 182 | ) |
|---|
| 183 | |
|---|
| 184 | def run (self): |
|---|
| 185 | if not self.skip_build: |
|---|
| 186 | self.run_command('build_twisted_plugins') |
|---|
| 187 | self.outfiles = self.copy_tree(self.build_dir, self.install_dir) |
|---|
| 188 | |
|---|
| 189 | def get_inputs (self): |
|---|
| 190 | return self.distribution.twisted_plugins or [] |
|---|
| 191 | |
|---|
| 192 | def get_outputs(self): |
|---|
| 193 | return self.outfiles or [] |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | class TwistedDistribution(Distribution): |
|---|
| 198 | def __init__(self,attrs=None): |
|---|
| 199 | self.twisted_plugins = None |
|---|
| 200 | Distribution.__init__(self, attrs) |
|---|
| 201 | self.cmdclass = {'install':twisted_install, |
|---|
| 202 | 'install_twisted_plugins':install_twisted_plugins, |
|---|
| 203 | 'build':twisted_build, |
|---|
| 204 | 'build_twisted_plugins':build_twisted_plugins, |
|---|
| 205 | 'sdist':twisted_sdist, |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | def has_twisted_plugins(self): |
|---|
| 209 | return self.twisted_plugins and len(self.twisted_plugins) > 0 |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | def setup(**attrs): |
|---|
| 213 | from distutils import core |
|---|
| 214 | attrs['distclass'] = TwistedDistribution |
|---|
| 215 | core.setup(**attrs) |
|---|