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
|
package Debian::PkgJs::Lib;
use strict;
use warnings;
use Dpkg::IPC;
use Dpkg::Version;
use LWP::UserAgent;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
&uscanResult &url_part &git_last_version &git_watch ®istry_watch
);
our $VERSION = '0.8.14';
sub uscanResult {
unless ( -r 'debian/watch' ) {
print STDERR "No debian/watch found\n";
sleep 1;
return ( undef, undef );
}
my ( $err, $out );
spawn(
exec => [qw(uscan --report --dehs)],
to_string => \$out,
error_to_string => \$err,
wait_child => 1,
no_check => 1,
# XXX: Backwards compatibility, remove after dpkg 1.24.0.
nocheck => 1,
timeout => 30,
);
my %data;
foreach (qw(upstream-url upstream-version)) {
if ( $out =~ m#<$_>(.+?)</$_># ) {
$data{$_} = $1;
}
else {
print STDERR "Uscan failed\n";
return;
}
}
$data{'upstream-version'} =~ s/\+.*$// if $data{'upstream-version'};
return ( $data{'upstream-version'}, $data{'upstream-url'} );
}
sub url_part {
my ($url) = @_;
return unless ($url);
my $ourl = $url;
return unless $url;
$url =~ s#git\@(.*?):#https://$1/#;
$url =~ s#^git.*\@#https://#;
$url =~ s#.*git.*://#https://#;
$url =~ s/\.git$//;
$url =~ s/[\W]+$//;
if ( $url =~ s#^(?:git\+)?(https?://)## ) {
my $prot = $1;
if ( $url =~ m#^([^/]+/[^/]+/[^/]+)# ) {
return $prot . $1;
}
}
die "Unable to parse $ourl";
}
sub git_last_version {
my ($repo) = @_;
return unless $repo;
my $ua = LWP::UserAgent->new( timeout => 10 );
$ua->env_proxy;
my $response = $ua->get("$repo/tags");
if ( $response->is_success ) {
if ( $repo =~ /github/ ) {
my @versions =
sort {
Dpkg::Version->new( "$a-0", check => 0 )
cmp Dpkg::Version->new( "$b-0", check => 0 )
} ( $response->content =~ m#.*/archive.*/v?([\d\.]+).tar.gz#g );
return @versions ? pop(@versions) : 0;
}
}
else {
return 0;
}
}
sub git_watch {
my ( $repo, $component, $type, $version, $after, $noctype, $dwatchVersion )
= @_;
$version = $version ? "v?($version(?:\.[\\d\\.]+)?)" : "v?([\\d\\.]+)";
if ( $dwatchVersion && $dwatchVersion > 4 ) {
$component = $component ? "Component: $component\n" : '';
$type = $type ? "Version-Schema: $type\n" : '';
$component .= "Ctype: nodejs\n" unless ($noctype);
}
else {
$component = $component ? "component=$component,\\\n" : '';
$type = $type ? " $type" : '';
$component .= "ctype=nodejs,\\\n" unless ($noctype);
}
my $name = $repo;
$name =~ s#.*/([^/]+)/?#$1#;
$name ||= "to-be-fixed";
if ( $repo =~ m#github.com/([^/]+)/([^/]+)# ) {
my ( $own, $pj ) = ( $1, $2 );
$after = $after ? "?after=$after" : '';
unless ( $dwatchVersion and $dwatchVersion > 4 ) {
return <<"EOF";
opts=\\
${component}dversionmangle=auto,\\
filenamemangle=s/.*?(\\d[\\d\\.-]*\@ARCHIVE_EXT\@)/node-$name-\$1/ \\
$repo/tags$after .*/archive.*/$version.tar.gz$type
EOF
}
return <<"EOF";
${component}Template: Github
Owner: $own
Project: $pj
EOF
}
else {
die "Unsupported repo $repo";
}
}
sub registry_watch {
my ( $pname, $component, $type, $version, $noctype, $dwatchVersion ) = @_;
my $localName = $component ? "node-$component" : '@PACKAGE@';
$version = $version ? "($version(?:\.[\\d\\.]+)?)" : "([\\d\\.]+)";
if ( $dwatchVersion && $dwatchVersion > 4 ) {
$component = $component ? "Component: $component\n" : '';
$type = $type ? "Version-Schema: $type\n" : '';
$component .= "Ctype: nodejs\n" unless ($noctype);
}
else {
$component = $component ? ",component=$component" : '';
$type = $type ? " $type" : '';
$component .= ",ctype=nodejs" unless ($noctype);
}
my $fname = $pname;
$fname =~ s#.*/##;
unless ( $dwatchVersion > 4 ) {
return <<"EOF";
# It is not recommended use npmregistry. Please investigate more.
# Take a look at https://wiki.debian.org/debian/watch/
opts="searchmode=plain$component,pgpmode=none,filenamemangle=s/^.*?(\\d[\\d\\.-]*\@ARCHIVE_EXT\@)/$localName-\$1/" \\
https://registry.npmjs.org/$pname https://registry.npmjs.org/$pname/-/$fname-$version\@ARCHIVE_EXT\@$type
EOF
}
return <<"EOF";
# It is not recommended use npmregistry. Please investigate more.
# Take a look at https://wiki.debian.org/debian/watch/
${component}${type}Search-Mode: plain
Pgp-Mode: node
Filename-Mangle: s/^.*?(\\d[\\d\\.-]*\@ARCHIVE_EXT\@)/$localName-\$1/
Source: https://registry.npmjs.org/$pname
Matching-Pattern: https://registry.npmjs.org/$pname/-/$fname-$version\@ARCHIVE_EXT\@$type
EOF
}
1;
|