from setuptools import setup, Extension
import os
import pwd

classifiers = ['Development Status :: 1 - Alpha',
               'Operating System :: POSIX :: Linux',
               'License :: OSI Approved :: MIT License',
               'Intended Audience :: Developers',
               'Programming Language :: Python :: 3',
               'Topic :: Software Development',
               'Topic :: Home Automation',
               'Topic :: System :: Hardware']

try:
      # Try to install under the cayenne user, if it exists.
      user = pwd.getpwnam('cayenne')
      user_id = user.pw_uid
      group_id = user.pw_gid
except KeyError:
      # Otherwise install under the user that ran sudo.
      user_id = int(os.environ['SUDO_UID'])
      group_id = int(os.environ['SUDO_GID'])
directories = ('/etc/myDevices', '/var/log/myDevices', '/var/run/myDevices')
for directory in directories:
      try:
            os.makedirs(directory)
      except FileExistsError:
            pass
      os.chown(directory, user_id, group_id)

setup(name             = 'myDevices',
      version          = '0.2.1',
      author           = 'myDevices',
      author_email     = 'N/A',
      description      = 'myDevices Cayenne agent',
      long_description = 'N/A',
      license          = 'N/A',
      keywords         = 'myDevices Cayenne IoT',
      url              = 'https://www.mydevices.com/',
      classifiers      = classifiers,
      packages         = ["myDevices", "myDevices.cloud", "myDevices.utils", "myDevices.system", "myDevices.sensors" , "myDevices.wifi", "myDevices.schedule", "myDevices.requests_futures", "myDevices.devices", "myDevices.devices.analog", "myDevices.devices.digital", "myDevices.devices.sensor", "myDevices.devices.shield", "myDevices.decorators"],
      install_requires = ['enum34', 'iwlib', 'jsonpickle', 'netifaces', 'psutil >= 0.7.0', 'requests'],
      data_files       = [('/etc/myDevices/scripts', ['scripts/config.sh'])]
      )

os.chmod('/etc/myDevices/scripts/config.sh', 0o0755)

@@ -53,16 +53,27 @@
 port pwd
+%0Aimport grp
 %0A%0Aclassi
@@ -477,18 +477,16 @@
 ry:%0A    
-  
 # Try to
@@ -530,24 +530,47 @@
 t exists.%0A  
+  username = 'cayenne'%0A
     user = p
@@ -585,21 +585,18 @@
 nam(
-'cayenne')%0A  
+username)%0A
     
@@ -617,18 +617,16 @@
 .pw_uid%0A
-  
     grou
@@ -665,18 +665,16 @@
 or:%0A    
-  
 # Otherw
@@ -715,18 +715,16 @@
 n sudo.%0A
-  
     user
@@ -761,18 +761,16 @@
 '%5D)%0A    
-  
 group_id
@@ -800,16 +800,61 @@
 _GID'%5D)%0A
+    username = pwd.getpwuid(user_id).pw_name%0A
 director
@@ -960,19 +960,13 @@
     
-  
 try:%0A
-    
     
@@ -996,18 +996,16 @@
 ry)%0A    
- 
- 
 except F
@@ -1032,19 +1032,13 @@
     
-    
 pass%0A
-  
     
@@ -2069,8 +2069,282 @@
 0o0755)%0A
+%0A# Add user to the i2c group if it isn't already a member%0Agroups = %5Bg.gr_name for g in grp.getgrall() if username in g.gr_mem%5D%0Aif not 'i2c' in groups:%0A    os.system('usermod -a -G i2c %7B%7D'.format(username))%0A    print('%5CnYou may need to re-login in order to use I2C devices')%0A

Add user to the i2c group during install.