From d4750d66e580efd65e6d5ef15d59cd9bc5e99e8d Mon Sep 17 00:00:00 2001 From: Ross Eccleston Date: Fri, 29 Nov 2019 21:10:04 +0000 Subject: [PATCH] Remove reliance on getting product version for model.zip/version.txt from FileVersionInfo and replace with using assembly custom attributes (#4505) --- src/Microsoft.ML.Core/Data/Repository.cs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ML.Core/Data/Repository.cs b/src/Microsoft.ML.Core/Data/Repository.cs index 40d7b7cc47..f82e0e6963 100644 --- a/src/Microsoft.ML.Core/Data/Repository.cs +++ b/src/Microsoft.ML.Core/Data/Repository.cs @@ -7,6 +7,8 @@ using System.Diagnostics; using System.IO; using System.IO.Compression; +using System.Linq; +using System.Reflection; using Microsoft.ML.Internal.Utilities; using Microsoft.ML.Runtime; @@ -305,10 +307,10 @@ public static RepositoryWriter CreateNew(Stream stream, IExceptionContext ectx = Contracts.CheckValueOrNull(ectx); ectx.CheckValue(stream, nameof(stream)); var rep = new RepositoryWriter(stream, ectx, useFileSystem); - var versionInfo = FileVersionInfo.GetVersionInfo(typeof(RepositoryWriter).Assembly.Location); + using (var ent = rep.CreateEntry(DirTrainingInfo, "Version.txt")) using (var writer = Utils.OpenWriter(ent.Stream)) - writer.WriteLine(versionInfo.ProductVersion); + writer.WriteLine(GetProductVersion()); return rep; } @@ -432,6 +434,24 @@ public void Commit() Flush(); Dispose(true); } + + private static string GetProductVersion() + { + var assembly = typeof(RepositoryWriter).Assembly; + + var assemblyInternationalVersionAttribute = assembly.CustomAttributes.FirstOrDefault(a => + a.AttributeType == typeof(AssemblyInformationalVersionAttribute)); + + if (assemblyInternationalVersionAttribute == null) + { + throw new ApplicationException($"Cannot determine product version from assembly {assembly.FullName}."); + } + + return assemblyInternationalVersionAttribute.ConstructorArguments + .First() + .Value + .ToString(); + } } [BestFriend]