Use SonarQube
Used SonarQube to increase overall code quality Files now live in their own directory Fixed some tests Added SonarQube directory to gitignore
This commit is contained in:
parent
58ceb31c1c
commit
6892191275
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.vscode/
|
||||
[Oo]bj/
|
||||
[Bb]in/
|
||||
.sonarqube/
|
@ -10,9 +10,9 @@ public class TranserFilesTest {
|
||||
|
||||
// Act
|
||||
utils.readPacmanConfig();
|
||||
utils.transfer();
|
||||
Action act = () => utils.transfer();
|
||||
|
||||
// Assert
|
||||
//Assert.NotEmpty(Directory.GetFiles("/home/rene/test/"));
|
||||
act.Should().NotThrow();
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,11 @@ public class checkForNewerStuffTest {
|
||||
public void checkForNewerPackages_throwsExceptionIfNoFilesExist() {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
File.Delete("/tmp/packages_before.txt");
|
||||
File.Delete("/tmp/packages_after.txt");
|
||||
File.Delete(utils.pacserverDirectory + "packages_before.txt");
|
||||
File.Delete(utils.pacserverDirectory + "packages_after.txt");
|
||||
|
||||
// Act
|
||||
Action act = () => utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt");
|
||||
Action act = () => utils.diff(utils.pacserverDirectory + "packages_before.txt", utils.pacserverDirectory + "packages_after.txt");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<FileNotFoundException>().WithMessage("Necessary files could not be found");
|
||||
@ -24,12 +24,12 @@ public class checkForNewerStuffTest {
|
||||
utils.readPacmanConfig();
|
||||
|
||||
// Act
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
|
||||
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||
utils.getEveryPackageNameAndVersion("after", utils.pacserverDirectory + "packages_after.txt");
|
||||
|
||||
// Assert
|
||||
File.Exists("/tmp/packages_before.txt").Should().BeTrue();
|
||||
File.Exists("/tmp/packages_before.txt").Should().BeTrue();
|
||||
File.Exists(utils.pacserverDirectory + "packages_before.txt").Should().BeTrue();
|
||||
File.Exists(utils.pacserverDirectory + "packages_before.txt").Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -37,7 +37,7 @@ public class checkForNewerStuffTest {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
utils.readPacmanConfig();
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||
|
||||
// Act
|
||||
List<String> packageList = utils.packageNamesAndVersion;
|
||||
@ -52,7 +52,7 @@ public class checkForNewerStuffTest {
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
|
||||
// Act
|
||||
Action act = () => utils.getEveryPackageNameAndVersion("test", "/tmp/test.txt");
|
||||
Action act = () => utils.getEveryPackageNameAndVersion("test", utils.pacserverDirectory + "test.txt");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentException>().WithMessage("No valid mode was given. Valid modes are before and after");
|
||||
|
@ -1,35 +1,54 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Pacserver.Utils;
|
||||
public class PacserverUtils {
|
||||
public string pacmanCacheDirectory = string.Empty;
|
||||
public string pacmanDatabaseDirectory = string.Empty;
|
||||
public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" };
|
||||
public partial class PacserverUtils {
|
||||
public string? pacserverDirectory { get; set; }
|
||||
public void prerequisites() {
|
||||
AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromMilliseconds(100));
|
||||
|
||||
pacserverDirectory = "/tmp/pacserver/";
|
||||
pacmanCacheDirectory = string.Empty;
|
||||
pacmanDatabaseDirectory = string.Empty;
|
||||
|
||||
if (!Directory.Exists(pacserverDirectory)) {
|
||||
Directory.CreateDirectory(pacserverDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
if (Directory.Exists(pacserverDirectory)) {
|
||||
Directory.Delete(pacserverDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
public string? pacmanCacheDirectory { get; set; }
|
||||
public string? pacmanDatabaseDirectory { get; set; }
|
||||
public readonly ImmutableList<String> pathsToDetermine = ImmutableList.Create("CacheDir", "DBPath");
|
||||
[GeneratedRegex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$", RegexOptions.NonBacktracking)] // https://regex101.com/r/GwWeui/2
|
||||
private static partial Regex CacheDirOrDBPathRegex();
|
||||
public void readPacmanConfig() {
|
||||
using (StreamReader file = new StreamReader("/etc/pacman.conf")) {
|
||||
Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2
|
||||
|
||||
string? line;
|
||||
|
||||
while ((line = file.ReadLine()) is not null) {
|
||||
foreach (string path in pathsToDetermine) {
|
||||
if (line.Contains(path)) {
|
||||
Match match = regex.Match(line);
|
||||
if (match.Success) {
|
||||
switch (path) {
|
||||
case "CacheDir":
|
||||
pacmanCacheDirectory = match.ToString();
|
||||
break;
|
||||
case "DBPath":
|
||||
pacmanDatabaseDirectory = match.ToString();
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Could not deal with " + match.ToString());
|
||||
}
|
||||
} else {
|
||||
string pathsToDetermineString = string.Join(",", pathsToDetermine);
|
||||
throw new Exception("Could not determine the necessary file paths: " + pathsToDetermineString);
|
||||
foreach (string path in pathsToDetermine.Where(path => line.Contains(path))) {
|
||||
Match match = CacheDirOrDBPathRegex().Match(line);
|
||||
if (match.Success) {
|
||||
switch (path) {
|
||||
case "CacheDir":
|
||||
pacmanCacheDirectory = match.ToString();
|
||||
break;
|
||||
case "DBPath":
|
||||
pacmanDatabaseDirectory = match.ToString();
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Could not deal with " + match.ToString());
|
||||
}
|
||||
} else {
|
||||
string pathsToDetermineString = string.Join(",", pathsToDetermine);
|
||||
throw new DirectoryNotFoundException("Could not determine the necessary file paths: " + pathsToDetermineString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -37,12 +56,12 @@ public class PacserverUtils {
|
||||
}
|
||||
|
||||
public List<String> packageNamesAndVersion = new List<String>();
|
||||
[GeneratedRegex(@".+\.pkg\.tar\.zst$", RegexOptions.NonBacktracking)]
|
||||
private static partial Regex onlyGetPackages();
|
||||
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();
|
||||
packageNamesAndVersion = Directory.GetFiles(pacmanCacheDirectory).Where(file => onlyGetPackages().IsMatch(file)).ToList();
|
||||
} else {
|
||||
Console.WriteLine("No packages found in pacman cache");
|
||||
}
|
||||
@ -108,7 +127,7 @@ public class PacserverUtils {
|
||||
public List<String> databases = new List<String>();
|
||||
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
||||
databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/").ToList();
|
||||
|
||||
|
||||
switch (mode) {
|
||||
case "before":
|
||||
writeDatabaseAccessTimeToFile(filePath);
|
||||
@ -125,7 +144,7 @@ public class PacserverUtils {
|
||||
if (File.Exists(filePath)) {
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
|
||||
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) {
|
||||
using (StreamWriter sw = new StreamWriter(filePath)) {
|
||||
foreach (string database in databases) {
|
||||
@ -136,9 +155,11 @@ public class PacserverUtils {
|
||||
}
|
||||
|
||||
public List<String> databasesToTransfer = new List<String>();
|
||||
[GeneratedRegex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/*db", RegexOptions.NonBacktracking)] // https://regex101.com/r/Wm5M0P/1
|
||||
private static partial Regex onlyGetDatabaseName();
|
||||
public void filterDiffOutputForDatabases() {
|
||||
foreach (string database in diffOfPackagesOrDatabases) {
|
||||
databasesToTransfer.Add(Regex.Match(database, @"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/*db").Value); // https://regex101.com/r/Wm5M0P/1
|
||||
databasesToTransfer.Add(onlyGetDatabaseName().Match(database).Value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,7 +169,7 @@ public class PacserverUtils {
|
||||
newerPackagesAndDatabases.AddRange(databasesToTransfer);
|
||||
}
|
||||
|
||||
public async void transfer() {
|
||||
public async Task transfer() {
|
||||
HttpClient client = new HttpClient();
|
||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/");
|
||||
MultipartFormDataContent content = new MultipartFormDataContent();
|
||||
|
@ -1,6 +1,9 @@
|
||||
using Pacserver.Utils;
|
||||
|
||||
public class Program {
|
||||
|
||||
protected Program() {
|
||||
}
|
||||
static void Main(string[] args) {
|
||||
if (args.Length == 0) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
@ -11,28 +14,31 @@ public class Program {
|
||||
}
|
||||
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
utils.prerequisites();
|
||||
utils.readPacmanConfig();
|
||||
|
||||
switch (args[0]) {
|
||||
case "before":
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
utils.checkIfDatabasesWereModified("before", "/tmp/databases_before.txt");
|
||||
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||
utils.checkIfDatabasesWereModified("before", utils.pacserverDirectory + "databases_before.txt");
|
||||
break;
|
||||
case "after":
|
||||
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
|
||||
utils.checkIfDatabasesWereModified("after", "/tmp/databases_after.txt");
|
||||
utils.getEveryPackageNameAndVersion("after", utils.pacserverDirectory + "packages_after.txt");
|
||||
utils.checkIfDatabasesWereModified("after", utils.pacserverDirectory + "databases_after.txt");
|
||||
|
||||
utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt");
|
||||
utils.saveDiffToFile("/tmp/package_diff.txt");
|
||||
utils.diff(utils.pacserverDirectory + "packages_before.txt", utils.pacserverDirectory + "packages_after.txt");
|
||||
utils.saveDiffToFile(utils.pacserverDirectory + "package_diff.txt");
|
||||
|
||||
utils.diff("/tmp/databases_before.txt", "/tmp/databases_after.txt");
|
||||
utils.saveDiffToFile("/tmp/database_diff.txt");
|
||||
utils.diff(utils.pacserverDirectory + "databases_before.txt", utils.pacserverDirectory + "databases_after.txt");
|
||||
utils.saveDiffToFile(utils.pacserverDirectory + "database_diff.txt");
|
||||
utils.filterDiffOutputForDatabases();
|
||||
|
||||
utils.packageNamesAndVersion = utils.readDiffFileToList("/tmp/package_diff.txt");
|
||||
utils.packageNamesAndVersion = utils.readDiffFileToList(utils.pacserverDirectory + "package_diff.txt");
|
||||
|
||||
utils.combinePackagesWithDatabases();
|
||||
utils.transfer();
|
||||
|
||||
utils.cleanup();
|
||||
break;
|
||||
default:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
|
Loading…
x
Reference in New Issue
Block a user