Re: portgen(1) use old Makefile as template when upgrading a port

2019-05-14 Thread Andrew Hewus Fresh
This patch lets us remove the FLAVORS if we're updating a port that is
moving from supporting python 2 and 3 to only supporting one of them, it
should also better handle removing the MODPY_VERSION in the very
unlikely case that a python 3 only port adds python 2 support.

Also able to simplify things as we ended up switching to copying
everything except stuff we know we don't want.

Noticed by kmos@ when trying out py-nose that does this

Comments, OK?

Index: infrastructure/lib/OpenBSD/PortGen/Port.pm
===
RCS file: /cvs/ports/infrastructure/lib/OpenBSD/PortGen/Port.pm,v
retrieving revision 1.16
diff -u -p -r1.16 Port.pm
--- infrastructure/lib/OpenBSD/PortGen/Port.pm  14 May 2019 15:00:01 -  
1.16
+++ infrastructure/lib/OpenBSD/PortGen/Port.pm  14 May 2019 16:44:49 -
@@ -312,6 +312,9 @@ sub write_makefile
my @template = $self->parse_makefile("Makefile.orig");
my %copy_values;
 
+   # Decisions elsewhere might effect which values to copy from the 
template
+   my %dont_copy = %{ delete $configs{dont_copy} || {} };
+
if (@template) {
%copy_values = map { $_->{key} => 1 }
grep { $_->{name} ne 'REVISION' }
@@ -380,17 +383,10 @@ sub write_makefile
}
 
# If we didn't get a value, copy from the template
-   # if we know we should and if so, also copy:
-   # * MULTI_PACKAGES until we understand them
-   # * any _DEPENDS that we didn't find
-   # * plus any MODPY_* variables
if ( not $value and %copy_values ) {
-   my $name = $line->{name};
-   my $copy =
-  $copy_values{$key}
-   || $name =~ /_DEPENDS$/
-   || $name =~ /^MODPY_/;
-   $value = $line->{value} if $copy;
+   $value = $line->{value}
+   if $copy_values{$key}
+   and not $dont_copy{$key};
}
 
next unless defined $value;
Index: infrastructure/lib/OpenBSD/PortGen/Port/PyPI.pm
===
RCS file: /cvs/ports/infrastructure/lib/OpenBSD/PortGen/Port/PyPI.pm,v
retrieving revision 1.13
diff -u -p -r1.13 PyPI.pm
--- infrastructure/lib/OpenBSD/PortGen/Port/PyPI.pm 14 May 2019 15:00:01 
-  1.13
+++ infrastructure/lib/OpenBSD/PortGen/Port/PyPI.pm 14 May 2019 16:44:49 
-
@@ -106,10 +106,12 @@ sub fill_in_makefile
 
if ( @versions > 1 ) {
shift @versions; # remove default, lowest
+   $self->{dont_copy}{MODPY_VERSION} = 1;
$self->set_other( 'FLAVORS', "python$_" ) for @versions;
$self->set_other( 'FLAVOR',  '' );
}
elsif ( @versions && $versions[0] != 2 ) {
+   $self->{dont_copy}{$_} = 1 for qw( FLAVORS FLAVOR );
$self->set_other(
MODPY_VERSION => "\${MODPY_DEFAULT_VERSION_$_}" )
for @versions;



Re: portgen(1) use old Makefile as template when upgrading a port

2019-05-09 Thread Andrew Hewus Fresh
Sigh, try this patch instead that actually works on new ports as well.

On Thu, May 09, 2019 at 02:42:08PM -0700, Andrew Hewus Fresh wrote:
> This patch relies on the previous one to have the Makefile.orig actually
> exist.  That's the one in the email with:
> Subject: portgen(1) copy old port when updating
> 
> What this one does is that if we're updating a port (that is, the
> Makefile.orig from the previous patch exists) we will use that as the
> template to copy from which means that if the port has variables that
> aren't in the default template we will put the new values for them in
> those positions.  It also knows about some important values that need to
> be copied over, for example the "EPOCH".
> 
> Rather than rewrite this to make it do what I want, I instead stole the
> version I had previously written and have been using and then modified
> it to work with portgen.  So although this is a "patch", good luck
> reading it.  The original code is here:
> 
> https://github.com/afresh1/openbsd-module-ports/blob/master/lib/OpenBSD/PackageModule/MakePort.pm#L80
> 
> Comments, Questions, OK?
> 
> Be careful, as because I haven't committed the previous patch, this one
> comes from git, based in /usr/ports/infrastructure, instead of cvs.

---
 lib/OpenBSD/PortGen/Port.pm | 155 +++-
 1 file changed, 101 insertions(+), 54 deletions(-)

diff --git a/lib/OpenBSD/PortGen/Port.pm b/lib/OpenBSD/PortGen/Port.pm
index 907728d..c8e6503 100644
--- a/lib/OpenBSD/PortGen/Port.pm
+++ b/lib/OpenBSD/PortGen/Port.pm
@@ -1,6 +1,7 @@
 # $OpenBSD: Port.pm,v 1.5 2019/04/21 03:47:40 afresh1 Exp $
 #
 # Copyright (c) 2015 Giannis Tsaraias 
+# Copyright (c) 2019 Andrew Hewus Fresh 
 #
 # Permission to use, copy, modify, and distribute this software for any
 # purpose with or without fee is hereby granted, provided that the above
@@ -210,77 +211,123 @@ sub name_new_port
return $name;
 }
 
-sub write_makefile
+sub parse_makefile
 {
-   my $self = shift;
+   my ( $self, $path ) = @_;
 
-   open my $tmpl, '<',
-   ports_dir() . '/infrastructure/templates/Makefile.template'
-   or die $!;
-   open my $mk, '>', 'Makefile' or die $!;
+   return unless -e $path;
 
-   my $output = "# \$OpenBSD\$\n";
-   my %vars_found;
+   my @makefile;
 
-   # such a mess, should fix
-   while ( defined( my $line = <$tmpl> ) ) {
-   my ( $value, $other_stuff );
+   my $parse = sub {
+   state $line = '';
+   $line .= shift;
+   return if /\\\n$/x;
 
-   # copy MAINTAINER line as is
-   if ( $line =~ /^MAINTAINER/ ) {
-   $output .= $line and next;
+   if ( $line =~ /^
+   (? \#?   ) \s*
+   (? [A-Z_]+   )
+   (?   \s* \?? = )
+   (?  \s*   )
+   (?   .*)
+   /xms ) {
+   my %line   = %+;
+   my $spaces = delete $line{spaces};
+   $line{tabs}  = $spaces =~ tr/\t/\t/;
+   $line{commented} = $line{comment} ? 1 : 0;
+
+   push @makefile, \%line;
+   } else {
+   chomp $line;
+   push @makefile, $line;
}
+   $line = '';
+   };
 
-   if ( $line =~ /(^#?([A-Z_]+)\s+\??=\s+)/ ) {
-   next unless defined $self->{$2};
-   $vars_found{$2} = 1;
-
-   ( $value, $other_stuff ) = ( $self->{$2}, $1 );
-
-   $other_stuff =~ s/^#//;
-   $other_stuff =~ s/(\?\?\?|$)/$value\n/;
-
-   # handle cases where replacing '???' isn't enough
-   if ( $other_stuff =~ /^PERMIT_PACKAGE_CDROM/ ) {
-   $output .= "# $self->{license}\n";
-   } elsif ( $other_stuff =~ /_DEPENDS/ ) {
-   $other_stuff = "\n" . $other_stuff;
-   } elsif ( $other_stuff =~ /^COMMENT/ ) {
-   $output .= "# original: $self->{full_comment}\n"
-   if $self->{full_comment};
-   } elsif ( $other_stuff =~ /^WANTLIB/ ) {
-   $other_stuff =~ s/=/+=/;
-   }
+   open my $fh, '<', $path or croak("Couldn't open $path: $!");
+   $parse->($_) while <$fh>;
+   close $fh;
 
-   $output .= $other_stuff;
-   }
+   return @makefile;
+}
 
-   $output .= $line if $line =~ /^\s+$/;
-   }
+sub write_makefile
+{
+   my ( $self, $di ) = @_;
+
+   my %configs = %{$self};
+   my $license = delete $configs{license};
 
-   # also write variables not found in the template
-   my