Tuesday, January 14, 2014

Compiling Adafruit Library for Python3

Wanted to compile the Adafruit BeagleBone Black GPIO library for python 3.  To accomplish this I did the following:

git clone git://github.com/adafruit/adafruit-beaglebone-io-python.git
cd adafruit-beaglebone-io-python
Now we need to change the setup.py script to be python3 compatible (I got errors with the SPI portion so I commented it out - don't need it for my purposes):

# Some Angstrom images are missing the py_compile module; get it if not
# present:
# Fix credit:https://github.com/alexanderhiam/PyBBIO/blob/master/setup.py
import random, os
python_lib_path = random.__file__.split('random')[0]
print(python_lib_path)
if not os.path.exists(python_lib_path + 'py_compile.py'):
  print("py_compile module missing; installing to %spy_compile.py", python_lib_path)
  import urllib2
  url = "http://hg.python.org/cpython/raw-file/4ebe1ede981e/Lib/py_compile.py"
  py_compile = urllib2.urlopen(url)
  with open(python_lib_path+'py_compile.py', 'w') as f:
    f.write(py_compile.read())
  print("testing py_compile...")
  try:
    import py_compile
    print("py_compile installed successfully")
  except Exceptions as e:
    print("*py_compile install failed, could not import")
    print("*Exception raised:")
    raise e
try:
    from overlays import builder
    builder.compile()
    builder.copy()
except:
    pass
import distribute_setup
distribute_setup.use_setuptools()
from setuptools import setup, Extension, find_packages
classifiers = ['Development Status :: 3 - Alpha',
               'Operating System :: POSIX :: Linux',
               'License :: OSI Approved :: MIT License',
               'Intended Audience :: Developers',
               'Programming Language :: Python :: 2.6',
               'Programming Language :: Python :: 2.7',
               'Programming Language :: Python :: 3',
               'Topic :: Software Development',
               'Topic :: Home Automation',
               'Topic :: System :: Hardware']
setup(name             = 'Adafruit_BBIO',
      version          = '0.0.19',
      author           = 'Justin Cooper',
      author_email     = 'justin@adafruit.com',
      description      = 'A module to control BeagleBone IO channels',
      long_description = open('README.rst').read() + open('CHANGELOG.rst').read(),
      license          = 'MIT',
      keywords         = 'Adafruit BeagleBone IO GPIO PWM ADC',
      url              = 'https://github.com/adafruit/adafruit-beaglebone-io-python/',
      classifiers      = classifiers,
      packages         = find_packages(),
      py_modules       = ['Adafruit_I2C'],
      ext_modules      = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c']),
                          Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c']),
                          Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c']),
#                          Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c']),
                          Extension('Adafruit_BBIO.UART', ['source/py_uart.c', 'source/c_uart.c', 'source/constants.c', 'source/common.c'])])



I saved my changes as setup3.py.  Make sure you have python3 and python3-dev installed (apt-get install).

When I ran python3 setup3.py install the first time I got an error:

source/common.c: In function ‘load_device_tree’:
source/common.c:360:5: error: format not a string literal and no format arguments [-Werror=format-security]

So I changed line 360 to the following:

fprintf(file,"%s", name);

and then it compiled (I originally tried it with SPI included and got an error thus why I commented it out above).





No comments:

Post a Comment