File size: 12,671 Bytes
de0d85f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | // Copyright Epic Games, Inc.All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutomationTool;
using EpicGame;
using EpicGames.Core;
using Microsoft.Extensions.Logging;
using static AutomationTool.CommandUtils;
namespace LyraTest
{
[Help("Commandlet for checking content to know if passes all the existing UEditorValidators.")]
[Help("Branch=<Name>", "Branch to use")]
[Help("CL=<value>", "Check file changes in the range LastCL,CL")]
[Help("p4shelved", "If specified, treat CL as a shelved file to check the contents")]
[Help("p4opened", "Check currently opened files instead of using CL ranged")]
[Help("MaxPackagesToLoad=<value>", "Maximum number of recent changes to check")]
[Help("LastGoodContentCLPath=<value>", "A directory location to store the 'last good' CL so we can determine CLs between runs.")]
[Help("SkipPrevCLFileExport", "Skip exporting the 'last good' CL.")]
public class LyraContentValidation : BuildCommand
{
public override void ExecuteBuild()
{
int ThisCLInt = 0;
int PrevCL = 0;
bool CheckOpenedFiles = ParseParam("opened");
bool CheckShelvedFiles = ParseParam("shelved");
string CommandletArgs = "";
bool RunCommandlet = false;
string PrevCLFilePath = "";
bool SkipPrevCLFileExport = ParseParam("SkipPrevCLFileExport");
//@TODO: Should derive these, otherwise it will break as soon as someone clones the projects
string GameProjectDirectory = "Samples/Games/Lyra";
string GameProject = "Lyra.uproject";
string ExtensionTypeListParam = ParseParamValue("ExtTypeList", ".uasset,.umap,.cpp,.c,.h,.inl,.ini,.uproject,.uplugin,.json");
List<string> ExtensionTypeList = new List<string>();
if (string.IsNullOrEmpty(ExtensionTypeListParam))
{
Logger.LogInformation("No extensions were passed in, defaulting to always run. Set -ExtTypeList to the extension typelist for triggering the commandlet");
RunCommandlet = true;
}
else
{
ExtensionTypeList = ExtensionTypeListParam.Split(',').ToList();
}
// if not checking open files, use CL ranges
if (CheckOpenedFiles)
{
// Quickly check if the CL has any files we're interested in. This is significantly faster than firing up the editor to do
// nothing
RunCommandlet = AreFileTypesModifiedInOpenChangelists(ExtensionTypeList);
// save anyone who forgot to run this without -p4
if (!P4Enabled)
{
throw new AutomationException("This script must be executed with -p4");
}
CommandletArgs = "-P4Opened -P4Client=" + P4Env.Client;
}
else
{
string ThisCL = ParseParamValue("CL");
if (string.IsNullOrEmpty(ThisCL))
{
throw new AutomationException("-CL=<num> or -opened must be specified.");
}
if (!int.TryParse(ThisCL, out ThisCLInt))
{
throw new AutomationException("-CL must be a number.");
}
if (CheckShelvedFiles)
{
// Quickly check if the CL has any files we're interested in. This is significantly faster than firing up the editor to do
// nothing
RunCommandlet = AreFileTypesModifiedInShelvedChangelist(ThisCL, ExtensionTypeList);
// filter is what's passed to p4 files. If shelved we'll use the syntax that pulls the shelved file list
CommandletArgs = String.Format("-P4Filter=@={0}", ThisCL);
}
else
{
string Branch = ParseParamValue("Branch");
if (string.IsNullOrEmpty(Branch))
{
throw new AutomationException("-Branch must be specified to check a CL range when -opened or -shelved are not present");
}
string LastGoodContentCLPath = ParseParamValue("LastGoodContentCLPath");
if (string.IsNullOrEmpty(LastGoodContentCLPath))
{
// Default to local storage for this file (Legacy behavior)
LastGoodContentCLPath = CombinePaths(CmdEnv.LocalRoot, "Engine", "Saved");
}
string PrevCLFileName = "PrevCL_" + Branch.Replace("/", "+") + ".txt";
PrevCLFilePath = CombinePaths(LastGoodContentCLPath, PrevCLFileName);
PrevCL = ReadPrevCLFile(PrevCLFilePath);
if (PrevCL <= 0)
{
Logger.LogInformation("Previous CL file didn't exist. Defaulting to none!");
RunCommandlet = true;
}
else if (PrevCL >= ThisCLInt)
{
Logger.LogInformation("Previous CL file shows a CL equal or newer than the current CL. This content was already checked. Skipping.");
RunCommandlet = false;
}
else
{
// +1 to the previous cl so it won't use content from the previous change
PrevCL++;
CommandletArgs = String.Format("-P4Filter={0}/{1}/...@{2},{3}", Branch, GameProjectDirectory, PrevCL, ThisCL);
Logger.LogInformation("Generated Filter: {CommandletArgs}", CommandletArgs);
RunCommandlet = WereFileTypesModifiedInChangelistRange(Branch, PrevCL, ThisCL, ExtensionTypeList);
}
if (!RunCommandlet)
{
Logger.LogInformation("No files in CL Range {PrevCL} -> {ThisCL} contained any files ending with extensions {ExtensionTypeListParam}, or they were already checked in a previous job, skipping commandlet run", PrevCL, ThisCL, ExtensionTypeListParam);
}
}
}
if (RunCommandlet)
{
string EditorExe = "UnrealEditor.exe";
EditorExe = AutomationTool.HostPlatform.Current.GetUnrealExePath(EditorExe);
string PerforceToken = P4.GetAuthenticationToken();
CommandletArgs += " -ini:Engine:[Core.System]:AssetLogShowsDiskPath=True -LogCmds=\"LogHttp Error\" ";
// Do we have a valid perforce token?
//if (!string.IsNullOrEmpty(PerforceToken))
{
CommandletArgs += String.Format(" -SCCProvider={0} -P4Port={1} -P4User={2} -P4Client={3} -P4Passwd={4}", "Perforce", P4Env.ServerAndPort, P4Env.User, P4Env.Client, PerforceToken);
// If skip export was specified (e.g. a preflight) don't export anything
}
string MaxPackagesToLoad = ParseParamValue("MaxPackagesToLoad", "2000");
CommandletArgs += String.Format(" -MaxPackagesToLoad={0}", MaxPackagesToLoad);
CommandUtils.RunCommandlet(new FileReference(CombinePaths(CmdEnv.LocalRoot, GameProjectDirectory, GameProject)), EditorExe, "ContentValidationCommandlet", CommandletArgs);
}
// Read the previous CL file one more time before writing to it, in case it changed while we were running
if (SkipPrevCLFileExport)
{
Logger.LogInformation("Not writing PrevCLFile as -SkipPrevCLFileExport was specified");
}
else
{
ExportPrevCL(PrevCL, ThisCLInt, PrevCLFilePath);
}
}
/// <summary>
/// Exports 'last good' CL information by reading the previous CL file before writing the current CL to it, in case it changed while we were running
/// </summary>
/// <param name="PrevCL"></param>
/// <param name="ThisCL"></param>
/// <param name="PrevCLFilePath"></param>
private void ExportPrevCL(int PrevCL, int ThisCL, string PrevCLFilePath)
{
if (ThisCL > 0)
{
if (PrevCL < ThisCL)
{
PrevCL = ReadPrevCLFile(PrevCLFilePath);
}
if (PrevCL < ThisCL)
{
Logger.LogInformation("Writing PrevCLFile {PrevCLFilePath}...", PrevCLFilePath);
WritePrevCLFile(PrevCLFilePath, ThisCL.ToString());
}
else
{
Logger.LogInformation("Not writing PrevCLFile {PrevCLFilePath}. The current CL was not newer", PrevCLFilePath);
}
}
else
{
Logger.LogInformation("Not writing PrevCLFile {PrevCLFilePath} as the current CL {ThisCL} was invalid", PrevCLFilePath, ThisCL);
}
}
private int ReadPrevCLFile(string PrevCLFilePath)
{
int RetVal = 0;
if (File.Exists(PrevCLFilePath))
{
string PrevCLString = "";
int RetryCount = 10;
bool bProceed = false;
do
{
try
{
PrevCLString = File.ReadAllText(PrevCLFilePath);
bProceed = true;
}
catch (Exception Ex)
{
if (RetryCount > 0)
{
Logger.LogInformation("Failed to read PrevCLFilePath {PrevCLFilePath}. Retrying in a few seconds. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
RetryCount--;
Thread.Sleep(TimeSpan.FromSeconds(5));
}
else
{
Logger.LogError("Failed to read PrevCLFilePath {PrevCLFilePath}. All Retries exhausted, skipping. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
bProceed = true;
}
}
} while (!bProceed);
if (int.TryParse(PrevCLString, out RetVal))
{
// Read the file successfully, and it was a number
}
else
{
Logger.LogWarning("{Text}", "Couldn't parse out the changelist number from the saved PrevCLFilePath file. " + PrevCLFilePath);
}
}
return RetVal;
}
private void WritePrevCLFile(string PrevCLFilePath, string ThisCL)
{
int RetryCount = 10;
bool bProceed = false;
do
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(PrevCLFilePath));
File.WriteAllText(PrevCLFilePath, ThisCL);
bProceed = true;
}
catch (Exception Ex)
{
if (RetryCount > 0)
{
Logger.LogInformation("Failed to write PrevCLFilePath {PrevCLFilePath}. Retrying in a few seconds. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
RetryCount--;
Thread.Sleep(TimeSpan.FromSeconds(5));
}
else
{
Logger.LogError("Failed to write PrevCLFilePath {PrevCLFilePath}. All Retries exhausted, skipping. Ex:{Arg1}", PrevCLFilePath, Ex.Message);
bProceed = true;
}
}
} while (!bProceed);
}
/// <summary>
/// Returns true if files with extensions in the provided list were modified in the specified changelist range
/// </summary>
/// <param name="Branch"></param>
/// <param name="PrevCL"></param>
/// <param name="ThisCL"></param>
/// <param name="ExtensionTypeList"></param>
/// <returns></returns>
private bool WereFileTypesModifiedInChangelistRange(string Branch, int PrevCL, string ThisCL, List<string> ExtensionTypeList)
{
// we don't need to do any of this if there was no extensions typelist passed in
if (ExtensionTypeList.Count != 0)
{
// check all the changes in FN from PrevCL to now
List<P4Connection.ChangeRecord> ChangeRecords;
CommandUtils.P4.Changes(out ChangeRecords, string.Format("{0}/...@{1},{2}", Branch, PrevCL, ThisCL), false);
foreach (P4Connection.ChangeRecord Record in ChangeRecords)
{
P4Connection.DescribeRecord DescribeRecord;
CommandUtils.P4.DescribeChangelist(Record.CL, out DescribeRecord, false);
// check all the files in each cl record
foreach (P4Connection.DescribeRecord.DescribeFile File in DescribeRecord.Files)
{
// if any of them end in extensions in our typelist, we need to build
foreach (string Extension in ExtensionTypeList)
{
if (File.File.EndsWith(Extension))
{
return true;
}
}
}
}
}
return false;
}
/// <summary>
/// Returns true if files with extensions in the provided list are in the specified shelved changelist
/// </summary>
/// <param name="ShelvedChangelist"></param>
/// <param name="ExtensionTypeList"></param>
/// <returns></returns>
private bool AreFileTypesModifiedInShelvedChangelist(string ShelvedChangelist, List<string> ExtensionTypeList)
{
// we don't need to do any of this if there was no extensions typelist passed in
if (ExtensionTypeList.Count != 0)
{
// Get all files in this changelist
IEnumerable<string> FileList = CommandUtils.P4.Files("@=" + ShelvedChangelist);
return FileList.Any(F => ExtensionTypeList.Contains(Path.GetExtension(F), StringComparer.OrdinalIgnoreCase));
}
return false;
}
/// <summary>
/// Returns true if files with extensions in the provided list are open locally
/// </summary>
/// <param name="ExtensionTypeList"></param>
/// <returns></returns>
private bool AreFileTypesModifiedInOpenChangelists(List<string> ExtensionTypeList)
{
// we don't need to do any of this if there was no extensions typelist passed in
if (ExtensionTypeList.Count != 0)
{
// Get all files in this changelist
IEnumerable<string> FileList = CommandUtils.P4.Opened("");
return FileList.Any(F => ExtensionTypeList.Contains(Path.GetExtension(F), StringComparer.OrdinalIgnoreCase));
}
return false;
}
}
}
|