File: jh_linkjars

package info (click to toggle)
javatools 0.81
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: perl: 1,358; sh: 939; java: 56; makefile: 39
file content (190 lines) | stat: -rwxr-xr-x 5,041 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl

=head1 NAME

jh_linkjars - populate folders with symlinks to jar files

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<jh_linkjars> [S<I<debhelper options>>]

B<jh_linkjars> [S<I<debhelper options>>] I<directory>

=head1 DESCRIPTION

If upstream ship convenience copies of third-party jar files which
have been removed (see L<jh_repack(1)>), but the build system refers
to that directory, B<jh_linkjars> can be used to populate the directory
with symlinks to the packaged jars in /usr/share/java.

It is called either with a directory on the command line or by
specifying one target directory per line in the file debian/linkjars.

B<jh_linkjars> will scan all of the (installed) build-dependencies and
create a symlink to every jar which is installed by those packages in
the target directory.

B<jh_linkjars> can be called with -u to remove all the symlinks in the
clean target.  This is done automatically by L<jh_clean(1)>.

=head1 FILES

=over 4

=item F<debian/linkjars>

List of directories to populate; one directory per line.

=back

=head1 OPTIONS

=over 4

=item B<-t>, B<--transitive>

Transitively link jar files (i.e. also include indirect dependencies in the target directory).

=item B<-u>, B<--unlink>

Remove all files/links in the target directories that B<jh_linkjars> created
(or would have created).

=back

Beyond the above, B<jh_linkjars> also accepts the shared
debhelper options documented in L<debhelper(7)>.

=cut

my ($UNLINK_JARS, $TRANSITIVE, @TARGET_DIRS, @JARS);

init(options => {
    'transitive|t' => \$TRANSITIVE,
    'unlink|u'     => \$UNLINK_JARS,
});

sub parse_deps_fields {
    my ($field) = @_;
    my @packages;
    $field =~ s/^\s*+,?//;
    $field =~ s/,?\s*+$//;
    $field =~ s/\r?\n/,/g;
    $field =~ s/,(?:\s*,)++\s*+/,/g;
    for my $clause (split(m/\s*+[,|]\s*+/, $field)) {
        next if $clause =~ m/^\s*$/;
        # Drop everything after [, ( or <.
        $clause =~ s/\s*+[[<(].*$//;
        push(@packages, $clause);
    }
    return @packages;
}

sub parse_dpkg_L {
    my ($output) = @_;
    my @lines;
    for my $line (split(qr/\n/, $output)) {
        $line =~ s/\s++$//;
        if ($line =~ m{^/usr/share/java/.+\.jar$}) {
            push(@lines, $line);
        }
    }
    return @lines;
}

sub find_jars {
    my $source = sourcepackage();
    my $bd = `grep-dctrl --no-field-names --show-field Build-Depends,Build-Depends-Indep -F source "${source}" debian/control`;
    error_exitcode("grep-dctrl --no-field-names --show-field Build-Depends,Build-Depends-Indep -F source \"${source}\" debian/control")
        if $?;
    my @packages = parse_deps_fields($bd);
    my %seen = map { $_ => 1 } @packages;
    my @all_jars;
    while (@packages) {
        my $pkg = pop(@packages);
        # Skip debhelper-compat because it is a virtual package #933715
        next if $pkg eq 'debhelper-compat';
        my $dpkg_output = `dpkg -L "$pkg"`;
        error_exitcode("dpkg -L \"$pkg\"") if $?;
        my @jars = parse_dpkg_L($dpkg_output);
        push(@all_jars, @jars);
        if (@jars and $TRANSITIVE) {
            my $raw_deps = `dpkg -s "$pkg" | sed -n '/^Depends:/s/.*: //p'`;
            error_exitcode("dpkg -s \"$pkg\" | sed -n '/^Depends:/s/.*: //p'") if $?;
            my @deps = grep { not exists($seen{$_}) } parse_deps_fields($raw_deps);
            $seen{$_} = 1 for @deps;
            push(@packages, @deps);
        }
    }
    return @all_jars;
}

#function findjars()
#{
#	pkg="$1"
#	if [ -z "$pkg" ]; then
#		pkg="$(sed -n '/^Source:/s/.*: //p' < debian/control)"
#		BDS=$(grep-dctrl --no-field-names --show-field Build-Depends,Build-Depends-Indep -F source "$pkg" debian/control | tr , ' ' | sed 's/([^)]*)//g')
#	else
#		BDS=$(dpkg -s "$pkg" | sed -n 's/([^)]*)//g;s/,/ /g;/^Depends:/s/.*: //p')
#	fi
#
#	JARS=""
#	for d in $BDS; do
#		j="$(dpkg -L $d | grep "^/usr/share/java/.*\.jar$")"
#		k=""
#		if [ -n "$j" ] &&  [ `getarg t transitive` ]; then
#			k=$(findjars "$d")
#		fi
#		JARS="$JARS $j $k"
#	done
#	echo $JARS
#}

if (@ARGV) {
    @TARGET_DIRS = $ARGV[0];
} elsif ( -f 'debian/linkjars') {
    @TARGET_DIRS = filearray('debian/linkjars');
}

# Stop here if there is nothing to do.
exit(0) if not @TARGET_DIRS;

@JARS = find_jars();

if ($UNLINK_JARS) {
    my @basenames = map { basename($_) } @JARS;
    for my $target_dir (@TARGET_DIRS) {
        rm_files(map {"${target_dir}/$_"} @basenames);
    }
} else {
    install_dir(@TARGET_DIRS);
    for my $target_dir (@TARGET_DIRS) {
        for my $jar (@JARS) {
            my $basename = basename($jar);
            verbose_print("Adding link for $jar to ${target_dir}");
            rm_files("${target_dir}/${basename}");
            make_symlink_raw_target($jar, "${target_dir}/${basename}");
        }
    }
}


=head1 SEE ALSO

L<debhelper(7)>

This program is a part of javahelper and uses debhelper as backend. There are
also tutorials in /usr/share/doc/javahelper.

=head1 AUTHOR

Niels Thykier <niels@thykier.net>

=cut