0

我有一个现有项目,需要在每个源文件的开头使用许可证头。问题是许可证标头不是静态的:

#+======================================================================
# \$HeadURL [filled in by svn] \$
# \$Id [filled in by svn] \$
#
# Project       : Project blah blah - only one line
#
# Description   : A longer description
#                 which may or may not span multiple lines
#
# Author(s)     : some author text
#                 but there may be a few more authors, too!
#
# Copyright (c) : 2010-YYYY Some company,
#                 and a few fixed lines of 
#                 address
#
# and a few more lines of fixed license code
# that does not change
#
#-======================================================================

我有一个现有的 perl 脚本,它扫描文件列表以确定文件类型(C、Java、bash 等)并进行基本检查以查看是否存在许可证序言。

如果没有,它可以插入必须手动更新的空白许可证标题。

但我想知道我该怎么做:

  1. 检测具有非静态信息的现有许可证,以及
  2. 扩展现有的 perl processFile($fileName, $type)函数(如下)以保留现有的“项目”、“描述”和“作者”信息?

我怀疑我可能需要在许可证模板中放置标记以指示动态文本,这应该保留在重新生成的标题中..?

您能否给我一些关于如何使用 perl 正则表达式或模式匹配器来获取当前变量信息的指示,以便我可以将其重新插入标题并更新年份?

我可以看到所有的魔法都需要在“for ($i = 0; $i < 5; ++$i)”循环中发生......

sub processFile {
    my $i;
    my $lineno = 0;
    my $filename = $_[0];
    my $type = $_[1];
    my @license = split(/\n/, $licenses{$type});
    my @contents;

    #print "$filename is a $type file\n";
    tie @contents, 'Tie::File', $filename or die $!;

    if ($prolog{$type}) {   # should not insert license at line 0
        my $len = scalar(@contents);
        while ($lineno < $len) {
            if ($contents[$lineno] =~ /^$prolog{$type}$/) {
                last;
            } else {
                $lineno++;
            }
        }
        if ($lineno >= $len) {
            # no prolog, so let's just insert it into the start
            $lineno = 0;
        } else {
            $lineno = $lineno + 1;
        }
    } else {
        $lineno = 0;
    }

    # Compare the first 5 lines excluding prolog with the license
    # header. If they match, the license header won't be inserted.
    for ($i = 0; $i < 5; ++$i) {
        my $line = $contents[$i + $lineno];
        $line =~ s/\$(\w+)\:.*\$/\$$1\$/;
        if ($line ne $license[$i]) {
            splice @contents, $lineno, 0, @license;
            push @processedFiles, $filename;
            last
        }
    }

    untie @contents;
}
4

1 回答 1

1

为了更深入地回答您的问题,我将不得不看看更多的股票标题是什么样的。正如我在评论中所写,您在此处展示的可用于 bash 或 Perl——可能是 Python 或 Ruby(但您没有提及这些)。

正如我所指出的,您可能会采用处理程序方法。您设置一个处理程序数组,每个处理程序告诉主处理器部分它是否处理该名称的文件。

package CommentFileHandler;

sub _unimpl { 
    local $Carp::CarpLevel = $Carp::CarpLevel + 1;
    Carp::croak( $_[0] . 'needs to implement ' . ( caller 1 )[3] );
}

sub accept_file     { &_unimpl; }
sub scan_for_header { &_unimpl; }
sub has_header      { &_unimpl; }
sub insert_header   { &_unimpl; }

package Perlish::CommentHandler;
use strict;
use warnings;
use parent -norequire 'CommentFileHandler';

sub accept_file { 
    my ( $self, $file_name, $h ) = @_;
    return 1 if $file_name =~ m/\.(?:cgi|(?:ba|[ck])sh|p[lmy]|rb)$/;
    # might need to scan for shbang.
    while ( <$h> ) { 
        if ( m/^#!/ ) { # we're locked in...
            return m/\b(python|perl|ruby|(?:ba|k)?sh) /;
        }
    }
    return;
}

sub scan_for_header {
    # might be as simple as this:
    my ( $self, $h ) = @_;
    my $text = <$h> . <$h>;
    return $text =~ m{
        \A        # Start of all text
        [#]       # literal hash
        [+]       # literal '+'
        ={70}     # 70 '='
        \s*?      # any number of spaces (wide accepter pattern) -- non-greedy
        $         # end of one line
        \s*?      # possible other control character
        ^         # start of the next
        [#]       # literal '#'
        \W+       # At least one "Non-word" word=[_0-9A-Za-z]
        HeadURL\b # The word 'HeadURL' 
    }msx; # x - means that we can expand it as above.
}

sub insert_header { 
    my ( $self, $h ) = @_;
    # expect $h to be rewound back to top of file: seek( $h, 0, 0 )
    print $h <<'END_COMMENT_HEADER';
#+======================================================================
# \$HeadURL [filled in by svn] \$
# \$Id [filled in by svn] \$
#
# Project       : Project blah blah - only one line
#
# Description   : A longer description
#                 which may or may not span multiple lines
#
# Author(s)     : some author text
#                 but there may be a few more authors, too!
#
# Copyright (c) : 2010-YYYY Some company,
#                 and a few fixed lines of 
#                 address
#
# and a few more lines of fixed license code
# that does not change
#
#-======================================================================
END_COMMENT_HEADER
    }

然后,您将跟踪每个已修改的文件,以便您有一个列表来手动编辑文件(如果已修改)。

您可以加载处理程序,如下所示:

my @file_handlers = qw<Perlish::CommentHandler CStyle::CommentHandler ...>;

use List::Util qw<first>;
File::Find::find( sub { 
        # sorry, I'm an "early return" guy.
        unless ( -r $File::Find::name and -w $File::Find::name ) { 
            say "File: $File::Find::name could not be processed.";
            return;
        }

        return unless my $handler 
            = first { $_->accept_file( $File::Find::name ) } @file_handlers
            ;

        # We *have* a handler from here on out.

        unless ( open( my $fh, '<', $File::Find::name )) {
            Carp::carp( "Could not open $File::Find::name!" );
            return;
        }

        # We *have* an open file from here on out...

        return unless $handler->scan_for_header( $File::Find::name, $fh );

        seek( $fh, 0, 0 ); # back to the start of the file.
        my $tmp = File::Temp->new( UNLINK => 1, SUFFIX => '.dat' );
        $handler->insert_header( $tmp );
        print $tmp, $_ while <$fh>;
        $tmp->seek( 0, 0 );
        $fh->close;

        unless ( open( $fh, '>', $File::Find::name )) {
            Carp::carp( "Could not open $File::Find::name to write out header!" );
            return; # You have to check on this
        }

        print $fh $_ while <$tmp>;
        $tmp->close;
        $fh->close;
        # printing out is an easy enough way to "record" our changes.
        say "File $File::Find::name was modified at " . localtime;

    } => @selected_roots 
    ); 
于 2013-01-18T13:38:23.563 回答