Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:eeich:hpc
gnu-compilers-hpc
spec_fix.pl
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File spec_fix.pl of Package gnu-compilers-hpc
#! /usr/bin/perl # # Copyright © 2016 Egbert Eich <eich@suse.com> # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that copyright # notice and this permission notice appear in supporting documentation, and # that the name of the copyright holders not be used in advertising or # publicity pertaining to distribution of the software without specific, # written prior permission. The copyright holders make no representations # about the suitability of this software for any purpose. It is provided "as # is" without express or implied warranty. # # THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO # EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, # DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # Replace: @@FOO@@ with value if value exists in replacement file # Replace: @@FOO?bar@@ with 'bar' if FOO exists and is non-empty, # otherwise leave empty. # Replace: @@PACKAGE_NAME@@ by specfile name (modulo extension) # # specfile name, template and baselib templates can be specified in replacement # file: # specifile: <specfile_name> # template: <template_file> # baselib_template: <baselib_template_file> # if no specfile template is specified, will search for: # a file: <specfile_name>.in # any file: *.in # if no specfile is specified, search for a file <template_file> with # the extend .spec.in removed. use File::Find; use Text::Diff; use File::Copy; use feature state; sub find_spec_in { if (/^\.$/) { return; } elsif (/.*\.spec\.in$/) { push @spec_in_list, ($File::Find::name); } $File::Find::prune = 1; return; } sub find_changes_files { if (/^\.$/) { return; } elsif (/.*\.changes$/) { push @changes_files_list, ($File::Find::name); } $File::Find::prune = 1; return; } sub get_changes_file { my $replace = shift @_; my $specfile = shift @_; my $file; my $tmp = ""; my $count = 0; $specfile =~ s/([^\.]+)\..*/\1/; $targetfile = $specfile . ".changes"; find (\&find_changes_files, './'); if (@changes_files_list == 1) { if ($changes_files_list[0] =~ /$targetfile/) { return; } else { return ( $targetfile, $changes_files_list[0] ); } } foreach $file (@changes_files_list) { next if ($file =~ /$targetfile/); return ($targetfile, $file) if ($$replace{PNAME} && $file =~ /$$replace{PNAME}\.changes/); $tmp = $file; $count ++; } if ($count == 1) { return ($targetfile, $tmp); } return; } sub test_for_changes { my $source = shift @_; my $target = shift @_; my $t, $ret = 0, @a; diff $source, $target, { STYLE => "OldStyle", OUTPUT => sub { $ret = -1 if (!$ret); $t = shift; @a = split /\n/, $t; foreach $t (@a) { $ret = 1 if ( $t =~ /^>.*/ ) }}}; return $ret; } sub fill_rep { state %replacements; state $specfile =""; state $specfile_template =""; state $baselib_template =""; my $last = 1; my $fh = shift @_; while ((chop($line = <$fh>))) { if ($line =~ /specfile:\s(.*)/) { $specfile=$1; } elsif ($line =~ /specfile_template:\s(.*)/) { $specfile_template=$1; } elsif ($line =~ /baselib_template:\s(.*)/) { $baselib_template=$1; } elsif ($line =~ /next:/) { $last = 0; } elsif ($line =~ /^-([A-Z_]+)$/) { delete $replacements{$1}; } elsif ($line =~ /^([A-Z_]+)\s(.*)$/) { $replacements{$1} = $2; } elsif ($line =~ /(^[A-Z_]+)$/) { $replacements{$1} = ""; } last if ( $last == 0 ); } return ( $last, $specfile, $specfile_template, $baselib_template, %replacements ); } sub replace_line { my $line = shift @_; my $replace = shift @_; local @fields; local $i, $start, $end, $tmp; @fields = split /\@\@/, $line; $outline = $fields[0]; if ( $line =~ /.*\@\@$/ ) { $fields[@fields] = ""; } for ( $i = 1; $i < @fields; $i++ ) { if ((!($i == (@fields - 1)) || ($line =~ /.*\@\@$/)) && (exists $$replace{$fields[$i]})) { $tmp = $$replace{$fields[$i]}; $outline .= $tmp . $fields[$i + 1]; $i++; } elsif ((!($i == (@fields - 1)) || ($line =~ /.*\@\@$/)) && ($fields[$i] =~ /([^\?]+)\?(.*)/ && (exists $$replace{$1}))) { $outline .= $2 if ($$replace{$1}); $outline .= $fields[$i + 1]; $i++; } else { $outline .= "@@" . $fields[$i]; } } $cnt = @fields; return $outline; } sub preamble { my $infile = shift @_; my $outfile = shift @_; open (OUTFILE, '>', $outfile) or die ("cannot open $outfile\n"); print OUTFILE "# DO NOT EDIT! Edit $infile instead.\n"; close OUTFILE; } sub replace_file { my $infile = shift @_; my $outfile = shift @_; my $replace = shift @_; local $outline; preamble ($infile, $outfile); open (INFILE, '<', $infile) or die ("cannot open $infile for reading\n"); open (OUTFILE, '>>', $outfile) or die ("cannot open $outfile\n"); while (chop($line = <INFILE>)) { $outline = replace_line ($line, $replace); # print "$outline\n"; print OUTFILE "$outline\n"; } close OUTFILE; } sub check_arg { local $opt = shift @_; local $arg = shift @_; die ("no argument to $opt" ) unless ( $arg ); die ("$opt requires an argument" ) if ( $arg =~ /-.*/ ); } while ( $a = shift @ARGV ) { if ( $a =~ /-r/ || $a =~ /--replacementlist/ ) { $replacementfile = shift @ARGV; check_arg $a, $replacementfile; } elsif ( $a =~ /-s/ || $a =~ /--specfile-template/ ) { $specfile_template = shift @ARGV; check_arg $a, $specfile_template; } elsif ( $a =~ /-b/ || $a =~ /--baselib-template/ ) { $baselibs_template = shift @ARGV; check_arg $a, $baselibs_template; } } if ( !$replacementfile ) { die ("no replacement file specified"); } open (FILE, $replacementfile); do { $specfile = ""; $specfile_template = ""; $baselibs_templatee = ""; @spec_in_list = (); ($last, $tmp1, $tmp2, $tmp3, %rep) = fill_rep(\*FILE); break if ($last); if ( $tmp1 && !$specfile ) { $specfile = $tmp1; } if ( $tmp2 && !$specfile_template ) { $specfile_template = $tmp2; } if ( $tmp3 && !$baselibs_template ) { $baselibs_template = $tmp3; } if ( !$specfile_template ) { if ( $specfile ) { $tmpfile = replace_line $specfile, \%rep ; if ( -e $tmpfile . ".spec.in" ) { $tmp = $tmpfile . ".spec.in"; $specfile_template = $tmp if (-e $tmp ); } } if ( !$specfile_template ){ find (\&find_spec_in, './'); if ( @spec_in_list == 1 ) { $specfile_template = @spec_in_list[0]; } elsif ( @spec_in_list > 1 ) { if ( $specfile ) { foreach $tmp (@spec_in_list) { if ( $tmp eq ($specfile . ".in")) { $specfile_template = $tmp; last; } } } } die ("no specfile_template file specified") unless ( $specfile_template ); } } if ( !$specfile ) { if ( $specfile_template =~ /(.+)\.spec\.in$/ ) { $specfile = $1 . ".spec"; } } if ($specfile && $specfile_template) { $specfile_template = replace_line $specfile_template, \%rep ; $specfile = replace_line $specfile, \%rep ; if (!exists $rep{PACKAGE_NAME} && $specfile =~ /(.*)\.[^\.]+/) { $rep{PACKAGE_NAME} = $1; } ( -e $specfile_template ) || die ("specfile_template doesn't exist\n"); print "Specfile template: $specfile_template\n"; print "Writing $specfile\n"; ( $targetfile, $changes_file ) = get_changes_file(\%rep, $specfile); if ( $targetfile && (! -e $targetfile || ($additions = test_for_changes ($changes_file, $targetfile)) < 0 )) { print "Copying $changes_file to $targetfile\n"; copy ( $changes_file, $targetfile ) or die ("copying failed"); } elsif ( $additions > 0 ) { print "WARNING: Not copying $changes_file to $targetfile - $targetfile has additions!\n"; } replace_file ($specfile_template, $specfile, \%rep); } if ($baselibs_template) { (! -e $baselibs_template ) && die ("$baselibs_template doesn't exist\n"); print "baselibs.conf template: $baselibs_template"; replace_file ($baselibs_template, baselibs.conf, \%rep); } } while (!$last); close FILE;
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor