File: Parser.pm

package info (click to toggle)
devscripts 2.25.30
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,504 kB
  • sloc: perl: 27,293; sh: 12,873; python: 4,466; makefile: 382
file content (84 lines) | stat: -rw-r--r-- 2,962 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package Devscripts::Uscan::WatchSource::Parser;

use Moo::Role;
use Devscripts::Uscan::Output;

sub parseWatchFile {
    my ($self, $watchFileHandle, $args) = @_;
    # Read in paragraph mode
    local $/ = "";
    my $map = sub {
        return map {
            my @lines = split /\n/, $_;
            my @res;
            foreach my $line (@lines) {
                # Skip comments and blank lines
                next if $line =~ /^\s*(?:#.*)$/;
                # Distinct rejection message for bad "version" line
                if ($line =~ /^version\s*=\s*(.*?)\s*$/i and $1 >= 5) {
                    uscan_die
"Malformed version line: $line, only 'Version: X' is allowed when using version >= 5";
                    # Valid rfc822 field
                } elsif ($line !~ /^([\w\-]+)\s*:\s*(.*?)\s*$/) {
                    die "Unable to parse line '$line', skipping";
                }
                my ($k, $v) = (lc($1), $2);
                $k =~ s/-//g;
                push @res, $k, $v;
            }
            push @res, _raw => $_;
            return {@res};
        } @_;
    };
    $self->commonOpts($map->($watchFileHandle->getline));
    unless (%{ $self->commonOpts }) {
        die "Unable to parse $args->{watchfile} empty header";
    }
    unless ($self->commonOpts->{version}) {
        die 'Missing "Version" field in header, skipping '
          . $args->{watchfile};
    }
    unless ($self->commonOpts->{version} >= 5) {
        die
"Malformed file $args->{watchfile}, version $self->{commonOpts}->{version} is lower than 5";
    }
    if ($self->commonOpts->{version}
        > $Devscripts::Uscan::Config::CURRENT_WATCHFILE_VERSION) {
        die
"$args->{watchfile} uses a newer version ($self->{commonOpts}->{version}) than supported ("
          . $Devscripts::Uscan::Config::CURRENT_WATCHFILE_VERSION
          . '), skipping this file';
    }
    $self->watch_version($self->commonOpts->{version});
    if ($self->commonOpts->{untrackable}) {
        uscan_warn "Untrackable project: " . $self->commonOpts->{untrackable};
        return;
    }
    my $line;
    my $found;
    while (defined($line = $watchFileHandle->getline)) {
        $found++;
        my $watchOptions = $map->($line);
        unless ($watchOptions->{source} || $watchOptions->{template}) {
            uscan_warn
"The following paragraph isn't well formatted, skipping it: << ==EOF==\n"
              . $watchOptions->{_raw}
              . "==EOF==\n";
            next;
        }
        foreach my $k (keys %{ $self->commonOpts }) {
            $watchOptions->{$k} //= $self->commonOpts->{$k};
        }

        push @{ $self->watchOptions }, $watchOptions;
    }
    if (!$found
        and ($self->commonOpts->{source} or $self->commonOpts->{template})) {
        push @{ $self->watchOptions },
          {
            map  { ($_ => $self->commonOpts->{$_}) }
            grep { $_ ne 'version' } keys %{ $self->commonOpts } };
    }
}

1;