summaryrefslogtreecommitdiff
path: root/cx_setup.py
blob: 377ba3ac29c0ee5cf9cb8c4e22b5c206b8570257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# A cx_freeze setup script to create PyMca executables
#
# Use "python cx_setup.py install"
#
# It expects a properly configured compiler.
#
# Under windows you may need to set MINGW = True (untested) if you are
# not using VS2003 (python 2.5) or VS2008 (python > 2.5)
#
# If everything works well one should find a directory in the build
# directory that contains the files needed to run the PyMca without Python
#
from cx_Freeze import setup, Executable
import sys
import os
import glob
import cx_Freeze.hooks as _hooks

MINGW = False
DEBUG = False

def load_PyQt4_Qt(finder, module):
    """the PyQt4.Qt module is an extension module which imports a number of
       other modules and injects their namespace into its own. It seems a
       foolish way of doing things but perhaps there is some hidden advantage
       to this technique over pure Python; ignore the absence of some of
       the modules since not every installation includes all of them."""
    try:
        #This modules does not seem to be always present 
        finder.IncludeModule("PyQt4._qt")
    except ImportError:
        pass
    finder.IncludeModule("PyQt4.QtCore")
    finder.IncludeModule("PyQt4.QtGui")
    finder.IncludeModule("sip")
    for name in ("PyQt4.QtSvg", "PyQt4.Qsci", "PyQt4.QtAssistant",
            "PyQt4.QtNetwork", "PyQt4.QtOpenGL", "PyQt4.QtScript",
            "PyQt4.QtSql", "PyQt4.QtSvg", "PyQt4.QtTest", "PyQt4.QtXml"):
        try:
            finder.IncludeModule(name)
        except ImportError:
            pass

_hooks.load_PyQt4_Qt = load_PyQt4_Qt

if 0:
    #This does not seem to solve the OpenGL problem
    def _load_OpenGL(finder, module):
        """
        OpenGL >= 3.0.0 is ctypes and setuptools based. Therefore, a plethora of
        modules are missed by the finder. So force inclusion of them all.
        """
        import OpenGL
        version = -1
        try:
            version = int(OpenGL.version.__version__.split(".")[0])
        except:
            pass
        if version >= 3:
            basedir, sep, basemod = module.path[0].rpartition(os.sep)
        for root, dirs, files in os.walk(module.path[0]):
            package = root.replace(basedir, "", 1).strip(sep).replace(sep, ".")
        if package != "OpenGL.tests": # ignore the OpenGL.tests package
            finder.IncludePackage(package)
    _hooks.load_OpenGL = _load_OpenGL


PyMcaInstallationDir = "build"
if sys.platform != "windows":
    PyMcaDir = os.path.join(PyMcaInstallationDir, "PyMca").replace(" ","_")
else:
    PyMcaDir =os.path.join(PyMcaInstallationDir, "PyMca")
#make sure PyMca is freshly built
if sys.platform == 'win32':
    if MINGW:
        # MinGW compiler needs two steps
        cmd = "python setup.py build -c mingw32"
        if os.system(cmd):
            print("Error building PyMca")
            sys.exit(1)

cmd = "python setup.py install --install-lib %s" % PyMcaInstallationDir
if os.system(cmd):
    print("Error building PyMca")
    sys.exit(1)

include_files = []

# this is critical for Qt to find image format plugins
include_files.append(("qtconffile", "qt.conf"))

# PyMca data
include_files.append(("changelog.txt", os.path.join("PyMcaData", "changelog.txt")))
include_files.append((os.path.join("PyMca", "PyMcaData"), "PyMcaData"))

# put the PyMca plugins as "data" to allow the user to add his own plugins
include_files.append((os.path.join("PyMca", "PyMcaPlugins"), "PyMcaPlugins"))

# Add the qt plugins directory
import PyQt4.Qt as qt
app = qt.QApplication([])
pluginsDir = str(qt.QLibraryInfo.location(qt.QLibraryInfo.PluginsPath))
for pluginSet in glob.glob(os.path.join(pluginsDir,'*')):
    plugin = os.path.basename(pluginSet)
    if plugin in ["imageformats"]:
        if sys.platform == 'win32':
            ext = "*dll"
        else:
            #for darwin platfrom I use py2app
            #this only applies to linux
            ext = "*so"
        destination = os.path.join("plugins", plugin)
        fList = glob.glob(os.path.join(pluginSet,ext))
        for f in fList:
            include_files.append((f,
                                 os.path.join(destination,os.path.basename(f))))

#I should use somehow absolute import ...
sys.path = [PyMcaDir, os.path.dirname(PyMcaDir)] + sys.path
try:
    import ctypes
    import OpenGL
    import Object3D
    OBJECT3D = True
    try:
        #ESRF very special distribution ...
        import Object3DCTools
        import Object3DQhull
        OBJECT3DCTOOLS = True
    except:
        #fine, it is inside Object3D
        OBJECT3DCTOOLS = False
except:
    OBJECT3D = False

try:
    import scipy
    SCIPY = True
    if DEBUG:
        print("ADDING SciPy DOUBLES THE SIZE OF THE DOWNLOADABLE PACKAGE...")
except:
    SCIPY = False

# For the time being I leave SciPy out
SCIPY = False

import PyMcaMain
import PyMcaPlugins
try:
    import matplotlib
    MATPLOTLIB = True
except ImportError:
    MATPLOTLIB = False

try:
    import pyopencl
    OPENCL = True
    import sift
except :
    OPENCL = False

if sys.platform.lower().startswith("linux"):
    # no sense to freeze
    OPENCL = False

try:
    import mdp
    MDP = True
except ImportError:
    MDP = False

H5PY_SPECIAL = False
try:
    import h5py
    if h5py.version.version < '1.2.0':
        includes = ['h5py._extras']
    elif h5py.version.version < '1.3.0':
        includes = ['h5py._stub', 'h5py._sync', 'h5py.utils']
    elif h5py.version.version < '2.0.0':
        includes = ['h5py._extras', 'h5py._stub', 'h5py.utils',
                    'h5py._conv', 'h5py._proxy']
    else:
        H5PY_SPECIAL = True
        includes = []        
except:
    includes = []

#some standard encodings
includes.append('encodings.ascii')
includes.append('encodings.utf_8')
includes.append('encodings.latin_1')

#make sure all PyMca modules are there
for module in glob.glob(os.path.join('PyMca', '*.py')):
    m = "PyMca."+os.path.basename(module)[:-3]
    if DEBUG:
        print("Adding %s" % m)
    includes.append(m)
    
if OBJECT3D:
    includes.append("logging")
    excludes = ["OpenGL", "Tkinter", "PyMca.Object3D", "Object3D",
                "PyMca.PyMcaPlugins", "PyMcaPlugins",
                "scipy", "Numeric", "numarray"] 
    #This requieres the use of the environmental variable MATPLOTLIBDATA
    #pointing to mpl-data directory to work
    special_modules =[os.path.dirname(ctypes.__file__),
                  os.path.dirname(OpenGL.__file__),
                  os.path.dirname(Object3D.__file__)]
    if H5PY_SPECIAL:
        special_modules.append(os.path.dirname(h5py.__file__))
    if OPENCL:
        special_modules.append(os.path.dirname(pyopencl.__file__))
        excludes.append("PyMca.sift")
        special_modules.append(os.path.dirname(sift.__file__))
    else:
        excludes.append("pyopencl")
    if MDP:
        #mdp versions above 2.5 need special treatment
        if mdp.__version__  > '2.5':
            special_modules.append(os.path.dirname(mdp.__file__))
    if MATPLOTLIB:
        special_modules.append(os.path.dirname(matplotlib.__file__))
    if SCIPY:
        special_modules.append(os.path.dirname(scipy.__file__))
    for f in special_modules:
            include_files.append((f,os.path.basename(f)))
    if OBJECT3DCTOOLS:
        excludes.append("Object3DCTools")
        excludes.append("Object3DQhull")
        for f in [Object3DCTools.__file__,Object3DQhull.__file__]: 
            o3ddir = os.path.dirname(Object3D.__file__)
            include_files.append((f, 
                            os.path.join(os.path.basename(o3ddir), os.path.basename(f))))
else:
    excludes = ["Tkinter", "PyMca.PyMcaPlugins",
                "PyMcaPlugins", "scipy", "Numeric", "numarray"]
    special_modules = [os.path.dirname(ctypes.__file__)]
    if H5PY_SPECIAL:
        special_modules.append(os.path.dirname(h5py.__file__))
    if OPENCL:
        excludes.append("PyMca.sift")
        special_modules.append(os.path.dirname(sift.__file__))
        special_modules.append(os.path.dirname(pyopencl.__file__))
    else:
        excludes.append("pyopencl")
    if MDP:
        #mdp versions above 2.5 need special treatment
        if mdp.__version__  > '2.5':
            special_modules.append(os.path.dirname(mdp.__file__))
    if MATPLOTLIB:
        special_modules.append(os.path.dirname(matplotlib.__file__))
    if SCIPY:
        special_modules.append(os.path.dirname(scipy.__file__))
    for f in special_modules:
            include_files.append((f,os.path.basename(f)))
    
for f in ['qt', 'qttable', 'qtcanvas', 'Qwt5']:
    excludes.append(f)

#Next line was for the plugins in frozen but now is in shared zip library
#include_files.append((PyMcaDir, "PyMca"))
buildOptions = dict(
        compressed = True,
        include_files = include_files,
        excludes = excludes,
        includes = includes,
        #includes = ["scipy.interpolate", "scipy.linalg"]
        #optimize=2,
        #packages = packages,
        #includes = ["Object3D"],
        #path = [PyMcaDir] + sys.path
        )
install_dir = PyMcaDir + " " + PyMcaMain.__version__
if not sys.platform.startswith('win'):
    install_dir = install_dir.replace(" ","")
if os.path.exists(install_dir):
    try:
        def dir_cleaner(directory):
            for f in glob.glob(os.path.join(directory,'*')):
                if os.path.isfile(f):
                    try:
                        os.remove(f)
                    except:
                        print("file <%s> not deleted" % f)
                if os.path.isdir(f):
                    dir_cleaner(f)
            try:
                os.rmdir(directory)
            except:
                print("directory ", directory, "not deleted")
        dir_cleaner(install_dir)
    except:
        print("Unexpected error:", sys.exc_info())
        pass
        
if os.path.exists('bin'):
    for f in glob.glob(os.path.join('bin','*')):
        os.remove(f)
    os.rmdir('bin')
installOptions = dict(
    install_dir= install_dir,
)

exec_list = ["PyMcaMain",
             "PyMcaBatch",
             "QStackWidget",
             "PeakIdentifier",
             "EdfFileSimpleViewer",
             "PyMcaPostBatch",
             "Mca2Edf",
             "ElementsInfo"]

for f in exec_list:
    executable = os.path.join(install_dir, f)
    if os.path.exists(executable):
        os.remove(executable)

         
executables = []
for python_module in exec_list:
    icon = None
    # this allows to map a different icon to each executable
    if sys.platform.startswith('win'):
        if python_module in ["PyMcaMain", "QStackWidget"]:
            icon = os.path.join(os.path.dirname(__file__), "icons", "PyMca.ico")
    executables.append(Executable(os.path.join(PyMcaDir, python_module+".py"),
                                  icon=icon))


setup(
        name = "PyMca",
        version = PyMcaMain.__version__,
        description = "PyMca %s" % PyMcaMain.__version__,
        options = dict(build_exe = buildOptions,
                       install_exe = installOptions
                       ),
        executables = executables)


if OPENCL:
    # pyopencl __init__.py needs to be patched
    initFile = os.path.join(install_dir, "pyopencl", "__init__.py")
    f = open(initFile, "r")
    content = f.readlines()
    f.close()
    i = 0
    i0 = 0
    for line in content:
        if "def _find_pyopencl_include_path():" in line:
            i0 = i - 1
        elif (i0 != 0) and ("}}}" in line):
            i1 = i
            break
        i += 1
    f = open(initFile, "w")
    for i in range(0, i0):
        f.write(content[i])
    txt ='\n'
    txt +='def _find_pyopencl_include_path():\n'
    txt +='     from os.path import dirname, join, realpath\n'
    txt +="     return '\"%s\"' % join(realpath(dirname(__file__)), \"cl\")"
    txt +="\n"
    txt +="\n"
    f.write(txt)
    for line in content[i1:]:
        f.write(line)
    f.close()

if not sys.platform.startswith('win'):
    #rename the executables to .exe for easier handling by the start scripts
    for f in exec_list:
        executable = os.path.join(install_dir, f)
        if os.path.exists(executable):
            os.rename(executable, executable+".exe")
        #create the start script
        text  = "#!/bin/bash\n"
        text += 'if test -e "./%s.exe"; then\n' % f
        text += '    export LD_LIBRARY_PATH=./:${LD_LIBRARY_PATH}\n'
        text += '    exec ./%s.exe $*\n' % f
        text += 'else\n'
        text += '    if test -z "${PYMCAHOME}" ; then\n'
        text += '        thisdir=`dirname $0` \n'
        text += '        export PYMCAHOME=${thisdir}\n'
        text += '    fi\n'
        text += '    export LD_LIBRARY_PATH=${PYMCAHOME}:${LD_LIBRARY_PATH}\n'
        text += '    exec ${PYMCAHOME}/%s.exe $*\n' % f
        text += 'fi\n'
        nfile = open(executable,'w')
        nfile.write(text)
        nfile.close()
        os.system("chmod 775 %s"  % executable)
        #generate the lowercase commands
        if f == "PyMcaMain":
            os.system("cp -f %s %s" % (executable, os.path.join(install_dir, 'pymca')))
        elif f == "QStackWidget":
            os.system("cp -f %s %s" % (executable, os.path.join(install_dir, 'pymcaroitool')))
        elif f == "EdfFileSimpleViewer":
            os.system("cp -f %s %s" % (executable, os.path.join(install_dir, 'edfviewer')))
        else:
            os.system("cp -f %s %s" % (executable,
                                       os.path.join(install_dir, f.lower())))
            if f == "PyMcaPostBatch":
                os.system("cp -f %s %s" % (executable, os.path.join(install_dir, 'rgbcorrelator')))
            

#cleanup
for f in glob.glob(os.path.join(os.path.dirname(__file__),"PyMca", "*.pyc")):
    os.remove(f)

if not sys.platform.startswith('win'):
    #Unix binary ...
    readline = 'libreadline.so.4'
    for dirname in ['/lib','/usr/lib']:
        fname = os.path.join(dirname, readline)
        if os.path.exists(fname):
            cmd =  "cp -f %s %s" % (fname, os.path.join(install_dir, readline))
            os.system(cmd)
            if dirname == '/lib':
                #readline is from suse82 systems at ESRF
                #and at a certain point I had to add these two libraries
                fname = '/usr/lib/libg2c.so.0'
                cmd = "cp -f %s %s" % (fname,
                                       os.path.join(install_dir, 'libg2c.so.0'))
                os.system(cmd)
                fname = '/usr/lib/libpng.so.3'
                cmd = "cp -f %s %s" % (fname,
                                       os.path.join(install_dir, 'libpng.so.3'))
                os.system(cmd)

        #numpy is now compiled with libgfortran at the ESRF
        for fname in glob.glob(os.path.join(dirname, "libgfortra*")):
            cmd = "cp -f %s %s" % (fname,
                                    os.path.join(install_dir, os.path.basename(fname)))
            os.system(cmd)

    #remove libX of the packaging system to use that of the target system
    for fname in glob.glob(os.path.join(install_dir,"libX*")):
        os.remove(fname)

    #remove libfontconfig.so of the package in order to use the one in the target system
    for fname in glob.glob(os.path.join(install_dir,"libfontconf*")):
        os.remove(fname)

    #end linux binary

# check if the library has been created
library = os.path.join(install_dir,"library.zip")
if not os.path.exists(library):
    print("PROBLEM")
    print("Cannot find zipped library: ")
    print(library)
    print("Please use python cx_setup.py install")
else:
    if DEBUG:
        print("NO PROBLEM")

if os.path.exists('bin'):
    for f in glob.glob(os.path.join('bin','*')):
        os.remove(f)
    os.rmdir('bin')

if not SCIPY:
    for f in ["SplinePlugins.py"]:
        plugin = os.path.join(install_dir, "PyMcaPlugins", f)
        if os.path.exists(plugin):
            print("Deleting plugin %s" % plugin)
            os.remove(plugin)

sys.exit(0)

########################## WHAT FOLLOWS IS UNUSED CODE ################
#                                                                     #               
# I was using it to add undetected modules to library.zip             #
#                                                                     #               
# It might be needed in the future. Code kept here as "backup".       #
#                                                                     #               
#######################################################################

#Add modules to library.zip
import zipfile
zf = zipfile.ZipFile(library, mode='a')
PY_COUNTER = 0
PYC_COUNTER = 0
SKIP_PYC = False
SKIP_PY = True
def addToZip(zf, path, zippath, full=False):
    global PY_COUNTER
    global PYC_COUNTER
    if os.path.basename(path).upper().endswith("HTML"):
        if DEBUG:
            print("NOT ADDING", path)
        if not full:
            return
    if path.upper().endswith(".PYC"):
        PYC_COUNTER += 1
        if SKIP_PYC:
            if not full:
                if DEBUG:
                    print("NOT ADDING", path)
                return
    if path.upper().endswith(".PY"):
        PY_COUNTER += 1
        if SKIP_PY:
            if not full:
                if DEBUG:
                    print("NOT ADDING", path)
                return
    if os.path.isfile(path):
        zf.write(path, zippath, zipfile.ZIP_DEFLATED)
    elif os.path.isdir(path):
        if not os.path.basename(path).upper().startswith("PLUGIN"):
            for nm in os.listdir(path):
                addToZip(zf,
                    os.path.join(path, nm), os.path.join(zippath, nm))
        else:
            if DEBUG:
                print("NOT ADDING", path)

addToZip(zf, PyMcaDir, os.path.basename(PyMcaDir), full=False)
    
#if PY_COUNTER > PYC_COUNTER:
#    print "WARNING: More .py files than  .pyc files. Check cx_setup.py"
if PY_COUNTER < PYC_COUNTER:
    print("WARNING: More .pyc files than  .py files. Check cx_setup.py")