Source

cx_Freeze / cx_Freeze / freezer.py

  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
"""
Base class for freezing scripts into executables.
"""

import datetime
import distutils.sysconfig
import imp
import marshal
import os
import shutil
import socket
import stat
import struct
import sys
import time
import zipfile

import cx_Freeze

__all__ = [ "ConfigError", "ConstantsModule", "Executable", "Freezer" ]

EXTENSION_LOADER_SOURCE = \
"""
import imp, os, sys

found = False
for p in sys.path:
    if not os.path.isdir(p):
        continue
    f = os.path.join(p, "%s")
    if not os.path.exists(f):
        continue
    m = imp.load_dynamic(__name__, f)
    import sys
    sys.modules[__name__] = m
    found = True
    break
if not found:
    del sys.modules[__name__]
    raise ImportError("No module named %%s" %% __name__)
"""


MSVCR_MANIFEST_TEMPLATE = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<noInheritable/>
<assemblyIdentity
    type="win32"
    name="Microsoft.VC90.CRT"
    version="9.0.21022.8"
    processorArchitecture="{PROC_ARCH}"
    publicKeyToken="1fc8b3b9a1e18e3b"/>
<file name="MSVCR90.DLL"/>
<file name="MSVCM90.DLL"/>
<file name="MSVCP90.DLL"/>
</assembly>
"""


class Freezer(object):

    def __init__(self, executables, constantsModules = [], includes = [],
            excludes = [], packages = [], replacePaths = [], compress = None,
            optimizeFlag = 0, copyDependentFiles = None, initScript = None,
            base = None, path = None, createLibraryZip = None,
            appendScriptToExe = None, appendScriptToLibrary = None,
            targetDir = None, binIncludes = [], binExcludes = [],
            binPathIncludes = [], binPathExcludes = [], icon = None,
            includeFiles = [], zipIncludes = [], silent = False,
            namespacePackages = [], metadata = None,
            includeMSVCR = False):
        self.executables = list(executables)
        self.constantsModules = list(constantsModules)
        self.includes = list(includes)
        self.excludes = list(excludes)
        self.packages = list(packages)
        self.namespacePackages = list(namespacePackages)
        self.replacePaths = list(replacePaths)
        self.compress = compress
        self.optimizeFlag = optimizeFlag
        self.copyDependentFiles = copyDependentFiles
        self.initScript = initScript
        self.base = base
        self.path = path
        self.createLibraryZip = createLibraryZip
        self.includeMSVCR = includeMSVCR
        self.appendScriptToExe = appendScriptToExe
        self.appendScriptToLibrary = appendScriptToLibrary
        self.targetDir = targetDir
        self.binIncludes = [os.path.normcase(n) \
                for n in self._GetDefaultBinIncludes() + binIncludes]
        self.binExcludes = [os.path.normcase(n) \
                for n in self._GetDefaultBinExcludes() + binExcludes]
        self.binPathIncludes = [os.path.normcase(n) for n in binPathIncludes]
        self.binPathExcludes = [os.path.normcase(n) \
                for n in self._GetDefaultBinPathExcludes() + binPathExcludes]
        self.icon = icon
        self.includeFiles = list(includeFiles)
        self.includeFiles = self._ProcessPathSpecs(includeFiles)
        self.zipIncludes = self._ProcessPathSpecs(zipIncludes)
        self.silent = silent
        self.metadata = metadata
        self._VerifyConfiguration()

    def _AddVersionResource(self, fileName):
        try:
            from win32verstamp import stamp
        except:
            print("*** WARNING *** unable to create version resource")
            print("install pywin32 extensions first")
            return
        versionInfo = VersionInfo(self.metadata.version,
                comments = self.metadata.long_description,
                description = self.metadata.description,
                company = self.metadata.author,
                product = self.metadata.name)
        stamp(fileName, versionInfo)

    def _CopyFile(self, source, target, copyDependentFiles = False,
            includeMode = False):
        normalizedSource = os.path.normcase(os.path.normpath(source))
        normalizedTarget = os.path.normcase(os.path.normpath(target))
        if normalizedTarget in self.filesCopied:
            return
        if normalizedSource == normalizedTarget:
            return
        self._RemoveFile(target)
        targetDir = os.path.dirname(target)
        self._CreateDirectory(targetDir)
        if not self.silent:
            sys.stdout.write("copying %s -> %s\n" % (source, target))
        shutil.copyfile(source, target)
        shutil.copystat(source, target)
        if includeMode:
            shutil.copymode(source, target)
        self.filesCopied[normalizedTarget] = None
        if copyDependentFiles:
            for source in self._GetDependentFiles(source):
                target = os.path.join(targetDir, os.path.basename(source))
                self._CopyFile(source, target, copyDependentFiles)

    def _CreateDirectory(self, path):
        if not os.path.isdir(path):
            if not self.silent:
                sys.stdout.write("creating directory %s\n" % path)
            os.makedirs(path)

    def _FreezeExecutable(self, exe):
        if self.createLibraryZip:
            finder = self.finder
        else:
            finder = self._GetModuleFinder(exe)
        if exe.script is None:
            scriptModule = None
        else:
            scriptModule = finder.IncludeFile(exe.script, exe.moduleName)
        self._CopyFile(exe.base, exe.targetName, exe.copyDependentFiles,
                includeMode = True)
        if self.includeMSVCR:
            self._IncludeMSVCR(exe)
        if exe.icon is not None:
            if sys.platform == "win32":
                import cx_Freeze.util
                cx_Freeze.util.AddIcon(exe.targetName, exe.icon)
            else:
                targetName = os.path.join(os.path.dirname(exe.targetName),
                        os.path.basename(exe.icon))
                self._CopyFile(exe.icon, targetName,
                        copyDependentFiles = False)
        if not os.access(exe.targetName, os.W_OK):
            mode = os.stat(exe.targetName).st_mode
            os.chmod(exe.targetName, mode | stat.S_IWUSR)
        if self.metadata is not None and sys.platform == "win32":
            self._AddVersionResource(exe.targetName)
        if not exe.appendScriptToLibrary:
            if exe.appendScriptToExe:
                fileName = exe.targetName
            else:
                baseFileName, ext = os.path.splitext(exe.targetName)
                fileName = baseFileName + ".zip"
                self._RemoveFile(fileName)
            if not self.createLibraryZip and exe.copyDependentFiles:
                scriptModule = None
            self._WriteModules(fileName, exe.initScript, finder, exe.compress,
                    exe.copyDependentFiles, scriptModule)

    def _GetBaseFileName(self, argsSource = None):
        if argsSource is None:
            argsSource = self
        name = argsSource.base
        if name is None:
            if argsSource.copyDependentFiles:
                name = "Console"
            else:
                name = "ConsoleKeepPath"
        argsSource.base = self._GetFileName("bases", name)
        if argsSource.base is None:
            raise ConfigError("no base named %s", name)

    def _GetDefaultBinExcludes(self):
        """Return the file names of libraries that need not be included because
           they would normally be expected to be found on the target system or
           because they are part of a package which requires independent
           installation anyway."""
        if sys.platform == "win32":
            return ["comctl32.dll", "oci.dll", "cx_Logging.pyd"]
        else:
            return ["libclntsh.so", "libwtc9.so"]

    def _GetDefaultBinIncludes(self):
        """Return the file names of libraries which must be included for the
           frozen executable to work."""
        if sys.platform == "win32":
            pythonDll = "python%s%s.dll" % sys.version_info[:2]
            return [pythonDll, "gdiplus.dll", "mfc71.dll", "msvcp71.dll",
                    "msvcr71.dll"]
        else:
            soName = distutils.sysconfig.get_config_var("INSTSONAME")
            if soName is None:
                return []
            pythonSharedLib = self._RemoveVersionNumbers(soName)
            return [pythonSharedLib]

    def _GetDefaultBinPathExcludes(self):
        """Return the paths of directories which contain files that should not
           be included, generally because they contain standard system
           libraries."""
        if sys.platform == "win32":
            import cx_Freeze.util
            systemDir = cx_Freeze.util.GetSystemDir()
            windowsDir = cx_Freeze.util.GetWindowsDir()
            return [windowsDir, systemDir, os.path.join(windowsDir, "WinSxS")]
        elif sys.platform == "darwin":
            return ["/lib", "/usr/lib", "/System/Library/Frameworks"]
        else:
            return ["/lib", "/lib32", "/lib64", "/usr/lib", "/usr/lib32",
                    "/usr/lib64"]

    def _GetDependentFiles(self, path):
        """Return the file's dependencies using platform-specific tools (the
           imagehlp library on Windows, otool on Mac OS X and ldd on Linux);
           limit this list by the exclusion lists as needed"""
        dependentFiles = self.dependentFiles.get(path)
        if dependentFiles is None:
            if sys.platform == "win32":
                origPath = os.environ["PATH"]
                os.environ["PATH"] = origPath + os.pathsep + \
                        os.pathsep.join(sys.path)
                import cx_Freeze.util
                dependentFiles = cx_Freeze.util.GetDependentFiles(path)
                os.environ["PATH"] = origPath
            else:
                dependentFiles = []
                if sys.platform == "darwin":
                    command = 'otool -L "%s"' % path
                    splitString = " (compatibility"
                    dependentFileIndex = 0
                else:
                    command = 'ldd "%s"' % path
                    splitString = " => "
                    dependentFileIndex = 1
                for line in os.popen(command):
                    parts = line.expandtabs().strip().split(splitString)
                    if len(parts) != 2:
                        continue
                    dependentFile = parts[dependentFileIndex].strip()
                    if dependentFile in ("not found", "(file not found)"):
                        fileName = parts[0]
                        if fileName not in self.linkerWarnings:
                            self.linkerWarnings[fileName] = None
                            message = "WARNING: cannot find %s\n" % fileName
                            sys.stdout.write(message)
                        continue
                    if dependentFile.startswith("("):
                        continue
                    pos = dependentFile.find(" (")
                    if pos >= 0:
                        dependentFile = dependentFile[:pos].strip()
                    if dependentFile:
                        dependentFiles.append(dependentFile)
            dependentFiles = self.dependentFiles[path] = \
                    [f for f in dependentFiles if self._ShouldCopyFile(f)]
        return dependentFiles

    def _GetFileName(self, dir, name, ext = None):
        if os.path.isabs(name):
            return name
        name = os.path.normcase(name)
        fullDir = os.path.join(os.path.dirname(cx_Freeze.__file__), dir)
        if os.path.isdir(fullDir):
            for fileName in os.listdir(fullDir):
                checkName, checkExt = \
                        os.path.splitext(os.path.normcase(fileName))
                if name == checkName and (ext is None or ext == checkExt):
                    return os.path.join(fullDir, fileName)

    def _GetInitScriptFileName(self, argsSource = None):
        if argsSource is None:
            argsSource = self
        name = argsSource.initScript
        if name is None:
            if argsSource.copyDependentFiles:
                name = "Console"
            else:
                name = "ConsoleKeepPath"
            if sys.version_info[0] >= 3:
                name += "3"
        argsSource.initScript = self._GetFileName("initscripts", name, ".py")
        if argsSource.initScript is None:
            raise ConfigError("no initscript named %s", name)

    def _GetModuleFinder(self, argsSource = None):
        if argsSource is None:
            argsSource = self
        finder = cx_Freeze.ModuleFinder(self.includeFiles, argsSource.excludes,
                argsSource.path, argsSource.replacePaths,
                argsSource.copyDependentFiles, compress = argsSource.compress)
        for name in argsSource.namespacePackages:
            package = finder.IncludeModule(name, namespace = True)
            package.ExtendPath()
        for name in argsSource.includes:
            finder.IncludeModule(name)
        for name in argsSource.packages:
            finder.IncludePackage(name)
        return finder

    def _IncludeMSVCR(self, exe):
        msvcRuntimeDll = None
        targetDir = os.path.dirname(exe.targetName)
        for fullName in self.filesCopied:
            path, name = os.path.split(os.path.normcase(fullName))
            if name.startswith("msvcr") and name.endswith(".dll"):
                msvcRuntimeDll = name
                for otherName in [name.replace("r", c) for c in "mp"]:
                    sourceName = os.path.join(self.msvcRuntimeDir, otherName)
                    if not os.path.exists(sourceName):
                        continue
                    targetName = os.path.join(targetDir, otherName)
                    self._CopyFile(sourceName, targetName)
                break
        if msvcRuntimeDll is not None and msvcRuntimeDll == "msvcr90.dll":
            if struct.calcsize("P") == 4:
                arch = "x86"
            else:
                arch = "amd64"
            manifest = MSVCR_MANIFEST_TEMPLATE.strip().replace("{PROC_ARCH}",
                    arch)
            fileName = os.path.join(targetDir, "Microsoft.VC90.CRT.manifest")
            sys.stdout.write("creating %s\n" % fileName)
            open(fileName, "w").write(manifest)

    def _PrintReport(self, fileName, modules):
        sys.stdout.write("writing zip file %s\n\n" % fileName)
        sys.stdout.write("  %-25s %s\n" % ("Name", "File"))
        sys.stdout.write("  %-25s %s\n" % ("----", "----"))
        for module in modules:
            if module.path:
                sys.stdout.write("P")
            else:
                sys.stdout.write("m")
            sys.stdout.write(" %-25s %s\n" % (module.name, module.file or ""))
        sys.stdout.write("\n")

    def _ProcessPathSpecs(self, specs):
        processedSpecs = []
        for spec in specs:
            if not isinstance(spec, (list, tuple)):
                source = target = spec
            elif len(spec) != 2:
                raise ConfigError("path spec must be a list or tuple of "
                        "length two")
            else:
                source, target = spec
            source = os.path.normpath(source)
            if not target:
                dirName, target = os.path.split(source)
            elif os.path.isabs(target):
                raise ConfigError("target path for include file may not be "
                        "an absolute path")
            processedSpecs.append((source, target))
        return processedSpecs

    def _RemoveFile(self, path):
        if os.path.exists(path):
            os.chmod(path, stat.S_IWRITE)
            os.remove(path)

    def _RemoveVersionNumbers(self, libName):
        tweaked = False
        parts = libName.split(".")
        while parts:
            if not parts[-1].isdigit():
                break
            parts.pop(-1)
            tweaked = True
        if tweaked:
            libName = ".".join(parts)
        return libName

    def _ShouldCopyFile(self, path):
        """Return true if the file should be copied to the target machine. This
           is done by checking the binPathIncludes, binPathExcludes,
           binIncludes and binExcludes configuration variables using first the
           full file name, then just the base file name, then the file name
           without any version numbers.
           
           Files are included unless specifically excluded but inclusions take
           precedence over exclusions."""

        # check for C runtime, if desired
        path = os.path.normcase(path)
        dirName, fileName = os.path.split(path)
        if fileName.startswith("msvcr") and fileName.endswith(".dll"):
            self.msvcRuntimeDir = dirName
            return self.includeMSVCR

        # check the full path
        if path in self.binIncludes:
            return True
        if path in self.binExcludes:
            return False

        # check the file name by itself (with any included version numbers)
        if fileName in self.binIncludes:
            return True
        if fileName in self.binExcludes:
            return False

        # check the file name by itself (version numbers removed)
        name = self._RemoveVersionNumbers(fileName)
        if name in self.binIncludes:
            return True
        if name in self.binExcludes:
            return False

        # check the path for inclusion/exclusion
        for path in self.binPathIncludes:
            if dirName.startswith(path):
                return True
        for path in self.binPathExcludes:
            if dirName.startswith(path):
                return False

        return True

    def _VerifyCanAppendToLibrary(self):
        if not self.createLibraryZip:
            raise ConfigError("script cannot be appended to library zip if "
                    "one is not being created")

    def _VerifyConfiguration(self):
        if self.compress is None:
            self.compress = True
        if self.copyDependentFiles is None:
            self.copyDependentFiles = True
        if self.createLibraryZip is None:
            self.createLibraryZip = True
        if self.appendScriptToExe is None:
            self.appendScriptToExe = False
        if self.appendScriptToLibrary is None:
            self.appendScriptToLibrary = \
                    self.createLibraryZip and not self.appendScriptToExe
        if self.targetDir is None:
            self.targetDir = os.path.abspath("dist")
        self._GetInitScriptFileName()
        self._GetBaseFileName()
        if self.path is None:
            self.path = sys.path
        if self.appendScriptToLibrary:
            self._VerifyCanAppendToLibrary()
        for sourceFileName, targetFileName in \
                self.includeFiles + self.zipIncludes:
            if not os.path.exists(sourceFileName):
                raise ConfigError("cannot find file/directory named %s",
                        sourceFileName)
            if os.path.isabs(targetFileName):
                raise ConfigError("target file/directory cannot be absolute")
        for executable in self.executables:
            executable._VerifyConfiguration(self)

    def _WriteModules(self, fileName, initScript, finder, compress,
            copyDependentFiles, scriptModule = None):
        initModule = finder.IncludeFile(initScript, "cx_Freeze__init__")
        if scriptModule is None:
            for module in self.constantsModules:
                module.Create(finder)
            modules = [m for m in finder.modules \
                    if m.name not in self.excludeModules]
        else:
            modules = [initModule, scriptModule]
            self.excludeModules[initModule.name] = None
            self.excludeModules[scriptModule.name] = None
        itemsToSort = [(m.name, m) for m in modules]
        itemsToSort.sort()
        modules = [m for n, m in itemsToSort]
        if not self.silent:
            self._PrintReport(fileName, modules)
        if scriptModule is None:
            finder.ReportMissingModules()
        targetDir = os.path.dirname(fileName)
        self._CreateDirectory(targetDir)
        filesToCopy = []
        if os.path.exists(fileName):
            mode = "a"
        else:
            mode = "w"
        outFile = zipfile.PyZipFile(fileName, mode, zipfile.ZIP_DEFLATED)
        for module in modules:
            if module.code is None and module.file is not None:
                fileName = os.path.basename(module.file)
                baseFileName, ext = os.path.splitext(fileName)
                if baseFileName != module.name and module.name != "zlib":
                    if "." in module.name:
                        fileName = module.name + ext
                    generatedFileName = "ExtensionLoader_%s.py" % \
                            module.name.replace(".", "_")
                    module.code = compile(EXTENSION_LOADER_SOURCE % fileName,
                            generatedFileName, "exec")
                target = os.path.join(targetDir, fileName)
                filesToCopy.append((module, target))
            if module.code is None:
                continue
            fileName = "/".join(module.name.split("."))
            if module.path:
                fileName += "/__init__"
            if module.file is not None and os.path.exists(module.file):
                mtime = os.stat(module.file).st_mtime
            else:
                mtime = time.time()
            zipTime = time.localtime(mtime)[:6]
            # starting with Python 3.3 the pyc file format contains the source
            # size; it is not actually used for anything except determining if
            # the file is up to date so we can safely set this value to zero
            if sys.version_info[:2] < (3, 3):
                header = imp.get_magic() + struct.pack("<i", int(mtime))
            else:
                header = imp.get_magic() + struct.pack("<ii", int(mtime), 0)
            data = header + marshal.dumps(module.code)
            zinfo = zipfile.ZipInfo(fileName + ".pyc", zipTime)
            if compress:
                zinfo.compress_type = zipfile.ZIP_DEFLATED
            outFile.writestr(zinfo, data)

        for sourceFileName, targetFileName in self.zipIncludes:
            outFile.write(sourceFileName, targetFileName)

        outFile.close()

        origPath = os.environ["PATH"]
        for module, target in filesToCopy:
            try:
                if module.parent is not None:
                    path = os.pathsep.join([origPath] + module.parent.path)
                    os.environ["PATH"] = path
                self._CopyFile(module.file, target, copyDependentFiles)
            finally:
                os.environ["PATH"] = origPath

    def Freeze(self):
        self.finder = None
        self.excludeModules = {}
        self.dependentFiles = {}
        self.filesCopied = {}
        self.linkerWarnings = {}
        self.msvcRuntimeDir = None
        import cx_Freeze.util
        cx_Freeze.util.SetOptimizeFlag(self.optimizeFlag)
        if self.createLibraryZip:
            self.finder = self._GetModuleFinder()
        for executable in self.executables:
            self._FreezeExecutable(executable)
        if self.createLibraryZip:
            fileName = os.path.join(self.targetDir, "library.zip")
            self._RemoveFile(fileName)
            self._WriteModules(fileName, self.initScript, self.finder,
                    self.compress, self.copyDependentFiles)
        for sourceFileName, targetFileName in self.includeFiles:
            if os.path.isdir(sourceFileName):
                for path, dirNames, fileNames in os.walk(sourceFileName):
                    shortPath = path[len(sourceFileName) + 1:]
                    if ".svn" in dirNames:
                        dirNames.remove(".svn")
                    if "CVS" in dirNames:
                        dirNames.remove("CVS")
                    fullTargetDir = os.path.join(self.targetDir,
                            targetFileName, shortPath)
                    self._CreateDirectory(fullTargetDir)
                    for fileName in fileNames:
                        fullSourceName = os.path.join(path, fileName)
                        fullTargetName = os.path.join(fullTargetDir, fileName)
                        self._CopyFile(fullSourceName, fullTargetName,
                                copyDependentFiles = False)
            else:
                fullName = os.path.join(self.targetDir, targetFileName)
                self._CopyFile(sourceFileName, fullName,
                        copyDependentFiles = False)


class ConfigError(Exception):

    def __init__(self, format, *args):
        self.what = format % args

    def __str__(self):
        return self.what


class Executable(object):

    def __init__(self, script, initScript = None, base = None, path = None,
            targetDir = None, targetName = None, includes = None,
            excludes = None, packages = None, replacePaths = None,
            compress = None, copyDependentFiles = None,
            appendScriptToExe = None, appendScriptToLibrary = None,
            icon = None, namespacePackages = None, shortcutName = None,
            shortcutDir = None):
        self.script = script
        self.initScript = initScript
        self.base = base
        self.path = path
        self.targetDir = targetDir
        self.targetName = targetName
        self.includes = includes
        self.excludes = excludes
        self.packages = packages
        self.namespacePackages = namespacePackages
        self.replacePaths = replacePaths
        self.compress = compress
        self.copyDependentFiles = copyDependentFiles
        self.appendScriptToExe = appendScriptToExe
        self.appendScriptToLibrary = appendScriptToLibrary
        self.icon = icon
        self.shortcutName = shortcutName
        self.shortcutDir = shortcutDir

    def __repr__(self):
        return "<Executable script=%s>" % self.script

    def _VerifyConfiguration(self, freezer):
        if self.path is None:
            self.path = freezer.path
        if self.targetDir is None:
            self.targetDir = freezer.targetDir
        if self.includes is None:
            self.includes = freezer.includes
        if self.excludes is None:
            self.excludes = freezer.excludes
        if self.packages is None:
            self.packages = freezer.packages
        if self.namespacePackages is None:
            self.namespacePackages = freezer.namespacePackages
        if self.replacePaths is None:
            self.replacePaths = freezer.replacePaths
        if self.compress is None:
            self.compress = freezer.compress
        if self.copyDependentFiles is None:
            self.copyDependentFiles = freezer.copyDependentFiles
        if self.appendScriptToExe is None:
            self.appendScriptToExe = freezer.appendScriptToExe
        if self.appendScriptToLibrary is None:
            self.appendScriptToLibrary = freezer.appendScriptToLibrary
        if self.initScript is None:
            self.initScript = freezer.initScript
        else:
            freezer._GetInitScriptFileName(self)
        if self.base is None:
            self.base = freezer.base
        else:
            freezer._GetBaseFileName(self)
        if self.appendScriptToLibrary:
            freezer._VerifyCanAppendToLibrary()
        if self.icon is None:
            self.icon = freezer.icon
        if self.targetName is None:
            name, ext = os.path.splitext(os.path.basename(self.script))
            baseName, ext = os.path.splitext(self.base)
            self.targetName = name + ext
        if self.appendScriptToLibrary:
            name, ext = os.path.splitext(self.targetName)
            self.moduleName = "%s__main__" % os.path.normcase(name)
        else:
            self.moduleName = "__main__"
        self.targetName = os.path.join(self.targetDir, self.targetName)


class ConstantsModule(object):

    def __init__(self, releaseString = None, copyright = None,
            moduleName = "BUILD_CONSTANTS", timeFormat = "%B %d, %Y %H:%M:%S"):
        self.moduleName = moduleName
        self.timeFormat = timeFormat
        self.values = {}
        self.values["BUILD_RELEASE_STRING"] = releaseString
        self.values["BUILD_COPYRIGHT"] = copyright

    def Create(self, finder):
        """Create the module which consists of declaration statements for each
           of the values."""
        today = datetime.datetime.today()
        sourceTimestamp = 0
        for module in finder.modules:
            if module.file is None:
                continue
            if module.inZipFile:
                continue
            if not os.path.exists(module.file):
                raise ConfigError("no file named %s (for module %s)",
                        module.file, module.name)
            timestamp = os.stat(module.file).st_mtime
            sourceTimestamp = max(sourceTimestamp, timestamp)
        sourceTimestamp = datetime.datetime.fromtimestamp(sourceTimestamp)
        self.values["BUILD_TIMESTAMP"] = today.strftime(self.timeFormat)
        self.values["BUILD_HOST"] = socket.gethostname().split(".")[0]
        self.values["SOURCE_TIMESTAMP"] = \
                sourceTimestamp.strftime(self.timeFormat)
        module = finder._AddModule(self.moduleName)
        sourceParts = []
        names = list(self.values.keys())
        names.sort()
        for name in names:
            value = self.values[name]
            sourceParts.append("%s = %r" % (name, value))
        source = "\n".join(sourceParts)
        module.code = compile(source, "%s.py" % self.moduleName, "exec")
        return module


class VersionInfo(object):

    def __init__(self, version, internalName = None, originalFileName = None,
            comments = None, company = None, description = None,
            copyright = None, trademarks = None, product = None, dll = False,
            debug = False, verbose = True):
        parts = version.split(".")
        while len(parts) < 4:
            parts.append("0")
        self.version = ".".join(parts)
        self.internal_name = internalName
        self.original_filename = originalFileName
        self.comments = comments
        self.company = company
        self.description = description
        self.copyright = copyright
        self.trademarks = trademarks
        self.product = product
        self.dll = dll
        self.debug = debug
        self.verbose = verbose
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.