1 |
Author: sniper |
2 |
Date: 2005-05-11 05:54:31 +0200 (Wed, 11 May 2005) |
3 |
New Revision: 10 |
4 |
|
5 |
Modified: |
6 |
code/g-cpan.pl |
7 |
Log: |
8 |
Code cleaning centralisation error messages (easy internationalisation by example) |
9 |
- Add ERR_ constants, use with fatal() |
10 |
- get rid of die() and use fatal() |
11 |
- add sub fatal() :) |
12 |
|
13 |
Add FIXME |
14 |
|
15 |
|
16 |
|
17 |
Modified: code/g-cpan.pl |
18 |
=================================================================== |
19 |
--- code/g-cpan.pl 2005-05-11 02:53:30 UTC (rev 9) |
20 |
+++ code/g-cpan.pl 2005-05-11 03:54:31 UTC (rev 10) |
21 |
@@ -30,6 +30,15 @@ |
22 |
use constant DEF_WGET_PROG => '/usr/bin/wget'; |
23 |
use constant DEF_BASH_PROG => '/bin/bash'; |
24 |
|
25 |
+##### ERRORS constants (easy internationalisation ;-) ##### |
26 |
+use constant ERR_FILE_NOTFOUND => "Couldn't find file '%s'"; # filename |
27 |
+use constant ERR_FOLDER_NOTFOUND => "Couldn't find folder '%s'"; # foldername |
28 |
+use constant ERR_OPEN_READ => "Couldn't open (read) file'%s' : %s"; # filename, $! |
29 |
+use constant ERR_OPEN_WRITE => "Couldn't open (write) file'%s' : %s"; # filename, $! |
30 |
+use constant ERR_OPEN_FOLDER => "Couldn't open folder '%s', %s"; # foldername, $! |
31 |
+use constant ERR_FOLDER_CREATE => "Couldn't create folder '%s' : %s"; # foldername, $! |
32 |
+ |
33 |
+ |
34 |
# predeclared subs |
35 |
sub printbig; |
36 |
|
37 |
@@ -166,19 +175,16 @@ |
38 |
} |
39 |
|
40 |
# Grab the whole available arches list, to include them later in ebuilds |
41 |
+print "Grabbing arch list\n" if $verbose; |
42 |
my $arches = do { |
43 |
- if ($verbose) { |
44 |
- print "Grabbing arch list\n"; |
45 |
- } |
46 |
- open my $tmp, "$PORTAGE_DIR/profiles/arch.list" |
47 |
- or die "Unable to open '$PORTAGE_DIR/profiles/arch.list' : $!"; |
48 |
- join " ", map { chomp; $_ } <$tmp>; |
49 |
- }; |
50 |
+ open my $tmp, "<$PORTAGE_DIR/profiles/arch.list" |
51 |
+ or fatal(ERR_OPEN_READ, "$PORTAGE_DIR/profiles/arch.list", $!); |
52 |
+ join " ", map { chomp; $_ } <$tmp>; |
53 |
+}; |
54 |
|
55 |
- #this should never find the dir, but just to be safe |
56 |
- unless ( -d $tmp_overlay_dir ) { |
57 |
- mkpath( [$tmp_overlay_dir], 1, 0755 ) |
58 |
- or die "Couldn't create '$tmp_overlay_dir': $|"; |
59 |
+#this should never find the dir, but just to be safe |
60 |
+if(not -d $tmp_overlay_dir) { |
61 |
+ mkpath($tmp_overlay_dir, 1, 0755) or fatal(ERR_FOLDER_CREATE, $tmp_overlay_dir, $!); |
62 |
} |
63 |
|
64 |
# Now we cat our dev-perl directory onto our overlay directory. |
65 |
@@ -186,15 +192,13 @@ |
66 |
#i.e. dev-perl/package |
67 |
my $perldev_overlay = File::Spec->catfile( $tmp_overlay_dir, 'perl-gcpan' ); |
68 |
|
69 |
-unless ( -d $perldev_overlay ) { |
70 |
- |
71 |
+if(not -d $perldev_overlay) { |
72 |
# create perldev overlay dir if not present |
73 |
- mkpath( [$perldev_overlay], 1, 0755 ) |
74 |
- or die "Couldn't create '$perldev_overlay': $|"; |
75 |
+ mkpath($perldev_overlay, 1, 0755) or fatal(ERR_FOLDER_CREATE, $perldev_overlay, $!); |
76 |
} |
77 |
|
78 |
# Now we export our overlay directory into the session's env vars |
79 |
-$ENV{'PORTDIR_OVERLAY'} = $tmp_overlay_dir; |
80 |
+$ENV{PORTDIR_OVERLAY} = $tmp_overlay_dir; |
81 |
|
82 |
# sub main.. well, sort of ;p |
83 |
if ($install) { |
84 |
@@ -212,11 +216,12 @@ |
85 |
upgrade_module($_) for (@GLIST); |
86 |
emerge_up_module(@GLIST); |
87 |
} |
88 |
+} |
89 |
|
90 |
+if($install or $upgrade) { |
91 |
+ clean_up(); |
92 |
} |
93 |
|
94 |
-if ( $install or $upgrade ) { clean_up() } |
95 |
- |
96 |
exit; |
97 |
|
98 |
########## |
99 |
@@ -225,6 +230,8 @@ |
100 |
|
101 |
# jrray printing functions |
102 |
sub printbig { |
103 |
+# FIXME Sniper |
104 |
+# 72 hard coded is bad |
105 |
print '*' x 72, "\n"; |
106 |
print '*', "\n"; |
107 |
print '*', "\n"; |
108 |
@@ -243,14 +250,13 @@ |
109 |
# see if an ebuild for $dir exists already. If so, return its name. |
110 |
my $found = ''; |
111 |
|
112 |
- foreach my $sdir ( |
113 |
- grep { -d $_ } ( |
114 |
- @PORTAGE_DEV_PERL, @OVERLAY_PERLS, |
115 |
- $perldev_overlay, @TMP_DEV_PERL_DIRS |
116 |
- ) |
117 |
- ) |
118 |
- { |
119 |
- opendir PDIR, $sdir; |
120 |
+ foreach my $sdir (@PORTAGE_DEV_PERL, @OVERLAY_PERLS, $perldev_overlay, @TMP_DEV_PERL_DIRS) { |
121 |
+ next if not -d $sdir; |
122 |
+ |
123 |
+ opendir PDIR, $sdir or fatal(ERR_OPEN_FOLDER, $sdir, $!); |
124 |
+ # FIXME Sniper |
125 |
+ # we have to use while() construction otherwise we full memory and we read ALL THE folder |
126 |
+ # even if we found the ebiuld on the 'first place', btw we can get rid of List::Util |
127 |
my @dirs = readdir(PDIR); |
128 |
closedir PDIR; |
129 |
$found ||= first { lc($_) eq lc($dir) }(@dirs); |
130 |
@@ -270,8 +276,12 @@ |
131 |
sub get_gcpans { |
132 |
my @g_list; |
133 |
foreach my $sdir ( grep { -d $_ } ( @PORTAGE_DEV_PERL, @OVERLAY_PERLS ) ) { |
134 |
+ # FIXME Sniper |
135 |
+ # Do we really need regexp here ? |
136 |
if ( $sdir =~ m/perl-gcpan/ ) { |
137 |
opendir PDIR, $sdir; |
138 |
+ # FIXME Sniper |
139 |
+ # use while() here |
140 |
my @dirs = readdir(PDIR); |
141 |
closedir PDIR; |
142 |
foreach my $dir (@dirs) { |
143 |
@@ -292,6 +302,8 @@ |
144 |
|
145 |
my $found = ''; |
146 |
|
147 |
+ # FIXME Sniper |
148 |
+ # not nice construct, put grep inside |
149 |
foreach my $sdir ( |
150 |
grep { -d $_ } ( |
151 |
@PORTAGE_DEV_PERL, @OVERLAY_PERLS, |
152 |
@@ -300,6 +312,8 @@ |
153 |
) |
154 |
{ |
155 |
opendir PDIR, $sdir; |
156 |
+ # FIXME Sniper |
157 |
+ # use while() here |
158 |
my @dirs = readdir(PDIR); |
159 |
closedir PDIR; |
160 |
$found ||= first { lc($_) eq lc($dir) }(@dirs); |
161 |
@@ -350,12 +364,14 @@ |
162 |
my $fulldir = File::Spec->catdir( $perldev_overlay, $dir ); |
163 |
my $filesdir = File::Spec->catdir( $fulldir, 'files' ); |
164 |
unless ( -d $fulldir ) { |
165 |
- mkdir $fulldir, 0755 or die "Couldn't create '$fulldir': $!"; |
166 |
+ mkdir($fulldir, 0755) or fatal(ERR_FOLDER_CREATE, $fulldir, $!); |
167 |
} |
168 |
unless ( -d $filesdir ) { |
169 |
- mkdir $filesdir, 0755 or die "Couldn't create '$filesdir': $!"; |
170 |
+ mkdir($filesdir, 0755) or fatal(ERR_FOLDER_CREATE, $filesdir, $!); |
171 |
} |
172 |
|
173 |
+ # FIXME Sniper |
174 |
+ # these tests are useles since done during mkdir() |
175 |
unless ( -d $fulldir ) { die "$fulldir not created!!\n" } |
176 |
unless ( -d $filesdir ) { die "$fulldir not created!!\n" } |
177 |
|
178 |
@@ -383,7 +399,7 @@ |
179 |
my $desc = $module->description || 'No description available.'; |
180 |
|
181 |
print "Writing to $ebuild\n" if ($verbose); |
182 |
- open EBUILD, ">$ebuild" or die "Could not write to '$ebuild': $!"; |
183 |
+ open EBUILD, ">$ebuild" or fatal(ERR_OPEN_WRITE, $ebuild, $!); |
184 |
print EBUILD <<"HERE"; |
185 |
|
186 |
# Copyright 1999-2004 Gentoo Foundation |
187 |
@@ -437,7 +453,7 @@ |
188 |
close EBUILD; |
189 |
|
190 |
# write the digest too |
191 |
- open DIGEST, ">$digest" or die "Could not write to '$digest': $!"; |
192 |
+ open DIGEST, ">$digest" or fatal(ERR_OPEN_WRITE, $digest, $!); |
193 |
print DIGEST $md5, "\n"; |
194 |
close DIGEST; |
195 |
} |
196 |
@@ -515,6 +531,8 @@ |
197 |
|
198 |
create_ebuild( $obj, $dir, $file, $build_dir, $prereq_pm, $md5string ); |
199 |
|
200 |
+ # FIXME Sniper |
201 |
+ # OH MY GOD ! |
202 |
system( '/bin/mv', '-f', $localfile, $PORTAGE_DISTDIR ); |
203 |
|
204 |
push @ebuild_list, "perl-gcpan/$dir"; |
205 |
@@ -602,6 +620,8 @@ |
206 |
} |
207 |
} |
208 |
|
209 |
+# TODO Sniper |
210 |
+# maybee put this in END {} block |
211 |
sub clean_up { |
212 |
|
213 |
#Probably don't need to do this, but for sanity's sake, we reset this var |
214 |
@@ -617,6 +637,8 @@ |
215 |
foreach my $ebuild_name (@ebuild_list) { |
216 |
$ebuild_name =~ m/.*\/(.*)-[^-]+\./; |
217 |
print "$0: emerging $ebuild_name\n"; |
218 |
+ # FIXME Sniper |
219 |
+ # check return values |
220 |
system( "emerge", "--oneshot", "--digest", $ebuild_name ); |
221 |
} |
222 |
} |
223 |
@@ -634,6 +656,8 @@ |
224 |
chomp($answer); |
225 |
if ( $answer =~ m|y|i ) { |
226 |
foreach my $ebuild_name (@ebuild_list) { |
227 |
+ # FIXME Sniper |
228 |
+ # check return values |
229 |
system( "emerge", "--oneshot", "--digest", $ebuild_name ); |
230 |
} |
231 |
} |
232 |
@@ -645,12 +669,14 @@ |
233 |
|
234 |
# Setting default configs |
235 |
my %conf; |
236 |
+ # FIXME Sniper |
237 |
+ # use constants |
238 |
$conf{PORTDIR} = "/usr/portage"; |
239 |
$conf{DISTDIR} = "/usr/portage/distfiles"; |
240 |
my @OVERLAYS = (); |
241 |
|
242 |
# Opening make.conf to find real user settings |
243 |
- open CONF, "<$MAKECONF" or die "Open $MAKECONF failed : $!"; |
244 |
+ open CONF, "<$MAKECONF" or fatal(ERR_OPEN_READ, $MAKECONF, $!); |
245 |
|
246 |
# And parsing it :) |
247 |
while ( defined( my $line = <CONF> ) ) { |
248 |
@@ -723,7 +749,7 @@ |
249 |
|
250 |
printbig "No CPAN Config found, auto-generating a basic one in $cpan_cfg_dir\n"; |
251 |
if(not -d $cpan_cfg_dir) { |
252 |
- mkpath($cpan_cfg_dir, 1, 0755 ) or die "Couldn't create folder '$cpan_cfg_dir' : $!"; |
253 |
+ mkpath($cpan_cfg_dir, 1, 0755 ) or fatal(ERR_FOLDER_CREATE, $cpan_cfg_dir, $!); |
254 |
} |
255 |
|
256 |
my $tmp_dir = -d $ENV{TMPDIR} ? $ENV{TMPDIR} : $ENV{HOME}; |
257 |
@@ -741,7 +767,7 @@ |
258 |
my $unzip_prog = -x DEF_UNZIP_PROG ? DEF_UNZIP_PROG : ''; |
259 |
my $wget_prog = -x DEF_WGET_PROG ? DEF_WGET_PROG : ''; |
260 |
|
261 |
- open CPANCONF, ">$cpan_cfg_file" or die "Couldn't create file '$cpan_cfg_file' : $!"; |
262 |
+ open CPANCONF, ">$cpan_cfg_file" or fatal(ERR_FOLDER_CREATE, $cpan_cfg_file, $!); |
263 |
print CPANCONF <<"SHERE"; |
264 |
|
265 |
# This is CPAN.pm's systemwide configuration file. This file provides |
266 |
@@ -795,16 +821,14 @@ |
267 |
# Simple useful sub. returns md5 hexdigest of the given argument |
268 |
# awaits a file name. |
269 |
sub file_md5sum { |
270 |
+ my ($file) = @_;; |
271 |
+ print "Computing MD5 Sum of $file\n" if $verbose; |
272 |
|
273 |
- my $file = $_[0]; |
274 |
- |
275 |
- if ($verbose) { |
276 |
- print "Computing MD5 Sum of $file\n"; |
277 |
- } |
278 |
- |
279 |
- open(DIGIFILE, $file ) or die "Can't open '$file': $!"; |
280 |
+ open DIGIFILE, $file or fatal(ERR_OPEN_READ, $file, $!); |
281 |
my $md5digest = Digest::MD5->new->addfile(*DIGIFILE)->hexdigest; |
282 |
- close(DIGIFILE); |
283 |
+ # FIXME Sniper |
284 |
+ # here, check the md5sum |
285 |
+ close DIGIFILE; |
286 |
|
287 |
return $md5digest; |
288 |
} |
289 |
@@ -822,6 +846,17 @@ |
290 |
} |
291 |
} |
292 |
|
293 |
+################################################# |
294 |
+# NAME : fatal |
295 |
+# AUTHOR: David "Sniper" Rigaudiere |
296 |
+# OBJECT: die like with pattern format |
297 |
+# |
298 |
+# IN: 0 scalar pattern sprintf format |
299 |
+# x LIST variables filling blank in pattern |
300 |
+################################################# |
301 |
+sub fatal { die sprintf(shift, @_) } |
302 |
+ |
303 |
+ |
304 |
sub exit_usage { |
305 |
print <<"USAGE"; |
306 |
Usage : $0 <Switch(es)> Module Name(s) |
307 |
|
308 |
|
309 |
-- |
310 |
gentoo-perl@g.o mailing list |