[Title] Add option prefixes for cli installation.
authoryongsung1.kim <yongsung1.kim@samsung.com>
Thu, 21 Mar 2013 08:51:01 +0000 (17:51 +0900)
committeryongsung1.kim <yongsung1.kim@samsung.com>
Thu, 21 Mar 2013 08:51:01 +0000 (17:51 +0900)
[Desc.] Usage : InstallManager.bin(exe, sh) [-install | -remove] [-r
repository] [-d distribution] [-l SDK target directory(optional)] [-p
meta package ....]
[Issue]

Change-Id: I7cde8a748a17bce183fdad527723420347ddbe72

InstallManager_java/src/org/tizen/installmanager/core/InstallManagerNoUI.java
InstallManager_java/src/org/tizen/installmanager/core/Options.java

index d320528..ad793d8 100644 (file)
@@ -34,12 +34,16 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-
+import java.util.regex.PatternSyntaxException;
 import org.tizen.installmanager.lib.Downloader;
 import org.tizen.installmanager.lib.ErrorController;
+import org.tizen.installmanager.lib.IFileSystemInformation;
 import org.tizen.installmanager.lib.Log;
+import org.tizen.installmanager.lib.Platform;
 import org.tizen.installmanager.lib.ErrorController.ErrorCode;
 import org.tizen.installmanager.lib.exception.IMNetworkException;
+import org.tizen.installmanager.lib.linux.LinuxFileSystemInfo;
+import org.tizen.installmanager.lib.win.WindowsFileSystemInfo;
 import org.tizen.installmanager.pkg.lib.PackageManager;
 import org.tizen.installmanager.pkg.model.Package;
 import org.tizen.installmanager.pkg.model.PackageSet;
@@ -54,6 +58,8 @@ public class InstallManagerNoUI {
        static final String DISTRIBUTION_INFO = "distribution.info";
        static final String ESSENTIAL_COMPONENT = "ESSENTIAL-COMPONENT";
        
+       static ViewController controller = null;
+       
        /**
         * Works for command line installation
         * @return If installation successes, return true. otherwise return false.
@@ -72,10 +78,13 @@ public class InstallManagerNoUI {
                }
                
                PackageManager pm = PackageManager.getInstance();
-               String targetDir = System.getProperty("user.home") + File.separator + Config.SDK_DIRECTORY;
+                       
+               String targetDir = setTargetDir();
                
                if (packageNames.contains("all")) {
-                       return installPackages(getAllPackages(), targetDir);
+                       if (checkAvailableSize(getAllPackages(), targetDir)) {
+                               return installPackages(getAllPackages(), targetDir);                            
+                       }
                } else if (!validateInstallPkgs(pm, packageNames)) {
                        return false;
                }
@@ -87,27 +96,38 @@ public class InstallManagerNoUI {
                PackageSet installableMetas = pm.getPackagesByNames(installableMetaNames);
                PackageSet installablePackages = pm.getDependsPackagesFromRepository(installableMetas);
                
-               return installPackages(installablePackages, targetDir);
+               if (checkAvailableSize(installablePackages, targetDir)) {
+                       return installPackages(installablePackages, targetDir);                 
+               } else {
+                       return false;
+               }
        }
        
-       private static boolean installPackages(PackageSet installablePackages, String targetDir)
-                       throws IMExitException {
-               int repeatCount = installablePackages.size();
-               for (int i = repeatCount; i > 0; i--) {
-                       try {
-                               if (InstallManager.getInstance().install(installablePackages, targetDir, null)) {
-                                       return true;
-                               } else {
-                                       return false;
-                               }
-                       } catch (IMNetworkException e) {
-                               Log.ExceptionLog(e);
-                               System.err.println("Network error has occured. retry the download => " + repeatCount);
-                               continue;
+       /**
+        * When cli installation, sets SDK target directory as user input.
+        * @return SDK install directory.
+        */
+       private static String setTargetDir() {
+               String inputTargetDir = Options.targetDir; 
+
+               String targetDir = null;
+               
+               if (inputTargetDir == null) {
+                       targetDir = System.getProperty("user.home") + File.separator + Config.SDK_DIRECTORY;
+               } else if (inputTargetDir.startsWith("~")) {
+                       targetDir = inputTargetDir.replaceFirst("~", System.getProperty("user.home"));
+                       if (checkCorrectPath(targetDir)) {
+                               return targetDir;
+                       } else {
+                               return null;
                        }
+               } else if (checkCorrectPath(inputTargetDir)) {
+                       targetDir = inputTargetDir;
+               } else {
+                       return null;
                }
-               
-               return false;
+
+               return targetDir;
        }
        
        /**
@@ -280,21 +300,21 @@ public class InstallManagerNoUI {
         * Refresh installation environment.
         */
        private static void refresh() {
-               ViewController controller = new ViewController();
+               controller = new ViewController();
                controller.init();
                
                // Set config information as input data.
                Config.getInstance().saveSnapshotSettings(Options.repository, Options.distribution, Options.serverType, Options.snapshotPath);
                
                PackageManager.dispose();
-               initInstallManager(controller);
+               initInstallManager();
        }
        
        /**
         * init IM's configuation and packages information.
         * @return
         */
-       private static boolean initInstallManager(ViewController controller) {
+       private static boolean initInstallManager() {
                ErrorController.setInstallationSuccess(true);
                
                controller = new ViewController();
@@ -329,21 +349,149 @@ public class InstallManagerNoUI {
                
                PackageSet downloadPackageList = pm.getLeafMetaPackages();
                
-               String boundary = Options.boundary;
-               if (boundary.equalsIgnoreCase("public")) {
-                       PackageSet partnerMetaPackages = pm.getPartnerMetaPackages();
-                       
-                       if (!partnerMetaPackages.isEmpty()) {
-                               downloadPackageList.removeAll(partnerMetaPackages);
+               return downloadPackageList;
+       }
+       
+       /**
+        * Check path which is correct depend on platform.
+        * @param targetPath
+        * @return Return true, if path is correct. otherwise return false.
+        */
+       private static boolean checkCorrectPath(String targetPath) {
+               try {
+                       if (Platform.isUbuntu() || Platform.isMacOS()) {
+                               if (!targetPath.matches("[^= ]+")) {
+                                       System.err.println("Value cannot contain the '=' character or spaces.");
+                                       return false;
+                               } else {
+                                       if (!targetPath.startsWith(System.getProperty("user.home"))) {
+                                               System.err.println("Set the installation path to the home directory.");
+                                               return false;
+                                       }
+                               }
+                       } else if (Platform.isWindows()) {
+                               if (!targetPath.matches("[^`~!@#$%^&*=? ]+")) {
+                                       System.err.println("Value cannot contain special characters or spaces.");
+                                       return false;
+                               }
                        }
-               } else if (boundary.equalsIgnoreCase("partner")) {
-                       PackageSet publicMetaPackages = pm.getPublicMetaPackages();
-                       
-                       if (!publicMetaPackages.isEmpty()) {
-                               downloadPackageList.removeAll(publicMetaPackages);
+               } catch (PatternSyntaxException e) {
+                       Log.ExceptionLog(e);
+               }
+               
+               return true;
+       }
+
+       private static boolean checkAvailableSize(PackageSet packageSet, String targetPath) {
+               long availableSize = getAvailableSpaceSize(targetPath);
+               long requiredSpace = getInstallPackageSize(packageSet);
+               
+               if (availableSize < 0) {
+                       System.err.println("Set the installation path correctly.");
+                       return false;
+               }
+               
+               if (requiredSpace > availableSize) {
+                       System.err.println("Not enough disk space for the installation. Select a different installation path.");
+                       return false;
+               }
+               
+               return true;
+       }
+       
+       private static long getAvailableSpaceSize(String selectedPath) {
+               File file = null;
+               File root = null;
+               
+               if (Platform.isLinux() || Platform.isMacOS()) {
+                       file = new File(System.getProperty("user.home"));
+               } else if (Platform.isWindows()) {
+                       file = new File(selectedPath);
+                       root = getSelectedRoot(selectedPath);
+               } else {
+                       throw new IMFatalException(ErrorCode.UNSUPPORTED_PLATFORM);
+               }
+           long usableSpace = file.getUsableSpace();
+           
+           if (usableSpace == 0) {
+               usableSpace = root.getUsableSpace();
+           } 
+           
+           return usableSpace;
+       }
+       
+       private static File getSelectedRoot(String selectedPath) {
+               IFileSystemInformation fsInfo;
+               if (Platform.isLinux() || Platform.isMacOS()) {
+                       fsInfo = new LinuxFileSystemInfo();
+               } else if (Platform.isWindows()) {
+                       fsInfo = new WindowsFileSystemInfo();
+               } else {
+                       fsInfo = new IFileSystemInformation() {
+
+                               @Override
+                               public List<File> getListMounts() {
+                                       // TODO Auto-generated method stub
+                                       return null;
+                               }
+
+                               @Override
+                               public File[] getListDevices() {
+                                       // TODO Auto-generated method stub
+                                       return null;
+                               }
+
+                               @Override
+                               public FileSystemType getFileSystemType(File disk) {
+                                       // TODO Auto-generated method stub
+                                       return FileSystemType.UNKNOWN;
+                               }
+                       };
+               }
+
+               List<File> mountList = fsInfo.getListMounts();
+               if (mountList == null) {
+                       return null;
+               }
+               
+               for (int i = 0; i < mountList.size(); i++) {
+                       if (selectedPath.toLowerCase().startsWith(
+                                       mountList.get(i).getAbsolutePath().toLowerCase())) {
+                               return mountList.get(i);
                        }
                }
                
-               return downloadPackageList;
+               return null;
+       }
+       
+       private static long getInstallPackageSize(PackageSet packageSet) {
+               List<String> listPkgName = new ArrayList<String>();
+               
+               for (Package pkg : packageSet) {
+                       listPkgName.add(pkg.getPackageName());
+               }
+               
+               return controller.getTotalSizeWithDepends(listPkgName, true);
+       }
+       
+       private static boolean installPackages(PackageSet installablePackages, String targetDir)
+                       throws IMExitException {
+               int repeatCount = installablePackages.size();
+               for (int i = repeatCount; i > 0; i--) {
+                       try {
+                               if (InstallManager.getInstance().install(installablePackages, targetDir, null)) {
+                                       return true;
+                               } else {
+                                       return false;
+                               }
+                       } catch (IMNetworkException e) {
+                               Log.ExceptionLog(e);
+                               System.err.println("Network error has occured. retry the download => " + repeatCount);
+                               continue;
+                       }
+               }
+               
+               return false;
        }
 }
index e3621ff..a36f9ee 100644 (file)
@@ -121,6 +121,7 @@ public class Options {
        public static String snapshotPath = null;
        public static ServerType serverType = null;
        public static String distribution = null;
+       public static String targetDir = null;
        
        public static List<String> packages = new ArrayList<String>();
        
@@ -177,32 +178,37 @@ public class Options {
                                if (iter.hasNext()) {
                                        doInstallNoUI = true;
                                        serverType = ServerType.SNAPSHOT;
-                                       repository = iter.next();
-                                       distribution = iter.next();
-                                       while (iter.hasNext()) { 
-                                               packages.add(iter.next());
-                                       }
+                                       workCliOptions(args);
                                } else {
                                        Log.err("-install option must have some arguments.");
-                                       System.err.println("-remove option must have some arguements" +
+                                       System.err.println("-install option must have some arguements" +
                                                        " such as specific meta packages");
                                        throw new IMFatalException(ErrorCode.WRONG_OPTION);
                                }
+
+                               if (!validateArgs()) {
+                                       Log.err("repository : " + repository + ", distribution : "
+                                                       + distribution + ", packages : " + packages);
+                                       System.err.println("-install option must have some arguements"
+                                                                       + " you can see usage using '-help'");
+                                       System.exit(0);
+                               }
+                               break;
                        } else if (arg.equals("-remove")) {
                                if (iter.hasNext()) {
                                        doRemoveNoUI = true;
-                                       while (iter.hasNext()) {
-                                               packages.add(iter.next());
-                                       }
+                                       workCliOptions(args);
                                } else {
                                        Log.err("-remove option must have some arguments.");
                                        System.err.println("-remove option must have some arguements" +
                                                        " such as specific meta packages or 'all'");
                                        throw new IMFatalException(ErrorCode.WRONG_OPTION);
                                }
+                               break;
                        } else if (arg.equals("-help")) {
                                System.out.println("Usage : InstallManager.bin(exe, sh) [-install | -remove]" +
-                                               " [repository] [distribution] [meta package ....]");
+                                               " [-r repository] [-d distribution] [-l SDK target directory(optional)]" +
+                                               " [-p meta package ....]");
                                System.exit(0);
                        } else if(arg.equals("-conf")) {
                                if(iter.hasNext()) {
@@ -313,4 +319,64 @@ public class Options {
                }
                return argsStr;
        }
+       
+       private static boolean validateArgs() {
+               if (Options.repository != null && Options.distribution != null && !Options.packages.isEmpty()) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+               
+       private static void workCliOptions(String[] args) {
+               ArrayList<String> argArray = new ArrayList<String>();
+               for (String t : args) {
+                       if (!t.equalsIgnoreCase("-install") && !t.equalsIgnoreCase("-remove")) {
+                               argArray.add(t);                                
+                       }
+               }
+               
+               Iterator<String> argIter = argArray.iterator();
+
+               // set repository
+               while (argIter.hasNext()) {
+                       if (argIter.next().equalsIgnoreCase("-r")) {
+                               argIter.remove();
+                               Options.repository = argIter.next();
+                               argIter.remove();
+                       }
+               }
+
+               // set target directory
+               argIter = argArray.iterator();
+               while (argIter.hasNext()) {
+                       if (argIter.next().equalsIgnoreCase("-l")) {
+                               argIter.remove();
+                               Options.targetDir = argIter.next();
+                               argIter.remove();
+                       }
+               }
+
+               // set distribution
+               argIter = argArray.iterator();
+               while (argIter.hasNext()) {
+                       if (argIter.next().equalsIgnoreCase("-d")) {
+                               argIter.remove();
+                               Options.distribution = argIter.next();
+                               argIter.remove();
+                       }
+               }
+
+               // set packages
+               argIter = argArray.iterator();
+               while (argIter.hasNext()) {
+                       if (argIter.next().equalsIgnoreCase("-p")) {
+                               argIter.remove();
+                               while (argIter.hasNext()) {
+                                       Options.packages.add(argIter.next());
+                                       argIter.remove();
+                               }
+                       }
+               }
+       }
 }