Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/perl -w
      2 #
      3 # uprev-spec - increments by 1 the value of the Release field in spec files
      4 # 
      5 # Dermot McCluskey 22-July-2003  Initial Version
      6 
      7 
      8 use strict;
      9 use warnings;
     10 use Getopt::Long qw(:config gnu_getopt no_auto_abbrev);
     11 
     12 my $exit_val = 0;
     13 my $verbose = 1;
     14 my $build = "";
     15 my @spec_files = ();
     16 
     17 
     18 &main ();
     19 exit $exit_val;
     20 
     21 
     22 
     23 sub print_msg ($)
     24 {
     25 	($verbose) && print shift;
     26 }
     27 
     28 
     29 sub set_quiet
     30 {
     31 	$verbose = 0;
     32 }
     33 
     34 
     35 sub process_args ($)
     36 {
     37 	push @spec_files, shift;
     38 }
     39 
     40 
     41 sub process_options
     42 {
     43 	Getopt::Long::Configure ("bundling");
     44     
     45 	GetOptions ('q|quiet' => \&set_quiet,
     46 		'h|help' => \&usage,
     47 		'build=n' => \$build,
     48 		'<>' => \&process_args);
     49 }
     50 
     51 
     52 sub usage (;$)
     53 {
     54 	print << "EOF";
     55 
     56 uprev-spec - increments by 1 the value of the Release field in spec files
     57 
     58 Usage: uprev-spec [options] specs...
     59 
     60 options:
     61     -q|--quiet:		Silent operation.
     62     -h|--help:		Print this help message.
     63     --build=n:		If specified, Release will be incremented to at least n
     64 
     65 specs...
     66     path(s) to spec file(s)
     67 EOF
     68 
     69 	exit 0;
     70 }
     71 
     72 
     73 sub up_rev ($)
     74 {
     75 	my $spec_file = shift;
     76 	my $spec_file_base = $spec_file;
     77 	my $temp_file;
     78 	my $found = 0;
     79 	my $out;
     80 
     81 	# if spec_file contains a leading dir, then split it out
     82 	if ($spec_file =~ /.+\/(.+)/) {
     83 		$spec_file_base = $1;
     84 	}
     85 
     86 	$temp_file = "/tmp/$spec_file_base.tmp";
     87 
     88 	&print_msg (sprintf ("%-31s ", $spec_file_base));
     89 
     90 	if (! -e $spec_file) {
     91 		&print_msg ("ERROR: no such spec file: $spec_file\n");
     92 		return 0;
     93 	}
     94 
     95 	if (! -w $spec_file) {
     96 		&print_msg ("ERROR: unable to write to spec file: $spec_file\n");
     97 		return 0;
     98 	}
     99 
    100 	if (-e $temp_file) {
    101 		if (unlink ($temp_file) != 1) {
    102 			&print_msg ("ERROR: Cannot delete old temp file $temp_file: $!\n");
    103 			return 0;
    104 		}
    105 	}
    106 
    107 	if (! open (SPECFILE, $spec_file)) {
    108 		&print_msg ("ERROR: Cannot read $spec_file: $!\n");
    109 		return 0;
    110 	}
    111 
    112 	if (! open (TEMPFILE, ">$temp_file")) {
    113 		&print_msg ("ERROR: Cannot create $temp_file: $!\n");
    114 		return 0;
    115 	}
    116 
    117 	while (<SPECFILE>) {
    118 		/^(release\s*:\s*)(\S*)(\s*)$/i && do {
    119 			my $label = $1;
    120 			my $rel = $2;
    121 			my $tail = $3;
    122 			my $lead = "";
    123 			my $newrel;
    124 
    125 			$found = 1;
    126 
    127 			# if Release value does not consist entirely of digits,
    128 			# then check for leading chars ending with digits
    129 			if ($rel !~ /^\d+$/) {
    130 				if ($rel =~ /^(.*\D+)(\d+)$/) {
    131 					$lead = $1;
    132 					$rel = $2;
    133 				} else {
    134 					&print_msg ("ERROR: non-numeric Release: $rel\n");
    135 					close (SPECFILE);
    136 					close (TEMPFILE);
    137 					return 0;
    138 				}
    139 			}
    140 
    141 			&print_msg (sprintf ("%7s ", "$lead$rel"));
    142 			$newrel = $rel + 1;
    143 
    144 			if (length ($build) && ($newrel < $build)) {
    145 				$newrel = $build;
    146 			}
    147 
    148 			&print_msg (sprintf ("%7s ", "$lead$newrel"));
    149 			print TEMPFILE "$label$lead$newrel$tail";
    150 			next;
    151 		};
    152 
    153 		# print all other lines unmodified
    154 		print TEMPFILE $_;
    155 	}
    156 
    157 	close (TEMPFILE);
    158 	close (SPECFILE);
    159 
    160 	if (! $found) {
    161 		&print_msg ("ERROR: no Release value in spec file\n");
    162 		return 0;
    163 	}
    164 
    165 	$out = `cp $temp_file $spec_file`;
    166 
    167 	if ($?) {
    168 		&print_msg ("ERROR: \"cp $temp_file $spec_file\": $out");
    169 		return 0;
    170 	}
    171 
    172 	unlink ($temp_file);
    173 	&print_msg ("OK\n");
    174 
    175 	return 1;
    176 }
    177 
    178 
    179 sub main
    180 {
    181 	&process_options ();
    182 
    183 	if (! @spec_files) {
    184 		&print_msg ("Nothing to do.\n");
    185 		return;
    186 	}
    187 
    188 	&print_msg ("Spec file			Release	New Rel	Status\n");
    189 	&print_msg ("===============================	=======	=======	=======\n");
    190     
    191 	foreach (@spec_files) {
    192 		&up_rev ($_) || $exit_val++;
    193 	}
    194 }
    195