]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/PackagingTool/RmPkg.py
2 # Install distribution package.
4 # Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 from optparse
import OptionParser
23 import Common
.EdkLogger
as EdkLogger
24 from Common
.BuildToolError
import *
25 from Common
.Misc
import *
26 from Common
.XmlParser
import *
29 from DependencyRules
import *
31 # Version and Copyright
33 __version__
= "%prog Version " + VersionNumber
34 __copyright__
= "Copyright (c) 2008, Intel Corporation All rights reserved."
36 ## Check environment variables
38 # Check environment variables that must be set for build. Currently they are
40 # WORKSPACE The directory all packages/platforms start from
41 # EDK_TOOLS_PATH The directory contains all tools needed by the build
42 # PATH $(EDK_TOOLS_PATH)/Bin/<sys> must be set in PATH
44 # If any of above environment variable is not set or has error, the build
47 def CheckEnvVariable():
49 if "WORKSPACE" not in os
.environ
:
50 EdkLogger
.error("RmPkg", ATTRIBUTE_NOT_AVAILABLE
, "Environment variable not found",
51 ExtraData
="WORKSPACE")
53 WorkspaceDir
= os
.path
.normpath(os
.environ
["WORKSPACE"])
54 if not os
.path
.exists(WorkspaceDir
):
55 EdkLogger
.error("RmPkg", FILE_NOT_FOUND
, "WORKSPACE doesn't exist", ExtraData
="%s" % WorkspaceDir
)
56 elif ' ' in WorkspaceDir
:
57 EdkLogger
.error("RmPkg", FORMAT_NOT_SUPPORTED
, "No space is allowed in WORKSPACE path",
58 ExtraData
=WorkspaceDir
)
59 os
.environ
["WORKSPACE"] = WorkspaceDir
61 ## Parse command line options
63 # Using standard Python module optparse to parse command line option of this tool.
65 # @retval Opt A optparse.Values object containing the parsed options
66 # @retval Args Target of build command
69 UsageString
= "%prog -g <guid> -n <version> [-y] [-q | -v] [-h]"
71 Parser
= OptionParser(description
=__copyright__
,version
=__version__
,prog
="RmPkg",usage
=UsageString
)
73 Parser
.add_option("-?", action
="help", help="show this help message and exit")
75 # Parser.add_option("-f", "--force", action="store_true", type=None, dest="ForceRemove",
76 # help="Force creation - overwrite existing one.")
78 Parser
.add_option("-y", "--yes", action
="store_true", dest
="Yes",
79 help="Not asking for confirmation when deleting files.")
81 Parser
.add_option("-n", "--package-version", action
="store", type="string", dest
="PackageVersion",
82 help="The version of distribution package to be removed.")
84 Parser
.add_option("-g", "--package-guid", action
="store", type="string", dest
="PackageGuid",
85 help="The GUID of distribution package to be removed.")
87 Parser
.add_option("-q", "--quiet", action
="store_const", dest
="LogLevel", const
=EdkLogger
.QUIET
,
88 help="Disable all messages except FATAL ERRORS.")
90 Parser
.add_option("-v", "--verbose", action
="store_const", dest
="LogLevel", const
=EdkLogger
.VERBOSE
,
91 help="Turn on verbose output")
93 Parser
.add_option("-d", "--debug", action
="store", type="int", dest
="LogLevel",
94 help="Enable debug messages at specified level.")
96 Parser
.set_defaults(LogLevel
=EdkLogger
.INFO
)
98 (Opt
, Args
)=Parser
.parse_args()
102 ## Remove all empty dirs under the path
103 def RemoveEmptyDirs(Path
):
104 # Remove all sub dirs
105 for Root
, Dirs
, Files
in os
.walk(Path
):
107 FullPath
= os
.path
.normpath(os
.path
.join(Root
, Dir
))
108 if os
.path
.isdir(FullPath
):
109 if os
.listdir(FullPath
) == []:
112 RemoveEmptyDirs(FullPath
)
114 if os
.path
.isdir(Path
) and os
.listdir(Path
) == []:
118 ## Tool entrance method
120 # This method mainly dispatch specific methods per the command line options.
121 # If no error found, return zero value so the caller of this tool can know
122 # if it's executed successfully or not.
124 # @retval 0 Tool was successful
125 # @retval 1 Tool failed
128 EdkLogger
.Initialize()
129 Options
= MyOptionParser()
131 if not Options
.PackageGuid
and not Options
.PackageVersion
:
132 EdkLogger
.error("RmPkg", OPTION_MISSING
, ExtraData
="The GUID and Version of distribution package must be specified")
134 if Options
.LogLevel
< EdkLogger
.DEBUG_9
:
135 EdkLogger
.SetLevel(Options
.LogLevel
+ 1)
137 EdkLogger
.SetLevel(Options
.LogLevel
)
140 WorkspaceDir
= os
.environ
["WORKSPACE"]
142 # Prepare check dependency
143 Db
= IpiDatabase(os
.path
.normpath(os
.path
.join(WorkspaceDir
, "Conf/DistributionPackageDatabase.db")))
145 Dep
= DependencyRules(Db
)
147 Guid
= Options
.PackageGuid
148 Version
= Options
.PackageVersion
151 if not Dep
.CheckDpExists(Guid
, Version
):
152 EdkLogger
.error("RmPkg", UNKNOWN_ERROR
, "This distribution package are not installed!")
155 if not Dep
.CheckDpDepexForRemove(Guid
, Version
):
156 print "Some packages/modules are depending on this distribution package, do you really want to remove it?"
157 print "Press Y to delete all files or press other keys to quit:"
158 Input
= Input
= sys
.stdin
.readline()
159 Input
= Input
.replace('\r', '').replace('\n', '')
160 if Input
.upper() != 'Y':
161 EdkLogger
.error("RmPkg", UNKNOWN_ERROR
, "User interrupt")
165 print "All files of the distribution package will be removed, do you want to continue?"
166 print "Press Y to remove all files or press other keys to quit:"
167 Input
= Input
= sys
.stdin
.readline()
168 Input
= Input
.replace('\r', '').replace('\n', '')
169 if Input
.upper() != 'Y':
170 EdkLogger
.error("RmPkg", UNKNOWN_ERROR
, "User interrupt")
174 for Item
in Db
.GetDpFileList(Guid
, Version
):
175 if os
.path
.isfile(Item
):
176 print "Removing file [%s] ..." % Item
179 MissingFileList
.append(Item
)
181 # Remove all empty dirs of package
182 for Item
in Db
.GetPackageListFromDp(Guid
, Version
):
183 Dir
= os
.path
.dirname(Item
[2])
186 # Remove all empty dirs of module
187 for Item
in Db
.GetStandaloneModuleInstallPathListFromDp(Guid
, Version
):
188 Dir
= os
.path
.dirname(Item
)
192 EdkLogger
.quiet("Update Distribution Package Database ...")
193 Db
.RemoveDpObj(Guid
, Version
)
194 EdkLogger
.quiet("DONE")
196 except FatalError
, X
:
197 if Options
and Options
.LogLevel
< EdkLogger
.DEBUG_9
:
198 EdkLogger
.quiet("(Python %s on %s) " % (platform
.python_version(), sys
.platform
) + traceback
.format_exc())
199 ReturnCode
= X
.args
[0]
200 except KeyboardInterrupt:
201 ReturnCode
= ABORT_ERROR
202 if Options
and Options
.LogLevel
< EdkLogger
.DEBUG_9
:
203 EdkLogger
.quiet("(Python %s on %s) " % (platform
.python_version(), sys
.platform
) + traceback
.format_exc())
208 "Unknown fatal error when removing package",
209 ExtraData
="\n(Please send email to edk2-buildtools-devel@lists.sourceforge.net for help, attaching following call stack trace!)\n",
212 EdkLogger
.quiet("(Python %s on %s) " % (platform
.python_version(), sys
.platform
) + traceback
.format_exc())
213 ReturnCode
= CODE_ERROR
217 if __name__
== '__main__':