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