File: pkgjs-easy-to-update

package info (click to toggle)
pkg-js-tools 0.15.23
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,980 kB
  • sloc: perl: 4,921; sh: 842; makefile: 36; javascript: 22
file content (175 lines) | stat: -rwxr-xr-x 4,392 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/perl

use strict;
use Debian::PkgJs::Banned;
use Debian::PkgJs::Cache;
use Debian::PkgJs::Dependencies;
use Debian::PkgJs::Version;
use Dpkg::IPC;
use File::Temp 'tempdir';
use Getopt::Long;
use JSON;
use LWP::UserAgent;
use Regexp::Assemble;

my $ra = Regexp::Assemble->new;
map { $ra->add($_) } ( keys %{$Debian::PkgJs::Banned::BANNED} ), qw(ws-iconv);
my $BANNED = '^' . $ra->as_string . '$';
$BANNED = qr/$BANNED/;
our $cache = Debian::PkgJs::Cache->new;

my $content;
my $ua = LWP::UserAgent->new( timeout => 10 );
$ua->env_proxy;
my $dir = tempdir();# CLEANUP => 1 );
my %opt;

# 1 - Get options
GetOptions(
    \%opt, qw(
      h|help
      v|version
      a|audit
    )
);

if ( $opt{h} ) {
    print <<EOF;
Usage: pkgjs-easy-to-update

Display package easy to update with new version.

Options:
 -h, --help: print this
 -a, --audit: launch a `npm audit` instead of searching packages to update

Note that --audit produces many false positive errors.
EOF
    exit;
}
elsif ( $opt{v} ) {
    print "$VERSION\n";
    exit;
}

print STDERR "Be patient, this process can take a long time.\n";
# 2 - Get list of package with a new version from UDD
{
    my $response = $ua->get('https://udd.debian.org/dmd/?email1=pkg-javascript-devel%40lists.alioth.debian.org&format=json');
    if ( $response->is_success ) {
        $content = JSON::from_json($response->decoded_content);
    }
    else {
        die "Unable to get list from udd.debian.org: ".$response->status_line;
    }
}
print STDERR "Got UDD list, building package.json\n";
chdir $dir;

# 3 - Parse result to build a temporary package.json

my $count    = 0;
my @toUpdate = map {
    my $name = $_->{':source'};
    $name =~ s/^node-//;
    my $version = fixVersion( $_->{':details'} );
    availableModules->{$name} && defined( $_->{':details'} )
      ? {
        source => $_->{':source'},
        name   => $name,
        new    => $version,
        old    => getVersion( $_->{':source'} ),
      }
      : ();
  }
  grep {
    #$count++ if $_->{':type'} eq "new upstream";
    my $source = $_->{':source'};
    $source =~ s/^(?:node|libjs)-//;
    $_->{':type'} eq "new upstream"
      and $source !~ $BANNED

      #and $count < 20
  } @$content;

my $fh;

# 4 - Build package-lock.json using npm
print STDERR "Got unstable versions, building package-lock.json\n";
while (1) {
    open $fh, '>', 'package.json' or die $!;
    print $fh '{"name":"foo","version":"0.1","dependencies":{';
    print $fh join( ',',
        map { qq("$_->{name}":"^$_->{old}") }
        grep { defined $_->{old} } @toUpdate );
    print $fh '}}';
    close $fh;

    my $stderr;
    spawn(
        exec =>
          [qw(npm i --package-lock-only --legacy-peer-deps --ignore-scripts)],
        wait_child      => 1,
        nocheck         => 1,
        error_to_string => \$stderr,
    );
    if ( $stderr =~ /No matching version found for (.*?)\@/s ) {
        my $drop = $1;
        print STDERR "Drop $drop, compatible version no more available\n";
        @toUpdate = grep { $_->{name} ne $drop } @toUpdate;
        next;
    }
    print STDERR $stderr if $stderr;
    last;
}

if ( $opt{a} ) {
    spawn(
        exec       => [ qw(npm audit --package-lock-only --ignore-scripts --json) ],
        wait_child => 1,
    );
    exit;
}

# 5 - Parse package-lock.json result and search for easy to update
{
    local $/ = undef;
    open $fh, 'package-lock.json' or die $!;
    $content = JSON::from_json(<$fh>);
    close $fh;
}

foreach my $pkg (@toUpdate) {
    my $newVersion =
      $content->{packages}->{"node_modules/$pkg->{name}"}->{version};
    my $oldVersion = fixVersion( $pkg->{old} );
    if ( $newVersion ne $oldVersion ) {
        print "$pkg->{source}: from $oldVersion to $newVersion\n";
    }
}

sub getVersion {
    my $src = shift;
    if ( my $version = $cache->get("version-$src") ) {
        return $version;
    }
    my $response = $ua->get(
        'https://api.ftp-master.debian.org/madison?text=on&s=sid&package='
          . $src );
    if ( $response->is_success ) {
        my @vals = split /\s*\|\s*/, $response->decoded_content;
        my $v    = fixVersion( $vals[1] );
        $cache->set( "version-$src", $v );
        return $v;
    }
    else {
        return undef;
    }
}

sub fixVersion {
    my $v = shift;
    $v =~ s/[^\d\.].*$//;
    $v =~ s/^(\d+\.\d+\.\d+).*$/$1/;
    return $v;
}