Пример #1
0
 /**
  * @since 5.6
  * @param commandsFile File containing the commands to execute
  * @param doExecute Whether to execute or list the actions
  * @param useResolver Whether to use full resolution or just execute individual actions
  */
 @SuppressWarnings("unchecked")
 public boolean executePending(File commandsFile, boolean doExecute, boolean useResolver) {
   int errorValue = 0;
   if (!commandsFile.isFile()) {
     return false;
   }
   List<String> pkgsToAdd = new ArrayList<String>();
   List<String> pkgsToInstall = new ArrayList<String>();
   List<String> pkgsToUninstall = new ArrayList<String>();
   List<String> pkgsToRemove = new ArrayList<String>();
   List<String> lines;
   try {
     lines = FileUtils.readLines(commandsFile);
     for (String line : lines) {
       line = line.trim();
       String[] split = line.split("\\s+", 2);
       if (split.length == 2) {
         if (split[0].equals(CommandInfo.CMD_INSTALL)) {
           if (doExecute) {
             if (useResolver) {
               pkgsToInstall.add(split[1]);
             } else {
               pkgInstall(split[1]);
             }
           } else {
             CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_INSTALL);
             cmdInfo.param = split[1];
             cmdInfo.pending = true;
           }
         } else if (split[0].equals(CommandInfo.CMD_ADD)) {
           if (doExecute) {
             if (useResolver) {
               pkgsToAdd.add(split[1]);
             } else {
               pkgAdd(split[1]);
             }
           } else {
             CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_ADD);
             cmdInfo.param = split[1];
             cmdInfo.pending = true;
           }
         } else if (split[0].equals(CommandInfo.CMD_UNINSTALL)) {
           if (doExecute) {
             if (useResolver) {
               pkgsToUninstall.add(split[1]);
             } else {
               pkgUninstall(split[1]);
             }
           } else {
             CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_UNINSTALL);
             cmdInfo.param = split[1];
             cmdInfo.pending = true;
           }
         } else if (split[0].equals(CommandInfo.CMD_REMOVE)) {
           if (doExecute) {
             if (useResolver) {
               pkgsToRemove.add(split[1]);
             } else {
               pkgRemove(split[1]);
             }
           } else {
             CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_REMOVE);
             cmdInfo.param = split[1];
             cmdInfo.pending = true;
           }
         } else {
           errorValue = 1;
         }
       } else if (split.length == 1) {
         if (line.length() > 0 && !line.startsWith("#")) {
           if (doExecute) {
             if ("init".equals(line)) {
               if (!addDistributionPackages()) {
                 errorValue = 1;
               }
             } else {
               if (useResolver) {
                 pkgsToInstall.add(line);
               } else {
                 pkgInstall(line);
               }
             }
           } else {
             if ("init".equals(line)) {
               CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_INIT);
               cmdInfo.pending = true;
             } else {
               CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_INSTALL);
               cmdInfo.param = line;
               cmdInfo.pending = true;
             }
           }
         }
       }
       if (errorValue != 0) {
         log.error("Error processing pending package/command: " + line);
       }
     }
     if (doExecute) {
       if (useResolver) {
         String oldAccept = accept;
         String oldRelax = relax;
         accept = "true";
         relax = "true";
         boolean success = pkgRequest(pkgsToAdd, pkgsToInstall, pkgsToUninstall, pkgsToRemove);
         accept = oldAccept;
         relax = oldRelax;
         if (!success) {
           errorValue = 2;
         }
       }
       if (errorValue != 0) {
         File bak = new File(commandsFile.getPath() + ".bak");
         bak.delete();
         commandsFile.renameTo(bak);
       } else {
         commandsFile.delete();
       }
     } else {
       cset.log(true);
     }
   } catch (IOException e) {
     log.error(e.getMessage());
   }
   return errorValue == 0;
 }