Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / UPT.py
CommitLineData
4234283c
LG
1## @file\r
2#\r
3# This file is the main entry for UPT \r
4#\r
5# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
6#\r
7# This program and the accompanying materials are licensed and made available \r
8# under the terms and conditions of the BSD License which accompanies this \r
9# distribution. The full text of the license may be found at \r
10# http://opensource.org/licenses/bsd-license.php\r
11#\r
12# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14#\r
15\r
16'''\r
17UPT\r
18'''\r
19\r
20## import modules\r
21#\r
22import sys\r
23import os.path\r
24from os import environ\r
25from sys import platform\r
26from optparse import OptionParser\r
27from traceback import format_exc\r
28from platform import python_version\r
29\r
30from Logger import StringTable as ST\r
31import Logger.Log as Logger\r
32from Logger.StringTable import MSG_VERSION\r
33from Logger.StringTable import MSG_DESCRIPTION\r
34from Logger.StringTable import MSG_USAGE\r
35from Logger.ToolError import FILE_NOT_FOUND\r
36from Logger.ToolError import OPTION_MISSING\r
37from Logger.ToolError import FILE_TYPE_MISMATCH\r
38from Logger.ToolError import OPTION_CONFLICT\r
39from Logger.ToolError import FatalError\r
40import MkPkg\r
41import InstallPkg\r
42import RmPkg\r
43from Library.Misc import CheckEnvVariable\r
44from Library import GlobalData\r
45from Core.IpiDb import IpiDatabase\r
46\r
47##\r
48# Version and Copyright\r
49#\r
50#VersionNumber = "1.0"\r
51#__version__ = "Revision " + VersionNumber\r
52#__copyright__ = "Copyright (c) 2011 Intel Corporation All Rights Reserved."\r
53\r
54## CheckConflictOption\r
55#\r
56# CheckConflictOption\r
57#\r
58def CheckConflictOption(Opt):\r
59 if (Opt.PackFileToCreate and Opt.PackFileToInstall and Opt.PackFileToRemove):\r
60 Logger.Error("UPT", OPTION_CONFLICT, ExtraData=ST.ERR_REQUIRE_I_C_R_OPTION)\r
61 elif Opt.PackFileToCreate and Opt.PackFileToInstall:\r
62 Logger.Error("UPT", OPTION_CONFLICT, ExtraData=ST.ERR_I_C_EXCLUSIVE)\r
63 elif Opt.PackFileToInstall and Opt.PackFileToRemove:\r
64 Logger.Error("UPT", OPTION_CONFLICT, ExtraData=ST.ERR_I_R_EXCLUSIVE)\r
65 elif Opt.PackFileToCreate and Opt.PackFileToRemove:\r
66 Logger.Error("UPT", OPTION_CONFLICT, ExtraData=ST.ERR_C_R_EXCLUSIVE)\r
67\r
68## SetLogLevel\r
69#\r
70def SetLogLevel(Opt):\r
71 if Opt.opt_verbose:\r
72 Logger.SetLevel(Logger.VERBOSE)\r
73 elif Opt.opt_quiet:\r
74 Logger.SetLevel(Logger.QUIET + 1)\r
75 elif Opt.debug_level != None:\r
76 if Opt.debug_level < 0 or Opt.debug_level > 9:\r
77 Logger.Warn("UPT", ST.ERR_DEBUG_LEVEL)\r
78 Logger.SetLevel(Logger.INFO)\r
79 else:\r
80 Logger.SetLevel(Opt.debug_level + 1)\r
81 elif Opt.opt_slient:\r
82 Logger.SetLevel(Logger.SILENT)\r
83 else:\r
84 Logger.SetLevel(Logger.INFO)\r
85\r
86## Main\r
87#\r
88# Main\r
89#\r
90def Main():\r
91 Logger.Initialize()\r
92\r
93 Parser = OptionParser(version=MSG_VERSION, description=MSG_DESCRIPTION,\r
94 prog="UPT.exe", usage=MSG_USAGE)\r
95\r
96 Parser.add_option("-d", "--debug", action="store", type="int", dest="debug_level", help=ST.HLP_PRINT_DEBUG_INFO)\r
97\r
98 Parser.add_option("-v", "--verbose", action="store_true", dest="opt_verbose",\r
99 help=ST.HLP_PRINT_INFORMATIONAL_STATEMENT)\r
100\r
101 Parser.add_option("-s", "--silent", action="store_true", dest="opt_slient", help=ST.HLP_RETURN_NO_DISPLAY)\r
102\r
103 Parser.add_option("-q", "--quiet", action="store_true", dest="opt_quiet", help=ST.HLP_RETURN_AND_DISPLAY)\r
104\r
105 Parser.add_option("-i", "--install", action="store", type="string", dest="Install_Distribution_Package_File",\r
106 help=ST.HLP_SPECIFY_PACKAGE_NAME_INSTALL)\r
107\r
108 Parser.add_option("-c", "--create", action="store", type="string", dest="Create_Distribution_Package_File",\r
109 help=ST.HLP_SPECIFY_PACKAGE_NAME_CREATE)\r
110\r
111 Parser.add_option("-r", "--remove", action="store", type="string", dest="Remove_Distribution_Package_File",\r
112 help=ST.HLP_SPECIFY_PACKAGE_NAME_REMOVE)\r
113\r
114 Parser.add_option("-t", "--template", action="store", type="string", dest="Package_Information_Data_File",\r
115 help=ST.HLP_SPECIFY_TEMPLATE_NAME_CREATE)\r
116\r
117 Parser.add_option("-p", "--dec-filename", action="append", type="string", dest="EDK2_DEC_Filename",\r
118 help=ST.HLP_SPECIFY_DEC_NAME_CREATE)\r
119\r
120 Parser.add_option("-m", "--inf-filename", action="append", type="string", dest="EDK2_INF_Filename",\r
121 help=ST.HLP_SPECIFY_INF_NAME_CREATE)\r
122\r
123 Parser.add_option("-f", "--force", action="store_true", dest="Yes", help=ST.HLP_DISABLE_PROMPT)\r
124\r
125 Parser.add_option("-n", "--custom-path", action="store_true", dest="CustomPath", help=ST.HLP_CUSTOM_PATH_PROMPT)\r
126\r
127 Parser.add_option("-x", "--free-lock", action="store_true", dest="SkipLock", help=ST.HLP_SKIP_LOCK_CHECK)\r
128\r
129 Opt = Parser.parse_args()[0]\r
130\r
131 Var2Var = [\r
132 ("PackageInformationDataFile", Opt.Package_Information_Data_File),\r
133 ("PackFileToInstall", Opt.Install_Distribution_Package_File),\r
134 ("PackFileToCreate", Opt.Create_Distribution_Package_File),\r
135 ("PackFileToRemove", Opt.Remove_Distribution_Package_File),\r
136 ("PackageFileList", Opt.EDK2_DEC_Filename),\r
137 ("ModuleFileList", Opt.EDK2_INF_Filename)\r
138 ]\r
139 for Var in Var2Var:\r
140 setattr(Opt, Var[0], Var[1])\r
141\r
142 try:\r
143 CheckEnvVariable()\r
144 except FatalError, XExcept:\r
145 if Logger.GetLevel() <= Logger.DEBUG_9:\r
146 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + format_exc())\r
147 return XExcept.args[0]\r
148\r
149 GlobalData.gWORKSPACE = os.path.normpath(environ["WORKSPACE"])\r
150 WorkspaceDir = GlobalData.gWORKSPACE\r
151\r
152 SetLogLevel(Opt)\r
153\r
154 GlobalData.gDB = IpiDatabase(os.path.normpath(os.path.join(WorkspaceDir, "Conf/DistributionPackageDatabase.db")))\r
155 GlobalData.gDB.InitDatabase(Opt.SkipLock)\r
156\r
157 #\r
158 # Make sure the Db will get closed correctly\r
159 #\r
160 try:\r
161 ReturnCode = 0\r
162 CheckConflictOption(Opt)\r
163\r
164 RunModule = None\r
165 if Opt.PackFileToCreate:\r
166 if Opt.PackageInformationDataFile:\r
167 if not os.path.exists(Opt.PackageInformationDataFile):\r
168 if not os.path.exists(os.path.join(WorkspaceDir, Opt.PackageInformationDataFile)):\r
169 Logger.Error("\nUPT", FILE_NOT_FOUND, ST.ERR_NO_TEMPLATE_FILE % Opt.PackageInformationDataFile)\r
170 else:\r
171 Opt.PackageInformationDataFile = os.path.join(WorkspaceDir, Opt.PackageInformationDataFile)\r
172 else:\r
173 Logger.Error("UPT", OPTION_MISSING, ExtraData=ST.ERR_REQUIRE_T_OPTION)\r
174 if not Opt.PackFileToCreate.endswith('.dist'):\r
175 Logger.Error("CreatePkg", FILE_TYPE_MISMATCH, ExtraData=ST.ERR_DIST_EXT_ERROR % Opt.PackFileToCreate)\r
176 RunModule = MkPkg.Main\r
177\r
178 elif Opt.PackFileToInstall:\r
179 if not Opt.PackFileToInstall.endswith('.dist'):\r
180 Logger.Error("InstallPkg", FILE_TYPE_MISMATCH, ExtraData=ST.ERR_DIST_EXT_ERROR % Opt.PackFileToInstall)\r
181 \r
182 #\r
183 # check file existence, if not absolute path, then try current working directory, then $(WORKSPACE) \r
184 #\r
185 Existed = True\r
186 if os.path.isabs(Opt.PackFileToInstall):\r
187 if not (os.path.exists(Opt.PackFileToInstall) and os.path.isfile(Opt.PackFileToInstall)):\r
188 Existed = False\r
189 else:\r
190 AbsPath = os.path.normpath(os.path.join(os.getcwd(), Opt.PackFileToInstall))\r
191 if not (os.path.exists(AbsPath) and os.path.isfile(AbsPath)):\r
192 AbsPath = os.path.normpath(os.path.join(WorkspaceDir, Opt.PackFileToInstall))\r
193 if not (os.path.exists(AbsPath) and os.path.isfile(AbsPath)):\r
194 Existed = False\r
195 \r
196 if Existed:\r
197 Opt.PackFileToInstall = AbsPath\r
198 \r
199 if not Existed:\r
200 Logger.Error("InstallPkg", FILE_NOT_FOUND, ST.ERR_INSTALL_DIST_NOT_FOUND % Opt.PackFileToInstall)\r
201\r
202 setattr(Opt, 'PackageFile', Opt.PackFileToInstall)\r
203 RunModule = InstallPkg.Main\r
204\r
205 elif Opt.PackFileToRemove:\r
206 if not Opt.PackFileToRemove.endswith('.dist'):\r
207 Logger.Error("RemovePkg", FILE_TYPE_MISMATCH, ExtraData=ST.ERR_DIST_EXT_ERROR % Opt.PackFileToRemove)\r
208 head, tail = os.path.split(Opt.PackFileToRemove)\r
209 if head or not tail:\r
210 Logger.Error("RemovePkg",\r
211 FILE_TYPE_MISMATCH,\r
212 ExtraData=ST.ERR_DIST_FILENAME_ONLY_FOR_REMOVE % Opt.PackFileToRemove)\r
213\r
214 setattr(Opt, 'DistributionFile', Opt.PackFileToRemove)\r
215 RunModule = RmPkg.Main\r
216 else:\r
217 Parser.print_usage()\r
218 return OPTION_MISSING\r
219\r
220 ReturnCode = RunModule(Opt)\r
221 except FatalError, XExcept:\r
222 ReturnCode = XExcept.args[0]\r
223 if Logger.GetLevel() <= Logger.DEBUG_9:\r
224 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + \\r
225 format_exc())\r
226 finally:\r
227 GlobalData.gDB.CloseDb()\r
228\r
229 return ReturnCode\r
230\r
231if __name__ == '__main__':\r
232 RETVAL = Main()\r
233 #\r
234 # 0-127 is a safe return range, and 1 is a standard default error\r
235 #\r
236 if RETVAL < 0 or RETVAL > 127:\r
237 RETVAL = 1\r
238 sys.exit(RETVAL)\r