public inbox for development@lists.ipfire.org
 help / color / mirror / Atom feed
* [PATCH 0/1] pakfire: implement function to parse meta files
@ 2021-06-24 23:30 Robin Roevens
  2021-06-24 23:30 ` [PATCH] " Robin Roevens
  0 siblings, 1 reply; 3+ messages in thread
From: Robin Roevens @ 2021-06-24 23:30 UTC (permalink / raw)
  To: development

[-- Attachment #1: Type: text/plain, Size: 553 bytes --]

Hi

As Jonatan pointed out some time ago, there is a lot of code duplication
in the current pakfire code. 
This patch introduces a function to parse meta files which is now called
instead of the repetative blocks parsing the files throughout the
pakfire code.

This also in preparation of upcomming changes to use extra meta-data
introduced in my earlier patch (and preventing once more a bunch of code
duplication there).

Regards
Robin


-- 
Dit bericht is gescanned op virussen en andere gevaarlijke
inhoud door MailScanner en lijkt schoon te zijn.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] pakfire: implement function to parse meta files
  2021-06-24 23:30 [PATCH 0/1] pakfire: implement function to parse meta files Robin Roevens
@ 2021-06-24 23:30 ` Robin Roevens
  2022-02-06 10:56   ` Peter Müller
  0 siblings, 1 reply; 3+ messages in thread
From: Robin Roevens @ 2021-06-24 23:30 UTC (permalink / raw)
  To: development

[-- Attachment #1: Type: text/plain, Size: 7274 bytes --]

Removing a lot of duplicate code parsing meta files, now replaced by
simple function calls, resulting in all metadata in one hash.

Signed-off-by: Robin Roevens <robin.roevens(a)disroot.org>
---
 src/pakfire/lib/functions.pl | 129 +++++++++++++----------------------
 1 file changed, 49 insertions(+), 80 deletions(-)

diff --git a/src/pakfire/lib/functions.pl b/src/pakfire/lib/functions.pl
index f9a19b60d..79ef7a12d 100644
--- a/src/pakfire/lib/functions.pl
+++ b/src/pakfire/lib/functions.pl
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2007-2015   IPFire Team   <info(a)ipfire.org>                   #
+# Copyright (C) 2007-2021   IPFire Team   <info(a)ipfire.org>                   #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
@@ -376,11 +376,10 @@ sub dbgetlist {
 	}
 
 	# Update the meta database if new packages was in the package list
-	my @meta;
 	my $file;
 	my $line;
 	my $prog;
-	my ($name, $version, $release);
+	my %metadata;
 	my @templine;
 
 	open(FILE, "<$Conf::dbdir/lists/packages_list.db");
@@ -395,28 +394,14 @@ sub dbgetlist {
 		next if ( $file eq ".." );
 		next if ( $file eq "meta-" );
 		next if ( $file =~ /^old/ );
-		open(FILE, "<$Conf::dbdir/meta/$file");
-		@meta = <FILE>;
-		close(FILE);
-		foreach $line (@meta) {
-			@templine = split(/\: /,$line);
-			if ("$templine[0]" eq "Name") {
-				$name = $templine[1];
-				chomp($name);
-			} elsif ("$templine[0]" eq "ProgVersion") {
-				$version = $templine[1];
-				chomp($version);
-			} elsif ("$templine[0]" eq "Release") {
-				$release = $templine[1];
-				chomp($release);
-			}
-		}
+		%metadata = parsemetafile("$Conf::dbdir/meta/$file");
+
 		foreach $prog (@db) {
 			@templine = split(/\;/,$prog);
-			if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) {
-				move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name");
-				fetchfile("meta/meta-$name", "");
-				move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name");
+			if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" ne "$templine[2]")) {
+				move("$Conf::dbdir/meta/meta-$metadata{'Name'}","$Conf::dbdir/meta/old_meta-$metadata{'Name'}");
+				fetchfile("meta/meta-$metadata{'Name'}", "");
+				move("$Conf::cachedir/meta-$metadata{'Name'}", "$Conf::dbdir/meta/meta-$metadata{'Name'}");
 			}
 		}
 	}
@@ -429,12 +414,11 @@ sub dblist {
 	#   filter may be: all, notinstalled, installed
 	my $filter = shift;
 	my $forweb = shift;
-	my @meta;
 	my @updatepaks;
 	my $file;
 	my $line;
 	my $prog;
-	my ($name, $version, $release);
+	my %metadata;
 	my @templine;
 	
 	### Make sure that the list is not outdated. 
@@ -470,30 +454,16 @@ sub dblist {
 			next if ( $file eq "." );
 			next if ( $file eq ".." );
 			next if ( $file =~ /^old/ );
-			open(FILE, "<$Conf::dbdir/installed/$file");
-			@meta = <FILE>;
-			close(FILE);
-			foreach $line (@meta) {
-				@templine = split(/\: /,$line);
-				if ("$templine[0]" eq "Name") {
-					$name = $templine[1];
-					chomp($name);
-				} elsif ("$templine[0]" eq "ProgVersion") {
-					$version = $templine[1];
-					chomp($version);
-				} elsif ("$templine[0]" eq "Release") {
-					$release = $templine[1];
-					chomp($release);
-				}
-			}
+			%metadata = parsemetafile("$Conf::dbdir/installed/$file");
+
 			foreach $prog (@db) {
 				@templine = split(/\;/,$prog);
-				if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$forweb" ne "notice")) {
-					push(@updatepaks,$name);
+				if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" < "$templine[2]" && "$forweb" ne "notice")) {
+					push(@updatepaks,$metadata{'Name'});
 					if ("$forweb" eq "forweb") {
-						print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
+						print "<option value=\"$metadata{'Name'}\">Update: $metadata{'Name'} -- Version: $metadata{'ProgVersion'} -> $templine[1] -- Release: $metadata{'Release'} -> $templine[2]</option>\n";
 					} else {
-						my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
+						my $command = "Update: $metadata{'Name'}\nVersion: $metadata{'ProgVersion'} -> $templine[1]\nRelease: $metadata{'Release'} -> $templine[2]\n";
 						if ("$Pakfire::enable_colors" eq "1") {
 							print "$color{'lila'}$command$color{'normal'}\n";
 						} else {
@@ -548,18 +518,9 @@ sub resolvedeps_one {
 	
 	message("PAKFIRE RESV: $pak: Resolving dependencies...");
 	
-	open(FILE, "<$Conf::dbdir/meta/meta-$pak");
-	my @file = <FILE>;
-	close(FILE);
-	
-	my $line;
-	my (@templine, @deps, @all);
-	foreach $line (@file) {
-		@templine = split(/\: /,$line);
-		if ("$templine[0]" eq "Dependencies") {
-			@deps = split(/ /, $templine[1]);
-		}
-	}
+	my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
+	my @all;
+	my @deps = split(/ /, $metadata{'Dependencies'});
 	chomp (@deps);
 	foreach (@deps) {
 		if ($_) {
@@ -679,20 +640,40 @@ sub getsize {
 	
 	getmetafile("$pak");
 	
-	open(FILE, "<$Conf::dbdir/meta/meta-$pak");
-	my @file = <FILE>;
+	if (my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak")) {
+		return $metadata{'Size'};
+	}
+	return 0;
+}
+
+sub parsemetafile {
+	### This subroutine returns a hash with the contents of a meta- file
+	#   Pass path to metafile as argument: Pakfire::parsemetafile("$Conf::dbdir/meta/meta-$pak")
+	#   Usage is always with an argument.
+	my $metafile = shift;
+
+	my %metadata = ();
+
+	my @templine;
+	my @file;
+
+	if (! -e $metafile ) {
+		return 0;
+	}
+
+	open(FILE, "<$metafile");
+	@file = <FILE>;
 	close(FILE);
 	
-	my $line;
-	my @templine;
-	foreach $line (@file) {
-		@templine = split(/\: /,$line);
-		if ("$templine[0]" eq "Size") {
+	foreach (@file) {
+		@templine = split(/\: /,$_);
+		if ($templine[1]) {
 			chomp($templine[1]);
-			return $templine[1];
+			$metadata{"$templine[0]"} = $templine[1];
 		}
 	}
-	return 0;
+
+	return %metadata;
 }
 
 sub decryptpak {
@@ -715,20 +696,8 @@ sub getpak {
 
 	getmetafile("$pak");
 	
-	open(FILE, "<$Conf::dbdir/meta/meta-$pak");
-	my @file = <FILE>;
-	close(FILE);
-	
-	my $line;
-	my $file;
-	my @templine;
-	foreach $line (@file) {
-		@templine = split(/\: /,$line);
-		if ("$templine[0]" eq "File") {
-			chomp($templine[1]);
-			$file = $templine[1];
-		}
-	}
+	my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
+	my $file = $metadata{'File'};
 	
 	unless ($file) {
 		message("No filename given in meta-file.");
-- 
2.31.1


-- 
Dit bericht is gescanned op virussen en andere gevaarlijke
inhoud door MailScanner en lijkt schoon te zijn.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] pakfire: implement function to parse meta files
  2021-06-24 23:30 ` [PATCH] " Robin Roevens
@ 2022-02-06 10:56   ` Peter Müller
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Müller @ 2022-02-06 10:56 UTC (permalink / raw)
  To: development

[-- Attachment #1: Type: text/plain, Size: 7756 bytes --]

Hello Robin,

again, sorry for the late reply. This patch looks good to me.

Reviewed-by: Peter Müller <peter.mueller(a)ipfire.org>

Thanks, and best regards,
Peter Müller


> Removing a lot of duplicate code parsing meta files, now replaced by
> simple function calls, resulting in all metadata in one hash.
> 
> Signed-off-by: Robin Roevens <robin.roevens(a)disroot.org>
> ---
>  src/pakfire/lib/functions.pl | 129 +++++++++++++----------------------
>  1 file changed, 49 insertions(+), 80 deletions(-)
> 
> diff --git a/src/pakfire/lib/functions.pl b/src/pakfire/lib/functions.pl
> index f9a19b60d..79ef7a12d 100644
> --- a/src/pakfire/lib/functions.pl
> +++ b/src/pakfire/lib/functions.pl
> @@ -2,7 +2,7 @@
>  ###############################################################################
>  #                                                                             #
>  # IPFire.org - A linux based firewall                                         #
> -# Copyright (C) 2007-2015   IPFire Team   <info(a)ipfire.org>                   #
> +# Copyright (C) 2007-2021   IPFire Team   <info(a)ipfire.org>                   #
>  #                                                                             #
>  # This program is free software: you can redistribute it and/or modify        #
>  # it under the terms of the GNU General Public License as published by        #
> @@ -376,11 +376,10 @@ sub dbgetlist {
>  	}
>  
>  	# Update the meta database if new packages was in the package list
> -	my @meta;
>  	my $file;
>  	my $line;
>  	my $prog;
> -	my ($name, $version, $release);
> +	my %metadata;
>  	my @templine;
>  
>  	open(FILE, "<$Conf::dbdir/lists/packages_list.db");
> @@ -395,28 +394,14 @@ sub dbgetlist {
>  		next if ( $file eq ".." );
>  		next if ( $file eq "meta-" );
>  		next if ( $file =~ /^old/ );
> -		open(FILE, "<$Conf::dbdir/meta/$file");
> -		@meta = <FILE>;
> -		close(FILE);
> -		foreach $line (@meta) {
> -			@templine = split(/\: /,$line);
> -			if ("$templine[0]" eq "Name") {
> -				$name = $templine[1];
> -				chomp($name);
> -			} elsif ("$templine[0]" eq "ProgVersion") {
> -				$version = $templine[1];
> -				chomp($version);
> -			} elsif ("$templine[0]" eq "Release") {
> -				$release = $templine[1];
> -				chomp($release);
> -			}
> -		}
> +		%metadata = parsemetafile("$Conf::dbdir/meta/$file");
> +
>  		foreach $prog (@db) {
>  			@templine = split(/\;/,$prog);
> -			if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) {
> -				move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name");
> -				fetchfile("meta/meta-$name", "");
> -				move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name");
> +			if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" ne "$templine[2]")) {
> +				move("$Conf::dbdir/meta/meta-$metadata{'Name'}","$Conf::dbdir/meta/old_meta-$metadata{'Name'}");
> +				fetchfile("meta/meta-$metadata{'Name'}", "");
> +				move("$Conf::cachedir/meta-$metadata{'Name'}", "$Conf::dbdir/meta/meta-$metadata{'Name'}");
>  			}
>  		}
>  	}
> @@ -429,12 +414,11 @@ sub dblist {
>  	#   filter may be: all, notinstalled, installed
>  	my $filter = shift;
>  	my $forweb = shift;
> -	my @meta;
>  	my @updatepaks;
>  	my $file;
>  	my $line;
>  	my $prog;
> -	my ($name, $version, $release);
> +	my %metadata;
>  	my @templine;
>  	
>  	### Make sure that the list is not outdated. 
> @@ -470,30 +454,16 @@ sub dblist {
>  			next if ( $file eq "." );
>  			next if ( $file eq ".." );
>  			next if ( $file =~ /^old/ );
> -			open(FILE, "<$Conf::dbdir/installed/$file");
> -			@meta = <FILE>;
> -			close(FILE);
> -			foreach $line (@meta) {
> -				@templine = split(/\: /,$line);
> -				if ("$templine[0]" eq "Name") {
> -					$name = $templine[1];
> -					chomp($name);
> -				} elsif ("$templine[0]" eq "ProgVersion") {
> -					$version = $templine[1];
> -					chomp($version);
> -				} elsif ("$templine[0]" eq "Release") {
> -					$release = $templine[1];
> -					chomp($release);
> -				}
> -			}
> +			%metadata = parsemetafile("$Conf::dbdir/installed/$file");
> +
>  			foreach $prog (@db) {
>  				@templine = split(/\;/,$prog);
> -				if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$forweb" ne "notice")) {
> -					push(@updatepaks,$name);
> +				if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" < "$templine[2]" && "$forweb" ne "notice")) {
> +					push(@updatepaks,$metadata{'Name'});
>  					if ("$forweb" eq "forweb") {
> -						print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
> +						print "<option value=\"$metadata{'Name'}\">Update: $metadata{'Name'} -- Version: $metadata{'ProgVersion'} -> $templine[1] -- Release: $metadata{'Release'} -> $templine[2]</option>\n";
>  					} else {
> -						my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
> +						my $command = "Update: $metadata{'Name'}\nVersion: $metadata{'ProgVersion'} -> $templine[1]\nRelease: $metadata{'Release'} -> $templine[2]\n";
>  						if ("$Pakfire::enable_colors" eq "1") {
>  							print "$color{'lila'}$command$color{'normal'}\n";
>  						} else {
> @@ -548,18 +518,9 @@ sub resolvedeps_one {
>  	
>  	message("PAKFIRE RESV: $pak: Resolving dependencies...");
>  	
> -	open(FILE, "<$Conf::dbdir/meta/meta-$pak");
> -	my @file = <FILE>;
> -	close(FILE);
> -	
> -	my $line;
> -	my (@templine, @deps, @all);
> -	foreach $line (@file) {
> -		@templine = split(/\: /,$line);
> -		if ("$templine[0]" eq "Dependencies") {
> -			@deps = split(/ /, $templine[1]);
> -		}
> -	}
> +	my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
> +	my @all;
> +	my @deps = split(/ /, $metadata{'Dependencies'});
>  	chomp (@deps);
>  	foreach (@deps) {
>  		if ($_) {
> @@ -679,20 +640,40 @@ sub getsize {
>  	
>  	getmetafile("$pak");
>  	
> -	open(FILE, "<$Conf::dbdir/meta/meta-$pak");
> -	my @file = <FILE>;
> +	if (my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak")) {
> +		return $metadata{'Size'};
> +	}
> +	return 0;
> +}
> +
> +sub parsemetafile {
> +	### This subroutine returns a hash with the contents of a meta- file
> +	#   Pass path to metafile as argument: Pakfire::parsemetafile("$Conf::dbdir/meta/meta-$pak")
> +	#   Usage is always with an argument.
> +	my $metafile = shift;
> +
> +	my %metadata = ();
> +
> +	my @templine;
> +	my @file;
> +
> +	if (! -e $metafile ) {
> +		return 0;
> +	}
> +
> +	open(FILE, "<$metafile");
> +	@file = <FILE>;
>  	close(FILE);
>  	
> -	my $line;
> -	my @templine;
> -	foreach $line (@file) {
> -		@templine = split(/\: /,$line);
> -		if ("$templine[0]" eq "Size") {
> +	foreach (@file) {
> +		@templine = split(/\: /,$_);
> +		if ($templine[1]) {
>  			chomp($templine[1]);
> -			return $templine[1];
> +			$metadata{"$templine[0]"} = $templine[1];
>  		}
>  	}
> -	return 0;
> +
> +	return %metadata;
>  }
>  
>  sub decryptpak {
> @@ -715,20 +696,8 @@ sub getpak {
>  
>  	getmetafile("$pak");
>  	
> -	open(FILE, "<$Conf::dbdir/meta/meta-$pak");
> -	my @file = <FILE>;
> -	close(FILE);
> -	
> -	my $line;
> -	my $file;
> -	my @templine;
> -	foreach $line (@file) {
> -		@templine = split(/\: /,$line);
> -		if ("$templine[0]" eq "File") {
> -			chomp($templine[1]);
> -			$file = $templine[1];
> -		}
> -	}
> +	my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
> +	my $file = $metadata{'File'};
>  	
>  	unless ($file) {
>  		message("No filename given in meta-file.");

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-02-06 10:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-24 23:30 [PATCH 0/1] pakfire: implement function to parse meta files Robin Roevens
2021-06-24 23:30 ` [PATCH] " Robin Roevens
2022-02-06 10:56   ` Peter Müller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox