Gentoo Archives: gentoo-user

From: reader@×××××××.com
To: gentoo-user@l.g.o
Subject: [gentoo-user] Re: walk thru new ._cfg000 - a rough perl script
Date: Fri, 04 Jan 2008 04:22:02
Message-Id: 87y7b6qlh3.fsf@newsguy.com
In Reply to: [gentoo-user] walk thru new ._cfg000 - a rough perl script by reader@newsguy.com
1 reader@×××××××.com writes:
2
3 > I've included the bare script in line at the
4
5 OOps maybe not..
6
7 ===========================
8 ./updetc.pl
9
10 #!/usr/bin/perl
11
12 ## find /etc -iname '._cfg????_*'
13 use strict;
14 use warnings;
15 use File::Find;
16
17 my $myscript;
18 ($myscript = $0) =~ s/^.*\///;
19 # Variable to create directory where needed
20 my $STST = 'START_STOP';
21
22
23 ## produces something like: 091207_101630
24 ## Which means: Aug 12 2007 10:16:30
25 my ($mon,$mday,$year,$hour,$min,$sec) = (localtime(time))[4,3,5,2,1,0];
26 # $year += 1900; ## prints 2005
27 $year -= 100; ## prints 05 (with %02d)
28 $mon += 1;
29 my $PaddedDateStr = sprintf "%02d%02d%02d%s%02d%02d%02d", $mon,$mday,$year,"_",$hour,$min,$sec;
30
31 if(!@ARGV){
32 usage();
33 print "Usage tripped at " . __LINE__ . "\n";
34 exit;
35 }
36 my $dir;
37 my $switch = shift;
38 $dir = shift || die "We need two cmdline arguments: $!";
39
40 if ( ! -d $dir){
41 usage();
42 print "No directory <$dir> can be found\n";
43 print "Usage tripped at " . __LINE__ . "\n";
44 }
45
46 find(\&wanted, $dir);
47 my @ar;
48 sub wanted {
49 if(/^\._cfg0000/){
50 push @ar,$File::Find::name;
51 }
52 }
53 if(!@ar){
54 print "
55 No temporary ._cfg0000_*files were found... exiting\n";
56 exit;
57 }
58 if($switch =~ /\-s\s*$/){
59 ## we are in `show' mode
60 for(@ar){
61 print "$_\n";
62 }
63 exit;
64 }
65
66 if($switch =~ /\-c\s*$/){
67 ## cp new versions over old
68 ## get our filename for echoing and its root
69 my $newconf1 = $ar[0];
70 (my $fs_conf = $newconf1) =~ s/\._cfg0+_//;
71
72 print
73 " ====* Replacing old conf versions with new,, backup both *====
74 We find the new ._cfg000 files and offer a chance to view diffs
75 followed by choosing to use or not. But either way they are
76 stored for future reference or use.
77
78 If you decide to use the conf files we OVERWRITE the old ones but
79 keep a dated copy in a special directory <START_STOP> made for that
80 purpose, wherever needed in the filesystem.
81 Like:
82 copy <$newconf1>
83 OVERWRITING
84 <$fs_conf>
85 (NOTE: We will save renamed copies of both the one on the fs and the
86 new version. New versions are also backed up to that special directory
87 <START_STOP/> created throughout filesystem as needed.)
88
89 If you want more information before proceeding..
90 Press Ctrl-c now and type:
91 \`$myscript help' to learn the names to search for and
92 the detailed proceedure
93 ";
94 print "Press enter to continue or select the Ctrl-c option\n";
95 <STDIN>;
96 print
97 "===== * ===== * ===== * ===== * =====\n";
98 my $ans;
99 for(@ar){
100 ## Setup some more file name renditions we are likely to need
101 my $newconf = $_;
102 ## Reassing fs_conf so old value doesn't pop up unwanted from above
103 ($fs_conf = $newconf) =~ s/\._cfg0+_//;
104 (my $prepended = $newconf) =~ s/(^.*)(\._cfg0+_.*$)/$2/;
105 (my $conf_path = $newconf) =~ s/\/\._cfg0+.*$//;
106 (my $fs_conf_short = $fs_conf) =~ s/^.*\///;
107 my $stt_conf = "START-USE" . $prepended;
108 my $stp_conf = "STOP-USE_" . $fs_conf_short;
109 my $nouse_conf = "NOUSE".$newconf;
110
111 ## Test to see if me have a matching conf already on
112 ## the fs in use.
113 if ( ! -e $fs_conf){
114 ## None present.. so we offer to install AND save it in STST
115 print "
116 <$newconf>
117 has no counterpart on the fs.. Shall we install it like this:
118 cp <$newconf> (over writing!)
119 <$fs_conf>
120 and
121 mv <$newconf
122 <$conf_path/$STST/$stt_conf-$PaddedDateStr>
123 ";
124 print "
125 Any thing but <y> will rename the newconf to:
126 $conf_path/$STST/NOUSE$prepended-$PaddedDateStr
127
128 ";
129 print " [y/n] > ";
130 $ans = <STDIN>;
131 ## Make sure we have the $STST directory
132 if (! -d "$conf_path/$STST") {
133 print "Creating directory $conf_path/$STST\n";
134 mkdir "$conf_path/$STST" ||
135 die "Cannot mkdir $conf_path/$STST: $!";
136 }
137 if ($ans !~ /\by\b/) {
138 print "
139 =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
140 Moving
141 $newconf
142 to
143 $conf_path/$STST/NOUSE$prepended
144 ";
145 rename $newconf, "$conf_path/$STST/NOUSE".$prepended ."-". $PaddedDateStr || die
146 "Can't rename $newconf to $conf_path: $!";
147 print "=-=-=-=-=-=-=-=-=-=-= Rename succeded -=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
148 next;
149 }else {
150
151 print "
152 =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
153 Ok, you've decided to start using this conf file.
154 cp $newconf (installing it)
155 $fs_conf
156 and moving
157 $newconf to
158 $conf_path/$STST/$stt_conf-$PaddedDateStr
159 ";
160 ## open the START-USE file
161 open(START,">$conf_path/$STST/$stt_conf-$PaddedDateStr")
162 or die "Can't open <$conf_path/$STST/$stp_conf-$PaddedDateStr>: $!";
163 ## Write our size tested slurped data to that file Now theoretically
164 ## we have solid copy of the working conf file written to the STST directory
165 print START slurp($fs_conf) . "\n";
166 close(START);
167
168 ## now we rename $newconf over $fs_conf
169 rename $newconf, $fs_conf or die
170 "Can't rename $newconf to $fs_conf: $!";
171 print " =-=-=-=-=-=-=-=-=-=-= Rename Suceeded -=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
172 next;
173 }
174 }else {
175 ## we handle the files that do match their counterparts
176 print " Incoming <$newconf>
177 Shall we install it as described; overwriting the existing one?
178 ";
179 print "
180 Anything but <y> will rename the new conf listed above to
181 $conf_path/$STST/NOUSE$prepended-$PaddedDateStr
182 for reference
183 ";
184 print "
185 **Take a look at the diffs before deciding if you need to**
186 diff $newconf $fs_conf
187
188 ";
189 print " [y/n] > ";
190 $ans = '';
191 $ans = <STDIN>;
192 ## Make sure we have the $STST directory
193 if (! -d "$conf_path/$STST") {
194 print "Creating directory $conf_path/$STST\n";
195 mkdir "$conf_path/$STST" ||
196 die "Cannot mkdir $conf_path/$STST: $!";
197 }
198 if ($ans !~ /\by\b/) {
199 print "
200 =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
201 Moving
202 <$newconf>
203 to
204 <$conf_path/$STST/NOUSE$prepended-$PaddedDateStr>
205 ";
206 rename $newconf, "$conf_path/$STST/NOUSE".$prepended."-".$PaddedDateStr || die
207 "Can't rename <$newconf> to <$conf_path>h: $!";
208 print"
209 =-=-=-=-=-=-=-=-=-=-= Rename Succeeded -=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
210 next;
211 }else {
212 print "
213 =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
214 Copy the working file to
215 <$conf_path/$STST/$stp_conf-$PaddedDateStr>
216 copy the newconf
217 <$newconf>
218 to
219 <$conf_path/$STST/$stt_conf-$PaddedDateStr>
220 And finally rename
221 <$newconf>
222 to
223 <$fs_conf>
224 ";
225
226 ## open the STOP-USE file
227 open(STOP,">$conf_path/$STST/$stp_conf-$PaddedDateStr")
228 or die "Can't open <$conf_path/$STST/$stp_conf-$PaddedDateStr>: $!";
229 ## Write our size tested slurped data to that file Now theoretically
230 ## we have solid copy of the working conf file written to the STST directory
231 print STOP slurp($fs_conf) . "\n";
232 close(STOP);
233
234 ## open the START-USE file
235 open(START,">$conf_path/$STST/$stt_conf-$PaddedDateStr")
236 or die "Can't open <$$conf_path/$STST/$stt_conf-$PaddedDateStr>: $!";
237 ## Write our size tested slurped data to that file Now theoretically
238 ## we have solid copy of the working conf file written to the STST directory
239 print START slurp($newconf) || die
240 "reprint of $newconf to <$conf_path/$STST/$stt_conf-$PaddedDateStr> Failed: $!";
241 close(START);
242
243 ## now we rename $newconf over $fs_conf
244 rename $newconf, $fs_conf or die "Can't rename $newconf to $fs_conf: $!";
245 print " =-=-=-=-=-=-=-=-=-=-= Rename Succeeded -=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
246 }
247 }
248 }
249 }
250
251 ## This routine avoids having to use another module like
252 ## File::Copy to get single file copies preformed.
253 sub slurp {
254 ## Compare reported size to read size
255 ## (will find funky file endings like from dos files)
256 ## amongst other things.
257 my $file = $_;
258 open(my $fh,'<:raw', $file)
259 or die "Cannot open '$file' $!";
260 my $size = -s $fh;
261 $size == read $fh, my $data, $size
262 or die "Cannot \`read' <$file> $!";
263 return $data ;
264 }
265
266 sub usage {
267 print "
268 Purpose: Find files that need updating after emerge update operations
269 on gentoo linux
270 Usage: \`$myscript -[cs] DIRECTORY'
271 flags: -c carry out the copy move operations in DIRECTORY <recursive>
272 -s show the existing ._cfg0000* files in DIRECTORY <recursive>
273
274 Notes: \`$myscript' will give you a chance to bail in several places
275 before any changes are made. It will keep you posted as to what
276 it will do prior to doing it.
277
278 You will also get a ready made diff command to check out what
279 has changed if you want to use it. (requires another terminal
280 running to past the ready made `diff' command in) ";
281 }
282
283 --
284 gentoo-user@g.o mailing list