summaryrefslogtreecommitdiff
path: root/BaseTools/Source/Python/PackagingTool/RmPkg.py
blob: e7eedd0776cb3b2906df721bc5944afe9478f23f (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
## @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 sys
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 IpiDb import *
from DependencyRules 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("RmPkg", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
                        ExtraData="WORKSPACE")

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

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

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

#    Parser.add_option("-f", "--force", action="store_true", type=None, dest="ForceRemove",
#            help="Force creation - overwrite existing one.")

    Parser.add_option("-y", "--yes", action="store_true", dest="Yes",
            help="Not asking for confirmation when deleting files.")

    Parser.add_option("-n", "--package-version", action="store", type="string", dest="PackageVersion",
            help="The version of distribution package to be removed.")

    Parser.add_option("-g", "--package-guid", action="store", type="string", dest="PackageGuid",
            help="The GUID of distribution package to be removed.")

    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

## Remove all empty dirs under the path
def RemoveEmptyDirs(Path):
    # Remove all sub dirs
    for Root, Dirs, Files in os.walk(Path):
        for Dir in Dirs:
            FullPath = os.path.normpath(os.path.join(Root, Dir))
            if os.path.isdir(FullPath):
                if os.listdir(FullPath) == []:
                    os.rmdir(FullPath)
                else:
                    RemoveEmptyDirs(FullPath)
    # Remove itself
    if os.path.isdir(Path) and os.listdir(Path) == []:
        os.rmdir(Path)
        

## 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 not Options.PackageGuid and not Options.PackageVersion:
            EdkLogger.error("RmPkg", OPTION_MISSING, ExtraData="The GUID and Version of distribution package must be specified")
        
        if Options.LogLevel < EdkLogger.DEBUG_9:
            EdkLogger.SetLevel(Options.LogLevel + 1)
        else:
            EdkLogger.SetLevel(Options.LogLevel)

        CheckEnvVariable()
        WorkspaceDir = os.environ["WORKSPACE"]

        # Prepare check dependency
        Db = IpiDatabase(os.path.normpath(os.path.join(WorkspaceDir, "Conf/DistributionPackageDatabase.db")))
        Db.InitDatabase()
        Dep = DependencyRules(Db)
        
        Guid = Options.PackageGuid
        Version = Options.PackageVersion
        
        # Check Dp existing
        if not Dep.CheckDpExists(Guid, Version):
            EdkLogger.error("RmPkg", UNKNOWN_ERROR, "This distribution package are not installed!")
        
        # Check Dp depex
        if not Dep.CheckDpDepexForRemove(Guid, Version):
            print "Some packages/modules are depending on this distribution package, do you really want to remove it?"
            print "Press Y to delete all files or press other keys to quit:"
            Input = Input = sys.stdin.readline()
            Input = Input.replace('\r', '').replace('\n', '')
            if Input.upper() != 'Y':
                EdkLogger.error("RmPkg", UNKNOWN_ERROR, "User interrupt")

        # Remove all files
        if not Options.Yes:
            print "All files of the distribution package will be removed, do you want to continue?"
            print "Press Y to remove all files or press other keys to quit:"
            Input = Input = sys.stdin.readline()
            Input = Input.replace('\r', '').replace('\n', '')
            if Input.upper() != 'Y':
                EdkLogger.error("RmPkg", UNKNOWN_ERROR, "User interrupt")
        
        # Remove all files
        MissingFileList = []
        for Item in Db.GetDpFileList(Guid, Version):
            if os.path.isfile(Item):
                print "Removing file [%s] ..." % Item
                os.remove(Item)
            else:
                MissingFileList.append(Item)
        
        # Remove all empty dirs of package
        for Item in Db.GetPackageListFromDp(Guid, Version):
            Dir = os.path.dirname(Item[2])
            RemoveEmptyDirs(Dir)

        # Remove all empty dirs of module
        for Item in Db.GetStandaloneModuleInstallPathListFromDp(Guid, Version):
            Dir = os.path.dirname(Item)
            RemoveEmptyDirs(Dir)
        
        # update database
        EdkLogger.quiet("Update Distribution Package Database ...")
        Db.RemoveDpObj(Guid, Version)
        EdkLogger.quiet("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(
                    "\nRmPkg",
                    CODE_ERROR,
                    "Unknown fatal error when removing package",
                    ExtraData="\n(Please send email to dev@buildtools.tianocore.org 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())