forked from tlmquintino/cookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookup.pl
executable file
·354 lines (281 loc) · 10.9 KB
/
cookup.pl
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env perl
# (C) Copyright 2011-2012 Tiago Quintino
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
use warnings;
use strict;
#==============================================================================
# Modules
#==============================================================================
use Cwd;
use Getopt::Long;
use File::Find;
use File::Basename;
use File::Path;
use Data::Dumper;
no warnings 'File::Find'; # dont issue warnings for 'weird' files
#==============================================================================
# main variables
#==============================================================================
my $default_cookbook = Cwd::abs_path(dirname(__FILE__) . "/" . "cookbook") ;
my $default_sandbox = $ENV{HOME}."/tmp";
my $default_prefix = $ENV{HOME}."/local";
my $default_larder_path = "cookup/Larder";
my %options = ( prefix => $default_prefix,
sandbox => $default_sandbox,
cookbook => $default_cookbook,
'larder-path' => $default_larder_path, );
my %recipes = ();
my @package_list = ();
#==============================================================================
# main functions
#==============================================================================
sub parse_commandline() # Parse command line
{
GetOptions ( \%options,
'download',
'unpack',
'cook',
'link',
'unlink',
'help',
'verbose',
'debug=s',
'nodeps',
'nolink',
'nolarder',
'larder-path=s',
'skip-checksum',
'list',
'dry-run',
'prefix=s',
'cookbook=s',
'sandbox=s',
'packages=s@',
);
# show help if required
if( exists $options{help} )
{
print <<ZZZ;
cookup.pl : easy build and install for UNIX platforms
usage: cookup.pl <action> [options]
actions:
--download download the package sources
--unpack downloads and unpacks the package sources
--cook cookup the packages following the recipe
--unlink unlink the packages
--link link the packages
options:
--help shows this help
--verbose print every comand before executing
--debug=level sets the debug level (debug=2 shows command outputs)
--nodeps don't check dependencies
--nolarder don't install in larder mode
--larder-path partial path to the larder [$default_larder_path]
--nolink don't link in larder mode
--list list all the recipes in the cookbook
--dry-run don't actually do it, just list the packages that would be cooked
--prefix install dir prefix [$default_prefix]
--cookbook use directory as cookbook [$default_cookbook]
--sandbox use directory as sandbox for building [$default_sandbox]
--packages=list comma separated list of packages to apply actions on
--skip-checksum don't check if checksum matches after download
EXAMPLE:
cookup.pl --cook --packages=wget
ZZZ
exit(0);
}
if( exists $options{packages} ) # process comma separated list
{
my @packages = split(',',join(",",@{$options{packages}}));
# print "@packages\n";
$options{packages} = \@packages;
}
my $prefix = $options{'prefix'};
my $sandbox = $options{'sandbox'};
my $cookbook = $options{'cookbook'};
if( not $options{'dry-run'} )
{
mkpath $prefix unless ( -w $prefix ); # create prefix dir if does not exist
mkpath $sandbox unless ( -w $sandbox ); # create sandbox dir if does not exist
}
# resolve relative paths to absolute paths
die "cannot write to directory '".$prefix."'\n" unless ( -w $prefix && -d $prefix );
$prefix = Cwd::abs_path( $prefix );
die "cannot write to directory '".$sandbox."'\n" unless ( -w $sandbox && -d $sandbox );
$sandbox = Cwd::abs_path( $sandbox );
die "cannot read from directory '".$cookbook."'\n" unless ( -d $cookbook && -r $cookbook );
$cookbook = Cwd::abs_path( $cookbook);
}
#==============================================================================
sub prepare()
{
# prepend paths with installation prefix
# but avoid warnings of uninitialized variables
my $path = $ENV{PATH}; $path = "" unless ($path);
my $libpath = $ENV{LIBPATH}; $libpath = "" unless ($libpath);
my $ldpath = $ENV{LD_LIBRARY_PATH}; $ldpath = "" unless ($ldpath);
my $dypath = $ENV{DYLD_LIBRARY_PATH}; $dypath = "" unless ($dypath);
my $prefix = $options{'prefix'};
$ENV{PATH} = $prefix."/bin:".$path;
$ENV{LIBPATH} = $prefix."/lib:".$prefix."/lib64:".$libpath;
$ENV{LD_LIBRARY_PATH} = $prefix."/lib:".$prefix."/lib64:".$ldpath;
$ENV{DYLD_LIBRARY_PATH} = $prefix."/lib:".$prefix."/lib64:".$dypath;
}
#==============================================================================
sub found_recipe
{
my ($fname,$path,$suffix) = fileparse($_, qr/\.[^.]*/);
#print "path [$path] name [$fname] suffix [$suffix]\n";
if( $suffix eq ".pm")
{
require "$fname$suffix";
my $recipe = $fname->new();
my $name = $recipe->name();
my $version = $recipe->version();
my $namevrs = "$name-$version";
$recipes{$name} = $recipe if( $fname eq $name ); # add if is the master version
# FTM: turned off version
# $recipes{$namevrs} = $recipe;
print "> found recipe for " . $recipe->name . "-" . $version . "\n" if( $options{debug} );
}
}
#==============================================================================
sub find_recipes
{
my @cookbook;
my $cookbook_path = Cwd::abs_path($options{cookbook});
push (@cookbook, $cookbook_path);
print "searching for recipes in $cookbook_path\n" if( $options{verbose} );
find( \&found_recipe, @cookbook );
}
#==============================================================================
sub list_available_recipes
{
foreach my $package ( sort keys %recipes )
{
my $recipe = $recipes{$package};
my $package_name = $recipe->package_name;
if( exists $options{verbose} )
{
print "$package_name\n";
}
else
{
print "$package_name ( ".$recipe->url()." )\n";
}
}
}
#==============================================================================
sub process_one_package
{
my $package = shift;
my $recipe = $recipes{$package};
my $package_name = $recipe->package_name;
print "package [$package_name]\n";
# setup variables
$recipe->verbose( $options{verbose} ) unless ( !exists $options{verbose} );
$recipe->debug( $options{debug} ) unless ( !exists $options{debug} );
$recipe->sandbox( $options{sandbox} );
$recipe->skip_checksum( $options{'skip-checksum'} );
$recipe->larder( 0 ) if ( exists $options{'nolarder'} );
if( exists $options{nolarder} )
{
$recipe->prefix ( $options{prefix} );
}
else
{
$recipe->prefix_base ( $options{prefix} );
$recipe->prefix_extra( $options{'larder-path'} . '/' . $recipe->name() . '/' . $recipe->version() );
$recipe->prefix ( $recipe->prefix_base() . '/' . $recipe->prefix_extra() );
}
$recipe->unlink_larder() if( exists $options{unlink} && !exists $options{'dry-run'} );
print "> installing to [" . $recipe->prefix() . "]\n" if( $options{verbose} );
my $built = 0;
if( -d $recipe->prefix() && !exists $options{nolarder} )
{
print "> " . $recipe->name() . " already installed in [" . $recipe->prefix() . "]\n" if( $options{verbose} );
$built = 1;
}
my $cook = 0; $cook |= exists $options{'cook'} && !$built;
my $unpack = 0; $unpack |= exists $options{'unpack'} || $cook;
my $download = 0; $download |= exists $options{'download'} || $unpack;
my $check = 0; $check |= !exists $options{'skip-checksum'} && $download;
my $link = 0; $link |= exists $options{'link'} || ( $cook && !exists $options{nolarder} && !exists $options{nolink} );
# print "built " . $built . "\n";
# print "cook " . $cook . "\n";
# print "unpack " . $unpack . "\n";
# print "download " . $download . "\n";
# print "check " . $check . "\n";
# print "link " . $link . "\n";
if( not exists $options{'dry-run'} )
{
$recipe->download_src() if ( $download );
$recipe->check_src() if ( $check );
$recipe->unpack_src() if ( $unpack );
$recipe->cook() if ( $cook );
$recipe->link_larder() if ( $link );
}
}
#==============================================================================
sub transverse_dependency_tree
{
my (@list) = @_;
# print Dumper( @list ) if( $options{debug} );
my $cookbook = $options{cookbook};
foreach my $package ( @list )
{
if( exists( $recipes{$package} ) )
{
my $recipe = $recipes{$package};
my $package_name = $recipe->package_name;
my @depends = $recipe->depends();
transverse_dependency_tree( @depends ) if( scalar @depends != 0 );
if ( !( grep( /^$package$/, @package_list ) ) ) # dont add if already in list
{
push @package_list, $package;
}
}
else
{
die "no recipe for '$package' in our cookbook [$cookbook]" ;
}
}
}
#==============================================================================
sub process_packages_list
{
my $cookbook = $options{cookbook};
# verify all packages exist
foreach my $package ( @{$options{packages}} )
{
if( ! exists($recipes{$package}) )
{
die "no recipe for '$package' in our cookbook [$cookbook]" ;
}
}
if( ! exists($options{nodeps}) )
{
transverse_dependency_tree( @{$options{packages}} );
}
else # just copy whatever was passed to the package_list
{
@package_list = @{$options{packages}};
}
# verify all packages exist
foreach my $package ( @package_list )
{
process_one_package( $package );
}
}
#==============================================================================
# Main execution
#==============================================================================
parse_commandline();
prepare();
push @INC, Cwd::abs_path(dirname(__FILE__)); # Recipe class is in same dir as the script
push @INC, $options{cookbook};
find_recipes();
list_available_recipes() unless (!exists $options{list});
process_packages_list() unless (!exists $options{packages});