Compare commits

..

3 Commits

Author SHA1 Message Date
dc811d7462
Merge pull request 'Major changes in package acquisition' (#4) from refactor/packageAcquisition into dev
Reviewed-on: #4
2023-06-18 20:55:56 +00:00
07982425e0
Write package name and version into text file 2023-06-18 22:55:15 +02:00
e609caf892
Major changes in package acquisition
This breaks a bunch of things that depended on the older version of this method
2023-06-18 22:44:43 +02:00
3 changed files with 57 additions and 17 deletions

View File

@ -22,8 +22,8 @@ public class checkForNewerStuffTest {
utils.readPacmanConfig();
// Act
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt");
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
// Assert
File.Exists("/tmp/before_update.txt").Should().BeTrue();
@ -35,7 +35,7 @@ public class checkForNewerStuffTest {
// Arrange
PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
// Act
List<String> packageList = utils.packageNamesAndVersion;
@ -52,7 +52,7 @@ public class checkForNewerStuffTest {
utils.pacmanDatabaseDirectory = "/tmp/";
// Act
Action act = () => utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
Action act = () => utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
// Assert
act.Should().Throw<Exception>().WithMessage("How did you execute this without any packages?");

View File

@ -37,16 +37,49 @@ public class PacserverUtils {
}
public List<String> packageNamesAndVersion = new List<String>();
public void getEveryPackageNameAndVersionViaFolderName(string filePath) {
string[] directories = Directory.GetDirectories(pacmanDatabaseDirectory + "local/");
foreach (string directory in directories) {
packageNamesAndVersion.Add(new DirectoryInfo(directory).Name);
public void getEveryPackageNameAndVersion(string mode, string filePath) {
Regex regex = new Regex(@".+\.pkg\.tar\.zst$");
if (Directory.Exists(pacmanCacheDirectory)) {
if (Directory.GetFiles(pacmanCacheDirectory) is not null) {
packageNamesAndVersion = Directory.GetFiles(pacmanCacheDirectory).Where(file => regex.IsMatch(file)).ToList();
} else {
Console.WriteLine("No packages found in pacman cache");
}
} else {
Console.WriteLine("No pacman cache directory found");
}
if (packageNamesAndVersion.Capacity > 0) {
File.WriteAllLines(filePath, packageNamesAndVersion);
} else {
throw new Exception("How did you execute this without any packages?");
switch (mode) {
case "before":
writePackageNamesAndVersionToFile(filePath);
break;
case "after":
writePackageNamesAndVersionToFile(filePath);
break;
default:
throw new ArgumentException("No valid mode was given. Valid modes are before and after");
}
}
public void writePackageNamesAndVersionToFile(string filePath) {
if (!File.Exists(filePath)) {
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (StreamWriter sw = new StreamWriter(filePath)) {
foreach (string package in packageNamesAndVersion) {
sw.WriteLine(package);
}
}
}
} else if (File.Exists(filePath)) {
File.Delete(filePath);
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (var sw = new StreamWriter(filePath)) {
foreach (string package in packageNamesAndVersion) {
sw.WriteLine(package);
}
}
}
}
}
@ -85,8 +118,9 @@ public class PacserverUtils {
}
}
} else if (File.Exists(filePath)) {
using (File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (var sw = new StreamWriter(filePath, true)) {
File.Delete(filePath);
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (var sw = new StreamWriter(filePath)) {
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
}
}

View File

@ -19,14 +19,14 @@ public class Program {
break;
case "packagesBefore":
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
break;
case "packagesAfter":
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt");
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
break;
case "diffPackages":
utils.diff("/tmp/before_update.txt", "/tmp/after_update.txt");
utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt");
string packages = string.Join("\n", utils.diffOfPackagesOrDatabases);
Console.WriteLine(packages);
break;
@ -49,6 +49,12 @@ public class Program {
string filteredDatabases = string.Join("\n", utils.databasesToTransfer);
Console.WriteLine(filteredDatabases);
break;
case "getEveryPackageInCache":
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
string allPackages = string.Join("\n", utils.packageNamesAndVersion);
Console.WriteLine(allPackages);
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(args[0] + " is not a recognized option.");