summaryrefslogtreecommitdiffstats
path: root/BaseTools/Source/Python/PackagingTool/MkPkg.py
blob: ec2136811dce224d805f5df3e132874c8c7b7a2e (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
## @file
# Install distribution package.
#
# Copyright (c) 2007, Intel Corporation
# All rights reserved. This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
import os
import os.path
import sys
import glob
import shutil
import traceback
import platform
from optparse import OptionParser
import md5
import time
import uuid

from PackageFile import *
import Common.EdkLogger as EdkLogger
from Common.BuildToolError import *
from Common.Misc import *
from Common.XmlParser import *
from CommonDataClass.DistributionPackageClass import *
from Common.DecClassObjectLight import Dec
from Common.InfClassObjectLight import Inf

from PackageFile import *

# Version and Copyright
VersionNumber = "0.1"
__version__ = "%prog Version " + VersionNumber
__copyright__ = "Copyright (c) 2008, Intel Corporation  All rights reserved."

## Check environment variables
#
#  Check environment variables that must be set for build. Currently they are
#
#   WORKSPACE           The directory all packages/platforms start from
#   EDK_TOOLS_PATH      The directory contains all tools needed by the build
#   PATH                $(EDK_TOOLS_PATH)/Bin/<sys> must be set in PATH
#
#   If any of above environment variable is not set or has error, the build
#   will be broken.
#
def CheckEnvVariable():
    # check WORKSPACE
    if "WORKSPACE" not in os.environ:
        EdkLogger.error("MkPkg", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
                        ExtraData="WORKSPACE")

    WorkspaceDir = os.path.normpath(os.environ["WORKSPACE"])
    if not os.path.exists(WorkspaceDir):
        EdkLogger.error("MkPkg", FILE_NOT_FOUND, "WORKSPACE doesn't exist", ExtraData="%s" % WorkspaceDir)
    elif ' ' in WorkspaceDir:
        EdkLogger.error("MkPkg", FORMAT_NOT_SUPPORTED, "No space is allowed in WORKSPACE path", 
                        ExtraData=WorkspaceDir)
    os.environ["WORKSPACE"] = WorkspaceDir

## Parse command line options
#
# Using standard Python module optparse to parse command line option of this tool.
#
#   @retval Opt   A optparse.Values object containing the parsed options
#   @retval Args  Target of build command
#
def MyOptionParser():
    UsageString = "%prog -m <module_file> -p <package_file> [-o distribution_file] " + \
                   "[-x xml-file-header] [-t tools-directory] [-f misc-files] [-q | -v] [-h]"

    Parser = OptionParser(description=__copyright__,version=__version__,prog="MkPkg",usage=UsageString)

    Parser.add_option("-?", action="help", help="show this help message and exit")

    Parser.add_option("-o", "--output-file", action="store", type="string", dest="DistributionFile",
            help="Specify the distribution file to be created.")

    Parser.add_option("-f", "--misc-files", action="append", type="string", dest="MiscFiles",
            help="Specify any misc files.")

    Parser.add_option("-x", "--xml-file-header", action="store", type=None, dest="TemplateFile",
            help="Specify the xml file which includes header information for creating the distribution file.")

    Parser.add_option("-t", "--tools-directory", action="store", type=None, dest="ToolsDir",
            help="Specify the directory name of tools.")

    Parser.add_option("-m", "--module", action="append", type="string", dest="ModuleFileList",
            help="The inf file of module to be distributed standalone.")

    Parser.add_option("-p", "--package", action="append", type="string", dest="PackageFileList",
            help="The dec file of package to be distributed.")

    Parser.add_option("-q", "--quiet", action="store_const", dest="LogLevel", const=EdkLogger.QUIET,
            help="Disable all messages except FATAL ERRORS.")

    Parser.add_option("-v", "--verbose", action="store_const", dest="LogLevel", const=EdkLogger.VERBOSE,
            help="Turn on verbose output")

    Parser.add_option("-d", "--debug", action="store", type="int", dest="LogLevel",
            help="Enable debug messages at specified level.")

    Parser.set_defaults(LogLevel=EdkLogger.INFO)

    (Opt, Args)=Parser.parse_args()
    # error check
    if not Opt.ModuleFileList and not Opt.PackageFileList:
        EdkLogger.error("MkPkg", OPTION_NOT_SUPPORTED, ExtraData="At least one package file or module file must be specified")
    if Opt.TemplateFile:
        if not os.path.exists(Opt.TemplateFile):
            EdkLogger.error(
                            "\nMkPkg",
                            FILE_NOT_FOUND,
                            "Template file [%s] not found" % Opt.TemplateFile
                            )
    return Opt

## Tool entrance method
#
# This method mainly dispatch specific methods per the command line options.
# If no error found, return zero value so the caller of this tool can know
# if it's executed successfully or not.
#
#   @retval 0     Tool was successful
#   @retval 1     Tool failed
#
def Main():
    EdkLogger.Initialize()
    Options = MyOptionParser()
    try:
        if Options.LogLevel < EdkLogger.DEBUG_9:
            EdkLogger.SetLevel(Options.LogLevel + 1)
        else:
            EdkLogger.SetLevel(Options.LogLevel)

        CheckEnvVariable()
        WorkspaceDir = os.environ["WORKSPACE"]
        
        # Init DistributionFile
        if not Options.DistributionFile:
            Options.DistributionFile = "DistributionPackage.zip"
        
        # Check Tools Dir
        if Options.ToolsDir:
            if not os.path.isdir(os.path.normpath(os.path.join(WorkspaceDir, Options.ToolsDir))):
                EdkLogger.error(
                                "\nMkPkg",
                                FILE_NOT_FOUND,
                                "Tools directory [%s] not found" % Options.ToolsDir
                                )
        
        # Check misc files
        if Options.MiscFiles:
            for Item in Options.MiscFiles:
                FullPath = os.path.normpath(os.path.join(WorkspaceDir, Item))
                if not os.path.isfile(FullPath):
                    EdkLogger.error(
                                    "\nMkPkg",
                                    FILE_NOT_FOUND,
                                    "Misc file [%s] not found" % Item
                                    )
        
        #Check package file existing and valid
        if Options.PackageFileList:
            for Item in Options.PackageFileList:
                (Name, Ext) = os.path.splitext(Item)
                if Ext.upper() != '.DEC':
                    EdkLogger.error(
                    "\nMkPkg",
                    OPTION_VALUE_INVALID,
                    "[%s] is not a valid package name" % Item
                    )
                Path = os.path.normpath(os.path.join(WorkspaceDir, Item))
                if not os.path.exists(Path):
                    EdkLogger.error(
                        "\nMkPkg",
                        FILE_NOT_FOUND,
                        "[%s] not found" % Item
                        )
        #Check module file existing and valid
        if Options.ModuleFileList:
            for Item in Options.ModuleFileList:
                (Name, Ext) = os.path.splitext(Item)
                if Ext.upper() != '.INF':
                    EdkLogger.error(
                    "\nMkPkg",
                    OPTION_VALUE_INVALID,
                    "[%s] is not a valid module name" % Item
                    )
                Path = os.path.normpath(os.path.join(WorkspaceDir, Item))
                if not os.path.exists(Path):
                    EdkLogger.error(
                        "\nMkPkg",
                        FILE_NOT_FOUND,
                        "[%s] not found" % Item
                        )

        ContentFile = PackageFile("content.zip", "w")
        DistPkg = DistributionPackageClass()
        DistPkg.GetDistributionPackage(WorkspaceDir, Options.PackageFileList, Options.ModuleFileList)
        DistPkgXml = DistributionPackageXml()
        for Item in DistPkg.PackageSurfaceArea:
            ContentFile.Pack(os.path.dirname(os.path.normpath(os.path.join(WorkspaceDir,Item[2]))))
        for Item in DistPkg.ModuleSurfaceArea:
            ContentFile.Pack(os.path.dirname(os.path.normpath(os.path.join(WorkspaceDir,Item[2]))))

        # Add tools files and information
        if Options.ToolsDir:
            ToolsFiles = MiscFileClass()
            ToolsRoot = os.path.normpath(os.path.join(WorkspaceDir, Options.ToolsDir))
            ContentFile.Pack(ToolsRoot)
            ToolsFileList = GetFiles(ToolsRoot, ['CVS', '.svn'])
            for Item in ToolsFileList:
                OriPath = Item[len(WorkspaceDir)+1:]
                FileObj = FileClass()
                FileObj.Filename = OriPath
                (Name, Ext) = os.path.splitext(OriPath)
                if Ext.upper() in ['EXE', 'COM', 'EFI']:
                    FileObj.Executable = 'True'
                ToolsFiles.Files.append(FileObj)
            DistPkg.Tools = ToolsFiles
        
        # Add misc files and information
        if Options.MiscFiles:
            MiscFiles = MiscFileClass()
            for Item in Options.MiscFiles:
                ContentFile.PackFile(Item)
                FileObj = FileClass()
                FileObj.Filename = Item
                (Name, Ext) = os.path.splitext(Item)
                if Ext.upper() in ['EXE', 'COM', 'EFI']:
                    FileObj.Executable = 'True'
                MiscFiles.Files.append(FileObj)
            DistPkg.MiscellaneousFiles = MiscFiles
                
        print "Compressing Distribution Package File ..."
        ContentFile.Close()
        
        # Add temp distribution header
        if Options.TemplateFile:
            TempXML = DistributionPackageXml()
            DistPkg.Header = TempXML.FromXml(Options.TemplateFile).Header
        # Add init dp information
        else:
            DistPkg.Header.Name = 'Distribution Package'
            DistPkg.Header.Guid = str(uuid.uuid4())
            DistPkg.Header.Version = '1.0'
        
        # Add Md5Sigature
        Md5Sigature = md5.new(open(str(ContentFile)).read())
        DistPkg.Header.Signature = Md5Sigature.hexdigest()
        # Add current Date
        DistPkg.Header.Date = str(time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()))

        # Finish final dp file
        DistPkgFile = PackageFile(Options.DistributionFile, "w")
        DistPkgFile.PackFile(str(ContentFile))
        DistPkgFile.PackData(DistPkgXml.ToXml(DistPkg), "dist.pkg")
        DistPkgFile.Close()
        print "DONE"

    except FatalError, X:
        if Options and Options.LogLevel < EdkLogger.DEBUG_9:
            EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
        ReturnCode = X.args[0]
    except KeyboardInterrupt:
        ReturnCode = ABORT_ERROR
        if Options and Options.LogLevel < EdkLogger.DEBUG_9:
            EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
    except:
        EdkLogger.error(
                    "\nMkPkg",
                    CODE_ERROR,
                    "Unknown fatal error when creating [%s]" % Options.DistributionFile,
                    ExtraData="\n(Please send email to edk2-buildtools-devel@lists.sourceforge.net for help, attaching following call stack trace!)\n",
                    RaiseError=False
                    )
        EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
        ReturnCode = CODE_ERROR
    finally:
        Progressor.Abort()

if __name__ == '__main__':
    sys.exit(Main())