Commit Diff
Diff:
fabdae14a807b9bc4b639b6b09e9e106562bd709
27374ec973ec20d56c66196700e04ea4244f9686
Commit:
27374ec973ec20d56c66196700e04ea4244f9686
Tree:
3711d56a9031d9da107cb7a94ca13b3c7aa38724
Author:
fredgnix <fredgnix@a8afeb8c-277c-4b4a-a4f6-ef4c85adc32b>
Committer:
fredgnix <fredgnix@a8afeb8c-277c-4b4a-a4f6-ef4c85adc32b>
Date:
Mon Jan 30 21:43:19 2012 UTC
Message:
code cleanup, options more human readable
blob - 1ff3eefe2c688ef0927f7fd8c5fd9fb25a9952ab
blob + 85a5b2eb5d068ba18aa533fffc2d7b102ddbd780
--- Pkgtxt2db.pm
+++ Pkgtxt2db.pm
@@ -8,31 +8,32 @@ package Pkgtxt2db;
#
# License: BSD Revised
#
-
+use 5.010;
use strict;
use warnings;
use LWP::Simple;
+our $pkgtxt = "PACKAGES.TXT";
+our @releases = ("13.37","current");
+our $release;
+our $url;
+our $target;
+our @targets = qw/salix32 salix64 slack32 slack64/;
+our %pkgdb;
#
-# Get the PACKAGES.TXT file
-#
+# Salix vars
#
-# Salix
+our $salix32 = "salix32";
+our $salix64 = "salix64";
our $salix_mirror = "http://salix.enialis.net";
-our $arch32 = "i486";
-our $arch64 = "x86_64";
-our $release = "13.37";
-our $pkgtxt = "/tmp/salixPACKAGES.TXT";
-our $url32 = "$salix_mirror/$arch32/$release/PACKAGES.TXT";
-our $url64 = "$salix_mirror/$arch64/$release/PACKAGES.TXT";
-# Slackware
+#
+# Slackware vars
+#
+our $slack32 = "slack32";
+our $slack64 = "slack64";
our $slack_mirror="http://slackware.org.uk/slackware";
-our $a32 = "slackware";
-our $a64 = "slackware64";
-our $slackurl32 = "$slack_mirror/${a32}-${release}/PACKAGES.TXT";
-our $slackurl64 = "$slack_mirror/${a64}-${release}/PACKAGES.TXT";
-our $slackpkgtxt = "/tmp/slackwarePACKAGES.TXT";
+#
sub new {
my $self = {};
@@ -40,77 +41,77 @@ sub new {
return $self;
}
-#
-# build an array with all file lines
#
-# Salix
-# Pkgtxt2db->getsalix_d32()
+# choose the release
#
-sub getsalix_d32 {
- my $status = getstore($url32,$pkgtxt);
- if ( is_success($status) ) {
- print "PACKAGES.TXT downloaded correctly.\n";
+sub checkrelease {
+ if ($release ~~ @releases) {
+ print "Release: $release\n";
} else {
- print "PACKAGES.TXT not downloaded: $status\n";
+ die "Not a valid release, see --help\n";
+ }
}
-}
-sub getsalix_d64 {
- my $status = getstore($url64,$pkgtxt);
- if ( is_success($status) ) {
- print "PACKAGES.TXT downloaded correctly.\n";
+
+#
+# choose the target between salix or slackware, i486 or x86_64
+#
+sub checktarget {
+ if ($target ~~ @targets) {
+ print "Target: $target\n";
} else {
- print "PACKAGES.TXT not dowloaded: $status\n";
+ die "You must choose a valid target, see --help\n";
}
}
-sub salix_data {
- open F, "<$pkgtxt" or die "No Salix PACKAGES.TXT file, aborting.\n";
- our @salix_d = <F>;
- close (F);
- return @salix_d;
-}
-
-# Slackware
-# Pkgtxt2db->getslack_d32()
-#
-sub getslack_d32 {
- my $status = getstore($slackurl32,$slackpkgtxt);
- if ( is_success($status) ) {
- print "PACKAGES.TXT downloaded correctly.\n";
- } else {
- print "PACKAGES.TXT not downloaded: $status\n";
+#
+# build an array with all file lines
+#
+# Get the PACKAGES.TXT file
+# Pkgtxt2db->getpkgtxt()
+#
+sub getpkgtxt {
+ if ($target eq $salix32) {
+ $url = "$salix_mirror/i486/$release/$pkgtxt";
+ } elsif ($target eq $salix64) {
+ $url = "$salix_mirror/x86_64/$release/$pkgtxt";
+ } elsif ($target eq $slack32) {
+ $url = "$slack_mirror/slackware-${release}/$pkgtxt";
+ } elsif ($target eq $slack64) {
+ $url = "$slack_mirror/slackware64-${release}/$pkgtxt";
}
-}
-sub getslack_d64 {
- my $status = getstore($slackurl64,$slackpkgtxt);
+ print "URL: $url\n";
+ my $status = getstore($url,$pkgtxt);
if ( is_success($status) ) {
print "PACKAGES.TXT downloaded correctly.\n";
} else {
print "PACKAGES.TXT not downloaded: $status\n";
}
}
-sub slack_data {
- open G, "<$slackpkgtxt" or die $!;
- our @slack_d = <G>;
- close (G);
- return @slack_d;
+
+
+#
+# turn PACKAGES.TXT to a array
+#
+sub getdata {
+ open F, "<$pkgtxt" or die "No PACKAGES.TXT file, aborting.\n";
+ our @data = <F>;
+ close (F);
+ return @data;
}
#
# Make the hash of array database
#
-# Salix DB
-# Pkgtxt2db->mkdasalixdb()
+# build the DB
+# Pkgtxt2db->mkdadb()
#
-our %pkgdb;
-
-sub mkdasalixdb {
+sub mkdadb {
our $self = shift;
- our @d = salix_data();
+ our @d = getdata();
our $pname;
our $pkgname;
-
+
foreach (@d) {
chomp $_;
if ($_ =~ /^$/){
@@ -154,65 +155,12 @@ sub mkdasalixdb {
}
#
-# Slackware DB
-# Pkgtxt2db->mkdaslackdb()
-#
-our %slackdb;
-
-sub mkdaslackdb {
- our $self = shift;
-
- our @da = slack_data();
- our $name;
- our $pkg;
-
- foreach (@da) {
- chomp $_;
- if ($_ =~ /^$/){
- next;
- }
- if ($_ =~ /(^PACKAGE NAME:\s\s)(.*)/) {
- $name = "$2";
- $name =~ /^(.*)-([^-]*)-([^-]*)-([^-]*).t[glx]z$/;
- $pkg = "$1";
- $slackdb{$pkg}[0] = "$name";
- $slackdb{$pkg}[1] = "$2";
- $slackdb{$pkg}[2] = "$3";
- $slackdb{$pkg}[3] = "$4";
- next;
- }
- if ($_ =~ /(^PACKAGE LOCATION:\s\s\.)(.*)/) {
- $slackdb{$pkg}[4] = "$2";
- next;
- }
- if ($_ =~ /(^PACKAGE REQUIRED:\s\s)(.*)/) {
- $slackdb{$pkg}[5] = "$2";
- next;
- }
- if ($_ =~ /(^PACKAGE\sSIZE\s\(compressed\):\s\s)(.*)/) {
- $slackdb{$pkg}[6] = "$2";
- next;
- }
- if ($_ =~ /(^PACKAGE\sSIZE\s\(uncompressed\):\s\s)(.*)/) {
- $slackdb{$pkg}[7] = "$2";
- next;
- }
- if ($_ =~ /\Q${pkg}\E:(.*)/) {
- $slackdb{$pkg}[8] = $slackdb{$pkg}[8] . "\n" . "$1";
- next;
- }
- else {
- next;
- }
- return %slackdb;
- }
-}
-
-#
# CSV
#
-sub salix2csv {
+sub tocsv {
open(C, ">pkgtxt.csv") or die "Unable to open pkgtxt.csv for writing, aborting.";
+ # choose the CSV separator, \t = tab \@ = @ ....
+ # avoid the comma (,) as it is the separator for dependancies
my $c = "\t";
print C "pkgname${c}pkgver${c}arch${c}pkgrel${c}location${c}dep${c}sizeC${c}sizeU\n";
for my $p ( sort keys %pkgdb ) {
@@ -220,30 +168,17 @@ sub salix2csv {
$p, $pkgdb{$p}[1], $pkgdb{$p}[2], $pkgdb{$p}[3], $pkgdb{$p}[4], $pkgdb{$p}[5], $pkgdb{$p}[6], $pkgdb{$p}[7];
}
close (C);
- print "Salix pkgtxt.csv has been built.\n";
+ print "pkgtxt.csv has been built.\n";
}
-sub slack2csv {
- open(D, ">pkgtxt.csv") or die "Unable to open pkgtxt.csv for writing, aborting.";
- my $c = "\t";
- print D "pkgname${c}pkgver${c}arch${c}pkgrel${c}location${c}dep${c}sizeC${c}sizeU\n";
- for my $q ( sort keys %slackdb ) {
- printf D "%s$c%s$c%s$c%s$c%s$c%s$c%s$c%s$c%s\n",
- $q, $slackdb{$q}[1], $slackdb{$q}[2], $slackdb{$q}[3], $slackdb{$q}[4], $slackdb{$q}[5], $slackdb{$q}[6], $slackdb{$q}[7];
- }
- close (D);
- print "Slackware pkgtxt.csv has been built.\n";
-}
-
-
#
# JSON
#
-sub salix2json {
+sub tojson {
my $out = "pkgtxt.json";
my $n = keys %pkgdb;
open(J, ">$out") or die "Unable to open $out for writing, aborting.";
- print J "\[\n";
+ print J "\[\n";
for my $p ( (keys %pkgdb)[0..($n-2)] ) {
print J " \{\n";
print J " \"pkgname\": \"$p\",\n";
@@ -272,44 +207,7 @@ sub salix2json {
}
print J "\]\n";
close (J);
- print "Salix pkgtxt.json has been built.\n";
+ print "pkgtxt.json has been built.\n";
}
-
-sub slack2json {
- my $out = "pkgtxt.json";
- my $nb = keys %slackdb;
- open(K, ">$out") or die "Unable to open $out for writing, aborting.";
- print K "\[\n";
- for my $q ( (keys %slackdb)[0..($nb-2)] ) {
- print K " \{\n";
- print K " \"pkgname\": \"$q\",\n";
- print K " \"pkgver\": \"$slackdb{$q}[1]\",\n";
- print K " \"arch\": \"$slackdb{$q}[2]\",\n";
- print K " \"pkgver\": \"$slackdb{$q}[3]\",\n";
- print K " \"location\": \"$slackdb{$q}[4]\",\n";
- print K " \"dep\": \"$slackdb{$q}[5]\",\n";
- print K " \"sizeC\": \"$slackdb{$q}[6]\",\n";
- print K " \"sizeU\": \"$slackdb{$q}[7]\"\n";
- print K " \"pkgdesc\": \"$slackdb{$q}[8]\"\n";
- print K " \},\n";
- }
- for my $q ( (keys %slackdb)[($nb-1)] ) {
- print K " \{\n";
- print K " \"pkgname\": \"$q\",\n";
- print K " \"pkgver\": \"$slackdb{$q}[1]\",\n";
- print K " \"arch\": \"$slackdb{$q}[2]\",\n";
- print K " \"pkgver\": \"$slackdb{$q}[3]\",\n";
- print K " \"location\": \"$slackdb{$q}[4]\",\n";
- print K " \"dep\": \"$slackdb{$q}[5]\",\n";
- print K " \"sizeC\": \"$slackdb{$q}[6]\",\n";
- print K " \"sizeU\": \"$slackdb{$q}[7]\"\n";
- print K " \"pkgdesc\": \"$slackdb{$q}[8]\"\n";
- print K " \}\n";
- }
- print K "\]\n";
- close (K);
- print "Slackware pkgtxt.json has been built.\n";
-}
-
1;
blob - aa604be79e558b97557a59456d608293fdf59396
blob + f5c6c7c89bee49bc080b089f1fdf87e03295b454
--- pkgtxt2db
+++ pkgtxt2db
@@ -7,16 +7,17 @@
# License: BSD Revised
#
# Convert the Slackware/Salix PACKAGES.TXT file some various database
-# formats : CSV, json,
+# formats : CSV, json,
#
use strict;
use warnings;
use Getopt::Long;
-use Pkgtxt2db::Pkgtxt2db;
+#use Pkgtxt2db::Pkgtxt2db;
+use Pkgtxt2db;
-my $VERSION = "0.1.1";
+my $VERSION = "0.1.11";
my $Pkgtxt2db = Pkgtxt2db->new();
@@ -26,18 +27,16 @@ my $Pkgtxt2db = Pkgtxt2db->new();
sub usage {
print "Usage: $0 [options]\n\n";
print "Options:\n";
- print " -a | --asc32\t\tconvert Salix PACKAGES.TXT to a CSV DB\n";
- print " -b | --bsc64\t\tconvert Salix64 PACKAGES.TXT to a CSV DB\n";
- print " -c | --cslc32\t\tconvert Slackware PACKAGES.TXT to a CSV DB\n";
- print " -d | --dslc64\t\tconvert Slackware64 PACKAGES.TXT to a CSV DB\n";
- print "\n";
- print " -e | --esj32\t\tconvert Salix PACKAGES.TXT to a JSON DB\n";
- print " -f | --fsj64\t\tconvert Salix64 PACKAGES.TXT to a JSON DB\n";
- print " -g | --gslj32\t\tconvert Slackware PACKAGES.TXT to a JSON DB\n";
- print " -i | --islj64\t\tconvert Slackware64 PACKAGES.TXT to a JSON DB\n";
- print "\n";
+ print " -c | --csv\t\tconvert PACKAGES.TXT to a CSV DB\n";
+ print " -j | --json\t\tconvert PACKAGES.TXT to a JSON DB\n";
+ print " -t | --target\t\tuse with -c or -j, choose slackware or salix\n";
+ print " -r | --release\t\tuse with -t, choose the release\n";
print " -h | --help\t\tprint this message\n";
print " -v | --version\t\tprint pkgtxt2db version\n";
+ print "\n";
+ print "See the man page (man pkgtxt2db) for detailed informations.\n";
+ print "\n";
+
exit 0;
}
@@ -46,29 +45,16 @@ sub usage {
#
my $version;
my $help;
+my $csv;
+my $json;
-my $salix32csv;
-my $salix64csv;
-my $slack32csv;
-my $slack64csv;
-
-my $salix32json;
-my $salix64json;
-my $slack32json;
-my $slack64json;
-
-
GetOptions(
'version' => \$version,
'help' => \&usage,
- 'asc32' => \$salix32csv,
- 'bsc64' => \$salix64csv,
- 'cslc32' => \$slack32csv,
- 'dslc64' => \$slack64csv,
- 'esj32' => \$salix32json,
- 'fsj64' => \$salix64json,
- 'gslj32' => \$slack32json,
- 'islj64' => \$slack64json,
+ 'release=s' => \$Pkgtxt2db::release,
+ 'target=s' => \$Pkgtxt2db::target,
+ 'csv' => \$csv,
+ 'json' => \$json,
);
if ($version) {
print "Pkgtxt2db, version $VERSION\n";
@@ -76,55 +62,25 @@ if ($version) {
print "http://www.salixos.org/wiki/index.php/Pkgtxt2db\n";
exit 0;
}
-if ($salix32csv) {
- Pkgtxt2db->getsalix_d32();
- Pkgtxt2db->mkdasalixdb();
- Pkgtxt2db->salix2csv();
+if ($csv) {
+ Pkgtxt2db->checktarget();
+ Pkgtxt2db->checkrelease();
+ Pkgtxt2db->getpkgtxt();
+ Pkgtxt2db->mkdadb();
+ Pkgtxt2db->getdata();
+ Pkgtxt2db->tocsv();
exit 0;
}
-if ($salix64csv) {
- Pkgtxt2db->getsalix_d64();
- Pkgtxt2db->mkdasalixdb();
- Pkgtxt2db->salix2csv();
+if ($json) {
+ Pkgtxt2db->checktarget();
+ Pkgtxt2db->checkrelease();
+ Pkgtxt2db->getpkgtxt();
+ Pkgtxt2db->mkdadb();
+ Pkgtxt2db->getdata();
+ Pkgtxt2db->tojson();
exit 0;
- }
-if ($slack32csv) {
- Pkgtxt2db->getslack_d32();
- Pkgtxt2db->mkdaslackdb();
- Pkgtxt2db->slack2csv();
- exit 0;
- }
-if ($slack64csv) {
- Pkgtxt2db->getslack_d64();
- Pkgtxt2db->mkdaslackdb();
- Pkgtxt2db->slack2csv();
- exit 0;
- }
-if ($salix32json) {
- Pkgtxt2db->getsalix_d32();
- Pkgtxt2db->mkdasalixdb();
- Pkgtxt2db->salix2json();
- exit 0;
- }
-if ($salix64json) {
- Pkgtxt2db->getsalix_d64();
- Pkgtxt2db->mkdasalixdb();
- Pkgtxt2db->salix2json();
- exit 0;
- }
-if ($slack32json) {
- Pkgtxt2db->getslack_d32();
- Pkgtxt2db->mkdaslackdb();
- Pkgtxt2db->slack2json();
- exit 0;
- }
-if ($slack64json) {
- Pkgtxt2db->getslack_d64();
- Pkgtxt2db->mkdaslackdb();
- Pkgtxt2db->slack2json();
- exit 0;
- }
+ }
else {
die "Wrong usage, run pkgtxt2db --help for information.\n";
-}
+}
Frédéric Galusik