[Title] cli: support old profiles conversion
authorJihoon Song <jihoon80.song@samsung.com>
Mon, 24 Jun 2013 07:17:05 +0000 (16:17 +0900)
committerJihoon Song <jihoon80.song@samsung.com>
Mon, 24 Jun 2013 07:17:05 +0000 (16:17 +0900)
[Desc.]
[Issue]

Change-Id: Ic5e03ccb88ef6acad74e16accbaec6f6698f6142

org.tizen.cli/src/org/tizen/cli/exec/sign/Main.java

index 8fa31a9..fbb8d0f 100644 (file)
 package org.tizen.cli.exec.sign;
 
 import static org.tizen.common.util.FilenameUtil.addTailingPath;
+import static org.tizen.common.util.IOUtil.tryClose;
 import static org.tizen.common.util.ObjectUtil.nvl;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.text.MessageFormat;
 import java.util.List;
 
@@ -38,8 +42,6 @@ import org.apache.commons.cli.ParseException;
 
 import static org.tizen.cli.exec.LaunchOptionConstants.OPT_NOCHECK;
 import static org.tizen.cli.exec.LaunchOptionConstants.DESC_NOCHECK;
-import static org.tizen.cli.exec.LaunchOptionConstants.OPT_DEVELOP;
-import static org.tizen.cli.exec.LaunchOptionConstants.DESC_DEVELOP_PROFILE;
 
 import org.tizen.cli.exec.AbstractLauncher;
 import org.tizen.cli.exec.Help;
@@ -51,7 +53,11 @@ import org.tizen.common.file.FileHandler.Attribute;
 import org.tizen.common.sign.command.ReadSigningProfileFileCommand;
 import org.tizen.common.sign.command.SignCommand;
 import org.tizen.common.sign.preferences.SigningProfile;
+import org.tizen.common.sign.preferences.SigningProfileContainer;
+import org.tizen.common.sign.preferences.SigningProfileItem;
 import org.tizen.common.util.FilenameUtil;
+import org.tizen.sdblib.util.IOUtil;
+import org.tizen.sdblib.util.StringUtil;
 
 
 /**
@@ -150,7 +156,7 @@ extends AbstractLauncher
 //        opts.addOption( OptionBuilder.hasArg().withLongOpt( OPT_EXCLUDE ).withDescription( DESC_EXCLUDE ).create( OPT_EXCLUDE.substring( 0, 1 ) ) );
         
         opts.addOption( OptionBuilder.hasArg().withLongOpt( OPT_PROFILE ).withDescription( DESC_PROFILE ).create( OPT_PROFILE.substring( 0, 1 ) ) );
-        opts.addOption( OptionBuilder.withLongOpt( OPT_DEVELOP ).withDescription( DESC_DEVELOP_PROFILE ).create( OPT_DEVELOP.substring( 0, 1 ) ) );
+//        opts.addOption( OptionBuilder.withLongOpt( OPT_DEVELOP ).withDescription( DESC_DEVELOP_PROFILE ).create( OPT_DEVELOP.substring( 0, 1 ) ) );
         
         return opts;
     }
@@ -245,6 +251,39 @@ extends AbstractLauncher
         }
         profilesFilePath = convertPath( profilesFilePath );
         
+        SigningProfileContainer container = new SigningProfileContainer() {
+            @Override
+            protected void preprocessForItemLoad(SigningProfile profile,
+                    String version) {
+                // remove automatically insert dist1 for CLI
+            }
+        };
+        InputStream in = getFileHandler().read( profilesFilePath );
+        try {
+            String version = container.readProfileXML( in );
+            if ( version == null ) {
+                getPrompter().error( "Cannot load profiles in " + profilesFilePath );
+                return;
+            }
+            
+            // if old version
+            if ( StringUtil.isEmpty( version ) ) {
+                getPrompter().notify( "Old version profile detected. Converting this automatically." );
+                
+                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+                container.writeProfileXML( outStream );
+                InputStream inForOut = new ByteArrayInputStream( outStream.toByteArray() );
+                
+                try {
+                    getFileHandler().write( profilesFilePath, inForOut );
+                } finally {
+                    tryClose( in );
+                }
+            }
+        } finally {
+            IOUtil.tryClose( in );
+        }
+        
         final ReadSigningProfileFileCommand readProfile = new ReadSigningProfileFileCommand( profilesFilePath, profileName );
         getExecutor().execute( readProfile );
         logger.info( "Profiles file[{}] is read", profilesFilePath );
@@ -256,11 +295,21 @@ extends AbstractLauncher
             return ;
         }
         
-        if ( cmdLine.hasOption( OPT_DEVELOP ) ) {
-            // Always add a developer certificate first because must be "signature1.xml"
-            profile.createProfileItemForDeveloper();
+        for ( int i = 0; i < SigningProfile.MAX_DISTRIBUTOR; i++ ) {
+            SigningProfileItem item = profile.getProfileItem( i );
+            String keyLocation = item.getKeyLocation();
+            if ( ! StringUtil.isEmpty( keyLocation ) ) {
+                String msg = ( i == SigningProfile.AUTHOR_ORDINAL ) ? "Author certficate: " : "Distributor" + i + " certificate : ";
+                getPrompter().notify( msg + item.getKeyLocation() );
+            }
         }
         
+        // deprecated --develop option
+//        if ( cmdLine.hasOption( OPT_DEVELOP ) ) {
+//            // Always add a developer certificate first because must be "signature1.xml"
+//            profile.createProfileItemForDeveloper();
+//        }
+        
         final SignCommand command = new SignCommand( baseDir, profile );
         command.setIncludes( null );
         command.setExcludes( excludes );