Adding Output File to Python Extension

I created a custom build_extone to create a funky extension that I am trying to make pip friendly. Below is a summary version of what I am doing.

foo_ext = Extension(
  name='foo/_foo',
  sources=foo_sources,
)

class MyBuildExt(build_ext):
  def build_extension(self, ext):
    # This standalone script builds the __init__.py file 
    #  and some .h files for the extension
    check_call(['python', 'build_init_file.py'])

    # Now that we've created all the init and .h code
    #  build the C/C++ extension using setuptools/distutils
    build_ext.build_extension(self, ext)

    # Include the generated __init__.py in the build directory 
    #  which is something like `build/lib.linux-x86/foo/`.  
    #  How can I get setuptools/distutils to install the 
    #  generated file automatically?!
    generated_file = 'Source/foo/__init__.py'
    output_path = '/'.join(self.get_outputs()[0].split('/')[:-1])
    self.move_file(generated_file, output_path)

setup(
    ...,
    ext_modules = [foo_ext],
    cmdclass={'build_ext' : MyBuildExt},
)

After packing this module and installing it using pip, I have a module fooin my virtualenv site-packages site directory. The directory structure is as follows.

foo/
foo/__init__.py
foo/_foo.so

The file egg-info/SOURCES.txt does not include the file __init__.pythat I manually created / moved. When I do pip uninstall foo, the command leaves foo/__init__.pyin my virtual package sites. I would like to remove the entire package. How to add the generated file __init__.py, which I manually moved to the assembly directory, in the list of installed output files?

, , !

:

  • packages=['foo'] - , pip . / - .
+3
2

, distutils Python, packages=['foo'], -, ( foo setup.py script), , , , package_dir={'foo': 'Source'} . setup.py script packages, build build_py ( ) Python , .

, foo/__init__.py build_ext, build_py. :

class MyBuild(build):
  sub_commands = [('build_clib', build.has_c_libraries),
                  ('build_ext', build.has_ext_modules),
                  ('build_py', build.has_pure_modules),
                  ('build_scripts', build.has_scripts),
                 ]

setup(..., cmdclass={'build': MyBuild, 'build_ext': MyBuildExt})

sub_commands ( , , , ); , , . build_py build_clib. Python 2.7, , 2to3.

+4

Extension (foo._foo), .

= ['foo'] ?

+1

All Articles