summaryrefslogtreecommitdiffstats
path: root/BaseTools/Source/Python/PackagingTool/InstallPkg.py
blob: 8e4d45da21c2a8f04b5faa34e2b2f0b3953a6f3c (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
## @file
# Install distribution package.
#
# Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
# 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 sys
import glob
import shutil
import traceback
import platform
from optparse import OptionParser

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

from PackageFile import *
from IpiDb import *
from DependencyRules import *
import md5

# 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("InstallPkg", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
                        ExtraData="WORKSPACE")

    WorkspaceDir = os.path.normpath(os.environ["WORKSPACE"])
    if not os.path.exists(WorkspaceDir):
        EdkLogger.error("InstallPkg", FILE_NOT_FOUND, "WORKSPACE doesn't exist", ExtraData="%s" % WorkspaceDir)
    elif ' ' in WorkspaceDir:
        EdkLogger.error("InstallPkg", 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 -i <distribution_package> [-t] [-f] [-q | -v] [-h]"

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

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

    Parser.add_option("-i", "--distribution-package", action="store", type="string", dest="PackageFile",
            help="The distribution package to be installed")

    Parser.add_option("-t", "--install-tools", action="store_true", type=None, dest="Tools",
            help="Specify it to install tools or ignore the tools of the distribution package.")
    
    Parser.add_option("-f", "--misc-files", action="store_true", type=None, dest="MiscFiles",
            help="Specify it to install misc file or ignore the misc files of the distribution package.")

    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()

    return Opt

def InstallNewPackage(WorkspaceDir, Path):
    FullPath = os.path.normpath(os.path.join(WorkspaceDir, Path))
    if os.path.exists(FullPath):
        print "Directory [%s] already exists, please select another location, press [Enter] with no input to quit:" %Path
        Input = sys.stdin.readline()
        Input = Input.replace('\r', '').replace('\n', '')
        if Input == '':
            EdkLogger.error("InstallPkg", UNKNOWN_ERROR, "User interrupt")
        Input = Input.replace('\r', '').replace('\n', '')
        return InstallNewPackage(WorkspaceDir, Input)
    else:
        return Path

def InstallNewFile(WorkspaceDir, File):
    FullPath = os.path.normpath(os.path.join(WorkspaceDir, File))
    if os.path.exists(FullPath):
        print "File [%s] already exists, please select another path, press [Enter] with no input to quit:" %File
        Input = sys.stdin.readline()
        Input = Input.replace('\r', '').replace('\n', '')
        if Input == '':
            EdkLogger.error("InstallPkg", UNKNOWN_ERROR, "User interrupt")
        Input = Input.replace('\r', '').replace('\n', '')
        return InstallNewFile(WorkspaceDir, Input)
    else:
        return File

## 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 = None
    DistFileName = 'dist.pkg'
    ContentFileName = 'content.zip'
    DistFile, ContentZipFile, UnpackDir = None, None, None
    
    Options = MyOptionParser()
    try:
        if Options.LogLevel < EdkLogger.DEBUG_9:
            EdkLogger.SetLevel(Options.LogLevel + 1)
        else:
            EdkLogger.SetLevel(Options.LogLevel)

        CheckEnvVariable()
        WorkspaceDir = os.environ["WORKSPACE"]
        if not Options.PackageFile:
            EdkLogger.error("InstallPkg", OPTION_NOT_SUPPORTED, ExtraData="Must specify one distribution package")

        # unzip dist.pkg file
        EdkLogger.quiet("Unzipping and parsing distribution package XML file ... ")
        DistFile = PackageFile(Options.PackageFile)
        UnpackDir = os.path.normpath(os.path.join(WorkspaceDir, ".tmp"))
        DistPkgFile = DistFile.UnpackFile(DistFileName, os.path.normpath(os.path.join(UnpackDir, DistFileName)))
        if not DistPkgFile:
            EdkLogger.error("InstallPkg", FILE_NOT_FOUND, "File [%s] is broken in distribution package" %DistFileName)
        
        # Generate distpkg
        DistPkgObj = DistributionPackageXml()
        DistPkg = DistPkgObj.FromXml(DistPkgFile)

        # prepare check dependency
        Db = IpiDatabase(os.path.normpath(os.path.join(WorkspaceDir, "Conf/DistributionPackageDatabase.db")))
        Db.InitDatabase()
        Dep = DependencyRules(Db)
        
        # Check distribution package exist
        if Dep.CheckDpExists(DistPkg.Header.Guid, DistPkg.Header.Version):
            EdkLogger.error("InstallPkg", UNKNOWN_ERROR, "This distribution package has been installed", ExtraData=DistPkg.Header.Name)
        
        # unzip contents.zip file
        ContentFile = DistFile.UnpackFile(ContentFileName, os.path.normpath(os.path.join(UnpackDir, ContentFileName)))
        ContentZipFile = PackageFile(ContentFile)
        if not ContentFile:
            EdkLogger.error("InstallPkg", FILE_NOT_FOUND, "File [%s] is broken in distribution package" %ContentFileName)
        
        # verify MD5 signature
        Md5Sigature = md5.new(open(ContentFile).read())
        if DistPkg.Header.Signature != Md5Sigature.hexdigest():
            EdkLogger.error("InstallPkg", FILE_CHECKSUM_FAILURE, ExtraData=ContentFile)
        
        # Check package exist and install
        for Guid,Version,Path in DistPkg.PackageSurfaceArea:
            PackagePath = os.path.dirname(Path)
            NewPackagePath = PackagePath
            Package = DistPkg.PackageSurfaceArea[Guid,Version,Path]
            EdkLogger.info("Installing package ... %s" % Package.PackageHeader.Name)
            if Dep.CheckPackageExists(Guid, Version):
                EdkLogger.quiet("Package [%s] has been installed" %Path)
            NewPackagePath = InstallNewPackage(WorkspaceDir, PackagePath)
            Package.FileList = []
            for Item in Package.MiscFiles.Files:
                FromFile = os.path.join(PackagePath, Item.Filename)
                ToFile = os.path.normpath(os.path.join(WorkspaceDir, NewPackagePath, Item.Filename))
                ContentZipFile.UnpackFile(FromFile, ToFile)
                Package.FileList.append(ToFile)
            
            # Update package
            Package.PackageHeader.CombinePath = Package.PackageHeader.CombinePath.replace(PackagePath, NewPackagePath, 1)
            # Update modules of package
            Module = None
            for ModuleGuid, ModuleVersion, ModulePath in Package.Modules:
                Module = Package.Modules[ModuleGuid, ModuleVersion, ModulePath]
                NewModulePath = ModulePath.replace(PackagePath, NewPackagePath, 1)
                del Package.Modules[ModuleGuid, ModuleVersion, ModulePath]
                Package.Modules[ModuleGuid, ModuleVersion, NewModulePath] = Module
            del DistPkg.PackageSurfaceArea[Guid,Version,Path]
            DistPkg.PackageSurfaceArea[Guid,Version,Package.PackageHeader.CombinePath] = Package

#            SaveFileOnChange(os.path.join(Options.InstallDir, ModulePath, Module.Header.Name, ".inf"), Inf.ModuleToInf(Module), False)
#            EdkLogger.info("Installing package ... %s" % Package.Header.Name)
#            shutil.copytree(os.path.join(ContentFileDir, Path), Options.InstallDir)
#            SaveFileOnChange(os.path.join(Options.InstallDir, Path, Package.Header.Name, ".dec"), Dec.PackageToDec(Package), False)

        # Check module exist and install
        Module = None
        for Guid,Version,Path in DistPkg.ModuleSurfaceArea:
            ModulePath = os.path.dirname(Path)
            NewModulePath = ModulePath
            Module = DistPkg.ModuleSurfaceArea[Guid,Version,Path]
            EdkLogger.info("Installing module ... %s" % Module.ModuleHeader.Name)
            if Dep.CheckModuleExists(Guid, Version):
                EdkLogger.quiet("Module [%s] has been installed" %Path)
            NewModulePath = InstallNewPackage(WorkspaceDir, ModulePath)
            Module.FileList = []
            for Item in Module.MiscFiles.Files:
                ModulePath = ModulePath[os.path.normpath(ModulePath).rfind(os.path.normpath('/'))+1:]
                FromFile = os.path.join(ModulePath, Item.Filename)
                ToFile = os.path.normpath(os.path.join(WorkspaceDir, NewModulePath, Item.Filename))
                ContentZipFile.UnpackFile(FromFile, ToFile)
                Module.FileList.append(ToFile)
            
#            EdkLogger.info("Installing module ... %s" % Module.Header.Name)
#            shutil.copytree(os.path.join(ContentFileDir, Path), Options.InstallDir)
#            SaveFileOnChange(os.path.join(Options.InstallDir, Path, Module.Header.Name, ".inf"), Inf.ModuleToInf(Module), False)
            
            # Update module
            Module.ModuleHeader.CombinePath = Module.ModuleHeader.CombinePath.replace(os.path.dirname(Path), NewModulePath, 1)
            del DistPkg.ModuleSurfaceArea[Guid,Version,Path]
            DistPkg.ModuleSurfaceArea[Guid,Version,Module.ModuleHeader.CombinePath] = Module
#            
#        
#        for Guid,Version,Path in DistPkg.PackageSurfaceArea:
#            print Guid,Version,Path
#            for item in DistPkg.PackageSurfaceArea[Guid,Version,Path].FileList:
#                print item
#        for Guid,Version,Path in DistPkg.ModuleSurfaceArea:
#            print Guid,Version,Path
#            for item in DistPkg.ModuleSurfaceArea[Guid,Version,Path].FileList:
#                print item

        if Options.Tools:
            EdkLogger.info("Installing tools ... ")
            for File in DistPkg.Tools.Files:
                FromFile = File.Filename
                ToFile = InstallNewFile(WorkspaceDir, FromFile)
                ContentZipFile.UnpackFile(FromFile, ToFile)
        if Options.MiscFiles:
            EdkLogger.info("Installing misc files ... ")
            for File in DistPkg.MiscellaneousFiles.Files:
                FromFile = File.Filename
                ToFile = InstallNewFile(WorkspaceDir, FromFile)
                ContentZipFile.UnpackFile(FromFile, ToFile)

        # update database
        EdkLogger.quiet("Update Distribution Package Database ...")
        Db.AddDPObject(DistPkg)

    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(
                    "\nInstallPkg",
                    CODE_ERROR,
                    "Unknown fatal error when installing [%s]" % Options.PackageFile,
                    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:
        EdkLogger.quiet("Removing temp files ... ")
        if DistFile:
            DistFile.Close()
        if ContentZipFile:
            ContentZipFile.Close()
        if UnpackDir:
            shutil.rmtree(UnpackDir)
        
        EdkLogger.quiet("DONE")
        Progressor.Abort()

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