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/
|
.vscode/
|
||||||
[Oo]bj/
|
[Oo]bj/
|
||||||
[Bb]in/
|
[Bb]in/
|
||||||
|
.sonarqube/
|
@ -10,9 +10,9 @@ public class TranserFilesTest {
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
utils.readPacmanConfig();
|
utils.readPacmanConfig();
|
||||||
utils.transfer();
|
Action act = () => utils.transfer();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
//Assert.NotEmpty(Directory.GetFiles("/home/rene/test/"));
|
act.Should().NotThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,11 @@ public class checkForNewerStuffTest {
|
|||||||
public void checkForNewerPackages_throwsExceptionIfNoFilesExist() {
|
public void checkForNewerPackages_throwsExceptionIfNoFilesExist() {
|
||||||
// Arrange
|
// Arrange
|
||||||
PacserverUtils utils = new PacserverUtils();
|
PacserverUtils utils = new PacserverUtils();
|
||||||
File.Delete("/tmp/packages_before.txt");
|
File.Delete(utils.pacserverDirectory + "packages_before.txt");
|
||||||
File.Delete("/tmp/packages_after.txt");
|
File.Delete(utils.pacserverDirectory + "packages_after.txt");
|
||||||
|
|
||||||
// Act
|
// 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
|
// Assert
|
||||||
act.Should().Throw<FileNotFoundException>().WithMessage("Necessary files could not be found");
|
act.Should().Throw<FileNotFoundException>().WithMessage("Necessary files could not be found");
|
||||||
@ -24,12 +24,12 @@ public class checkForNewerStuffTest {
|
|||||||
utils.readPacmanConfig();
|
utils.readPacmanConfig();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||||
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
|
utils.getEveryPackageNameAndVersion("after", utils.pacserverDirectory + "packages_after.txt");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
File.Exists("/tmp/packages_before.txt").Should().BeTrue();
|
File.Exists(utils.pacserverDirectory + "packages_before.txt").Should().BeTrue();
|
||||||
File.Exists("/tmp/packages_before.txt").Should().BeTrue();
|
File.Exists(utils.pacserverDirectory + "packages_before.txt").Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -37,7 +37,7 @@ public class checkForNewerStuffTest {
|
|||||||
// Arrange
|
// Arrange
|
||||||
PacserverUtils utils = new PacserverUtils();
|
PacserverUtils utils = new PacserverUtils();
|
||||||
utils.readPacmanConfig();
|
utils.readPacmanConfig();
|
||||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
List<String> packageList = utils.packageNamesAndVersion;
|
List<String> packageList = utils.packageNamesAndVersion;
|
||||||
@ -52,7 +52,7 @@ public class checkForNewerStuffTest {
|
|||||||
PacserverUtils utils = new PacserverUtils();
|
PacserverUtils utils = new PacserverUtils();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
Action act = () => utils.getEveryPackageNameAndVersion("test", "/tmp/test.txt");
|
Action act = () => utils.getEveryPackageNameAndVersion("test", utils.pacserverDirectory + "test.txt");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
act.Should().Throw<ArgumentException>().WithMessage("No valid mode was given. Valid modes are before and after");
|
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;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Pacserver.Utils;
|
namespace Pacserver.Utils;
|
||||||
public class PacserverUtils {
|
public partial class PacserverUtils {
|
||||||
public string pacmanCacheDirectory = string.Empty;
|
public string? pacserverDirectory { get; set; }
|
||||||
public string pacmanDatabaseDirectory = string.Empty;
|
public void prerequisites() {
|
||||||
public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" };
|
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() {
|
public void readPacmanConfig() {
|
||||||
using (StreamReader file = new StreamReader("/etc/pacman.conf")) {
|
using (StreamReader file = new StreamReader("/etc/pacman.conf")) {
|
||||||
Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2
|
|
||||||
string? line;
|
string? line;
|
||||||
|
|
||||||
while ((line = file.ReadLine()) is not null) {
|
while ((line = file.ReadLine()) is not null) {
|
||||||
foreach (string path in pathsToDetermine) {
|
foreach (string path in pathsToDetermine.Where(path => line.Contains(path))) {
|
||||||
if (line.Contains(path)) {
|
Match match = CacheDirOrDBPathRegex().Match(line);
|
||||||
Match match = regex.Match(line);
|
if (match.Success) {
|
||||||
if (match.Success) {
|
switch (path) {
|
||||||
switch (path) {
|
case "CacheDir":
|
||||||
case "CacheDir":
|
pacmanCacheDirectory = match.ToString();
|
||||||
pacmanCacheDirectory = match.ToString();
|
break;
|
||||||
break;
|
case "DBPath":
|
||||||
case "DBPath":
|
pacmanDatabaseDirectory = match.ToString();
|
||||||
pacmanDatabaseDirectory = match.ToString();
|
break;
|
||||||
break;
|
default:
|
||||||
default:
|
throw new ArgumentException("Could not deal with " + match.ToString());
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
} 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>();
|
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) {
|
public void getEveryPackageNameAndVersion(string mode, string filePath) {
|
||||||
Regex regex = new Regex(@".+\.pkg\.tar\.zst$");
|
|
||||||
|
|
||||||
if (Directory.Exists(pacmanCacheDirectory)) {
|
if (Directory.Exists(pacmanCacheDirectory)) {
|
||||||
if (Directory.GetFiles(pacmanCacheDirectory) is not null) {
|
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 {
|
} else {
|
||||||
Console.WriteLine("No packages found in pacman cache");
|
Console.WriteLine("No packages found in pacman cache");
|
||||||
}
|
}
|
||||||
@ -108,7 +127,7 @@ public class PacserverUtils {
|
|||||||
public List<String> databases = new List<String>();
|
public List<String> databases = new List<String>();
|
||||||
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
||||||
databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/").ToList();
|
databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/").ToList();
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case "before":
|
case "before":
|
||||||
writeDatabaseAccessTimeToFile(filePath);
|
writeDatabaseAccessTimeToFile(filePath);
|
||||||
@ -125,7 +144,7 @@ public class PacserverUtils {
|
|||||||
if (File.Exists(filePath)) {
|
if (File.Exists(filePath)) {
|
||||||
File.Delete(filePath);
|
File.Delete(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) {
|
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) {
|
||||||
using (StreamWriter sw = new StreamWriter(filePath)) {
|
using (StreamWriter sw = new StreamWriter(filePath)) {
|
||||||
foreach (string database in databases) {
|
foreach (string database in databases) {
|
||||||
@ -136,9 +155,11 @@ public class PacserverUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<String> databasesToTransfer = new List<String>();
|
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() {
|
public void filterDiffOutputForDatabases() {
|
||||||
foreach (string database in diffOfPackagesOrDatabases) {
|
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);
|
newerPackagesAndDatabases.AddRange(databasesToTransfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void transfer() {
|
public async Task transfer() {
|
||||||
HttpClient client = new HttpClient();
|
HttpClient client = new HttpClient();
|
||||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/");
|
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/");
|
||||||
MultipartFormDataContent content = new MultipartFormDataContent();
|
MultipartFormDataContent content = new MultipartFormDataContent();
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
using Pacserver.Utils;
|
using Pacserver.Utils;
|
||||||
|
|
||||||
public class Program {
|
public class Program {
|
||||||
|
|
||||||
|
protected Program() {
|
||||||
|
}
|
||||||
static void Main(string[] args) {
|
static void Main(string[] args) {
|
||||||
if (args.Length == 0) {
|
if (args.Length == 0) {
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
@ -11,28 +14,31 @@ public class Program {
|
|||||||
}
|
}
|
||||||
|
|
||||||
PacserverUtils utils = new PacserverUtils();
|
PacserverUtils utils = new PacserverUtils();
|
||||||
|
utils.prerequisites();
|
||||||
utils.readPacmanConfig();
|
utils.readPacmanConfig();
|
||||||
|
|
||||||
switch (args[0]) {
|
switch (args[0]) {
|
||||||
case "before":
|
case "before":
|
||||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||||
utils.checkIfDatabasesWereModified("before", "/tmp/databases_before.txt");
|
utils.checkIfDatabasesWereModified("before", utils.pacserverDirectory + "databases_before.txt");
|
||||||
break;
|
break;
|
||||||
case "after":
|
case "after":
|
||||||
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
|
utils.getEveryPackageNameAndVersion("after", utils.pacserverDirectory + "packages_after.txt");
|
||||||
utils.checkIfDatabasesWereModified("after", "/tmp/databases_after.txt");
|
utils.checkIfDatabasesWereModified("after", utils.pacserverDirectory + "databases_after.txt");
|
||||||
|
|
||||||
utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt");
|
utils.diff(utils.pacserverDirectory + "packages_before.txt", utils.pacserverDirectory + "packages_after.txt");
|
||||||
utils.saveDiffToFile("/tmp/package_diff.txt");
|
utils.saveDiffToFile(utils.pacserverDirectory + "package_diff.txt");
|
||||||
|
|
||||||
utils.diff("/tmp/databases_before.txt", "/tmp/databases_after.txt");
|
utils.diff(utils.pacserverDirectory + "databases_before.txt", utils.pacserverDirectory + "databases_after.txt");
|
||||||
utils.saveDiffToFile("/tmp/database_diff.txt");
|
utils.saveDiffToFile(utils.pacserverDirectory + "database_diff.txt");
|
||||||
utils.filterDiffOutputForDatabases();
|
utils.filterDiffOutputForDatabases();
|
||||||
|
|
||||||
utils.packageNamesAndVersion = utils.readDiffFileToList("/tmp/package_diff.txt");
|
utils.packageNamesAndVersion = utils.readDiffFileToList(utils.pacserverDirectory + "package_diff.txt");
|
||||||
|
|
||||||
utils.combinePackagesWithDatabases();
|
utils.combinePackagesWithDatabases();
|
||||||
utils.transfer();
|
utils.transfer();
|
||||||
|
|
||||||
|
utils.cleanup();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user