502be2499b88ad6daad414e82fcedc60da41406c
[mirror_edk2.git] / Tools / Java / Source / DbTools / src / org / tianocore / DbTools / DbCmds.java
1 // @file
2 // DbCmds command-line interface to the classes that
3 // update the FrameworkDatabase.db file based on WORKSPACE Contents
4 //
5 // Copyright (c) 2006, Intel Corporation All rights reserved.
6 //
7 // This program and the accompanying materials are licensed and made
8 // available under the terms and conditions of the BSD License which
9 // accompanies this distribution. The full text of the license may
10 // be found at http://opensource.org/licenses/bsd-license.php
11 //
12 // THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 //
15 // This program is the command line interface to the CombineMsa class, which
16 // will take the following arguments:
17 //
18 // Input:
19 // -t, --test Test the Workspace against the FrameworkDatabase.db file
20 // Returns 0 if valid, returns 1 if failed
21 //
22 // -f, --fix Fix the FrameworkDatabase.db file, so that it matches the
23 // the contents of the WORKSPACE
24 // Returns 0 if valid, returns 1 if failed
25 //
26 // -v [-v] Verbose Flag - sum of these will be used to set different
27 // levels of verbosity
28 //
29 // -i Interactive, when used with -f or --fix, will query the user
30 // regarding adds and deletes of packages and platforms from
31 // the database.
32 //
33 // -a, --add Add an SPD or FPD file to the FrameworkDatabase.db file. The
34 // SPD/FPD file must exist, or the command will fail.
35 // Returns 0 if valid, returns 1 if failed
36 //
37 // -r, --del Remove an SPD or FPD file from the FrameworkDatabase.db file.
38 // If the SPD/FPD file exists, the user will be queried to
39 // remove it from the directory tree. For SPD files, the user
40 // will also be presented with a list of Modules in the SPD
41 // file, with the query to remove the modules as well as the
42 // SPD file.
43 // Returns 0 if valid, returns 1 if failed
44 //
45 // -u Display the UiName for all Packages and Platforms currently in
46 // the FrameworkDatabase.db file.
47 // Returns 0 if valid, returns 1 if failed
48 //
49 // -c Display a CSV listing of Type (SPD|FPD) UiName and Filename of
50 // every entry in the FrameworkDatabase.db file.
51 // Returns 0 if valid, returns 1 if failed
52 //
53 // No Options Display a list of Type (Package|Platfrom) and Filename of every
54 // entry in the FrameworkDatabase.db file.
55 // Returns 0 if valid, returns 1 if failed
56 //
57 // -h, -?, --help Displays this usage and exits.
58 // Returns 0 if valid, returns 1 if failed
59 //
60 //
61 // Output:
62 // Displayed information
63 //
64 // Modifies - OPTIONAL
65 // FrameworkDatabase.db
66 //
67
68 package org.tianocore.DbTools;
69
70 import java.io.*;
71
72 public class DbCmds {
73
74 protected enum Cmd {
75 SHOW_CSV, SHOW_PLATFORMS, SHOW_PACKAGES, FIX_DB, TEST_DB, FIND_SOMETHING, SHOW_FAR, ADD_SOMETHING,
76 DELETE_SOMETHING
77 }
78
79 private int DEBUG = 0;
80
81 private static final String copyright = "Copyright (c) 2006, Intel Corporation All rights reserved.";
82
83 private static final String version = "Version 0.1";
84
85 private static final String Specification = "FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052";
86
87 private int VERBOSE = 0;
88
89 public boolean INTERACTIVE = false;
90
91 private String workspace = System.getenv("WORKSPACE");
92
93 private final String frameworkDatabase = workspace + File.separator + "Tools" + File.separator + "Conf"
94 + File.separator + "FrameworkDatabase.db";
95
96 private final int ESUCCESS = 0;
97
98 private final int EFAILURE = 1;
99
100 private final int MIN_VERBOSE = 0;
101
102 private final int MED_VERBOSE = 1;
103
104 private final int MAX_VERBOSE = 2;
105
106 private final static int FOUND = 1;
107
108 private final static int NOTFOUND = 0;
109
110 private boolean TEST = false;
111
112 private int result = ESUCCESS;
113
114 private Cmd commandToCall;
115
116 private boolean isPlatform = false;
117
118 private boolean isPackage = false;
119
120 private boolean isFar = false;
121
122 private boolean QUIET = false;
123
124 private String commandArgs = "";
125
126 private String whatToFind = "";
127
128 public int DbUpdateCmdLine(String[] args) {
129
130 if (testFile(frameworkDatabase) == NOTFOUND) {
131 System.out.println("ERROR: E000 Invalid Workspace!");
132 System.out.println("The environment variable, WORKSPACE, does not point to a valid workspace");
133 System.out.println("DbUpdate Aborted!");
134 System.err.flush();
135 System.exit(EFAILURE);
136 }
137 result = parseCmdLine(args);
138 if (result == ESUCCESS) {
139 if (DEBUG > 2)
140 System.out.println("Parse Succeeded!");
141 if (VERBOSE > MAX_VERBOSE)
142 System.out.println("WORKSPACE: " + workspace);
143 if (DEBUG > 1)
144 System.out.println("Command to call: " + commandToCall.toString());
145 result = processCmdLine(commandToCall);
146 } else {
147 if (QUIET == false) {
148 System.out.println("Invalid Arguments");
149 outputUsage();
150 result = EFAILURE;
151 }
152 }
153 if (DEBUG > 2)
154 System.out.println(" Result: " + result);
155
156 return result;
157 }
158
159 private int processCmdLine(Cmd cmdCode) {
160 UpdateDb dbUp = new UpdateDb();
161 int res = ESUCCESS;
162 switch (cmdCode) {
163 case SHOW_CSV:
164 // display current Database contents in CSV format
165 // Modifies: NOTHING
166 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))
167 System.out.println("Display contents of the FrameworkDatabase.db file in CSV format.");
168 result = dbUp.getCsvEntries(frameworkDatabase, VERBOSE);
169 break;
170 case FIX_DB:
171 // Automatically make the database match the contents of the workspace
172 // Modifies: FrameworkDatabase.db
173 if ((VERBOSE > MIN_VERBOSE) && (QUIET == false)) {
174 if (TEST == false)
175 System.out.println("Adjusting the FrameworkDatabase to match the contents of the WORKSPACE: "
176 + workspace);
177 if (TEST)
178 System.out.println("Verify the FrameworkDatabase matches the contents of the WORKSPACE: "
179 + workspace);
180 }
181 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))
182 System.out.println("Scan the Workspace and update the FrameworkDatabase.db file.");
183 result = dbUp.fixDatabase(workspace, VERBOSE, INTERACTIVE, TEST, QUIET);
184
185 break;
186 case ADD_SOMETHING:
187 // Add a Platform, package or FAR to the workspace.
188 // Modifies: FrameworkDatabase.db
189 if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPlatform))
190 System.out.println("Add Platform " + commandArgs + " to the FrameworkDatabase.db file");
191 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPackage))
192 System.out.println("Add Package " + commandArgs + " to the FrameworkDatabase.db file");
193 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isFar))
194 System.out.println("Add Framework Archive " + commandArgs + " to the FrameworkDatabase.db file");
195 result = dbUp.addItem(frameworkDatabase, commandArgs, VERBOSE, INTERACTIVE);
196 break;
197 case DELETE_SOMETHING:
198 // Remove a platform, package or FAR from the workspace
199 // Modifies: FrameworkDatabase.db, AND Filesystem
200 if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPlatform))
201 System.out.println("Removing Platform " + commandArgs + " from the FrameworkDatabase.db file");
202 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPackage))
203 System.out.println("Removing Package " + commandArgs + " from the FrameworkDatabase.db file");
204 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isFar))
205 System.out.println("Removing Framework Archive " + commandArgs + " from the FrameworkDatabase.db file");
206 result = dbUp.addItem(workspace, commandArgs, VERBOSE, INTERACTIVE);
207 break;
208 case FIND_SOMETHING:
209 // Find something in the workspace
210 // Modifies: NOTHING
211 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))
212 System.out.println("Finding " + whatToFind + " " + commandArgs + " in the Workspace");
213 result = dbUp.findItem(workspace, VERBOSE, whatToFind, commandArgs);
214 break;
215 case SHOW_FAR:
216 // Display FAR information for all SPDs in the workspace
217 // Modifies: NOTHING
218 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))
219 System.out.println("Display Framework Archives in the Workspace");
220 result = dbUp.findFars(workspace, VERBOSE);
221 break;
222 case SHOW_PACKAGES:
223 // Display SPD information for all SPDs in the workspace
224 // Modifies: NOTHING
225 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))
226 System.out.println("Display Packages in the Workspace");
227 result = dbUp.findSpds(workspace, VERBOSE);
228 break;
229 case SHOW_PLATFORMS:
230 // Display FPD information for all SPDs in the workspace
231 // Modifies: NOTHING
232 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))
233 System.out.println("Display Platforms in the Workspace");
234 result = dbUp.findFpds(workspace, VERBOSE);
235 break;
236 default:
237 // ERROR IF WE GET HERE!
238 if ((DEBUG > 3) || (VERBOSE > MAX_VERBOSE))
239 System.out.println("We could not process the following: " + commandToCall.toString());
240 else if (QUIET == false)
241 outputUsage();
242 result = EFAILURE;
243 break;
244 }
245 return res;
246 }
247
248 private int parseCmdLine(String[] args) {
249
250 // Default is to fix the database.
251 commandToCall = Cmd.FIX_DB;
252 if (args.length == NOTFOUND) {
253 if ((DEBUG > 3) || (VERBOSE > MAX_VERBOSE))
254 System.out.println("NO ARGUMENTS! " + commandToCall.toString());
255 return (ESUCCESS);
256 }
257
258 for (int i = 0; i < args.length; i++)
259 if (args[i].toLowerCase().contentEquals("-q"))
260 QUIET = true;
261
262 for (int i = 0; i < args.length; i++) {
263 String arg = args[i].trim();
264 // This is the list of valid options
265 if (!((arg.toLowerCase().startsWith("-t")) || (arg.toLowerCase().startsWith("--t"))
266 || (arg.toLowerCase().startsWith("-q")) || (arg.toLowerCase().startsWith("-i"))
267 || (arg.toLowerCase().startsWith("-f")) || (arg.toLowerCase().startsWith("--f"))
268 || (arg.toLowerCase().startsWith("-c")) || (arg.toLowerCase().startsWith("--c"))
269 || (arg.toLowerCase().startsWith("-a")) || (arg.toLowerCase().startsWith("--a"))
270 || (arg.toLowerCase().startsWith("-r")) || (arg.toLowerCase().startsWith("--d"))
271 || (arg.toLowerCase().startsWith("-v")) || (arg.toLowerCase().startsWith("--c"))
272 || (arg.toLowerCase().startsWith("--p")) || (arg.toLowerCase().startsWith("-h"))
273 || (arg.toLowerCase().startsWith("--h")) || (arg.toLowerCase().startsWith("/h")) || (arg
274 .toLowerCase()
275 .startsWith("-?")))) {
276 // This is the fall through, we got something we did not know how to
277 // process!
278 if (args[i].startsWith("-")) {
279 System.out.println("ERROR: E1004 Unknown Option: " + arg);
280 System.out.println("Try running with -h or --help");
281 } else {
282 System.out.println("ERROR: E1005 Unknown Argument: " + arg);
283 System.out.println("Try running with -h or --help");
284 }
285 System.out.println("Program Aborted!");
286 System.err.flush();
287 System.exit(EFAILURE);
288 }
289 if ((arg.toLowerCase().contentEquals("-t")) || (arg.toLowerCase().contains("--test"))) {
290 // Test Workspace, do not fix.
291 TEST = true;
292 }
293 if (arg.toLowerCase().contentEquals("-q")) {
294 QUIET = true;
295 }
296 if (arg.toLowerCase().contentEquals("-i")) {
297 INTERACTIVE = true;
298 }
299 if ((arg.toLowerCase().contentEquals("-f")) || (arg.toLowerCase().contains("--fix"))) {
300 // Non-interactive fix of the database
301 commandToCall = Cmd.FIX_DB;
302 }
303 if ((arg.toLowerCase().contentEquals("-c")) || (arg.toLowerCase().contains("--csv"))) {
304 // Dump database in CSV format
305 commandToCall = Cmd.SHOW_CSV;
306 }
307 if ((arg.toLowerCase().trim().contentEquals("-a")) || (arg.toLowerCase().contains("--add"))) {
308 i++;
309 if (args[i].startsWith("-")) {
310 System.out.println("ERROR: E002 Missing Argument!");
311 System.out
312 .println("The add function requires an argument, either a package name or a platform name!");
313 System.out.println("DbUpdate Aborted!");
314 System.err.flush();
315 System.exit(EFAILURE);
316 }
317 commandToCall = Cmd.ADD_SOMETHING;
318 commandArgs = args[i];
319 setArgType(commandArgs);
320 }
321 if ((arg.toLowerCase().trim().contentEquals("-r")) || (arg.toLowerCase().contains("--del"))) {
322 i++;
323 if (args[i].startsWith("-")) {
324 System.out.println("ERROR: E002 Missing Argument!");
325 System.out
326 .println("The remove function requires an argument, either a package name or a platform name!");
327 System.out.println("DbUpdate Aborted!");
328 System.err.flush();
329 System.exit(EFAILURE);
330 }
331 commandToCall = Cmd.DELETE_SOMETHING;
332 commandArgs = args[i];
333 setArgType(commandArgs);
334 }
335 if (arg.toLowerCase().contains("--find")) {
336 commandToCall = Cmd.FIND_SOMETHING;
337 i++;
338 if (args[i].toLowerCase().contains("--lib"))
339 whatToFind = "LIBRARY";
340 else if (args[i].toLowerCase().contains("--guid"))
341 whatToFind = "GUID";
342 else if (args[i].toLowerCase().contains("--ppi"))
343 whatToFind = "PPI";
344 else if (args[i].toLowerCase().contains("--prot"))
345 whatToFind = "PROTOCOL";
346 else if (args[i].toLowerCase().contains("--pcd"))
347 whatToFind = "PCD";
348 else if (args[i].startsWith("-")) {
349 System.out.println("ERROR: E001 Invalid Argument");
350 System.out.println("The find function takes either a qualifier of --guid, --ppi,");
351 System.out.println(" --proto, --pcd, or the string to search for.");
352 System.err.flush();
353 System.exit(EFAILURE);
354 } else
355 commandArgs = args[i];
356
357 if (!whatToFind.contentEquals("")) {
358 i++;
359 if (args[i].startsWith("-")) {
360 System.out.println("ERROR: E001 Invalid Argument");
361 System.out.println("The find function qualifier (--guid, --ppi, --proto, --pcd)");
362 System.out.println(" must be followed by the string to search for.");
363 System.err.flush();
364 System.exit(EFAILURE);
365 } else
366 commandArgs = args[i];
367 }
368
369 }
370 if (arg.trim().contentEquals("-v")) {
371 VERBOSE++;
372 }
373 if (arg.toLowerCase().contains("--lib")) {
374 whatToFind = "LIBRARY";
375 }
376 if (arg.toLowerCase().contains("--guid")) {
377 whatToFind = "GUID";
378 }
379 if (arg.toLowerCase().contains("--ppi")) {
380 whatToFind = "PPI";
381 }
382 if (arg.toLowerCase().contains("--prot")) {
383 whatToFind = "PROTOCOL";
384 }
385 if (arg.toLowerCase().contains("--pcd")) {
386 whatToFind = "PCD";
387 }
388 if (arg.toLowerCase().contentEquals("-d") || arg.trim().toLowerCase().contains("--debug")) {
389 if ((i + 1 == args.length) || (args[i + 1].trim().startsWith("-"))) {
390 DEBUG = 1;
391 } else if (i + 1 < args.length) {
392 String pattern = "^\\d+";
393 if (args[i + 1].trim().matches(pattern)) {
394 i++;
395 DEBUG = Integer.parseInt(args[i]);
396 } else
397 DEBUG = 1;
398 }
399 }
400 if (arg.trim().contentEquals("-V")) {
401 System.out.println("DbTools, " + version);
402 System.out.println(copyright);
403 System.out.println(Specification);
404 System.err.flush();
405 System.exit(ESUCCESS);
406 }
407 if (arg.toLowerCase().trim().startsWith("--pack")) {
408 commandToCall = Cmd.SHOW_PACKAGES;
409 }
410 if (arg.toLowerCase().trim().startsWith("--far")) {
411 commandToCall = Cmd.SHOW_FAR;
412 }
413 if (arg.toLowerCase().trim().startsWith("--plat")) {
414 commandToCall = Cmd.SHOW_PLATFORMS;
415 }
416 if ((arg.toLowerCase().contentEquals("-h")) || (arg.toLowerCase().contentEquals("-?"))
417 || (arg.toLowerCase().startsWith("/h")) || (arg.toLowerCase().contentEquals("--help"))) {
418 outputUsage();
419 System.exit(EFAILURE);
420 }
421
422 }
423 return ESUCCESS;
424 }
425
426 private int testFile(String Filename) {
427 File tFile = new File(Filename);
428 if (DEBUG > 4)
429 System.out.println("File is located: " + tFile.getPath());
430 if (tFile.exists())
431 return FOUND;
432 else
433 return NOTFOUND;
434 }
435
436 private void setArgType(String argv) {
437 if (argv.trim().toLowerCase().contains(".spd"))
438 isPackage = true;
439 if (argv.trim().toLowerCase().contains(".fpd"))
440 isPlatform = true;
441 if (argv.trim().toLowerCase().contains(".far"))
442 isFar = true;
443 }
444
445 public String getArgType() {
446 String argt = "UNKNOWN";
447 if (isPackage)
448 argt = "SPD";
449 if (isFar)
450 argt = "FAR";
451 if (isPlatform)
452 argt = "FPD";
453 return argt;
454 }
455
456 private static void outputUsage() {
457
458 System.out.println("DbTool, " + version);
459 System.out.println(copyright);
460 System.out.println("Usage:");
461 System.out.println(" DbTool [-v] [-t] [-q] [-V] [--package] [--platform] [-h | -? | --help]");
462 System.out.println(" where:");
463 System.out.println(" -h | -? | --help OPTIONAL - This Help Text");
464 System.out
465 .println(" -t | --test OPTIONAL - Test the FrameworkDatabase Contents against the WORKSPACE");
466 System.out
467 .println(" -q OPTIONAL - Quiet mode - pass or fail only on return value, nothing is printed");
468 System.out
469 .println(" -f | --fix OPTIONAL - Automatically fix (non-interactive) the Database file to match the WORKSPACE.");
470 System.out
471 .println(" -v OPTIONAL - Verbose, print information messages. Adding more -v arguments increases verbosity.");
472 System.out.println(" --package OPTIONAL - Show all Packages installed in the WORKSPACE.");
473 System.out.println(" --platforms OPTIONAL - Show all Platforms installed in the WORKSPACE.");
474 System.out.println(" -V OPTIONAL - Display Verision information and exit.");
475 //
476 // TODO: Implement the following options.
477 //
478 /**
479 System.out.println("");
480 System.out.println(" =================== Options below this line have not been implemented in this release ===================");
481 System.out
482 .println(" -i OPTIONAL - Force interactive on commands that modify the WORKSPACE");
483 System.out
484 .println(" --far OPTIONAL - Show all Framework Archives installed in the WORKSPACE.");
485 System.out
486 .println(" --find [qualifier] value OPTIONAL - Search the WORKSPACE for value, with one and only one optional Qualifier.");
487 System.out.println(" qualifiers: --guid Find a GUID by Guid C Name");
488 System.out.println(" --prot Find a Protocol or ProtocolNotify by C Name");
489 System.out.println(" --ppi Find a PPI or PpiNotify by C Name");
490 System.out.println(" --pcd Find a PCD entry by C Name");
491 System.out.println(" --lib Find information about a Library Class");
492 System.out
493 .println(" -c OPTIONAL - Print a comma separated value listing of TYPE,UiName,Filename");
494 System.out
495 .println(" -a, --add value OPTIONAL - Add a value (package, platform or far) to the database");
496 System.out
497 .println(" -r, --del value OPTIONAL - Remove a value (package, platform or far) from the database");
498 System.out.println("");
499 */
500 }
501 }