1 | #!/usr/bin/env perl |
---|
2 | # |
---|
3 | # 2018/01/17 Gabriel Moreau |
---|
4 | # |
---|
5 | # apt-get install yamllint libyaml-syck-perl libtemplate-perl |
---|
6 | |
---|
7 | use strict; |
---|
8 | use warnings; |
---|
9 | |
---|
10 | use File::Copy qw{copy}; |
---|
11 | use YAML::Syck; |
---|
12 | use Getopt::Long(); |
---|
13 | use Cwd(); |
---|
14 | use Template; |
---|
15 | |
---|
16 | |
---|
17 | my ($verbose); |
---|
18 | Getopt::Long::GetOptions( |
---|
19 | 'verbose' => \$verbose, |
---|
20 | ); |
---|
21 | |
---|
22 | |
---|
23 | my %CMD_DB = ( |
---|
24 | 'help' => \&cmd_help, |
---|
25 | 'version' => \&cmd_version, |
---|
26 | 'check' => \&cmd_check, |
---|
27 | 'make-link' => \&cmd_make_link, |
---|
28 | 'make-author' => \&cmd_make_author, |
---|
29 | 'make-licence' => \&cmd_make_licence, |
---|
30 | 'make-copyright' => \&cmd_make_copyright, |
---|
31 | 'list-licence' => \&cmd_list_licence, |
---|
32 | ); |
---|
33 | |
---|
34 | ################################################################ |
---|
35 | # main program |
---|
36 | ################################################################ |
---|
37 | |
---|
38 | my $cmd = shift @ARGV || 'help'; |
---|
39 | if (defined $CMD_DB{$cmd}) { |
---|
40 | $CMD_DB{$cmd}->(@ARGV); |
---|
41 | } |
---|
42 | else { |
---|
43 | print {*STDERR} "project-meta: command $cmd not found\n\n"; |
---|
44 | $CMD_DB{'help'}->(); |
---|
45 | exit 1; |
---|
46 | } |
---|
47 | |
---|
48 | exit; |
---|
49 | |
---|
50 | ################################################################ |
---|
51 | # subroutine |
---|
52 | ################################################################ |
---|
53 | |
---|
54 | ################################################################ |
---|
55 | # command |
---|
56 | ################################################################ |
---|
57 | |
---|
58 | sub cmd_help { |
---|
59 | print <<'END'; |
---|
60 | project-meta - opendata project metafile manager |
---|
61 | |
---|
62 | project-meta check |
---|
63 | project-meta make-link |
---|
64 | project-meta make-author |
---|
65 | project-meta make-licence |
---|
66 | project-meta make-copyright |
---|
67 | project-meta make-licence |
---|
68 | project-meta list-licence |
---|
69 | END |
---|
70 | } |
---|
71 | |
---|
72 | ################################################################ |
---|
73 | |
---|
74 | sub cmd_version { |
---|
75 | print "0.01\n"; |
---|
76 | } |
---|
77 | |
---|
78 | ################################################################ |
---|
79 | |
---|
80 | sub print_ok { |
---|
81 | my ($key, $test) = @_; |
---|
82 | |
---|
83 | printf "%-35s : %s\n", $key, $test ? 'yes' : 'no'; |
---|
84 | } |
---|
85 | |
---|
86 | ################################################################ |
---|
87 | |
---|
88 | sub cmd_check { |
---|
89 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
90 | |
---|
91 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
92 | my $current_dir = Cwd::getcwd(); |
---|
93 | my $dap_folder = $meta->{'public-dap'}{'dap-folder'}; |
---|
94 | |
---|
95 | print_ok 'project/acronym', $acronym =~ m{\d\d\w[\w\d_]+}; |
---|
96 | print_ok 'public-dap/dap-folder', $dap_folder ne '' and $dap_folder =~ m{^/}; |
---|
97 | print_ok 'dap-folder not match current_dir', $dap_folder !~ m{$current_dir}; |
---|
98 | |
---|
99 | #print YAML::Syck::Dump($meta); |
---|
100 | } |
---|
101 | |
---|
102 | ################################################################ |
---|
103 | |
---|
104 | sub addfolder2list { |
---|
105 | my ($folderdb, $folder) = @_; |
---|
106 | |
---|
107 | $folder =~ s{/[^/]+$}{}; |
---|
108 | |
---|
109 | return if $folder !~ m{/}; |
---|
110 | |
---|
111 | $folderdb->{$folder}++; |
---|
112 | return addfolder2list($folderdb, $folder); |
---|
113 | } |
---|
114 | |
---|
115 | ################################################################ |
---|
116 | |
---|
117 | sub cmd_make_link { |
---|
118 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
119 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
120 | my $current_dir = Cwd::getcwd(); |
---|
121 | my $dap_folder = $meta->{'public-dap'}{'dap-folder'}; |
---|
122 | |
---|
123 | # Create a list of the folder |
---|
124 | my %folders; |
---|
125 | for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) { |
---|
126 | addfolder2list(\%folders, $dataset); |
---|
127 | } |
---|
128 | |
---|
129 | print "chmod o+rX,o-w $current_dir\n"; |
---|
130 | print "mkdir -p $dap_folder/$acronym\n" if not -d "$dap_folder/$acronym"; |
---|
131 | for my $folder (sort keys %folders) { |
---|
132 | print "chmod o+rX,o-w $current_dir/$folder\n"; |
---|
133 | print "mkdir -p $dap_folder/$acronym/$folder\n" if -d "$current_dir/$folder"; |
---|
134 | } |
---|
135 | |
---|
136 | for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) { |
---|
137 | if ($dataset =~ m{/}) { |
---|
138 | # Folder case |
---|
139 | my $folder = $dataset =~ s{/[^/]+$}{}r; |
---|
140 | print "ln --symbolic --target-directory $dap_folder/$acronym/$folder/ $current_dir/$dataset\n"; |
---|
141 | } |
---|
142 | else { |
---|
143 | # File case |
---|
144 | print "ln --symbolic --target-directory $dap_folder/$acronym/ $current_dir/$dataset\n"; |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | print "chmod -R o+rX,o-w $dap_folder/$acronym/\n"; |
---|
149 | } |
---|
150 | |
---|
151 | ################################################################ |
---|
152 | |
---|
153 | sub cmd_make_author { |
---|
154 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
155 | |
---|
156 | my $current_dir = Cwd::getcwd(); |
---|
157 | |
---|
158 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
159 | my $authors_list = $meta->{'project'}{'authors'}; |
---|
160 | |
---|
161 | if (-f "$current_dir/AUTHORS.txt") { |
---|
162 | # Test for manual or automatically generated file |
---|
163 | # Automatically generated file by project-meta |
---|
164 | my $automatic; |
---|
165 | open my $fh, '<', "$current_dir/AUTHORS.txt" or die $!; |
---|
166 | for my $line (<$fh>) { |
---|
167 | $line =~ m/Automatically generated .* project-meta/i and $automatic++; |
---|
168 | } |
---|
169 | close $fh; |
---|
170 | |
---|
171 | if (not $automatic) { |
---|
172 | print "Warning: AUTHORS.txt already exists\n"; |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | print "Warning: update AUTHORS.txt\n"; |
---|
177 | } |
---|
178 | |
---|
179 | my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d'); |
---|
180 | my $msg_format = ''; |
---|
181 | $tt->process('AUTHORS.tt', |
---|
182 | { |
---|
183 | acronym => $acronym, |
---|
184 | authorlist => $authors_list, |
---|
185 | }, \$msg_format) || die $tt->error; |
---|
186 | |
---|
187 | open my $fh, '>', "$current_dir/AUTHORS.txt" or die $!; |
---|
188 | print $fh "$msg_format\n\n"; |
---|
189 | close $fh; |
---|
190 | } |
---|
191 | |
---|
192 | ################################################################ |
---|
193 | |
---|
194 | sub cmd_make_licence { |
---|
195 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
196 | |
---|
197 | my $current_dir = Cwd::getcwd(); |
---|
198 | |
---|
199 | if (-f "$current_dir/LICENCE.txt") { |
---|
200 | print "Warning: LICENCE.txt already exists\n"; |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | my $licence = $meta->{'public-dap'}{'data-licence'}; |
---|
205 | |
---|
206 | if (not -f "/usr/share/project-meta/licence.d/$licence.txt") { |
---|
207 | print "Error: licence $licence doesn't exists in project-meta database\n"; |
---|
208 | exit 1; |
---|
209 | } |
---|
210 | |
---|
211 | copy("/usr/share/project-meta/licence.d/$licence.txt", "$current_dir/LICENCE.txt") |
---|
212 | or die "Error: licence copy failed - $!"; |
---|
213 | |
---|
214 | print "Info: LICENCE.txt file create\n"; |
---|
215 | return; |
---|
216 | } |
---|
217 | |
---|
218 | ################################################################ |
---|
219 | |
---|
220 | sub cmd_make_copyright { |
---|
221 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
222 | |
---|
223 | my $current_dir = Cwd::getcwd(); |
---|
224 | |
---|
225 | if (-f "$current_dir/COPYRIGHT.txt") { |
---|
226 | # Test for manual or automatically generated file |
---|
227 | # Automatically generated file by project-meta |
---|
228 | my $automatic; |
---|
229 | open my $fh, '<', "$current_dir/COPYRIGHT.txt" or die $!; |
---|
230 | for my $line (<$fh>) { |
---|
231 | $line =~ m/Automatically generated .* project-meta/i and $automatic++; |
---|
232 | } |
---|
233 | close $fh; |
---|
234 | |
---|
235 | if (not $automatic) { |
---|
236 | print "Warning: COPYRIGHT.txt already exists\n"; |
---|
237 | return; |
---|
238 | } |
---|
239 | |
---|
240 | print "Warning: update COPYRIGHT.txt\n"; |
---|
241 | } |
---|
242 | |
---|
243 | my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d'); |
---|
244 | my $msg_format = ''; |
---|
245 | $tt->process('COPYRIGHT.tt', |
---|
246 | { |
---|
247 | title => $meta->{'project'}{'title'}, |
---|
248 | acronym => $meta->{'project'}{'acronym'}, |
---|
249 | authorlist => $meta->{'project'}{'authors'}, |
---|
250 | description => $meta->{'project'}{'short-description'}, |
---|
251 | licence => $meta->{'public-dap'}{'data-licence'}, |
---|
252 | doi => $meta->{'publication'}{'doi'}, |
---|
253 | }, \$msg_format) || die $tt->error; |
---|
254 | |
---|
255 | open my $fh, '>', "$current_dir/COPYRIGHT.txt" or die $!; |
---|
256 | print $fh "$msg_format\n\n"; |
---|
257 | close $fh; |
---|
258 | } |
---|
259 | |
---|
260 | ################################################################ |
---|
261 | |
---|
262 | sub cmd_list_licence { |
---|
263 | opendir my $dh, '/usr/share/project-meta/licence.d/' or die $!; |
---|
264 | for my $licence (readdir $dh) { |
---|
265 | # Keep only file |
---|
266 | next if not -f "/usr/share/project-meta/licence.d/$licence"; |
---|
267 | |
---|
268 | # Keep only .txt file |
---|
269 | next if not $licence =~ m/\.txt$/; |
---|
270 | |
---|
271 | $licence =~ s/\.txt$//; |
---|
272 | print "$licence\n"; |
---|
273 | } |
---|
274 | closedir $dh; |
---|
275 | } |
---|
276 | |
---|
277 | ################################################################ |
---|
278 | # documentation |
---|
279 | ################################################################ |
---|
280 | |
---|
281 | __END__ |
---|
282 | |
---|
283 | =head1 NAME |
---|
284 | |
---|
285 | project-meta - opendata project metafile manager |
---|
286 | |
---|
287 | |
---|
288 | =head1 USAGE |
---|
289 | |
---|
290 | project-meta check |
---|
291 | project-meta make-link |
---|
292 | project-meta make-author |
---|
293 | project-meta make-licence |
---|
294 | project-meta make-copyright |
---|
295 | project-meta make-licence |
---|
296 | project-meta list-licence |
---|
297 | |
---|
298 | =head1 DESCRIPTION |
---|
299 | |
---|
300 | project-meta is a small tool to maintain a set of open data files. |
---|
301 | |
---|
302 | =head1 COMMANDS |
---|
303 | |
---|
304 | Some command are defined in the source code but are not documented here. |
---|
305 | Theses could be not well defined, not finished, not well tested... |
---|
306 | You can read the source code and use them at your own risk |
---|
307 | (like for all the Klask code). |
---|
308 | |
---|
309 | =head2 check |
---|
310 | |
---|
311 | project-meta check |
---|
312 | |
---|
313 | Check your F<PROJECT-META.yml> has the good key. |
---|
314 | If your metafile is not a valid YAML file, |
---|
315 | you can use C<yamllint> command to check just it's format. |
---|
316 | |
---|
317 | =head2 make-licence |
---|
318 | |
---|
319 | project-meta make-licence |
---|
320 | |
---|
321 | Copy the licence file from the project-meta licence database at the current folder with the file name: LICENCE.txt. |
---|
322 | |
---|
323 | The licence is defined in the PROJECT-META.yml specification under the key C<public-dap/data-licence>. |
---|
324 | The list of possible licence is given with the command L<list-licence>. |
---|
325 | |
---|
326 | =head2 list-licence |
---|
327 | |
---|
328 | project-meta list-licence |
---|
329 | |
---|
330 | Give the list of all the open data licence supported by the project-meta licence database. |
---|
331 | At this time the possible licence are: |
---|
332 | |
---|
333 | license-ouverte-v2.0 |
---|
334 | open-database-license-v1.0 |
---|
335 | |
---|
336 | |
---|
337 | =head1 METAFILE SPECIFICATION |
---|
338 | |
---|
339 | Each project must have an open data metafile which describe the project : C<PROJECT-META.yml>. |
---|
340 | The file is in YAML format because this is a human readable style of text file. |
---|
341 | |
---|
342 | You can find in the project-meta software a C<PROJECT-META.sample.yml> example. |
---|
343 | This one is actually the master reference specification! |
---|
344 | |
---|
345 | |
---|
346 | =head1 BUG |
---|
347 | |
---|
348 | - make-link: folder/folder |
---|
349 | - make-link: support multiple doi |
---|
350 | - metafile: remove prop publication/url |
---|
351 | |
---|
352 | =head1 SEE ALSO |
---|
353 | |
---|
354 | yamllint |
---|
355 | |
---|
356 | |
---|
357 | =head1 AUTHOR |
---|
358 | |
---|
359 | Written by Gabriel Moreau, LEGI UMR5519, CNRS, Grenoble - France |
---|
360 | |
---|
361 | =head1 SPECIAL THANKS |
---|
362 | |
---|
363 | The list of people below did not directly contribute to project-meta's source code |
---|
364 | but provided me with some data, returned bugs |
---|
365 | or helped me in another small task like having new ideas ... |
---|
366 | Maybe I forgot your contribution in recent years, |
---|
367 | please forgive me in advance and send me an e-mail to correct this. |
---|
368 | |
---|
369 | Joel Sommeria, Julien Chauchat, Cyrille Bonamy, Antoine Mathieu. |
---|
370 | |
---|
371 | |
---|
372 | =head1 LICENSE AND COPYRIGHT |
---|
373 | |
---|
374 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
375 | |
---|
376 | Copyright (C) 2017-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>. |
---|