Sat Oct 19 19:10:31 2019 UTC ()
pkgtools/R2pkg: make license handling code simpler


(rillig)
diff -r1.22 -r1.23 pkgsrc/pkgtools/R2pkg/files/R2pkg.R
diff -r1.17 -r1.18 pkgsrc/pkgtools/R2pkg/files/R2pkg_test.R

cvs diff -r1.22 -r1.23 pkgsrc/pkgtools/R2pkg/files/R2pkg.R (expand / switch to unified diff)

--- pkgsrc/pkgtools/R2pkg/files/R2pkg.R 2019/10/19 18:43:51 1.22
+++ pkgsrc/pkgtools/R2pkg/files/R2pkg.R 2019/10/19 19:10:31 1.23
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: R2pkg.R,v 1.22 2019/10/19 18:43:51 rillig Exp $ 1# $NetBSD: R2pkg.R,v 1.23 2019/10/19 19:10:31 rillig Exp $
2# 2#
3# Copyright (c) 2014,2015,2016,2017,2018,2019 3# Copyright (c) 2014,2015,2016,2017,2018,2019
4# Brook Milligan. All rights reserved. 4# Brook Milligan. All rights reserved.
5# 5#
6# Redistribution and use in source and binary forms, with or without 6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions 7# modification, are permitted provided that the following conditions
8# are met: 8# are met:
9# 1. Redistributions of source code must retain the above copyright 9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer. 10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright 11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the 12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution. 13# documentation and/or other materials provided with the distribution.
14# 3. Neither the name of the author nor the names of any contributors 14# 3. Neither the name of the author nor the names of any contributors
@@ -598,36 +598,26 @@ write.Makefile <- function(orig_mklines, @@ -598,36 +598,26 @@ write.Makefile <- function(orig_mklines,
598 varassign('LICENSE', license), 598 varassign('LICENSE', license),
599 '', 599 '',
600 dependencies[1], 600 dependencies[1],
601 varassign('USE_LANGUAGES', use_languages), 601 varassign('USE_LANGUAGES', use_languages),
602 '', 602 '',
603 '.include "../../math/R/Makefile.extension"', 603 '.include "../../math/R/Makefile.extension"',
604 dependencies[2], 604 dependencies[2],
605 '.include "../../mk/bsd.pkg.mk"', 605 '.include "../../mk/bsd.pkg.mk"',
606 recursive = TRUE) 606 recursive = TRUE)
607 607
608 writeLines(lines, 'Makefile') 608 writeLines(lines, 'Makefile')
609} 609}
610 610
611construct.line <- function(df,key,value) 
612{ 
613 key <- df[df$key==key,'key'] 
614 operator <- df[df$key==key,'operator'] 
615 delimiter <- df[df$key==key,'delimiter'] 
616 value <- df[df$key==key,value] 
617 df$new_line[df$key==key] <- paste0(key,operator,delimiter,value) 
618 df 
619} 
620 
621element <- function(mklines, varname, field, quiet=FALSE) 611element <- function(mklines, varname, field, quiet=FALSE)
622{ 612{
623 i <- match(varname, mklines$key, 0) 613 i <- match(varname, mklines$key, 0)
624 if (i != 0 && mklines$key_value[i]) 614 if (i != 0 && mklines$key_value[i])
625 return(mklines[i, field]) 615 return(mklines[i, field])
626 616
627 if (!quiet) 617 if (!quiet)
628 { 618 {
629 if (i == 0) 619 if (i == 0)
630 level.warning(varname, ' not found') 620 level.warning(varname, ' not found')
631 else 621 else
632 level.warning(varname, ' is not a key-value field') 622 level.warning(varname, ' is not a key-value field')
633 } 623 }
@@ -675,57 +665,37 @@ make.new_license <- function(df,license) @@ -675,57 +665,37 @@ make.new_license <- function(df,license)
675 665
676license.in.pkgsrc <- function(license) { license %in% sapply(licenses,'[',1) } 666license.in.pkgsrc <- function(license) { license %in% sapply(licenses,'[',1) }
677 667
678make.license <- function(df) 668make.license <- function(df)
679{ 669{
680 # message('===> make.license():') 670 # message('===> make.license():')
681 old_license <- element(df,'LICENSE','old_value') 671 old_license <- element(df,'LICENSE','old_value')
682 old_todo <- element(df,'LICENSE','old_todo') 672 old_todo <- element(df,'LICENSE','old_todo')
683 new_license <- element(df,'LICENSE','new_value') 673 new_license <- element(df,'LICENSE','new_value')
684 674
685 old_known <- license.in.pkgsrc(old_license) 675 old_known <- license.in.pkgsrc(old_license)
686 new_known <- license.in.pkgsrc(new_license) 676 new_known <- license.in.pkgsrc(new_license)
687 677
688 if (old_known && new_known) 678 license <- if (!old_known)
689 { 679 paste0(new_license, '\t# [R2pkg] previously: ', old_license)
690 if (case.insensitive.equals(old_license,new_license)) 680 else if (!new_known)
691 { 681 paste0(old_license, '\t# [R2pkg] updated to: ', new_license)
692 license <- old_license 682 else if (case.insensitive.equals(old_license, new_license))
693 todo <- old_todo 683 old_license
694 } 
695 else 
696 { 
697 license <- paste0(new_license,'\t# [R2pkg] previously: ',old_license) 
698 todo <- old_todo 
699 } 
700 } 
701 else if (old_known && !new_known) 
702 { 
703 license <- paste0(old_license,'\t# [R2pkg] updated to: ',new_license) 
704 todo <- '# TODO: ' 
705 } 
706 else if (!old_known && new_known) 
707 { 
708 license <- paste0(new_license,'\t# [R2pkg] previously: ',old_license) 
709 todo <- '' 
710 } 
711 else 684 else
712 { 685 paste0(new_license, '\t# [R2pkg] previously: ', old_license)
713 license <- paste0(new_license,'\t# [R2pkg] previously: ',old_license) 
714 todo <- '# TODO: ' 
715 } 
716 686
717 df$value[df$key == 'LICENSE'] <- license 687 df$value[df$key == 'LICENSE'] <- license
718 df$todo[df$key == 'LICENSE'] <- todo 688 df$todo[df$key == 'LICENSE'] <- if (new_known) old_todo else '# TODO: '
719 689
720 df 690 df
721} 691}
722 692
723make.r_pkgver <- function(df) element(df,'R_PKGVER','new_value') 693make.r_pkgver <- function(df) element(df,'R_PKGVER','new_value')
724 694
725find.order <- function(df,key,field) 695find.order <- function(df,key,field)
726{ 696{
727 x <- df[,key] 697 x <- df[,key]
728 value <- match(TRUE,x) 698 value <- match(TRUE,x)
729 if (!is.na(value)) 699 if (!is.na(value))
730 value <- df[value,field] 700 value <- df[value,field]
731 value 701 value

cvs diff -r1.17 -r1.18 pkgsrc/pkgtools/R2pkg/files/R2pkg_test.R (expand / switch to unified diff)

--- pkgsrc/pkgtools/R2pkg/files/R2pkg_test.R 2019/10/19 18:43:51 1.17
+++ pkgsrc/pkgtools/R2pkg/files/R2pkg_test.R 2019/10/19 19:10:31 1.18
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: R2pkg_test.R,v 1.17 2019/10/19 18:43:51 rillig Exp $ 1# $NetBSD: R2pkg_test.R,v 1.18 2019/10/19 19:10:31 rillig Exp $
2# 2#
3# Copyright (c) 2019 3# Copyright (c) 2019
4# Roland Illig. All rights reserved. 4# Roland Illig. All rights reserved.
5# 5#
6# Redistribution and use in source and binary forms, with or without 6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions 7# modification, are permitted provided that the following conditions
8# are met: 8# are met:
9# 1. Redistributions of source code must retain the above copyright 9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer. 10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright 11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the 12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution. 13# documentation and/or other materials provided with the distribution.
14# 3. Neither the name of the author nor the names of any contributors 14# 3. Neither the name of the author nor the names of any contributors
@@ -569,29 +569,26 @@ test_that('write.Makefile', { @@ -569,29 +569,26 @@ test_that('write.Makefile', {
569 'CATEGORIES=\tcategory', 569 'CATEGORIES=\tcategory',
570 '', 570 '',
571 'MAINTAINER=\t', # FIXME 571 'MAINTAINER=\t', # FIXME
572 'COMMENT=\tNA', # FIXME 572 'COMMENT=\tNA', # FIXME
573 'LICENSE=\tNA', # FIXME 573 'LICENSE=\tNA', # FIXME
574 '', 574 '',
575 'USE_LANGUAGES=\t# none', 575 'USE_LANGUAGES=\t# none',
576 '', 576 '',
577 '.include "../../math/R/Makefile.extension"', 577 '.include "../../math/R/Makefile.extension"',
578 '.include "../../mk/bsd.pkg.mk"' 578 '.include "../../mk/bsd.pkg.mk"'
579 )) 579 ))
580}) 580})
581 581
582# test_that('construct.line', { 
583# }) 
584 
585test_that('element', { 582test_that('element', {
586 mklines <- make_mklines( 583 mklines <- make_mklines(
587 'COMMENT=\tThe comment', 584 'COMMENT=\tThe comment',
588 'EMPTY=') 585 'EMPTY=')
589 586
590 expect_equal(element(mklines, 'COMMENT', 'order'), 1) 587 expect_equal(element(mklines, 'COMMENT', 'order'), 1)
591 expect_equal(element(mklines, 'COMMENT', 'old_value'), 'The comment') 588 expect_equal(element(mklines, 'COMMENT', 'old_value'), 'The comment')
592 expect_equal(element(mklines, 'UNKNOWN', 'order'), '???') # FIXME: should be a number 589 expect_equal(element(mklines, 'UNKNOWN', 'order'), '???') # FIXME: should be a number
593 expect_equal(element(mklines, 'EMPTY', 'old_value'), '') 590 expect_equal(element(mklines, 'EMPTY', 'old_value'), '')
594}) 591})
595 592
596# test_that('make.categories', { 593# test_that('make.categories', {
597# }) 594# })
@@ -609,28 +606,80 @@ test_that('make.comment', { @@ -609,28 +606,80 @@ test_that('make.comment', {
609 mklines$new_value[[1]] <- 'old Comment' 606 mklines$new_value[[1]] <- 'old Comment'
610 expect_equal(make.comment(mklines), 'Old comment') 607 expect_equal(make.comment(mklines), 'Old comment')
611}) 608})
612 609
613# test_that('make.new_license', { 610# test_that('make.new_license', {
614# }) 611# })
615 612
616# test_that('license.marked.todo', { 613# test_that('license.marked.todo', {
617# }) 614# })
618 615
619# test_that('license.in.pkgsrc', { 616# test_that('license.in.pkgsrc', {
620# }) 617# })
621 618
622# test_that('make.license', { 619test_that('make.license, old and new known and equal', {
623# }) 620 mklines <- make_mklines(
 621 'LICENSE=\tgnu-gpl-v2')
 622 mklines$new_value <- 'gnu-gpl-v2'
 623
 624 updated <- make.license(mklines)
 625
 626 expect_equal(updated$value, 'gnu-gpl-v2')
 627 expect_equal(updated$todo, '')
 628})
 629
 630test_that('make.license, old and new known and changed', {
 631 mklines <- make_mklines(
 632 'LICENSE=\tgnu-gpl-v2')
 633 mklines$new_value <- 'gnu-gpl-v3'
 634
 635 updated <- make.license(mklines)
 636
 637 expect_equal(updated$value, 'gnu-gpl-v3\t# [R2pkg] previously: gnu-gpl-v2')
 638 expect_equal(updated$todo, '')
 639})
 640
 641test_that('make.license, old known, new unknown', {
 642 mklines <- make_mklines(
 643 'LICENSE=\tgnu-gpl-v2')
 644 mklines$new_value <- 'unknown-license'
 645
 646 updated <- make.license(mklines)
 647
 648 expect_equal(updated$value, 'gnu-gpl-v2\t# [R2pkg] updated to: unknown-license')
 649 expect_equal(updated$todo, '# TODO: ')
 650})
 651
 652test_that('make.license, old unknown, new known', {
 653 mklines <- make_mklines(
 654 'LICENSE=\tunknown-license')
 655 mklines$new_value <- 'gnu-gpl-v2'
 656
 657 updated <- make.license(mklines)
 658
 659 expect_equal(updated$value, 'gnu-gpl-v2\t# [R2pkg] previously: unknown-license')
 660 expect_equal(updated$todo, '')
 661})
 662
 663test_that('make.license, old unknown, new also unknown', {
 664 mklines <- make_mklines(
 665 'LICENSE=\tunknown-license')
 666 mklines$new_value <- 'new-unknown'
 667
 668 updated <- make.license(mklines)
 669
 670 expect_equal(updated$value, 'new-unknown\t# [R2pkg] previously: unknown-license')
 671 expect_equal(updated$todo, '# TODO: ')
 672})
624 673
625# test_that('make.r_pkgver', { 674# test_that('make.r_pkgver', {
626# }) 675# })
627 676
628test_that('find.order', { 677test_that('find.order', {
629 mklines <- make_mklines( 678 mklines <- make_mklines(
630 'CATEGORIES=', 679 'CATEGORIES=',
631 'HOMEPAGE=', 680 'HOMEPAGE=',
632 'USE_TOOLS+=', 681 'USE_TOOLS+=',
633 '.include "other.mk"', 682 '.include "other.mk"',
634 '# comment') 683 '# comment')
635 684
636 vars_order <- find.order(mklines, 'key_value', 'order') 685 vars_order <- find.order(mklines, 'key_value', 'order')