Major upgrade
This commit is contained in:
227
Source/Program/Common/Lib/FileAndFolderManager.cs
Normal file
227
Source/Program/Common/Lib/FileAndFolderManager.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
// UNCLASSIFIED
|
||||
/*-------------------------------------------------------------------------
|
||||
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
|
||||
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
|
||||
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
|
||||
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
|
||||
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
|
||||
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
|
||||
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
|
||||
COMPANY.
|
||||
|
||||
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
|
||||
GOVERNMENT.
|
||||
|
||||
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
|
||||
-------------------------------------------------------------------------*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using Raytheon.Common;
|
||||
|
||||
namespace ProgramLib
|
||||
{
|
||||
/// <summary>
|
||||
/// Partial class... this is the main class responsible for parsing config.ini in order
|
||||
/// to construct file paths and folder paths
|
||||
/// </summary>
|
||||
internal partial class FileAndFolderManager
|
||||
{
|
||||
private Dictionary<Folders, string> foldersDict = new Dictionary<Folders, string>();
|
||||
private Dictionary<Files, string> filesDict = new Dictionary<Files, string>();
|
||||
|
||||
private IConfigurationFile _programGeneralConfig;
|
||||
private IConfigurationFile _programSpecificConfig;
|
||||
private bool _isThereHardware;
|
||||
private string _configSubFolderName;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="programGeneralConfig">the UUT part number</param>
|
||||
public FileAndFolderManager(IConfigurationFile programGeneralConfig, out IConfigurationFile programSpecificConfig, bool isThereHardware, string configSubFolderName)
|
||||
{
|
||||
_programGeneralConfig = programGeneralConfig;
|
||||
_isThereHardware = isThereHardware;
|
||||
_configSubFolderName = configSubFolderName;
|
||||
|
||||
ConstructFolderPaths();
|
||||
CreateFolders();
|
||||
ConstructFilePaths();
|
||||
|
||||
programSpecificConfig = _programSpecificConfig;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor
|
||||
/// </summary>
|
||||
~FileAndFolderManager()
|
||||
{
|
||||
if (foldersDict.ContainsKey(Folders.DATA_TEST))
|
||||
{
|
||||
if (Directory.Exists(foldersDict[Folders.DATA_TEST]) && Directory.Exists(foldersDict[Folders.DATA]))
|
||||
{
|
||||
// delete all empty test folders
|
||||
if (foldersDict[Folders.DATA_TEST].Contains(foldersDict[Folders.DATA]))
|
||||
{
|
||||
string parentFolder = Path.GetFullPath(Path.Combine(foldersDict[Folders.DATA_TEST], ".."));
|
||||
|
||||
while (!String.Equals(parentFolder, foldersDict[Folders.DATA], StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string[] dirs = Directory.GetDirectories(parentFolder);
|
||||
|
||||
foreach (string dir in dirs)
|
||||
{
|
||||
if (Util.IsDirectoryEmpty(dir))
|
||||
Directory.Delete(dir, true);
|
||||
}
|
||||
|
||||
if (Util.IsDirectoryEmpty(parentFolder))
|
||||
Directory.Delete(parentFolder, true);
|
||||
|
||||
parentFolder = Path.GetFullPath(Path.Combine(parentFolder, ".."));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ConstructTestFolder(UutInfo uutInfo, TestInfo testInfo)
|
||||
{
|
||||
string buildLevel = uutInfo.UutBuildLevel.ToString();
|
||||
if (uutInfo.UutBuildLevel == UutInfo.BuildLevel.NOT_SET)
|
||||
{
|
||||
buildLevel = "NO_UUT";
|
||||
}
|
||||
foldersDict[Folders.DATA_TEST] = Path.Combine(foldersDict[Folders.DATA], buildLevel, uutInfo.PartNumber,
|
||||
uutInfo.SerialNumber, testInfo.TestType.ToLower(), testInfo.TestStartDateTime.ToString("yyyy_MM_dd"),
|
||||
testInfo.TestName, testInfo.TestStartDateTime.ToString("HH_mm_ss"));
|
||||
|
||||
CreateFolders();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build folder paths so we can acccess them later
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void ConstructFolderPaths()
|
||||
{
|
||||
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
|
||||
string configHardwareOrSimSubFolderName = _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.SIM_CONFIG_FOLDER_NAME.ToString());
|
||||
if (_isThereHardware)
|
||||
configHardwareOrSimSubFolderName = _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.HARDWARE_CONFIG_FOLDER_NAME.ToString());
|
||||
|
||||
foldersDict[Folders.CONFIG] = Path.Combine(assemblyFolder, GeneralConstants.ConfigFolderName, configHardwareOrSimSubFolderName, _configSubFolderName);
|
||||
|
||||
string measurementConfigFolderName = _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.MEASUREMENT_CONFIG_FOLDER_NAME.ToString());
|
||||
foldersDict[Folders.CONFIG_MEASUREMENT] = Path.Combine(foldersDict[Folders.CONFIG], measurementConfigFolderName);
|
||||
|
||||
string programSpecificConfigFilePath = Path.Combine(GetFolder(Folders.CONFIG), _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.PROGRAM_SPECIFIC_CONFIG_FILE_NAME.ToString()));
|
||||
_programSpecificConfig = new ConfigurationFile(programSpecificConfigFilePath);
|
||||
|
||||
string dataRootPath;
|
||||
|
||||
// If primary drive exists on machine, all TE software files and folders should be located on that drive
|
||||
string drive = _programSpecificConfig.ReadValue(ProgramSpecificConfigIni.GENERAL.ToString(), ProgramSpecificConfigIni.PRIMARY_DRIVE.ToString());
|
||||
if (!Directory.Exists(drive))
|
||||
{
|
||||
drive = _programSpecificConfig.ReadValue(ProgramSpecificConfigIni.GENERAL.ToString(), ProgramSpecificConfigIni.SECONDARY_DRIVE.ToString());
|
||||
}
|
||||
dataRootPath = Path.Combine(drive, _programSpecificConfig.ReadValue(ProgramSpecificConfigIni.GENERAL.ToString(), ProgramSpecificConfigIni.DATA_BASE_FOLDER.ToString()));
|
||||
|
||||
if (!Path.IsPathRooted(dataRootPath))
|
||||
dataRootPath = Path.Combine(assemblyFolder, dataRootPath);
|
||||
else
|
||||
{
|
||||
Match regexMatch;
|
||||
|
||||
regexMatch = Regex.Match(dataRootPath, @"^([a-zA-Z]:\\).+", RegexOptions.IgnoreCase);
|
||||
|
||||
if (regexMatch.Success)
|
||||
{
|
||||
string driveLetter = regexMatch.Groups[1].Value;
|
||||
|
||||
if (!Directory.Exists(driveLetter))
|
||||
{
|
||||
|
||||
string err = $@"Invalid drive: {driveLetter}. Please provide a valid drive, ie C:\. File: {programSpecificConfigFilePath}. Section: {ProgramSpecificConfigIni.GENERAL.ToString()}. Key: {ProgramSpecificConfigIni.PRIMARY_DRIVE.ToString()} and Key: {ProgramSpecificConfigIni.SECONDARY_DRIVE.ToString()} were invalid drives.";
|
||||
|
||||
throw new Exception(err);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string err = $@"Invalid path: {dataRootPath}. Please specify desired folder name, i.e. Data. File: {programSpecificConfigFilePath}. Section: {ProgramSpecificConfigIni.GENERAL.ToString()}. Key: {ProgramSpecificConfigIni.DATA_BASE_FOLDER.ToString()}";
|
||||
|
||||
throw new Exception(err);
|
||||
}
|
||||
}
|
||||
|
||||
foldersDict[Folders.DATA] = Path.GetFullPath(dataRootPath);
|
||||
|
||||
string val = _programSpecificConfig.ReadValue(ProgramSpecificConfigIni.GENERAL.ToString(), ProgramSpecificConfigIni.DATA_GENERAL_FOLDER_NAME.ToString());
|
||||
foldersDict[Folders.DATA_GENERAL] = Path.Combine(dataRootPath, val);
|
||||
|
||||
val = _programSpecificConfig.ReadValue(ProgramSpecificConfigIni.GENERAL.ToString(), ProgramSpecificConfigIni.DATA_GENERAL_TEMP_FOLDER_NAME.ToString());
|
||||
foldersDict[Folders.DATA_GENERAL_TEMP] = Path.Combine(foldersDict[Folders.DATA_GENERAL], val);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// We create all the necessary folders
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void CreateFolders()
|
||||
{
|
||||
// create all the folders if they don't exist
|
||||
foreach (KeyValuePair<Folders, string> entry in foldersDict)
|
||||
{
|
||||
Directory.CreateDirectory(entry.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build file paths so we can acccess them later
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void ConstructFilePaths()
|
||||
{
|
||||
string nlogFileNamePrefix = _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.NLOG_FILE_NAME_PREFIX.ToString());
|
||||
filesDict[Files.NLOG_TEMP] = Path.Combine(GetFolder(FileAndFolderManager.Folders.DATA_GENERAL_TEMP), _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.NLOG_FOLDER_NAME.ToString()), DateTime.Now.ToString("yyyy_MM_dd"), Util.GenerateUniqueFilenameUsingDateTime(nlogFileNamePrefix, "log"));
|
||||
filesDict[Files.CABLE_SELF_TEST_RUN_LOG] = Path.Combine(GetFolder(FileAndFolderManager.Folders.DATA_GENERAL), _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.CABLE_SELF_TEST_RUN_LOG_FILE_NAME.ToString()));
|
||||
filesDict[Files.TEST_RUN_LOG] = Path.Combine(GetFolder(FileAndFolderManager.Folders.DATA_GENERAL), _programGeneralConfig.ReadValue(ProgramGeneralConfigIni.GENERAL.ToString(), ProgramGeneralConfigIni.TEST_RUN_LOG_FILE_NAME.ToString()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the full folder path
|
||||
/// </summary>
|
||||
/// <param name="folder"></param>
|
||||
/// <returns></returns>
|
||||
public string GetFolder(Folders folder)
|
||||
{
|
||||
if (foldersDict.ContainsKey(folder))
|
||||
{
|
||||
return foldersDict[folder];
|
||||
}
|
||||
else
|
||||
throw new Exception($"{folder.ToString()} is invalid");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the full file path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetFile(Files file)
|
||||
{
|
||||
if (filesDict.ContainsKey(file))
|
||||
{
|
||||
return filesDict[file];
|
||||
}
|
||||
else
|
||||
throw new Exception($"{file.ToString()} is invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user