text: """
  This is a setup.py script generated by py2applet
  
  Usage (OS X):
      python setup.py py2app
  
  Usage (Windows):
      python setup.py py2exe
      
  
  Edits by Pauli
  """
  
  import sys
  import os
  import os.path
  import re
  import subprocess
  import pdb
  import glob
  import time
  
  import pdb
  
  from siren.common_details import common_variables
  
  # Common info
  APP = common_variables["MAIN_FILENAME"]
  NAME = common_variables["PACKAGE_NAME"]
  SHORT_NAME = common_variables["PROJECT_NAME"]
  VERSION = common_variables["VERSION"]
  DESCRIPTION = common_variables["PROJECT_DESCRIPTION"]
  DESCRIPTION_LONG = common_variables["PROJECT_DESCRIPTION_LONG"]
  AUTHOR = common_variables["PROJECT_AUTHORS"]
  AUTHOR_EMAIL = common_variables["MAINTAINER_EMAIL"]
  URL = common_variables["PROJECT_URL"]
  LICENSE="Apache_2.0"
  COPYRIGHT=u'\u00A9 '+common_variables["COPYRIGHT_YEAR_FROM"]+'-' \
                 +common_variables["COPYRIGHT_YEAR_FROM"]+' ' \
                 +common_variables["PROJECT_AUTHORS"]
  HELP_PACKAGE_FILENAME = 'help.tar.gz'
  HELP_PACKAGE_URL = URL+HELP_PACKAGE_FILENAME
  HELP_UNPACK_CMD = 'tar xfz '+HELP_PACKAGE_FILENAME # command to unpack the help file to folder help/
  
  
  extra_options = dict(
      name=NAME,
      version=VERSION,
      description=(DESCRIPTION),
      author=AUTHOR,
      author_email=AUTHOR_EMAIL,
      url=URL,
      license=LICENSE,
      )
  
  ##### helpers
  def get_svnrevision():
      svn_revision = '-1'
      try:
          p = subprocess.check_output(['svn', 'info'])
      except (subprocess.CalledProcessError, OSError):
          print "No SVN found, using default svn revision (-1) instead"
      else:
          for line in p.splitlines():
              l = line.split()
              if l[0] == 'Revision:':
                  svn_revision = l[1]
                  break
      return svn_revision
  
  def get_git_hash():
      git_hash = '-1'
      try:
          git_hash = subprocess.check_output('git rev-parse HEAD', shell=True)
      except (subprocess.CalledProcessError, OSError):
          print "No GIT found, using default git revision (-1) instead"
      return git_hash.strip()
  
  def load_help_files():
      try:
          subprocess.check_call('curl "'+HELP_PACKAGE_URL+'" -o"'+HELP_PACKAGE_FILENAME+'"', shell=True)
      except CalledProcessError as e:
          sys.stderr.write('Downloading the help package failed, aborting:\n'+str(e))
          sys.exit(e.returncode)
      try:
          subprocess.check_call(HELP_UNPACK_CMD, shell=True)
      except CalledProcessError as e:
          sys.stderr.write('Unpacking the help package failed, aborting:\n'+str(e))
          sys.exit(e.returncode)
      try:
          subprocess.check_call('mv help/ siren/data/', shell=True);
      except CalledProcessError as e:
          sys.stderr.write('Moving the help files to siren/data/help/ failed:\n'+str(e))
          sys.exit(e.returncode)
          
          
  def clean_help_files():
      subprocess.call('rm -rf help/', shell=True)
      subprocess.call('rm -rf siren/data/help/', shell=True)
      subprocess.call('rm -rf '+HELP_PACKAGE_FILENAME, shell=True)
      
  
  #### platform specific
  if sys.platform == 'darwin':
  
      ################ MAC SETUP    
      # Bootstrap
      import ez_setup
      ez_setup.use_setuptools()
      
      from setuptools import setup
  
      # Get help files
      # load_help_files()
  
  
      # Rename exec_siren.py as Siren.py
      subprocess.call('cp '+APP+' Siren.py', shell=True)
      subprocess.check_call('mkdir -p siren/data/conf/', shell=True)
      subprocess.check_call('cp siren/*/*.xml siren/data/conf/', shell=True)
  
      # A custom plist to associate with .siren -files
      Plist = dict(CFBundleDocumentTypes = [dict(CFBundleTypeExtensions=['siren'],
                                                 CFBundleTypeName='Siren data file',
                                                 CFBundleTypeRole = 'Viewer',
                                                 CFBundleTypeIconFile = 'data/icons/siren_file_icon.icns'),
                                          dict(CFBundleTypeName   = 'fr.inria.siren.csv',
                                               CFBundleTypeRole   = 'Viewer',
                                               LSHandlerRank      = 'Alternate',
                                               LSItemContentTypes = ['public.comma-separated-values-text'])
                                                 ],
                  CFBundleShortVersionString = VERSION,
                  CFBundleVersion = get_git_hash(),
                  CFBundleName = SHORT_NAME,
                  CFBundleIdentifier = "fr.inria.siren",
                  NSHumanReadableCopyright = COPYRIGHT
          )
  
      ST_PACKAGES = ['pyproj', 'sklearn'] #'wx',  'sklearn', 'pyproj', 'mpl_toolkits']
      # ST_RESOURCES=['siren/data/help', 'LICENSE', 'CHANGELOG',
      # 'siren/interface/ui_confdef.xml','siren/views/views_confdef.xml', 'siren/reremi/miner_confdef.xml', 'siren/reremi/inout_confdef.xml']
      ST_RESOURCES=['siren/data', 'CHANGELOG']
      ICONS = ['siren/data/icons/siren_icon.icns', 'siren/data/icons/siren_file_icon.icns', 'siren/data/icons/siren_icon32x32.png', 'siren/data/icons/siren_icon.ico', 'siren/data/icons/usiren_icon.ico'] + glob.glob('siren/data/icons/*.png')
      LICENSES = ['siren/data/licenses'] #'LICENSE_basemap', 'LICENSE_matplotlib', 'LICENSE_python', 'LICENSE_wx', 'LICENSE_grako']
      # MATPLOTLIB_BACKENDS = ['wxagg']
  
      OPTIONS = {'argv_emulation': True,
      'iconfile': 'siren/data/icons/siren_icon.icns',
      'packages': ST_PACKAGES,
      #'matplotlib_backends': MATPLOTLIB_BACKENDS,
      #'includes': ['ui_confdef.xml'], 
      #'includes': ['reremi'],
      'resources': ST_RESOURCES, #+ICONS+LICENSES, #+bsm_files,
      'site_packages': False,
      'includes': ['server_siren'],
      'excludes' : ['email', 'pytz'],
      #'semi-standalone': True, # depends on local Python
      'plist': Plist,
      }
      # Set extra options
      extra_options.update(dict(
          app=['Siren.py'],
          #data_files=[APP],
          options={'py2app': OPTIONS},
          setup_requires=['py2app']
          ))
      # Run setup
      setup(**extra_options)
  
      # Post setup
      # Copy files
      print "Copying files..."
      ## subprocess.call("sed -i 's///'", shell=True)
      # subprocess.call('mkdir dist/third-party-licenses/', shell=True)
      # for f in LICENSES:
      #     subprocess.call('cp '+f+'/LICENSE* dist/third-party-licenses/', shell=True)
      subprocess.call('cp LICENSE dist/LICENSE', shell=True)
      subprocess.call('cp siren/data/licenses/README_mac.rtf dist/README.rtf', shell=True)
      subprocess.call('cp CHANGELOG dist/CHANGELOG', shell=True)
      subprocess.call('cp siren/data/help/Siren-UserGuide.pdf dist/UserGuide.pdf', shell=True)
      subprocess.call('rm dist/Siren.app/Contents/Resources/data/*_f.dat', shell=True)
  
      subprocess.call('ln -s /Applications dist/', shell=True)
      subprocess.call('cp siren/data/icons/siren_dmg_icon.icns dist/.VolumeIcon.icns', shell=True)
      # Set VolumeIcon's creator
      subprocess.call('SetFile -c icnC dist/.VolumeIcon.icns', shell=True)
      # Make read/write tmp disk image
      print "Creating initial disk image"
      subprocess.call('hdiutil create -srcfolder dist/ -volname '+SHORT_NAME+' -format UDRW -ov raw-'+SHORT_NAME+'.dmg', shell=True)
      # Attach the disk image
      subprocess.call('mkdir tmp', shell=True)
      subprocess.call('hdiutil attach raw-'+SHORT_NAME+'.dmg -mountpoint tmp', shell=True)
      # Set custom icon
      subprocess.call('SetFile -a C tmp', shell=True)
      # Detach
      print "Detaching"
      time.sleep(2)
      subprocess.call('hdiutil detach tmp', shell=True)
      subprocess.call('rm -rf tmp', shell=True)
      subprocess.call('rm -f '+SHORT_NAME+'.dmg', shell=True)
      # Convert
      print "Converting the disk image to the final one"
      time.sleep(1)
      subprocess.call('hdiutil convert raw-'+SHORT_NAME+'.dmg -format UDZO -o '+SHORT_NAME+'.dmg', shell=True)
      # Clean
      subprocess.call('rm -rf dist build', shell=True)
      subprocess.call('rm -f raw-'+SHORT_NAME+'.dmg', shell=True)
  
      # Clean help files
      # print "Cleaning help files"
      # clean_help_files()
  
      
  elif sys.platform == 'win32':
      ################ WINDOWS SETUP
      ## Should this be 'win64'??
      from distutils.core import setup
      import py2exe
      import matplotlib
  
      ST_PACKAGES = ['wx',  'sklearn', 'mpl_toolkits']
      DU_PACKAGES = ['siren', 'siren.reremi', 'siren.interface', 'siren.work', 'siren.views', 'siren.reremi.grako']
      PACKAGE_DATA = {'data\\conf': ['siren\\reremi\\miner_confdef.xml', 'siren\\reremi\\inout_confdef.xml', 'siren\\views\\views_confdef.xml', 'siren\\interface\\ui_confdef.xml',],
                      'data\\icons': glob.glob('siren\\data\\icons\\*.png') + glob.glob('siren\\data\\icons\\*.ico'),
                      'data\\help': glob.glob('siren\\data\\help\\*'),
                      'data\\licenses': ['LICENSE']+glob.glob('siren\\data\\licenses\\LICENSE*'),
  	            '': ['LICENSE']}
  
      
      OPTIONS = {
      'compressed': True,
      'packages': DU_PACKAGES+ST_PACKAGES,
      'dll_excludes': ['MSVCP90.dll','api-ms-win-core-string-l1-1-0.dll', 'api-ms-win-core-memory-l1-1-2.dll', 'api-ms-win-core-file-l1-2-1.dll', 'api-ms-win-core-libraryloader-l1-2-1.dll', 
      'api-ms-win-core-string-l2-1-0.dll', 'api-ms-win-core-profile-l1-1-0.dll', 'api-ms-win-core-string-obsolete-l1-1-0.dll', 'api-ms-win-core-debug-l1-1-1.dll', 'api-ms-win-core-sidebyside-l1-1-0.dll', 
      'api-ms-win-core-processthreads-l1-1-2.dll','api-ms-win-core-kernel32-legacy-l1-1-1.dll','api-ms-win-core-handle-l1-1-0.dll','api-ms-win-core-timezone-l1-1-0.dll','api-ms-win-core-processenvironment-l1-2-0.dll',
      'api-ms-win-core-processenvironment-l1-2-0.dll', 'api-ms-win-core-registry-l1-1-0.dll', 'api-ms-win-core-util-l1-1-0.dll', 'api-ms-win-core-atoms-l1-1-0.dll', 'api-ms-win-core-winrt-error-l1-1-1.dll', 'api-ms-win-core-heap-l1-2-0.dll',
      'api-ms-win-core-heap-l2-1-0.dll', 'api-ms-win-core-delayload-l1-1-1.dll', 'api-ms-win-core-rtlsupport-l1-2-0.dll', 'api-ms-win-core-libraryloader-l1-2-0.dll', 'api-ms-win-core-localization-l1-2-1.dll', 'api-ms-win-core-sysinfo-l1-2-1.dll',
      'api-ms-win-core-synch-l1-2-0.dll', 'api-ms-win-core-errorhandling-l1-1-1.dll', 'api-ms-win-core-shlwapi-obsolete-l1-2-0.dll', 'api-ms-win-core-localization-obsolete-l1-3-0.dll', 'api-ms-win-security-base-l1-2-0.dll',
      'QtCore4.dll','QGui4.dll','QtNetwork4.dll'],
      'includes' : ['matplotlib.backends.backend_wx', 'scipy.special.*', 'scipy.linalg.*', 'scipy.sparse.csgraph._validation','scipy.special._ufuncs_cxx','matplotlib.backends.backend_tkagg'],
      'excludes': ['matplotlib.backends.backend_qt4agg', 'PySide.QtSvg', 'PySide.QtNetwork', 'PySide.QtGui', 'PySide.QtCore', 'win32con', 'Tkinter', 'IPython', 'jupyter_core', '_sqlite3', 
      'email', '_gtkagg', '_tkagg', '_ssl', 'nose', 'pygments', 'doctest', 'scipy.ndimage', 'pandas',  'win32clipboard',
      'tornado', 'PIL', 'pydoc_data', 'curses', 'PySide', 'certifi'],
      }
      ### 
      # Set extra options
      MPL = matplotlib.get_py2exe_datafiles()
      ## C:\\Python27\\
      bsm_files = [f for f in glob.glob('C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Lib\\site-packages\\mpl_toolkits\\basemap\\data\\*') if not re.search("_f.dat", f)] 
      MTK = [('mpl_toolkits\\basemap\\data', bsm_files)]
      mkl_files = ['C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Scripts\\mk2_core.dll',
                   'C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Scripts\\mk2_intel_thread.dll',
                   'C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Scripts\\mk2ifcoremd.dll',
                   'C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Scripts\\mk2iomp5md.dll',
                   'C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Scripts\\mk2mmd.dll'] 
      mkl_files = glob.glob('C:\\Users\\pc-perso\\AppData\\Local\\Enthought\\Canopy\\User\\Scripts\\mk*.dll')
      MKL = [('', mkl_files)]
      extra_options.update(dict(
          windows=[{"script": APP, "icon_resources": [(1, "siren\\data\\icons\\siren_icon.ico")]}],
          data_files= PACKAGE_DATA.items() + MPL + MTK + MKL,
          options={'py2exe': OPTIONS})
          )
      # Run setup
      setup(**extra_options)
  
  
  else:
      ################ LINUX SETUP
      from distutils.core import setup
          
      DU_FILES = [APP[:-3], "server_siren"]
      DU_PACKAGES = ['siren', 'siren.reremi', 'siren.interface', 'siren.work', 'siren.views', 'siren.reremi.grako']
      PACKAGE_DATA = {'siren.reremi': ['miner_confdef.xml', 'inout_confdef.xml'],
                      'siren.views': ['views_confdef.xml'],
                      'siren.interface': ['ui_confdef.xml'],
                      'siren': ['data/icons/*.png', 'data/icons/*.ico', 'data/help/*', 'data/licenses/LICENSE*']}
  
      extra_options.update(dict(
          platforms="UNIX",
          description=DESCRIPTION,
          long_description=DESCRIPTION_LONG,
          package_data=PACKAGE_DATA,
          packages=DU_PACKAGES,
          py_modules=DU_FILES
          ))
      # Run setup
      setup(**extra_options)
  
  