Package logilab-common-0 :: Package 39 :: Package 0 :: Module twisted_distutils
[frames] | no frames]

Source Code for Module logilab-common-0.39.0.twisted_distutils

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