From 057739d6e450b13d5cf5562e10d252aef829eeef Mon Sep 17 00:00:00 2001 From: Serge Date: Thu, 16 Jan 2025 16:40:34 -0500 Subject: [PATCH] File name verifier (#1451) Boutiques output file name verification #1447 --- Bourreau/lib/boutiques_file_name_verifier.rb | 1 + .../lib/boutiques_file_name_verifier.rb | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 120000 Bourreau/lib/boutiques_file_name_verifier.rb create mode 100644 BrainPortal/lib/boutiques_file_name_verifier.rb diff --git a/Bourreau/lib/boutiques_file_name_verifier.rb b/Bourreau/lib/boutiques_file_name_verifier.rb new file mode 120000 index 000000000..70f8c4cf6 --- /dev/null +++ b/Bourreau/lib/boutiques_file_name_verifier.rb @@ -0,0 +1 @@ +../../BrainPortal/lib/boutiques_file_name_verifier.rb \ No newline at end of file diff --git a/BrainPortal/lib/boutiques_file_name_verifier.rb b/BrainPortal/lib/boutiques_file_name_verifier.rb new file mode 100644 index 000000000..668fc11c8 --- /dev/null +++ b/BrainPortal/lib/boutiques_file_name_verifier.rb @@ -0,0 +1,72 @@ + +# +# CBRAIN Project +# +# Copyright (C) 2008-2024 +# The Royal Institution for the Advancement of Learning +# McGill University +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# This module offers automatic verification of the +# files (or directories) names generated as tool output, and therefore have comply with CBRAIN file name conventions. +# +# For example, in the "inputs" section we might have: +# +# { +# "description": "The name of the folder to store outputs of XCPD processing.", +# "id": "output_dir", +# "name": "output_dir", +# "optional": false, +# "type": "String", +# "value-key": "[OUTPUT_DIR]", +# "default-value": "xcpd_output" +# } +# +# And the custom property is specified like +# +# "cbrain:integrator_modules": { +# "BoutiquesFileNameVerifier": [ +# "output_dir", +# "another_id" +# ] +# } +module BoutiquesFileNameVerifier + + # Note: to access the revision info of the module, + # you need to access the constant directly, the + # object method revision_info() won't work. + Revision_info=CbrainFileRevision[__FILE__] #:nodoc: + + def after_form #:nodoc: + descriptor = self.descriptor_for_after_form + verifs = descriptor.custom_module_info('BoutiquesFileNameVerifier') || [] + verifs.each do |inputid| # 'myinput' + found_match = Array(invoke_params[inputid]) + .map(&:presence) + .compact + .all? do |fname| + Userfile.is_legal_filename?(fname) + end + if ! found_match + input = descriptor.input_by_id(inputid) + params_errors.add(input.cb_invoke_name, "is not suitable for naming an output file. Please, change to a value that starts with a letter or number and avoid special or unprintable symbols") + end + end + + super # call all the normal code + end + +end