summaryrefslogtreecommitdiffstats
path: root/atoms_cc/buildtoolbox.pl
blob: 82355974e390f7f38f76403ddc6b0cdc4d3cb596 (plain)
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
#!/usr/bin/perl -w

# buildtoolbox.pl
# Usage: buildtoolbox.pl toolbox-archive [toolbox-name]

use strict;
use Cwd;

my ($TOOLBOXFILE, # Toolbox archive to compile
    $TOOLBOXNAME); # Name of the toolbox

# is_zip:
#    Return true if toolbox file extension is zip
sub is_zip {
	return $TOOLBOXFILE =~ /\.zip$/;
}

# get_tree_from_tgz:
#   Get all files (names) of the compressed (in tar.gz) sources
sub get_tree_from_tgz {
	my (@files);
	
	open my $fd, "tar -tzf ${TOOLBOXFILE}|";
	push(@files, $_) while <$fd>;
	chomp(@files);
	close $fd;
	return @files;
}

# get_tree_from_zip:
#   Get all files (names) of the compressed (in zip) sources
sub get_tree_from_zip {
	my (@files, $line);
	
	# tail & head are here to skip header & footer
	open my $fd, "unzip -l ${TOOLBOXFILE} | tail -n +4 | head -n -2 |";
	
	while(<$fd>) {
		# output format: size date time filename
		/\s*\d+\s+\d+-\d+-\d+\s+\d+:\d+\s+(.+)/ or die "Bad output of unzip";
		push(@files, $1);
	}
	
	chomp(@files);
	close $fd;
	return @files;
}

# get_tree:
#   Get all files (names) of the compressed sources
sub get_tree {
	if(is_zip()) {
		return get_tree_from_zip();
	}
	else {
		return get_tree_from_tgz();
	}
}

# get_description_from_tgz:
#    Extract DESCRIPTION file from the archive (in tar.gz format)
sub get_description_from_tgz {
	open my $fd, "tar -xOzf ${TOOLBOXFILE} ${TOOLBOXNAME}/DESCRIPTION |";
	return $fd;
}

# get_description_from_tgz:
#    Extract DESCRIPTION file from the archive (in zip format)
sub get_description_from_zip {
	open my $fd, "unzip -c ${TOOLBOXFILE} ${TOOLBOXNAME}/DESCRIPTION | tail -n +3 | head -n -1 |";
	return $fd;
}

# get_description:
#   Extract DESCRIPTION file from the archive
sub get_description {
	if(is_zip) {
		return get_description_from_zip();
	}
	else {
		return get_description_from_tgz();
	}
}

# read_description:
#   Check if DESCRIPTION file is correct, and parse it (return a hash
#   field => value).
#   First argument is a file descriptor for the DESCRIPTION file (see
#   get_description)
sub read_description {
	my ($fd) = shift;
	my @required = qw(Toolbox Version Title Author Maintainer
	                  Description License Category);
	my @optional = qw(Date Depends URL Entity);
	my (%infos, $key, $val);
	my (%lines, %correct);
	
	# Populate hash
	while(<$fd>) {
		die "\":\" not followed by a space at line $." if(/:(?! )/);
		if(/:/) { # New field
			($key, $val) = split(/: /, $_, 2);
			$infos{$key} = $val;
			$lines{$key} = $.;
			$correct{$key} = 0;
		}
		else { # Continuation of previous field
			$infos{$key} .= $_;
		}
	}
	
	# Check presence of required fields, mark them as correct
	foreach (@required) {
		if(!defined($infos{$_})) {
			die "Mandatory field \"$_\" not defined";
		}
		else {
			$correct{$_} = 1;
		}
	}
	
	# Mark optional fields as correct
	foreach (@optional) {
		if(defined($infos{$_})) {
			$correct{$_} = 1;
		}
	}
	
	# Check that there's no incorrect (= unknown) fields
	foreach (keys(%infos)) {
		if($correct{$_} == 0) {
			die "Unknown field \"$_\" (defined at line $lines{$_})";
		}
	}
	
	chomp %infos;
	return %infos;
}



# check_tree:
#   Given a source tree of a toolbox (see get_tree), check if it is correct
#   (required files are present, files are at their right place, and so on...)
sub check_tree {
	#~ my @tree = shift;
	#~ my %treehash;
	
	#~ print "$#tree\n";
	foreach (@_) { print "=> $_ \n"; };
	
	#~ foreach (@tree) {
		#~ # Make a hash from the tree (paths are keys, values are all to 1)
		#~ $treehash{$_} = 1;
		
		#~ # Check that all files are under a root which has the same name as the toolbox
		#~ if(!m#^\Q$TOOLBOXNAME\E/#) {
			#~ die "Incorrect archive: \"$_\" is not a child of \"$TOOLBOXNAME\"";
		#~ }
	#~ }
	
	#~ # Check that basic files are here
	#~ my @required = qw(DESCRIPTION DESCRIPTION-FUNCTIONS readme.txt license.txt
	                  #~ builder.sce loader.sce);
	#~ push(@required, "etc/$TOOLBOXNAME.start");
	#~ push(@required, "etc/$TOOLBOXNAME.end");
	
	#~ foreach (@required) {
		#~ print "$TOOLBOXNAME/$_\n";
		#~ print %treehash;
		#~ if(!defined($treehash{"$TOOLBOXNAME/$_"})) {
			#~ die "Incorrect archive: required file \"$_\" not present";
		#~ }
	#~ }
}

# Init global vars, check arguments
$TOOLBOXFILE = shift;
if(!defined($TOOLBOXFILE)) {
	die "Toolbox source file required";
}

if(! -r $TOOLBOXFILE) {
	die "$TOOLBOXFILE doesn't exists or can't be read";
}

$TOOLBOXNAME = shift || $1 if ($TOOLBOXFILE =~ /^([^.]+)/);




my @tree = get_tree();

check_tree(@tree, 1);