~mcfletch/eric/update-to-4.5.13

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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2002-2012 Detlev Offenbach <detlev@die-offenbachs.de>
#
# This is the install script for eric4.

"""
Installation script for the eric4 IDE and all eric4 related tools.
"""

import sys
import os
import re
import compileall
import py_compile
import glob
import shutil
import fnmatch
import distutils.sysconfig

# Define the globals.
progName = None
modDir = None
pyModDir = None
platBinDir = None
distDir = None
apisDir = None
doCleanup = True
doCompile = True
cfg = {}
progLanguages = ["Python", "Ruby"]
sourceDir = "eric"
configName = 'eric4config.py'

# Define blacklisted versions of the prerequisites
BlackLists = {
    "sip"         : ["4.11", "4.12.3"], 
    "PyQt4"       : ["4.7.5"], 
    "QScintilla2" : [], 
}

def exit(rcode=0):
    """
    Exit the install script.
    """
    if sys.platform.startswith("win"):
        raw_input("Press enter to continue...")
    
    sys.exit(rcode)

def usage(rcode = 2):
    """
    Display a usage message and exit.

    @param rcode the return code passed back to the calling process.
    """
    global progName, platBinDir, modDir, distDir, apisDir

    print
    print "Usage:"
    print "    %s [-chxz] [-a dir] [-b dir] [-d dir] [-f file] [-i dir]" % (progName)
    print "where:"
    print "    -h        display this help message"
    print "    -a dir    where the API files will be installed"
    if apisDir:
        print "              (default: %s)" % (apisDir)
    else:
        print "              (no default value)"
    print "    -b dir    where the binaries will be installed"
    print "              (default: %s)" % (platBinDir)
    print "    -d dir    where eric4 python files will be installed"
    print "              (default: %s)" % (modDir)
    print "    -f file   configuration file naming the various installation paths"
    if not sys.platform.startswith("win"):
        print "    -i dir    temporary install prefix"
        print "              (default: %s)" % (distDir)
    print "    -x        don't perform dependency checks (use on your own risk)"
    print "    -c        don't cleanup old installation first"
    print "    -z        don't compile the installed python files"
    print
    print "The file given to the -f option must be valid Python code defining a"
    print "dictionary called 'cfg' with the keys 'ericDir', 'ericPixDir', 'ericIconDir',"
    print "'ericDTDDir', 'ericCSSDir', 'ericStylesDir', 'ericDocDir', 'ericExamplesDir',"
    print "'ericTranslationsDir', 'ericTemplatesDir', 'ericCodeTemplatesDir',"
    print "'ericOthersDir','bindir', 'mdir' and 'apidir."
    print "These define the directories for the installation of the various parts of"\
          " eric4."

    exit(rcode)


def initGlobals():
    """
    Sets the values of globals that need more than a simple assignment.
    """
    global platBinDir, modDir, pyModDir, apisDir

    if sys.platform.startswith("win"):
        platBinDir = sys.exec_prefix
        if platBinDir.endswith("\\"):
            platBinDir = platBinDir[:-1]
    else:
        platBinDir = "/usr/local/bin"

    modDir = distutils.sysconfig.get_python_lib(True)
    pyModDir = modDir
    
    try:
        from PyQt4 import pyqtconfig
        pyqtDataDir = pyqtconfig._pkg_config["pyqt_mod_dir"]
        if os.path.exists(os.path.join(pyqtDataDir, "qsci")):
            # it's the installer
            qtDataDir = pyqtDataDir
        else:
            qtDataDir = pyqtconfig._pkg_config["qt_data_dir"]
    except (AttributeError, ImportError):
        qtDataDir = None
    if qtDataDir:
        apisDir = os.path.join(qtDataDir, "qsci", "api")
    else:
        apisDir = None


def copyToFile(name, text):
    """
    Copy a string to a file.

    @param name the name of the file.
    @param text the contents to copy to the file.
    """
    f = open(name, "w")
    f.write(text)
    f.close()


def wrapperName(dname, wfile):
    """
    Create the platform specific name for the wrapper script.
    
    @param dname name of the directory to place the wrapper into
    @param wfile basename (without extension) of the wrapper script
    @return the name of the wrapper script
    """
    if sys.platform.startswith("win"):
        wname = dname + "\\" + wfile + ".bat"
    else:
        wname = dname + "/" + wfile

    return wname


def createPyWrapper(pydir, wfile, isGuiScript = True):
    """
    Create an executable wrapper for a Python script.

    @param pydir the name of the directory where the Python script will eventually
        be installed
    @param wfile the basename of the wrapper
    @param isGuiScript flag indicating a wrapper script for a GUI
        application (boolean)
    @return the platform specific name of the wrapper
    """
    # all kinds of Windows systems
    if sys.platform.startswith("win"):
        wname = wfile + ".bat"
        if isGuiScript:
            wrapper = \
                '''@echo off\r\n''' \
                '''set PYDIR=%%~dp0\r\n''' \
                '''start "" "%%PYDIR%%\\pythonw.exe"''' \
                ''' "%s\\%s.pyw"''' \
                ''' %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9\r\n''' % (pydir, wfile)
        else:
            wrapper = \
                '''@"%s\\python.exe" "%s\\%s.py"''' \
                ''' %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9\r\n''' % (
                    platBinDir, pydir, wfile)

    # Mac OS X
    elif sys.platform == "darwin":
        wname = wfile
        wrapper = \
'''#!/bin/sh

exec "%s/bin/pythonw" "%s/%s.py" "$@"
''' % (sys.exec_prefix, pydir, wfile)

    # *nix systems
    else:
        wname = wfile
        wrapper = \
'''#!/bin/sh

exec "%s" "%s/%s.py" "$@"
''' % (sys.executable, pydir, wfile)

    copyToFile(wname, wrapper)
    os.chmod(wname, 0755)

    return wname


def copyTree(src, dst, filters, excludeDirs=[], excludePatterns=[]):
    """
    Copy Python, translation, documentation, wizards configuration,
    designer template files and DTDs of a directory tree.
    
    @param src name of the source directory
    @param dst name of the destination directory
    @param filters list of filter pattern determining the files to be copied
    @param excludeDirs list of (sub)directories to exclude from copying
    @keyparam excludePatterns list of filter pattern determining the files to be skipped
    """
    try:
        names = os.listdir(src)
    except OSError:
        return      # ignore missing directories (most probably the i18n directory)
    
    for name in names:
        skipIt = False
        for excludePattern in excludePatterns:
            if fnmatch.fnmatch(name, excludePattern):
                skipIt = True
                break
        if not skipIt:
            srcname = os.path.join(src, name)
            dstname = os.path.join(dst, name)
            for filter in filters:
                if fnmatch.fnmatch(srcname, filter):
                    if not os.path.isdir(dst):
                        os.makedirs(dst)
                    shutil.copy2(srcname, dstname)
                    break
            else:
                if os.path.isdir(srcname) and not srcname in excludeDirs:
                    copyTree(srcname, dstname, filters, excludePatterns=excludePatterns)


def createGlobalPluginsDir():
    """
    Create the global plugins directory, if it doesn't exist.
    """
    global cfg, distDir
    
    pdir = os.path.join(cfg['mdir'], "eric4plugins")
    fname = os.path.join(pdir, "__init__.py")
    if not os.path.exists(fname):
        if not os.path.exists(pdir):
            os.mkdir(pdir,  0755)
        f = open(fname, "wb")
        f.write(\
'''# -*- coding: utf-8 -*-

"""
Package containing the global plugins.
"""
'''        
        )
        f.close()


def cleanUp():
    """
    Uninstall the old eric files.
    """
    try:
        from eric4config import getConfig
    except ImportError:
        # eric4 wasn't installed previously
        return
    
    global pyModDir, progLanguages
    
    # Remove the wrapper scripts
    rem_wnames = [
        "eric4-api", "eric4-compare",
        "eric4-configure", "eric4-diff",
        "eric4-doc", "eric4-helpviewer",
        "eric4-qregexp", "eric4-re", 
        "eric4-trpreviewer", "eric4-uipreviewer",
        "eric4-unittest", "eric4",
        "eric4-tray", "eric4-editor", 
        "eric4-plugininstall", "eric4-pluginuninstall", 
        "eric4-pluginrepository", "eric4-sqlbrowser", 
        "eric4-webbrowser", "eric4-iconeditor", 
        "eric4_api", "eric4_compare",
        "eric4_configure", "eric4_diff",
        "eric4_doc", "eric4_helpviewer",
        "eric4_qregexp", "eric4_re", 
        "eric4_trpreviewer", "eric4_uipreviewer",
        "eric4_unittest", "eric4",
        "eric4_tray", "eric4_editor", 
        "eric4_plugininstall", "eric4_pluginuninstall", 
        "eric4_pluginrepository", "eric4_sqlbrowser", 
        "eric4_webbrowser", "eric4_iconeditor", 
    ]
    
    try:
        for rem_wname in rem_wnames:
            rwname = wrapperName(getConfig('bindir'), rem_wname)
            if os.path.exists(rwname):
                os.remove(rwname)
        
        # Cleanup our config file(s)
        for name in ['eric4config.py', 'eric4config.pyc', 'eric4.pth']:
            e4cfile = os.path.join(pyModDir, name)
            if os.path.exists(e4cfile):
                os.remove(e4cfile)
            
        # Cleanup the install directories
        for name in ['ericExamplesDir', 'ericDocDir', 'ericDTDDir', 'ericCSSDir',
                     'ericIconDir', 'ericPixDir', 'ericDir', 'ericTemplatesDir',
                     'ericCodeTemplatesDir', 'ericOthersDir', 'ericStylesDir']:
            if os.path.exists(getConfig(name)):
                shutil.rmtree(getConfig(name), True)
        
        # Cleanup translations
        for name in glob.glob(os.path.join(getConfig('ericTranslationsDir'), 'eric4_*.qm')):
            if os.path.exists(name):
                os.remove(name)
        
        # Cleanup API files
        try:
            apidir = getConfig('apidir')
            for progLanguage in progLanguages:
                for name in getConfig('apis'):
                    apiname = os.path.join(apidir, progLanguage, name)
                    if os.path.exists(apiname):
                        os.remove(apiname)
        except AttributeError:
            pass
        
        if sys.platform == "darwin":
            # delete the Mac app bundle
            if os.path.exists("/Developer/Applications/Eric4"):
                shutil.rmtree("/Developer/Applications/Eric4")
        
    except IOError, msg:
        sys.stderr.write('IOError: %s\nTry install with admin rights.\n' % msg)
        exit(7)
        
    except OSError, msg:
        sys.stderr.write('OSError: %s\nTry install with admin rights.\n' % msg)
        exit(7)


def installEric():
    """
    Actually perform the installation steps.
    """
    global distDir, doCleanup, cfg, progLanguages, sourceDir, configName
    
    # Create the platform specific wrappers.
    wnames = []
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_api", False))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_compare"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_configure"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_diff"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_doc", False))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_editor"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_iconeditor"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_plugininstall"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_pluginrepository"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_pluginuninstall"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_qregexp"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_re"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_sqlbrowser"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_tray"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_trpreviewer"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_uipreviewer"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_unittest"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4_webbrowser"))
    wnames.append(createPyWrapper(cfg['ericDir'], "eric4"))
    
    # set install prefix, if not None
    if distDir:
        for key in cfg.keys():
            cfg[key] = os.path.normpath(distDir + os.sep + cfg[key])
    
    try:
        # Install the files
        # make the install directories
        for key in cfg.keys():
            if not os.path.isdir(cfg[key]):
                os.makedirs(cfg[key])
        
        # copy the eric4 config file
        if distDir:
            shutil.copy(configName, cfg['mdir'])
            if os.path.exists(configName + 'c'):
                shutil.copy(configName + 'c', cfg['mdir'])
        else:
            shutil.copy(configName, modDir)
            if os.path.exists(configName + 'c'):
                shutil.copy(configName + 'c', modDir)
        
        # copy the eric4.pth file
        if distDir:
            shutil.copy('%s%seric4.pth' % (sourceDir, os.sep), cfg['mdir'])
        else:
            shutil.copy('%s%seric4.pth' % (sourceDir, os.sep), modDir)
        
        # copy the various parts of eric4
        copyTree(sourceDir, cfg['ericDir'], ['*.py', '*.pyc', '*.pyo', '*.pyw'], 
            ['%s%sExamples' % (sourceDir, os.sep)],
            excludePatterns=["eric4config.py*"])
        copyTree(sourceDir, cfg['ericDir'], ['*.rb'], 
            ['%s%sExamples' % (sourceDir, os.sep)])
        copyTree('%s%sPlugins' % (sourceDir, os.sep), 
            '%s%sPlugins' % (cfg['ericDir'], os.sep), 
            ['*.png'])
        copyTree('%s%sDocumentation' % (sourceDir, os.sep), 
                 cfg['ericDocDir'], ['*.html', '*.qch'])
        copyTree('%s%sDTDs' % (sourceDir, os.sep), 
                 cfg['ericDTDDir'], ['*.dtd'])
        copyTree('%s%sCSSs' % (sourceDir, os.sep), 
                 cfg['ericCSSDir'], ['*.css'])
        copyTree('%s%sStyles' % (sourceDir, os.sep), 
                 cfg['ericStylesDir'], ['*.qss'])
        copyTree('%s%si18n' % (sourceDir, os.sep), 
                 cfg['ericTranslationsDir'], ['*.qm'])
        copyTree('%s%sicons' % (sourceDir, os.sep), 
                 cfg['ericIconDir'], ['*.png', 'LICENSE*.*'])
        copyTree('%s%spixmaps' % (sourceDir, os.sep), 
                 cfg['ericPixDir'], ['*.png', '*.xpm', '*.ico'])
        copyTree('%s%sDesignerTemplates' % (sourceDir, os.sep), 
                 cfg['ericTemplatesDir'], ['*.tmpl'])
        copyTree('%s%sCodeTemplates' % (sourceDir, os.sep), 
                 cfg['ericCodeTemplatesDir'], ['*.tmpl'])
        copyTree('%s%sExamples' % (sourceDir, os.sep), cfg['ericExamplesDir'], 
                 ['*.py', '*.pyc', '*.pyo'])
        
        # copy the wrappers
        for wname in wnames:
            shutil.copy(wname, cfg['bindir'])
            os.remove(wname)
        
        # copy the license file
        shutil.copy('%s%sLICENSE.GPL3' % (sourceDir, os.sep), cfg['ericDir'])
        
        # create the global plugins directory
        createGlobalPluginsDir()
        
    except IOError, msg:
        sys.stderr.write('IOError: %s\nTry install with admin rights.\n' % msg)
        exit(7)
        
    except OSError, msg:
        sys.stderr.write('OSError: %s\nTry install with admin rights.\n' % msg)
        exit(7)
    
    # copy some text files to the doc area
    for name in ["LICENSE.GPL3", "THANKS", "changelog"]:
        try:
            shutil.copy('%s%s%s' % (sourceDir, os.sep, name), cfg['ericDocDir'])
        except EnvironmentError:
            print "Could not install '%s%s%s'." % (sourceDir, os.sep, name)
    for name in glob.glob(os.path.join(sourceDir, 'README*.*')):
        try:
            shutil.copy(name, cfg['ericDocDir'])
        except EnvironmentError:
            print "Could not install '%s%s%s'." % (sourceDir, os.sep, name)
   
    # copy some more stuff
    for name in ['default.e4k', 'default_Mac.e4k']:
        try:
            shutil.copy('%s%s%s' % (sourceDir, os.sep, name), cfg['ericOthersDir'])
        except EnvironmentError:
            print "Could not install '%s%s%s'." % (sourceDir, os.sep, name)
    
    # install the API file
    for progLanguage in progLanguages:
        apidir = os.path.join(cfg['apidir'], progLanguage.lower())
        if not os.path.exists(apidir):
            os.makedirs(apidir)
        for apiName in glob.glob(os.path.join(sourceDir, "APIs", progLanguage, "*.api")):
            try:
                shutil.copy(apiName, apidir)
            except EnvironmentError:
                print "Could not install '%s'." % apiName
    
    # Create a Mac application bundle
    if sys.platform == "darwin":
        createMacAppBundle(cfg['ericDir'])


def createMacAppBundle(pydir):
    """
    Create a Mac application bundle.

    @param pydir the name of the directory where the Python script will eventually
        be installed
    """
    global cfg, sourceDir
    
    dirs = {"contents": "/Developer/Applications/Eric4/eric4.app/Contents/",
            "exe": "/Developer/Applications/Eric4/eric4.app/Contents/MacOS",
            "icns": "/Developer/Applications/Eric4/eric4.app/Contents/Resources"}
    os.makedirs(dirs["contents"])
    os.mkdir(dirs["exe"])
    os.mkdir(dirs["icns"])
    
    starter = os.path.join(dirs["exe"], "eric")
    os.symlink(
        "%s/Resources/Python.app/Contents/MacOS/Python" % sys.exec_prefix,
        starter)
    
    wname = os.path.join(dirs["exe"], "eric4")
    path = os.getenv("PATH", "")
    if path:
        pybin = os.path.join(sys.exec_prefix, "bin")
        pathlist = path.split(os.pathsep)
        if pybin not in pathlist:
            pathlist.insert(0, pybin)
        path = os.pathsep.join(pathlist)
        wrapper = \
'''#!/bin/sh

PATH=%s
exec "%s" "%s/%s.py" "$@"
''' % (path, starter, pydir, "eric4")
    else:
        wrapper = \
'''#!/bin/sh

exec "%s" "%s/%s.py" "$@"
''' % (starter, pydir, "eric4")
    copyToFile(wname, wrapper)
    os.chmod(wname, 0755)
    
    shutil.copy(os.path.join(sourceDir, "pixmaps", "eric_2.icns"),
                os.path.join(dirs["icns"], "eric.icns"))
    
    copyToFile(os.path.join(dirs["contents"], "Info.plist"),
'''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
          "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>eric4</string>
    <key>CFBundleIconFile</key>
    <string>eric.icns</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>1.0</string>
    <key>CFBundleName</key>
    <string>eric4</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
</dict>
</plist>
'''        
    )


def createInstallConfig():
    """
    Create the installation config dictionary.
    """
    global modDir, platBinDir, cfg, apisDir
        
    ericdir = os.path.join(modDir, "eric4")
    cfg = {
        'ericDir'              : ericdir,
        'ericPixDir'           : os.path.join(ericdir, "pixmaps"),
        'ericIconDir'          : os.path.join(ericdir, "icons"),
        'ericDTDDir'           : os.path.join(ericdir, "DTDs"),
        'ericCSSDir'           : os.path.join(ericdir, "CSSs"),
        'ericStylesDir'        : os.path.join(ericdir, "Styles"),
        'ericDocDir'           : os.path.join(ericdir, "Documentation"),
        'ericExamplesDir'      : os.path.join(ericdir, "Examples"),
        'ericTranslationsDir'  : os.path.join(ericdir, "i18n"),
        'ericTemplatesDir'     : os.path.join(ericdir, "DesignerTemplates"),
        'ericCodeTemplatesDir' : os.path.join(ericdir, 'CodeTemplates'),
        'ericOthersDir'        : ericdir,
        'bindir'               : platBinDir,
        'mdir'                 : modDir,
    }
    if apisDir:
        cfg['apidir'] = apisDir
    else:
        cfg['apidir'] = os.path.join(ericdir, "api")
configLength = 15


def createConfig():
    """
    Create a config file with the respective config entries.
    """
    global cfg, sourceDir
    
    apis = []
    for progLanguage in progLanguages:
        for apiName in glob.glob(os.path.join(sourceDir, "APIs", progLanguage, "*.api")):
            apis.append(os.path.basename(apiName))
    
    fn = 'eric4config.py'
    config = \
"""# -*- coding: utf-8 -*-
#
# This module contains the configuration of the individual eric4 installation
#

_pkg_config = {
    'ericDir'              : r'%s',
    'ericPixDir'           : r'%s',
    'ericIconDir'          : r'%s',
    'ericDTDDir'           : r'%s',
    'ericCSSDir'           : r'%s',
    'ericStylesDir'        : r'%s',
    'ericDocDir'           : r'%s',
    'ericExamplesDir'      : r'%s',
    'ericTranslationsDir'  : r'%s',
    'ericTemplatesDir'     : r'%s',
    'ericCodeTemplatesDir' : r'%s',
    'ericOthersDir'        : r'%s',
    'bindir'               : r'%s',
    'mdir'                 : r'%s',
    'apidir'               : r'%s',
    'apis'                 : %s,
}

def getConfig(name):
    '''
    Module function to get a configuration value.

    @param name the name of the configuration value (string).
    '''
    try:
        return _pkg_config[name]
    except KeyError:
        pass

    raise AttributeError, '"%%s" is not a valid configuration value' %% name
""" % (cfg['ericDir'], cfg['ericPixDir'], cfg['ericIconDir'], 
       cfg['ericDTDDir'], cfg['ericCSSDir'], 
       cfg['ericStylesDir'], cfg['ericDocDir'],
       cfg['ericExamplesDir'], cfg['ericTranslationsDir'],
       cfg['ericTemplatesDir'],
       cfg['ericCodeTemplatesDir'], cfg['ericOthersDir'],
       cfg['bindir'], cfg['mdir'], 
       cfg['apidir'], apis)
    copyToFile(fn, config)


def doDependancyChecks():    
    """
    Perform some dependency checks.
    """
    print 'Checking dependencies'
    
    # perform dependency checks
    if sys.version_info < (2, 6, 0):
        print 'Sorry, you must have Python 2.6.0 or higher.'
        exit(5)
    if sys.version_info > (2, 9, 9):
        print 'Sorry, eric4 requires Python 2 for running.'
        exit(5)
    print "Python Version: %d.%d.%d" % sys.version_info[:3]
    
    try:
        import xml.etree
    except ImportError, msg:
        print('Your Python installation is missing the XML module.')
        print('Please install it and try again.')
        exit(5)
    
    try:
        from PyQt4.QtCore import qVersion, PYQT_VERSION
    except ImportError, msg:
        print 'Sorry, please install PyQt4.'
        print 'Error: %s' % msg
        exit(1)
    print "Found PyQt4"
    
    try:
        import PyQt4.QtHelp
    except ImportError, msg:
        print 'Sorry, please install QtHelp.'
        print 'Error: %s' % msg
        exit(1)
    print "Found QtHelp"
    
    try:
        from PyQt4 import Qsci
    except ImportError, msg:
        print "Sorry, please install QScintilla2 and"
        print "it's PyQt4 wrapper."
        print 'Error: %s' % msg
        exit(1)
    print "Found QScintilla2"
    
    # check version of Qt
    qtMajor = int(qVersion().split('.')[0])
    qtMinor = int(qVersion().split('.')[1])
    if qtMajor < 4 or (qtMajor == 4 and qtMinor < 6):
        print 'Sorry, you must have Qt version 4.6.0 or higher.'
        exit(2)
    print "Qt Version: %s" % qVersion()
    
    # check version of sip
    try:
        import sipconfig
        sipVersion = sipconfig.Configuration().sip_version_str
        # always assume, that snapshots are new enough
        if "snapshot" not in sipVersion:
            # check for blacklisted versions
            for vers in BlackLists["sip"]:
                if vers == sipVersion:
                    print 'Sorry, sip version %s is not compatible with eric4.' % vers
                    print 'Please install another version.'
                    exit(3)
    except ImportError:
        pass
    
    #check version of PyQt
    from PyQt4.QtCore import PYQT_VERSION_STR
    pyqtVersion = PYQT_VERSION_STR
    # always assume, that snapshots are new enough
    if "snapshot" not in pyqtVersion:
        while pyqtVersion.count('.') < 2:
            pyqtVersion += '.0'
        (maj, min, pat) = pyqtVersion.split('.')
        maj = int(maj)
        min = int(min)
        pat = int(pat)
        if maj < 4 or (maj == 4 and min < 6):
            print 'Sorry, you must have PyQt 4.6.0 or higher or' \
                  ' a recent snapshot release.'
            exit(4)
        # check for blacklisted versions
        for vers in BlackLists["PyQt4"]:
            if vers == pyqtVersion:
                print 'Sorry, PyQt4 version %s is not compatible with eric4.' % vers
                print 'Please install another version.'
                exit(4)
    print "PyQt Version: ", pyqtVersion
    
    #check version of QScintilla
    from PyQt4.Qsci import QSCINTILLA_VERSION_STR
    scintillaVersion = QSCINTILLA_VERSION_STR
    # always assume, that snapshots are new enough
    if "snapshot" not in scintillaVersion:
        while scintillaVersion.count('.') < 2:
            scintillaVersion += '.0'
        (maj, min, pat) = scintillaVersion.split('.')
        maj = int(maj)
        min = int(min)
        pat = int(pat)
        if maj < 2 or (maj == 2 and min < 3):
            print 'Sorry, you must have QScintilla 2.3.0 or higher or' \
                  ' a recent snapshot release.'
            exit(5)
        # check for blacklisted versions
        for vers in BlackLists["QScintilla2"]:
            if vers == scintillaVersion:
                print 'Sorry, QScintilla2 version %s is not compatible with eric4.' % vers
                print 'Please install another version.'
                exit(5)
    print "QScintilla Version: ", QSCINTILLA_VERSION_STR
    print "All dependencies ok."
    print


def compileUiFiles():
    """
    Compile the .ui files to Python sources.
    """
    global sourceDir
    try:
        from PyQt4.uic import compileUiDir
    except ImportError:
        from PyQt4.uic import compileUi
        
        def compileUiDir(dir, recurse = False, map = None, **compileUi_args):
            """
            Creates Python modules from Qt Designer .ui files in a directory or
            directory tree.
            
            Note: This function is a modified version of the one found in PyQt4.

            @param dir Name of the directory to scan for files whose name ends with
                '.ui'. By default the generated Python module is created in the same
                directory ending with '.py'.
            @param recurse flag indicating that any sub-directories should be scanned.
            @param map an optional callable that is passed the name of the directory
                containing the '.ui' file and the name of the Python module that will be
                created. The callable should return a tuple of the name of the directory
                in which the Python module will be created and the (possibly modified)
                name of the module.
            @param compileUi_args any additional keyword arguments that are passed to
                the compileUi() function that is called to create each Python module.
            """
            def compile_ui(ui_dir, ui_file):
                """
                Local function to compile a single .ui file.
                
                @param ui_dir directory containing the .ui file (string)
                @param ui_file file name of the .ui file (string)
                """
                # Ignore if it doesn't seem to be a .ui file.
                if ui_file.endswith('.ui'):
                    py_dir = ui_dir
                    py_file = ui_file[:-3] + '.py'

                    # Allow the caller to change the name of the .py file or generate
                    # it in a different directory.
                    if map is not None:
                        py_dir, py_file = map(py_dir, py_file)

                    # Make sure the destination directory exists.
                    try:
                        os.makedirs(py_dir)
                    except:
                        pass

                    ui_path = os.path.join(ui_dir, ui_file)
                    py_path = os.path.join(py_dir, py_file)

                    ui_file = open(ui_path, 'r')
                    py_file = open(py_path, 'w')

                    try:
                        compileUi(ui_file, py_file, **compileUi_args)
                    finally:
                        ui_file.close()
                        py_file.close()

            if recurse:
                for root, _, files in os.walk(dir):
                    for ui in files:
                        compile_ui(root, ui)
            else:
                for ui in os.listdir(dir):
                    if os.path.isfile(os.path.join(dir, ui)):
                        compile_ui(dir, ui)
    
    def pyName(py_dir, py_file):
        """
        Local function to create the Python source file name for the compiled .ui file.
        
        @param py_dir suggested name of the directory (string)
        @param py_file suggested name for the compile source file (string)
        @return tuple of directory name (string) and source file name (string)
        """
        return py_dir, "Ui_%s" % py_file
    
    compileUiDir(sourceDir, True, pyName)


def main(argv):
    """
    The main function of the script.

    @param argv the list of command line arguments.
    """
    import getopt

    # Parse the command line.
    global progName, modDir, doCleanup, doCompile, distDir, cfg, apisDir
    global sourceDir, configName
    
    progName = os.path.basename(argv[0])

    initGlobals()

    try:
        if sys.platform.startswith("win"):
            optlist, args = getopt.getopt(argv[1:],"chxza:b:d:f:")
        else:
            optlist, args = getopt.getopt(argv[1:],"chxza:b:d:f:i:")
    except getopt.GetoptError:
        usage()

    global platBinDir
    
    depChecks = True

    for opt, arg in optlist:
        if opt == "-h":
            usage(0)
        elif opt == "-a":
            apisDir = arg
        elif opt == "-b":
            platBinDir = arg
        elif opt == "-d":
            modDir = arg
        elif opt == "-i":
            distDir = os.path.normpath(arg)
        elif opt == "-x":
            depChecks = False
        elif opt == "-c":
            doCleanup = False
        elif opt == "-z":
            doCompile = False
        elif opt == "-f":
            try:
                execfile(arg, globals())
                if len(cfg) != configLength:
                    print "The configuration dictionary in '%s' is incorrect. Aborting"\
                        % arg
                    exit(6)
            except:
                cfg = {}
    
    installFromSource = not os.path.isdir(sourceDir)
    if installFromSource:
        sourceDir = os.path.dirname(__file__) or "."
        configName = os.path.join(sourceDir, "eric4config.py")
    
    if len(cfg) == 0:
        createInstallConfig()
    
    if depChecks:
        doDependancyChecks()
    
    # get rid of development config file, if it exists
    try:
        if installFromSource:
            os.rename(configName, configName + ".orig")
            configNameC = configName + 'c'
            if os.path.exists(configNameC):
                os.remove(configNameC)
        os.remove(configName)
    except EnvironmentError:
        pass
    
    # cleanup old installation
    try:
        if doCleanup:
            if distDir:
                shutil.rmtree(distDir, True)
            else:
                cleanUp()
    except IOError, msg:
        sys.stderr.write('IOError: %s\nTry install as root.\n' % msg)

    # Create a config file and delete the default one
    createConfig()

    # Compile .ui files
    print "Compiling user interface files..."
    compileUiFiles()
    
    if doCompile:
        print "\nCompiling source files..."
        if distDir:
            compileall.compile_dir(sourceDir, 
                                   ddir = os.path.join(distDir, modDir, cfg['ericDir']), 
                                   rx = re.compile("Python3"), 
                                   quiet = True)
            py_compile.compile(configName, 
                               dfile = os.path.join(distDir, modDir, "eric4config.py"))
        else:
            compileall.compile_dir(sourceDir, 
                                   ddir = os.path.join(modDir, cfg['ericDir']), 
                                   rx = re.compile("Python3"), 
                                   quiet = True)
            py_compile.compile(configName, 
                               dfile = os.path.join(modDir, "eric4config.py"))
    print "\nInstalling eric4 ..."
    installEric()
    
    # do some cleanup
    try:
        if installFromSource:
            os.remove(configName)
            configNameC = configName + 'c'
            if os.path.exists(configNameC):
                os.remove(configNameC)
            os.rename(configName + ".orig", configName)
    except EnvironmentError:
        pass
    
    print "\nInstallation complete."
    print
    
    # check PyXML version and output a message for broken PyXML (< 0.8.6)
    try:
        import _xmlplus
        v = _xmlplus.version_info
        if v < (0, 8, 6):
            from eric4.patch_pyxml import isPatched, patchPyXML
            if not isPatched():
                print "NOTE:"
                print "    Found PyXML %d.%d.%d, which needs a patch to work correctly" % \
                    (v[0], v[1], v[2])
                print "    with foreign characters. Please see 'README-PyXML.txt' for"
                print "    details."
                res = raw_input("    Shall pyXML be patched now (y/n)? ")
                if res in ["Y", "y"]:
                    patchPyXML()
    except ImportError:
        pass
    
    #check version of PyQt
    from PyQt4.QtCore import PYQT_VERSION_STR
    pyqtVersion = PYQT_VERSION_STR
    # always assume, that snapshots are new enough
    if pyqtVersion.find("snapshot-") == -1:
        while pyqtVersion.count('.') < 2:
            pyqtVersion += '.0'
        (maj, min, pat) = pyqtVersion.split('.')
        maj = int(maj)
        min = int(min)
        pat = int(pat)
        if min < 5:
            print
            print "You have to patch PyQt QNetworkAccessManager."
            print "See the patches directory for details."
    
    print
    if sys.platform.startswith("win"):
        raw_input("Press enter to continue...")
    
    
if __name__ == "__main__":
    try:
        main(sys.argv)
    except SystemExit:
        raise
    except:
        print \
"""An internal error occured.  Please report all the output of the program,
including the following traceback, to eric4-bugs@eric-ide.python-projects.org.
"""
        raise