Debian Codegraph @

wtools/MakeAceDB

raw
1 #!/usr/bin/perl
2 #
3 # NB perl sometimes lives somewhere else. You could try /usr/bin/perl, frinstance.
4 #
5 # Originally written by Andrew Menzies, Sanger Institute.
6 # Completed Sept, 2002 by Rob Clack, Sanger Institute.
7 #
8 #
9 # This is a perl script to generate a new AceDB database
10 # from existing wspec directory and zero or more .ace files
11 #
12 # It takes the following parameters on the command line. This is not
13 # perfect, but I don't have time to fix it. If you use a command file
14 # to specify the parameters, each is preceded by an identifying flag,
15 # so they can be in any order.
16 #
17 # However, if you run from the command line, the first 5 parameters can
18 # be in any order, but everything from the target directory on must be
19 # in the order specified.
20 #
21 # NB Be aware that the --userid and --groupid options have not been tested
22 # as I'm not a superuser, so don't have the necessary privileges.
23 #
24 # grpwr option - if 1 then group write access is enabled
25 #
26 # rdwr option - if 1 then enable read-write access, else readonly.
27 #
28 # userid - if provided and script is being run by root then everything
29 # will be owned by $userid
30 #
31 # groupid - if provided and the person running the script is a member
32 # of that group, the groupid will be changed to that supplied.
33 #
34 # tace executable - eg /home/bin/tace
35 # - if not supplied the script will exit.
36 #
37 # target directory - eg /home/work/newacedb
38 # This is where the new Acedb will be built,
39 # - if not supplied the script will exit.
40 # - if it already exists the script will exit.
41 #
42 # Source wspec dir - eg /group/production/acedb/wspec
43 # This is where all the template .wrm files are taken from.
44 # - if not supplied the script will exit.
45 # - if it doesn't exist the script will exit.
46 # - if any of the essential .wrm files are not present script will exit.
47 #
48 # Source .wrm files - eg /home/config/testace/passwd.wrm
49 # Zero or more individual .wrm files, these will overwrite
50 # those copied from the Source wspec directory.
51 # - if defined but found not to exist the script will exit.
52 #
53 # Source .ace files - eg /home/data/dump.1.ace
54 # Zero or more .ace files to read into the new database.
55 # - if not defined the database will be initialised but no data loaded.
56 # - if specified .ace file does not exist the script will exit.
57 #
58 # On normal completion of the script its exit status is 0, anything
59 # else is an error.
60 #
61 # It would be nice if it gave a running commentary of what it was doing
62 # but it doesn't. Sometime....
63 ##########################################################################
64
65 use strict ;
66 use Getopt::Long ;
67 use Pod::Usage;
68
69 my $comfile = ''; # command file used instead of command line options/args
70 my $line = '';
71 my $flag = '';
72 my $value = '';
73 my $man = 0 ;
74 my $help = 0 ;
75 my $login = '';
76 my $passwd = '';
77 my $userid = '';
78 my $groupid = '';
79 my $grpwr = 0 ; # if 1 then enable group write access
80 my $rdwr = 0 ; # if 1 then enable read-write, else readonly
81 my $tace = ''; # location of tace executable - required
82 my $targ_dir ; # where the database will be created - required
83 my $source_wspec; # wspec directory to get wrm files from - required
84 my @source_ace; # location of .ace files if any
85 my @source_wrm; # location of wrm files to override those in wspec
86
87 ##########################################################################
88 ### Essential .wrm files, must be in the $source_wspec directory
89 ##########################################################################
90
91 my @ess_wrms = qw( models.wrm
92 database.wrm
93 options.wrm );
94
95 ##########################################################################
96 ### Voluntary .wrm files. If these are in the $source_wspec directory
97 ### they will be copied across to the new database
98 ###
99 ### NB passwd.wrm is essential, but if there isn't one to copy, we create it.
100 ##########################################################################
101
102 my @vol_wrms = qw( cachesize.wrm
103 displays.wrm
104 layout.wrm
105 passwd.wrm
106 subclasses.wrm );
107
108
109 ##########################################################################
110 # Parse cmd line args.
111 # If a command file is specified with -file then read that to get flags and
112 # arguments, else take them from the command line.
113 ##########################################################################
114
115 GetOptions('help|?' => \$help,
116 man => \$man,
117 'file:s' => \$comfile,
118 'userid:s' => \$userid,
119 'groupid:s' => \$groupid,
120 'grpwr:s' => \$grpwr,
121 'rdwr:s' => \$rdwr,
122 'tace:s' => \$tace) or pod2usage(2);
123
124 pod2usage(1) if $help;
125 pod2usage(-exitval => 0, -verbose => 2) if $man;
126
127
128 if ($comfile ne '')
129 {
130 open (COMFILE, $comfile) || die "Can't open command file $comfile: $!";
131
132 while (defined ($line = <COMFILE>))
133 {
134 ($flag, $value) = split(/=/,$line);
135 chomp($value);
136
137 if ($flag eq "ace")
138 {
139 push(@source_ace, $value);
140 }
141 if ($flag eq "wrm")
142 {
143 push(@source_wrm, $value);
144 }
145 elsif ($flag eq "wspec")
146 {
147 $source_wspec = $value;
148 }
149 elsif ($flag eq "db")
150 {
151 $targ_dir = $value;
152 }
153 elsif ($flag eq "groupid")
154 {
155 $groupid = $value;
156 }
157 elsif ($flag eq "grpwr")
158 {
159 $grpwr = $value;
160 }
161 elsif ($flag eq "rdwr")
162 {
163 $rdwr = $value;
164 }
165 elsif ($flag eq "userid")
166 {
167 $userid = $value;
168 }
169 elsif ($flag eq "tace")
170 {
171 $tace = $value;
172 }
173 }
174
175 close (COMFILE) || die "Problem closing command file $comfile: $!";
176
177 }
178 else
179 {
180 pod2usage("$0: No files given.") if (@ARGV == 0) ;
181
182 foreach my $arg (@ARGV)
183 {
184
185 if ($arg =~ m/^.*?\/wspec/) { $source_wspec = $arg; }
186 elsif($arg =~ m/^.*?\.ace/) { push(@source_ace,$arg); }
187 elsif($arg =~ m/^.*?\.wrm/) { push(@source_wrm,$arg); }
188 else { $targ_dir = $arg; }
189 }
190 }
191
192 ##########################################################################
193 # verify that required parameters are present.
194 ##########################################################################
195
196 if ($targ_dir eq ''
197 || $source_wspec eq ''
198 || $tace eq '')
199 {
200 pod2usage("\nRequired params not found: one of tace, db directory or wspec.\n");
201 }
202
203 ##########################################################################
204 # do we need to pretend to be anyone else?
205 ##########################################################################
206
207 $login = getlogin();
208
209 if ($userid ne '')
210 {
211 if ($login ne "root")
212 {
213 die "\nOnly root can run this for a different user or group.\n\n";
214 }
215 }
216
217 ##########################################################################
218 # is the groupid a valid one for this user
219 ##########################################################################
220
221 my @groups = `groups`;
222 my $group;
223
224 if ($groupid ne '')
225 {
226 while (defined ($group = <@groups>))
227 {
228 if ($group eq $groupid) { last; }
229 }
230 if ($group ne $groupid)
231 {
232 die "\nYou are not a member of the group ($groupid) you've specified.\n\n";
233 }
234 }
235
236 ##########################################################################
237 ### Check everything
238 ##########################################################################
239
240 unless(defined $tace )
241 {
242 print "\n tace binary has not been specified.\n\n";
243 exit 1;
244 }
245
246 # I can't get -x operative to work properly under Cygwin.
247 # so I'm just going to assume the binary is executable on WIN32.
248
249 my $opsys = `uname`;
250 unless($opsys =~ /CYGWIN/)
251 {
252 unless( -x $tace)
253 {
254 print "\n tace: $tace \n is not executable.\n\n";
255 exit 1;
256 }
257 }
258
259
260 ##########################################################################
261 # wspec directory
262 ##########################################################################
263 # Because of some wierdnesses to do with end-of-lines in Windows
264 # we have to lose the last characters of these variables if they've
265 # been derived from a command file, but not otherwise.
266
267 if ($opsys =~ /CYGWIN/ && $comfile ne '')
268 {
269 chop($source_wspec); # remove CR
270 chop($targ_dir);
271 chop($tace);
272 }
273
274 unless(defined $source_wspec && -d $source_wspec)
275 {
276 print "\nwspec $source_wspec is not a directory.\n\n";
277 exit 1;
278 }
279
280 ##########################################################################
281 # essential wrm files
282 ##########################################################################
283
284 foreach my $wrm (@ess_wrms)
285 {
286
287 my $file = "$source_wspec/$wrm";
288
289 unless(-e $file && -r $file)
290 {
291 print "\nwrm file $file is not readable.\n\n";
292 exit 1;
293 }
294 }
295
296 ##########################################################################
297 # non-essential wrm files
298 ##########################################################################
299
300 if(defined $source_wrm[0])
301 {
302
303 foreach my $file (@source_wrm)
304 {
305 unless(-e $file && -r $file)
306 {
307 print "\nwrm file $file is not readable.\n\n";
308 exit 1;
309 }
310 }
311 }
312
313 ##########################################################################
314 # target directory
315 ##########################################################################
316
317 unless(defined $targ_dir)
318 {
319 print "\nNo target dir.\n";
320 exit 1;
321 }
322
323 if(-e $targ_dir)
324 {
325 print "\n$targ_dir already exists.\n\n";
326 exit 1;
327 }
328
329
330 ##########################################################################
331 # .ace files will be used if present
332 ##########################################################################
333
334 if (defined $source_ace[0])
335 {
336 foreach my $acef (@source_ace)
337 {
338 if (-e $acef)
339 {
340 if (-T $acef)
341 {
342 unless (-r $acef)
343 {
344 print "\n.ace file $acef not readable.\n\n";
345 exit 1;
346 }
347 }
348 else
349 {
350 print "\n.ace file is not a text file\n\n";
351 exit 1;
352 }
353 }
354 else
355 {
356 print "\n.ace file doesn't exist\n\n";
357 exit 1;
358 }
359 }
360 }
361 else
362 {
363 print "\nNo .ace files specified.\n";
364 print "Database will be initialised but no data will be loaded.\n\n";
365 }
366
367 ##########################################################################
368 # enable group write access if specified
369 ##########################################################################
370
371 if ( $grpwr == 1 )
372 {
373 umask ( umask() & 0757); # requires leading 0 to force octal
374 }
375
376 ##########################################################################
377 # Make the basic acedb directories.
378 ##########################################################################
379
380 my $dbwspec = "$targ_dir/wspec";
381 my $dbdatabase = "$targ_dir/database" ;
382 mkdir $targ_dir or die "Could not make directory: $targ_dir" ;
383 mkdir $dbdatabase or die "Could not make directory: $dbdatabase" ;
384 mkdir $dbwspec or die "Could not make directory: $dbwspec" ;
385
386 ##########################################################################
387 # populate new directories with required files
388 ##########################################################################
389
390 foreach my $wrm (@ess_wrms)
391 {
392
393 my $file = "$source_wspec/$wrm";
394
395 eval {unless(system("cp $file $dbwspec") == 0)
396 {die "Failed copying $file";}};
397 if($@) {die " Evaluation error copying $dbwspec: $@"; }
398
399 my $newfile = "$dbwspec/$wrm";
400
401 unless(-e $newfile) { exit 1; }
402
403 }
404
405 foreach my $wrm (@vol_wrms)
406 {
407
408 my $file = "$source_wspec/$wrm";
409
410 if(-e $file)
411 {
412
413 eval{ system("cp $file $dbwspec"); };
414
415 }
416
417 }
418
419 foreach my $file (@source_wrm)
420 {
421
422 eval{ unless(system("cp -f $file $dbwspec") == 0) { die; }};
423 if($@) { exit 1; }
424
425 }
426
427 ##########################################################################
428 # Ensure this user is in passwd.wrm. If not present, add her.
429 # If passwd.wrm doesn't exist, create a minimal one
430 ##########################################################################
431
432 if ( -e "$dbwspec/passwd.wrm" )
433 {
434 my $found = 0;
435 my $line;
436
437 # scan passwd.wrm for this user
438
439 open (PWDFILE, "<$dbwspec/passwd.wrm")
440 or die "Can't open passwd.wrm to read: $!";
441 while (defined ($line = <PWDFILE>))
442 {
443 if ($line =~ $login)
444 {
445 $found = 1;
446 last;
447 }
448 }
449 close PWDFILE;
450
451 # if not found, add this user to file
452
453 if (!$found)
454 {
455 chmod 644, "$dbwspec/passwd.wrm";
456 open (PWDFILE, ">>$dbwspec/passwd.wrm")
457 or die "Can't open passwd.wrm to write: $!";
458
459 print PWDFILE "$login \n";
460 print "\n$login not found in passwd.wrm, so I've added her.\n\n";
461 close PWDFILE;
462 }
463 }
464 else
465 {
466 # create new passwd.wrm file & add user to it.
467
468 my $date = `date`;
469 open (PWDFILE, ">$dbwspec/passwd.wrm")
470 or die "Can't create passwd.wrm: $!";
471 print PWDFILE "// passwd.wrm created: $date \n\n";
472
473 print PWDFILE "$login \n";
474
475 close CMDFILE;
476
477 print "\nNew password file created.\n\n";
478 }
479
480
481 ##########################################################################
482 # change ownership if required.
483 # not tested as I'm not a superuser :(
484 ##########################################################################
485
486 if ($userid ne '' || $groupid ne '')
487 {
488
489 # if userid has not been supplied, use -1 to leave as is
490
491 if ( $userid eq '')
492 {
493 $userid = -1;
494 }
495
496 # if groupid has not been supplied, use -1 to take default
497
498 if ( $groupid eq '')
499 {
500 $groupid = -1;
501 }
502
503 # build a list of the files and chown them
504
505 my @filenames = ($targ_dir ,
506 $dbdatabase,
507 $dbwspec ,
508 @source_ace,
509 @source_wrm) ;
510
511 chown($userid, $groupid, @filenames) == @filenames
512 or die "Can't chown @filenames: $!" ;
513 }
514
515 ##########################################################################
516 # run tace to initialise and populate the new database
517 # This requires a temporary command file in which we construct the
518 # ace commands we need to run to complete the operation.
519 ##########################################################################
520
521 my $temp2tace = "$targ_dir/MakeAceDB.$$.cmd";
522
523 open(CMDFILE, ">$temp2tace") or die "\nCan't create tace command file: $!\n\n";
524
525 if (defined (@source_ace[0]))
526 {
527 foreach my $file (@source_ace)
528 {
529
530 print CMDFILE "parse $file\n";
531
532 }
533 }
534 print CMDFILE "save\n";
535 print CMDFILE "exit\n";
536
537 close CMDFILE;
538
539 # the -init flag in the tace command line tells tace to initialise
540 # the database without further prompting.
541
542 unless(system("$tace -init $targ_dir < $temp2tace > /dev/null") == 0)
543 {
544
545 exit 1;
546 }
547
548 ##########################################################################
549 # delete the temporary command file
550 ##########################################################################
551
552 unlink $temp2tace;
553
554 ##########################################################################
555 # if a readonly database is required, remove all users from passwd.wrm
556 ##########################################################################
557
558 if ( $rdwr = 0 )
559 {
560 # overwrite existing passwd.wrm with an empty one
561
562 my $date = `date`;
563 open (PWDFILE, ">$dbwspec/passwd.wrm")
564 or die "Can't reopen passwd.wrm: $!";
565 print PWDFILE "// passwd.wrm created: $date \n\n";
566
567 close CMDFILE;
568 }
569
570 exit 0 ;
571
572
573 __END__
574
575 ##########################################################################
576 # start of pod
577 ##########################################################################
578
579 =head1 NAME
580
581 MakeAceDB - make an acedb database from scratch.
582
583 =head1 SYNOPSIS
584
585 MakeAceDB [options] -tace <tace path> new_db_dir wspec_dir [XXX.wrm...] [XXX.ace...]
586
587 Options:
588 -userid owner for database files
589 -groupid group for database files
590 -grpwr enables/disables group write access
591 -rdwr enables/disables owner write access
592 -tace tace executable to use for intialising database
593 -help brief help message
594 -man full documentation
595
596
597 =head1 OPTIONS
598
599 =over 8
600
601 =item B<-userid new_userid>
602
603 By default the database will be created with your userid as the owner for
604 database files/directories,
605 if you run this script as root you can specify a different owner.
606
607 =item B<-groupid new_groupid>
608
609 By default the database will be created with your default groupid as the group for database
610 files/directories, this option allows you to specify one of your alternate groups (use the
611 unix "groups" command to see what your alternate groups are). You must be root to specify
612 a group other than one of your alternate ones.
613
614 =item B<-rdwr 0 or 1>
615
616 By default the database will be created with whatever your current umask permissions are
617 for database files/directories. This option allows you to disable all write permissions
618 and thus build a readonly database. 0 = disable, 1 = enable write permission.
619
620 =item B<-grpwr 0 or 1>
621
622 This option allows you to enable group write permission if your umask doesn't already do so.
623 0 = disable, 1 = enable group write permission.
624
625 =item B<-help>
626
627 Print a brief help message and exits.
628
629 =item B<-man>
630
631 Prints the manual page and exits.
632
633 =back
634
635 =head1 ARGUMENTS
636
637 =over 8
638
639 =item B<-tace>
640
641 This mandatory argument specifies a tace executable to initialise the database, including
642 reading in the wspec/models.wrm file etc. If you also specify some XXX.ace files on
643 the command line, tace will additionally be used to read them into the database.
644
645 =item B<new_db_dir>
646
647 The directory in which to create all required subdirectories and files for the new database.
648 It must not already exist.
649
650 =item B<wspec_dir>
651
652 A wspec directory from which to copy the essential .wrm files for the database.
653
654 =item B<XXX.wrm>
655
656 Optional .wrm files to overwrite or augment the ones copied from wspec_dir.
657
658 =item B<XXX.ace>
659
660 Optional .ace files specifying data to be added to the database after its initialisation.
661
662 =back
663
664 =head1 DESCRIPTION
665
666 B<MakeAceDB> will read the given input file(s) and build an ace database. If any .ace files
667 are specified, it will load them into the new database, but if not, it will just leave the database
668 initialised and ready for use.
669
670 The primary wrm files are copied from the location given by the wspec parameter, but if any .wrm files
671 are specified, it will copy them over those taken from wspec. ie individually specified .wrm files
672 override any of the same name in the wspec directory.
673
674 =cut