crashanalysercmd/UI/CrashServer/Engine/Inputs/CACmdLineInputParameters.cs
changeset 0 818e61de6cd1
child 2 0c91f0baec58
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Text;
       
    19 using System.IO;
       
    20 using System.Reflection;
       
    21 using System.Collections.Generic;
       
    22 using SymbianUtils.Tracer;
       
    23 using CrashItemLib.Engine;
       
    24 using CrashItemLib.Crash;
       
    25 using CrashItemLib.Crash.Container;
       
    26 using CrashItemLib.PluginAPI;
       
    27 using SymbianUtils.FileSystem.Utilities;
       
    28 using SymbianXmlInputLib.Parser;
       
    29 using SymbianXmlInputLib.Parser.Nodes;
       
    30 using SymbianXmlInputLib.Elements;
       
    31 using SymbianXmlInputLib.Elements.Types.Category;
       
    32 using SymbianXmlInputLib.Elements.Types.Extension;
       
    33 using SymbianXmlInputLib.Elements.Types.FileSystem;
       
    34 using SymbianXmlInputLib.Elements.Types.Command;
       
    35 using CrashItemLib.Sink;
       
    36 using System.Text.RegularExpressions;
       
    37 using System.Globalization;
       
    38 
       
    39 
       
    40 namespace CrashAnalyserServerExe.Engine
       
    41 {
       
    42 	internal class CACmdLineInputParameters
       
    43 	{
       
    44 		#region Constructors
       
    45         public CACmdLineInputParameters( ITracer aTracer )
       
    46 		{
       
    47             iTracer = aTracer;
       
    48 
       
    49             Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
       
    50             iAppPath = Path.GetDirectoryName( myAssembly.Location );
       
    51 
       
    52             // CHECKME:
       
    53             // Source files are identified from command line current working directory.
       
    54             DirectoryInfo sourceDir = new DirectoryInfo( Environment.CurrentDirectory );
       
    55             iSources.AddRange( sourceDir.GetFiles( "*.bin", SearchOption.TopDirectoryOnly ) );
       
    56 
       
    57             // CHECKME:
       
    58             // Read selge.ini from tool directory or CWD
       
    59             FindDebugMetaDataFile( KDebugMetaDataFileSelgeIni );
       
    60 
       
    61             // CHECKME:
       
    62             // Read selge_event.ini from tool directory or CWD
       
    63             FindDebugMetaDataFile( KDebugMetaDataFileSelgeEventIni );
       
    64 
       
    65             // Sink parameters are fairly simple
       
    66             Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
       
    67             string commandLine = System.Environment.CommandLine;
       
    68             iSinkParams = new CISinkSerializationParameters( version, commandLine );
       
    69             iSinkParams.DetailLevel = CISinkSerializationParameters.TDetailLevel.EFull;
       
    70             iSinkParams.FileExtensionFailed = ".corrupt_ci";
       
    71             iSinkParams.FileExtensionFailed = ".ci";
       
    72 
       
    73             // CHECKME:
       
    74             // The output data is written to the same directory as the input file.
       
    75             iSinkParams.OutputDirectory = sourceDir;
       
    76         }
       
    77         #endregion
       
    78 
       
    79         #region Command line parsing
       
    80         public bool ParseCommandLine()
       
    81         {
       
    82             bool retval = true;
       
    83             string commandLine = System.Environment.CommandLine;
       
    84 
       
    85             string ppattern = @"\s*(-.\s*[^-]*)";
       
    86             MatchCollection matches = Regex.Matches(commandLine, ppattern, RegexOptions.None);
       
    87             if (matches.Count < 1)
       
    88             {
       
    89                 PrintCommandHelp();
       
    90                 System.Console.WriteLine("Error: No valid parameters given");                
       
    91                 throw new CACmdLineException("Error: No valid parameters given", CACmdLineException.KErrCommandLineError);
       
    92             }
       
    93             foreach (Match parameter in matches)
       
    94             {
       
    95                 Regex pparser = new Regex(@"(?<id>-.)\s*(?<content>.*)");
       
    96 
       
    97                 Match m = pparser.Match(parameter.ToString());
       
    98                 if (m.Success)
       
    99                 {
       
   100                     GroupCollection groups = m.Groups;
       
   101                     string paramId = m.Result("${id}").Trim();
       
   102                     string paramContent = m.Result("${content}").Trim();
       
   103 
       
   104                     if (paramId == "-a")
       
   105                     {
       
   106                         ArchivePath = paramContent;
       
   107                     }
       
   108                     if (paramId == "-s")
       
   109                     {
       
   110                         SkippedPath = paramContent;
       
   111                         ErrorPath = paramContent + @"\errors";
       
   112                     }
       
   113                     if (paramId == "-f")
       
   114                     {
       
   115                         DecodeWithoutSymbols = true;
       
   116                     }
       
   117                     if (paramId == "-t")
       
   118                     {
       
   119                         TestWithoutMovingFiles = true;
       
   120                     }
       
   121 
       
   122                 }
       
   123                 else
       
   124                 {
       
   125                     System.Console.WriteLine("Error: No parameters found");
       
   126                     retval = false;
       
   127                 }          
       
   128 
       
   129             }
       
   130 
       
   131             //Parameter scanning finished - validate content
       
   132             if (ArchivePath == string.Empty)
       
   133             {
       
   134                 System.Console.WriteLine("Error: No archive path given");
       
   135                 retval = false;
       
   136             }
       
   137             else if (!Directory.Exists(ArchivePath))
       
   138             {
       
   139                 System.Console.WriteLine("Error: Archive path " + ArchivePath +" cannot be found");
       
   140                 retval = false;
       
   141           
       
   142             }
       
   143             if (SkippedPath == string.Empty)
       
   144             {
       
   145                 System.Console.WriteLine("Error: No skipped file path given");
       
   146                 retval = false;
       
   147             }
       
   148             else if (!Directory.Exists(SkippedPath))
       
   149             {
       
   150                 System.Console.WriteLine("Error: Skipped path " + SkippedPath + " cannot be found");
       
   151                 retval = false;
       
   152             }
       
   153             else //skipped path exists, create error path if not there
       
   154             {
       
   155                 if (!Directory.Exists(ErrorPath))
       
   156                 {
       
   157                     Directory.CreateDirectory(ErrorPath);
       
   158                 }
       
   159                 
       
   160             }
       
   161 
       
   162             //make sure paths are absolute
       
   163             iArchivePath = Path.GetFullPath(iArchivePath);
       
   164             iSkippedPath = Path.GetFullPath(iSkippedPath);
       
   165             iErrorPath = Path.GetFullPath(iErrorPath);
       
   166 
       
   167             //add weekly division        
       
   168             DateTime today = DateTime.Now;
       
   169             CultureInfo culInfo = CultureInfo.CurrentCulture;
       
   170             int weekNum = culInfo.Calendar.GetWeekOfYear(today, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
       
   171             int year = culInfo.Calendar.GetYear(today);
       
   172 
       
   173             iArchivePath = iArchivePath + @"\" + year + "_" + weekNum.ToString().PadLeft(2, '0');
       
   174             iSkippedPath = iSkippedPath + @"\" + year + "_" + weekNum.ToString().PadLeft(2, '0');
       
   175             iErrorPath = iErrorPath + @"\" + year + "_" + weekNum.ToString().PadLeft(2, '0');
       
   176 
       
   177 
       
   178             if (TestWithoutMovingFiles)
       
   179             {
       
   180                 System.Console.WriteLine("Test mode parameter -t given: Not moving any files!" );
       
   181                 retval = true;
       
   182             }
       
   183             else if (retval) //Archive & skipped directories exsits, clean up paths and add week numbers
       
   184             {
       
   185                 
       
   186                 if (!Directory.Exists(iArchivePath))
       
   187                 {
       
   188                     Directory.CreateDirectory(iArchivePath);
       
   189                 }
       
   190                 if (!Directory.Exists(iSkippedPath))
       
   191                 {
       
   192                     Directory.CreateDirectory(iSkippedPath);
       
   193                 }
       
   194                 if (!Directory.Exists(iErrorPath))
       
   195                 {
       
   196                     Directory.CreateDirectory(iErrorPath);
       
   197                 }                    
       
   198             }
       
   199             else
       
   200             {
       
   201                 PrintCommandHelp();
       
   202             }
       
   203             System.Console.WriteLine("Using archive path " + ArchivePath + ", skipped path " + SkippedPath + " and error path " + ErrorPath);
       
   204     
       
   205 
       
   206             return retval;
       
   207         }
       
   208 
       
   209         private void PrintCommandHelp()
       
   210         {
       
   211             System.Console.WriteLine("Command line parameters:");
       
   212             System.Console.WriteLine("-a C:\\folderarchive\\   --Location where to move files to permanent archive");
       
   213             System.Console.WriteLine("-s C:\\folder\\skipped\\  --Location whére to put skipped files to wait reprocessing");
       
   214             System.Console.WriteLine("-f --Force decoding even if files are without symbols");
       
   215             System.Console.WriteLine("-t --Test mode, will not move any files, ignores -a and -s");
       
   216 
       
   217         }
       
   218 
       
   219         #endregion
       
   220 
       
   221         #region API
       
   222         #endregion
       
   223 
       
   224         #region Properties
       
   225         public CISinkSerializationParameters SinkParameters
       
   226         {
       
   227             get { return iSinkParams; }
       
   228         }
       
   229 
       
   230         public CACmdLineFSEntityList<CACmdLineFileSource> SourceFiles
       
   231         {
       
   232             get { return iSources; }
       
   233         }
       
   234 
       
   235         public CACmdLineFSEntityList<CACmdLineFSEntity> MetaDataFiles
       
   236         {
       
   237             get { return iMetaData; }
       
   238         }
       
   239 
       
   240         public string ArchivePath
       
   241         {
       
   242             get { return iArchivePath; }
       
   243             set { iArchivePath = value; }
       
   244         }
       
   245         public string SkippedPath
       
   246         {
       
   247             get { return iSkippedPath; }
       
   248             set { iSkippedPath = value; }
       
   249         }
       
   250         public string ErrorPath
       
   251         {
       
   252             get { return iErrorPath; }
       
   253             set { iErrorPath = value; }
       
   254         }
       
   255         public bool DecodeWithoutSymbols
       
   256         {
       
   257             get { return iDecodeWithoutSymbols; }
       
   258             set { iDecodeWithoutSymbols = value; }
       
   259         }
       
   260         public bool TestWithoutMovingFiles
       
   261         {
       
   262             get { return iTestWithoutMovingFiles; }
       
   263             set { iTestWithoutMovingFiles = value; }
       
   264         }
       
   265 
       
   266         #endregion
       
   267 
       
   268         #region Internal constants
       
   269         private const string KDebugMetaDataFileSelgeIni = "selge.ini";
       
   270         private const string KDebugMetaDataFileSelgeEventIni = "selge_event.ini";
       
   271         #endregion
       
   272 
       
   273         #region Internal methods
       
   274         private void FindDebugMetaDataFile( string aFileName )
       
   275         {
       
   276             // First try current working directory, and if not, then try application path.
       
   277             DirectoryInfo sourceDir = new DirectoryInfo( Environment.CurrentDirectory );
       
   278 
       
   279             string fileName = Path.Combine( sourceDir.FullName, aFileName );
       
   280             FileInfo file = new FileInfo( fileName );
       
   281             if ( file.Exists )
       
   282             {
       
   283                 iMetaData.Add( file );
       
   284             }
       
   285             else
       
   286             {
       
   287                 // Try app path
       
   288                 fileName = Path.Combine(iAppPath, aFileName);
       
   289                 file = new FileInfo( fileName );
       
   290                 if ( file.Exists )
       
   291                 {
       
   292                     iMetaData.Add( file );
       
   293                 }
       
   294                 else
       
   295                 {
       
   296                     iTracer.Trace( "WARNING: Could not find debug meta data file: " + aFileName );
       
   297                 }
       
   298             }
       
   299         }
       
   300         #endregion
       
   301 
       
   302 
       
   303         #region Data members
       
   304         private readonly ITracer iTracer;
       
   305         private readonly string iAppPath;
       
   306         private readonly CISinkSerializationParameters iSinkParams;
       
   307         private CACmdLineFSEntityList<CACmdLineFSEntity> iMetaData = new CACmdLineFSEntityList<CACmdLineFSEntity>();
       
   308         private CACmdLineFSEntityList<CACmdLineFileSource> iSources = new CACmdLineFSEntityList<CACmdLineFileSource>();
       
   309 
       
   310         private string iArchivePath = string.Empty;       
       
   311         private string iSkippedPath = string.Empty;      
       
   312         private string iErrorPath = string.Empty;    
       
   313         private bool iTestWithoutMovingFiles = false;
       
   314         private bool iDecodeWithoutSymbols = false;
       
   315 
       
   316 
       
   317 
       
   318   
       
   319 
       
   320         #endregion
       
   321 	}
       
   322 }