From 190582b6dc0828ccbeb589ef0e0e29e479602784 Mon Sep 17 00:00:00 2001 From: mikx Date: Wed, 22 Nov 2023 22:05:05 -0500 Subject: [PATCH] initial commit --- .gitignore | 261 +++++++++++++++++++++++++++++++++ MxB.sln | 25 ++++ MxB/App.config | 6 + MxB/MxB.csproj | 61 ++++++++ MxB/Program.cs | 257 ++++++++++++++++++++++++++++++++ MxB/Properties/AssemblyInfo.cs | 36 +++++ MxB/Utilities/Message.cs | 91 ++++++++++++ MxB/packages.config | 5 + 8 files changed, 742 insertions(+) create mode 100644 .gitignore create mode 100644 MxB.sln create mode 100644 MxB/App.config create mode 100644 MxB/MxB.csproj create mode 100644 MxB/Program.cs create mode 100644 MxB/Properties/AssemblyInfo.cs create mode 100644 MxB/Utilities/Message.cs create mode 100644 MxB/packages.config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c4efe2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,261 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/MxB.sln b/MxB.sln new file mode 100644 index 0000000..9069e4c --- /dev/null +++ b/MxB.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32519.379 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MxB", "MxB\MxB.csproj", "{DC1CB87A-2C00-4AD6-93E4-BE8D4CED9575}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DC1CB87A-2C00-4AD6-93E4-BE8D4CED9575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC1CB87A-2C00-4AD6-93E4-BE8D4CED9575}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC1CB87A-2C00-4AD6-93E4-BE8D4CED9575}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC1CB87A-2C00-4AD6-93E4-BE8D4CED9575}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1298B396-E89C-4B93-A89E-C1728A93E5AF} + EndGlobalSection +EndGlobal diff --git a/MxB/App.config b/MxB/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/MxB/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MxB/MxB.csproj b/MxB/MxB.csproj new file mode 100644 index 0000000..bd8d0ef --- /dev/null +++ b/MxB/MxB.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + {DC1CB87A-2C00-4AD6-93E4-BE8D4CED9575} + Exe + MxB + MxB + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\SharpZipLib.1.3.3\lib\net45\ICSharpCode.SharpZipLib.dll + + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MxB/Program.cs b/MxB/Program.cs new file mode 100644 index 0000000..223eaa0 --- /dev/null +++ b/MxB/Program.cs @@ -0,0 +1,257 @@ +using ICSharpCode.SharpZipLib.Zip; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace MxB +{ + internal class Program + { + public class RootJobs + { + public bool enabled { get; set; } + public string name { get; set; } + public List source { get; set; } + public List destination { get; set; } + public bool zip { get; set; } + public bool inc { get; set; } + } + + public static string version = "1.5.0"; + public static string zipname = ""; + public static string zippath = ""; + static void Main(string[] args) + { + string month = ZeroPad(DateTime.Now.Month); + string day = ZeroPad(DateTime.Now.Day); + string year = ZeroPad(DateTime.Now.Year); + string hour = ZeroPad(DateTime.Now.Hour); + string minute = ZeroPad(DateTime.Now.Minute); + + var lp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + + Utilities.Message.Splash(); + + if (args.Length == 0) + { + var dirl = Directory.EnumerateFiles(@"jobs", "*.json"); + foreach (var f in dirl) + { + RootJobs j = JsonConvert.DeserializeObject(File.ReadAllText($@"{f}")); + Utilities.Message.CMW($"[{j.name}] Processing...", true, 1); + if (j.enabled) + { + int sc = j.source.Count; + int scur = 1; + foreach (var js in j.source) + { + var sourceType = js.Split('|')[0]; + var sourceName = js.Split('|')[1]; + var sourcePath = js.Split('|')[2]; + DirectoryInfo dInfo = new DirectoryInfo(sourcePath); + long sizeOfDir = DirectorySize(dInfo, true); + Utilities.Message.CMW($"[{j.name}][{sourceType}][{((double)sizeOfDir) / (1024 * 1024):N2}MB]{scur}/{sc}] Processing...", true, 1); + if (!Directory.Exists($@"tmp\{j.name}")) { Directory.CreateDirectory($@"tmp\{j.name}"); } + if (!Directory.Exists($@"tmp\{j.name}\{sourceName}")) { Directory.CreateDirectory($@"tmp\{j.name}\{sourceName}"); } + if (args.Contains("--exclude")) { + CopyFilesRecursively(sourcePath, $@"tmp\{j.name}\{sourceName}", args); + } else + { + CopyFilesRecursively(sourcePath, $@"tmp\{j.name}\{sourceName}", null); + } + scur++; + } + foreach (var dest in j.destination) + { + if (!Directory.Exists($@"{dest}\{j.name}")) { Directory.CreateDirectory($@"{dest}\{j.name}"); } + if (j.zip) + { + CreateZip(j.name, day, hour, lp); + File.Copy($@"tmp\{j.name}-D{day}-H{hour}.zip", $@"{dest}\{j.name}\{j.name}-D{day}-H{hour}-M{minute}.zip"); + ClearFolder($@"tmp"); + } + else + { + CopyFilesRecursively($@"tmp\{j.name}", dest, null); + } + } + } + else + { + Utilities.Message.CMW($"[{j.name}] Disabled > Skipping.", true, 1); + } + } + } else if (args.Contains("--job")) + { + string jnarg = args[1]; + RootJobs j = JsonConvert.DeserializeObject(File.ReadAllText($@"{lp}\jobs\{jnarg}.json")); + Utilities.Message.CMW($"[{j.name}] Processing...", true, 1); + if (j.enabled) + { + int sc = j.source.Count; + int scur = 1; + foreach (var js in j.source) + { + var sourceType = js.Split('|')[0]; + var sourceName = js.Split('|')[1]; + var sourcePath = js.Split('|')[2]; + long sizeOfDir = 0; + if (sourceType == "D") { DirectoryInfo dInfo = new DirectoryInfo(sourcePath); sizeOfDir = DirectorySize(dInfo, true); } + if (sourceType == "F") { FileInfo fInfo = new FileInfo(sourcePath); sizeOfDir = FileSize(fInfo); } + + Utilities.Message.CMW($"[{j.name}][{sourceType}][{((double)sizeOfDir) / (1024 * 1024):N2}MB]{scur}/{sc}] Processing...", true, 1); + if (!Directory.Exists($@"tmp\{j.name}")) { Directory.CreateDirectory($@"tmp\{j.name}"); } + switch (sourceType) + { + case "D": + if (!Directory.Exists($@"tmp\{j.name}\{sourceName}")) { Directory.CreateDirectory($@"tmp\{j.name}\{sourceName}"); } + if (args.Contains("--exclude")) + { + CopyFilesRecursively(sourcePath, $@"tmp\{j.name}\{sourceName}", args); + } + else + { + CopyFilesRecursively(sourcePath, $@"tmp\{j.name}\{sourceName}", null); + } + break; + case "F": + if (!Directory.Exists($@"tmp\{j.name}\{sourceName}")) { Directory.CreateDirectory($@"tmp\{j.name}\{sourceName}"); } + string fn = Path.GetFileName(sourcePath); + File.Copy(sourcePath, $@"tmp\{j.name}\{sourceName}\{fn}"); + break; + } + scur++; + } + foreach (var dest in j.destination) + { + if (!Directory.Exists($@"{dest}\{j.name}")) { Directory.CreateDirectory($@"{dest}\{j.name}"); } + if (j.zip) + { + CreateZip(j.name, day, hour, lp); + File.Copy($@"tmp\{j.name}-D{day}-H{hour}.zip", $@"{dest}\{j.name}\{j.name}-D{day}-H{hour}-M{minute}.zip"); + ClearFolder($@"tmp"); + } + else + { + CopyFilesRecursively($@"tmp\{j.name}", dest, null); + } + } + } + else + { + Utilities.Message.CMW($"[{j.name}] Disabled > Skipping.", true, 1); + } + } else if (args.Contains("--clean")) + { + int cc = Convert.ToInt32(args[2]); + string jnarg = args[1]; + RootJobs j = JsonConvert.DeserializeObject(File.ReadAllText($@"{lp}\jobs\{jnarg}.json")); + Utilities.Message.CMW($"[{j.name}] Cleaning...", true, 1); + foreach (var d in j.destination) + { + var directory = new DirectoryInfo(d); + var query = directory.GetFiles("*.zip", SearchOption.AllDirectories); + foreach (var file in query.OrderBy(file => file.CreationTime).Skip(cc)) + { + file.Delete(); + } + } + } + } + + static long FileSize(FileInfo fInfo) + { + // Enumerate all the files + long totalSize = fInfo.Length; + + return totalSize; + + } + + static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir) + { + // Enumerate all the files + long totalSize = dInfo.EnumerateFiles() + .Sum(file => file.Length); + + // If Subdirectories are to be included + if (includeSubDir) + { + // Enumerate all sub-directories + totalSize += dInfo.EnumerateDirectories() + .Sum(dir => DirectorySize(dir, true)); + } + return totalSize; + + } + + private static void CopyFilesRecursively(string sourcePath, string targetPath, string[] exclude) + { + //Now Create all of the directories + foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) + { + Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); + } + + //Copy all the files & Replaces any files with the same name + foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) + { + var ex = Path.GetExtension(newPath); + if (exclude != null) + { + string[] ext = exclude[3].Split(',').ToArray(); + if (!ext.Contains(ex)) + { + File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true); + } + } else + { + File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true); + } + } + } + + private static void ClearFolder(string FolderName) + { + DirectoryInfo dir = new DirectoryInfo(FolderName); + + foreach (FileInfo fi in dir.GetFiles()) + { + fi.Delete(); + } + + foreach (DirectoryInfo di in dir.GetDirectories()) + { + ClearFolder(di.FullName); + di.Delete(); + } + } + + public static void CreateZip(string bn, string day, string hour, string lp) + { + // Compress name in tmp dir. + FastZip fz = new FastZip(); + zipname = $@"{bn}-D{day}-H{hour}.zip"; + zippath = $@"{lp}\tmp\{zipname}"; + fz.CreateZip(zippath, $@"{lp}\tmp\{bn}", true, ""); + } + + public static string ZeroPad(int src) + { + if (src < 10) + { + return $"0{src.ToString()}"; + } + else + { + return src.ToString(); + } + } + } +} diff --git a/MxB/Properties/AssemblyInfo.cs b/MxB/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2990f50 --- /dev/null +++ b/MxB/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MxB")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MxB")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("dc1cb87a-2c00-4ad6-93e4-be8d4ced9575")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/MxB/Utilities/Message.cs b/MxB/Utilities/Message.cs new file mode 100644 index 0000000..36248a3 --- /dev/null +++ b/MxB/Utilities/Message.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MxB.Utilities +{ + internal class Message + { + public static void CMW(string msg, bool time, int color) + { + string seconds = ""; + string minutes = ""; + string hours = ""; + if (DateTime.Now.Second < 10) + { + seconds = String.Format("0{0}", DateTime.Now.Second); + } + else + { + seconds = DateTime.Now.Second.ToString(); + } + + if (DateTime.Now.Minute < 10) + { + minutes = String.Format("0{0}", DateTime.Now.Minute); + } + else + { + minutes = DateTime.Now.Minute.ToString(); + } + + if (DateTime.Now.Hour < 10) + { + hours = String.Format("0{0}", DateTime.Now.Hour); + } + else + { + hours = DateTime.Now.Hour.ToString(); + } + + string date = String.Format("{0}:{1}:{2}", hours, minutes, seconds); + + File.AppendAllText("mxg.logs", String.Format("[{0}] {1}", date, msg) + Environment.NewLine); + + //color switch + ConsoleColor cc = ConsoleColor.White; + switch (color) + { + case 0: + //nothing + break; + case 1: + cc = ConsoleColor.Cyan; + break; + case 2: + cc = ConsoleColor.Green; + break; + case 3: + cc = ConsoleColor.Red; + break; + } + + if (time) + { + Console.ForegroundColor = cc; + Console.WriteLine(String.Format("[{0}] {1}", date, msg)); + } + else + { + Console.ForegroundColor = cc; + Console.WriteLine(String.Format("")); + } + + Console.ForegroundColor = ConsoleColor.White; + } + + public static void Splash() + { + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine(""); + Console.WriteLine("#### MxB"); + Console.WriteLine(string.Format("#### VERSION: {0}", Program.version)); + Console.WriteLine("#### DEV: mikx"); + Console.WriteLine(""); + Console.ForegroundColor = ConsoleColor.White; + } + } +} diff --git a/MxB/packages.config b/MxB/packages.config new file mode 100644 index 0000000..2defa35 --- /dev/null +++ b/MxB/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file