3 # Copyright (C) 2010-2012 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 import src.debug as DBG
29 # internationalization
30 satdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
31 gettext.install('salomeTools', os.path.join(satdir, 'src', 'i18n'))
33 # Define all possible option for config command : sat config <options>
34 parser = src.options.Options()
35 parser.add_option('v', 'value', 'string', 'value',
36 _("Optional: print the value of CONFIG_VARIABLE."))
37 parser.add_option('g', 'debug', 'string', 'debug',
38 _("Optional: print the debugging mode value of CONFIG_VARIABLE."))
39 parser.add_option('e', 'edit', 'boolean', 'edit',
40 _("Optional: edit the product configuration file."))
41 parser.add_option('i', 'info', 'list2', 'info',
42 _("Optional: get information on product(s). This option accepts a comma separated list."))
43 parser.add_option('l', 'list', 'boolean', 'list',
44 _("Optional: list all available applications."))
45 parser.add_option('', 'show_patchs', 'boolean', 'show_patchs',
46 _("Optional: synthetic list of all patches used in the application"))
47 parser.add_option('', 'show_properties', 'boolean', 'show_properties',
48 _("Optional: synthetic list of all properties used in the application"))
49 parser.add_option('c', 'copy', 'boolean', 'copy',
50 _("""Optional: copy a config file to the personal config files directory.
51 \tWARNING the included files are not copied.
52 \tIf a name is given the new config file takes the given name."""))
53 parser.add_option('n', 'no_label', 'boolean', 'no_label',
54 _("Internal use: do not print labels, Works only with --value and --list."))
55 parser.add_option('', 'completion', 'boolean', 'completion',
56 _("Internal use: print only keys, works only with --value."))
57 parser.add_option('s', 'schema', 'boolean', 'schema',
61 '''Class that helps to find an application pyconf
62 in all the possible directories (pathList)
64 def __init__(self, pathList):
67 :param pathList list: The list of paths where to search a pyconf.
69 self.pathList = pathList
71 def __call__(self, name):
72 if os.path.isabs(name):
73 return src.pyconf.ConfigInputStream(open(name, 'rb'))
75 return src.pyconf.ConfigInputStream(
76 open(os.path.join( self.get_path(name), name ), 'rb') )
77 raise IOError(_("Configuration file '%s' not found") % name)
79 def get_path( self, name ):
80 '''The method that returns the entire path of the pyconf searched
81 :param name str: The name of the searched pyconf.
83 for path in self.pathList:
84 if os.path.exists(os.path.join(path, name)):
86 raise IOError(_("Configuration file '%s' not found") % name)
89 '''Class that manages the read of all the configuration files of salomeTools
91 def __init__(self, datadir=None):
94 def _create_vars(self, application=None, command=None, datadir=None):
95 '''Create a dictionary that stores all information about machine,
96 user, date, repositories, etc...
98 :param application str: The application for which salomeTools is called.
99 :param command str: The command that is called.
100 :param datadir str: The repository that contain external data
102 :return: The dictionary that stores all information.
106 var['user'] = src.architecture.get_user()
107 var['salometoolsway'] = os.path.dirname(
108 os.path.dirname(os.path.abspath(__file__)))
109 var['srcDir'] = os.path.join(var['salometoolsway'], 'src')
110 var['internal_dir'] = os.path.join(var['srcDir'], 'internal_config')
111 var['sep']= os.path.sep
113 # datadir has a default location
114 var['datadir'] = os.path.join(var['salometoolsway'], 'data')
115 if datadir is not None:
116 var['datadir'] = datadir
118 var['personalDir'] = os.path.join(os.path.expanduser('~'),
120 src.ensure_path_exists(var['personalDir'])
122 var['personal_applications_dir'] = os.path.join(var['personalDir'],
124 src.ensure_path_exists(var['personal_applications_dir'])
126 var['personal_products_dir'] = os.path.join(var['personalDir'],
128 src.ensure_path_exists(var['personal_products_dir'])
130 var['personal_archives_dir'] = os.path.join(var['personalDir'],
132 src.ensure_path_exists(var['personal_archives_dir'])
134 var['personal_jobs_dir'] = os.path.join(var['personalDir'],
136 src.ensure_path_exists(var['personal_jobs_dir'])
138 var['personal_machines_dir'] = os.path.join(var['personalDir'],
140 src.ensure_path_exists(var['personal_machines_dir'])
142 # read linux distributions dictionary
143 distrib_cfg = src.pyconf.Config(os.path.join(var['srcDir'],
147 # set platform parameters
148 dist_name = src.architecture.get_distribution(
149 codes=distrib_cfg.DISTRIBUTIONS)
150 dist_version = src.architecture.get_distrib_version(dist_name,
151 codes=distrib_cfg.VERSIONS)
152 dist = dist_name + dist_version
154 var['dist_name'] = dist_name
155 var['dist_version'] = dist_version
157 var['python'] = src.architecture.get_python_version()
159 var['nb_proc'] = src.architecture.get_nb_proc()
160 node_name = platform.node()
161 var['node'] = node_name
162 var['hostname'] = node_name
164 # set date parameters
165 dt = datetime.datetime.now()
166 var['date'] = dt.strftime('%Y%m%d')
167 var['datehour'] = dt.strftime('%Y%m%d_%H%M%S')
168 var['hour'] = dt.strftime('%H%M%S')
170 var['command'] = str(command)
171 var['application'] = str(application)
173 # Root dir for temporary files
174 var['tmp_root'] = os.sep + 'tmp' + os.sep + var['user']
175 # particular win case
176 if src.architecture.is_windows() :
177 var['tmp_root'] = os.path.expanduser('~') + os.sep + 'tmp'
181 def get_command_line_overrides(self, options, sections):
182 '''get all the overwrites that are in the command line
184 :param options: the options from salomeTools class
185 initialization (like -l5 or --overwrite)
186 :param sections str: The config section to overwrite.
187 :return: The list of all the overwrites to apply.
190 # when there are no options or not the overwrite option,
191 # return an empty list
192 if options is None or options.overwrite is None:
196 for section in sections:
197 # only overwrite the sections that correspond to the option
198 over.extend(filter(lambda l: l.startswith(section + "."),
202 def get_config(self, application=None, options=None, command=None,
204 '''get the config from all the configuration files.
206 :param application str: The application for which salomeTools is called.
207 :param options class Options: The general salomeToos
208 options (--overwrite or -l5, for example)
209 :param command str: The command that is called.
210 :param datadir str: The repository that contain
211 external data for salomeTools.
212 :return: The final config.
213 :rtype: class 'src.pyconf.Config'
216 # create a ConfigMerger to handle merge
217 merger = src.pyconf.ConfigMerger()#MergeHandler())
219 # create the configuration instance
220 cfg = src.pyconf.Config()
222 # =====================================================================
223 # create VARS section
224 var = self._create_vars(application=application, command=command,
227 cfg.VARS = src.pyconf.Mapping(cfg)
229 cfg.VARS[variable] = var[variable]
231 # apply overwrite from command line if needed
232 for rule in self.get_command_line_overrides(options, ["VARS"]):
233 exec('cfg.' + rule) # this cannot be factorized because of the exec
235 # =====================================================================
236 # Load INTERNAL config
237 # read src/internal_config/salomeTools.pyconf
238 src.pyconf.streamOpener = ConfigOpener([
239 os.path.join(cfg.VARS.srcDir, 'internal_config')])
241 internal_cfg = src.pyconf.Config(open(os.path.join(cfg.VARS.srcDir,
242 'internal_config', 'salomeTools.pyconf')))
243 except src.pyconf.ConfigError as e:
244 raise src.SatException(_("Error in configuration file:"
245 " salomeTools.pyconf\n %(error)s") % \
248 merger.merge(cfg, internal_cfg)
250 # apply overwrite from command line if needed
251 for rule in self.get_command_line_overrides(options, ["INTERNAL"]):
252 exec('cfg.' + rule) # this cannot be factorized because of the exec
254 # =====================================================================
255 # Load LOCAL config file
256 # search only in the data directory
257 src.pyconf.streamOpener = ConfigOpener([cfg.VARS.datadir])
259 local_cfg = src.pyconf.Config(open(os.path.join(cfg.VARS.datadir,
261 PWD = ('LOCAL', cfg.VARS.datadir) )
262 except src.pyconf.ConfigError as e:
263 raise src.SatException(_("Error in configuration file: "
264 "local.pyconf\n %(error)s") % \
266 except IOError as error:
268 raise src.SatException( e );
269 merger.merge(cfg, local_cfg)
271 # When the key is "default", put the default value
272 if cfg.LOCAL.base == "default":
273 cfg.LOCAL.base = os.path.abspath(
274 os.path.join(cfg.VARS.salometoolsway,
277 if cfg.LOCAL.workdir == "default":
278 cfg.LOCAL.workdir = os.path.abspath(
279 os.path.join(cfg.VARS.salometoolsway,
281 if cfg.LOCAL.log_dir == "default":
282 cfg.LOCAL.log_dir = os.path.abspath(
283 os.path.join(cfg.VARS.salometoolsway,
287 if cfg.LOCAL.archive_dir == "default":
288 cfg.LOCAL.archive_dir = os.path.abspath(
289 os.path.join(cfg.VARS.salometoolsway,
293 # apply overwrite from command line if needed
294 for rule in self.get_command_line_overrides(options, ["LOCAL"]):
295 exec('cfg.' + rule) # this cannot be factorized because of the exec
297 # =====================================================================
299 projects_cfg = src.pyconf.Config()
300 projects_cfg.addMapping("PROJECTS",
301 src.pyconf.Mapping(projects_cfg),
303 projects_cfg.PROJECTS.addMapping("projects",
304 src.pyconf.Mapping(cfg.PROJECTS),
305 "The projects definition\n")
307 for project_pyconf_path in cfg.PROJECTS.project_file_paths:
308 if not os.path.exists(project_pyconf_path):
309 msg = _("WARNING: The project file %s cannot be found. "
310 "It will be ignored\n" % project_pyconf_path)
311 sys.stdout.write(msg)
313 project_name = os.path.basename(
314 project_pyconf_path)[:-len(".pyconf")]
316 project_pyconf_dir = os.path.dirname(project_pyconf_path)
317 project_cfg = src.pyconf.Config(open(project_pyconf_path),
318 PWD=("", project_pyconf_dir))
319 except Exception as e:
320 msg = _("ERROR: Error in configuration file: "
321 "%(file_path)s\n %(error)s\n") % \
322 {'file_path' : project_pyconf_path, 'error': str(e) }
323 sys.stdout.write(msg)
325 projects_cfg.PROJECTS.projects.addMapping(project_name,
326 src.pyconf.Mapping(projects_cfg.PROJECTS.projects),
327 "The %s project\n" % project_name)
328 projects_cfg.PROJECTS.projects[project_name]=project_cfg
329 projects_cfg.PROJECTS.projects[project_name]["file_path"] = \
332 merger.merge(cfg, projects_cfg)
334 # apply overwrite from command line if needed
335 for rule in self.get_command_line_overrides(options, ["PROJECTS"]):
336 exec('cfg.' + rule) # this cannot be factorized because of the exec
338 # =====================================================================
339 # Create the paths where to search the application configurations,
340 # the product configurations, the products archives,
341 # the jobs configurations and the machines configurations
342 cfg.addMapping("PATHS", src.pyconf.Mapping(cfg), "The paths\n")
343 cfg.PATHS["APPLICATIONPATH"] = src.pyconf.Sequence(cfg.PATHS)
344 cfg.PATHS.APPLICATIONPATH.append(cfg.VARS.personal_applications_dir, "")
347 cfg.PATHS["PRODUCTPATH"] = src.pyconf.Sequence(cfg.PATHS)
348 cfg.PATHS.PRODUCTPATH.append(cfg.VARS.personal_products_dir, "")
349 cfg.PATHS["ARCHIVEPATH"] = src.pyconf.Sequence(cfg.PATHS)
350 cfg.PATHS.ARCHIVEPATH.append(cfg.VARS.personal_archives_dir, "")
351 cfg.PATHS["JOBPATH"] = src.pyconf.Sequence(cfg.PATHS)
352 cfg.PATHS.JOBPATH.append(cfg.VARS.personal_jobs_dir, "")
353 cfg.PATHS["MACHINEPATH"] = src.pyconf.Sequence(cfg.PATHS)
354 cfg.PATHS.MACHINEPATH.append(cfg.VARS.personal_machines_dir, "")
356 # initialise the path with local directory
357 cfg.PATHS["ARCHIVEPATH"].append(cfg.LOCAL.archive_dir, "")
359 # Loop over the projects in order to complete the PATHS variables
360 # as /data/tmpsalome/salome/prerequis/archives for example ARCHIVEPATH
361 for project in cfg.PROJECTS.projects:
362 for PATH in ["APPLICATIONPATH",
364 "ARCHIVEPATH", #comment this for default archive #8646
367 if PATH not in cfg.PROJECTS.projects[project]:
369 cfg.PATHS[PATH].append(cfg.PROJECTS.projects[project][PATH], "")
371 # apply overwrite from command line if needed
372 for rule in self.get_command_line_overrides(options, ["PATHS"]):
373 exec('cfg.' + rule) # this cannot be factorized because of the exec
375 # AT END append APPLI_TEST directory in APPLICATIONPATH, for unittest
376 appli_test_dir = os.path.join(satdir, "test", "APPLI_TEST")
377 if appli_test_dir not in cfg.PATHS.APPLICATIONPATH:
378 cfg.PATHS.APPLICATIONPATH.append(appli_test_dir, "unittest APPLI_TEST path")
380 # =====================================================================
381 # Load APPLICATION config file
382 if application is not None:
383 # search APPLICATION file in all directories in configPath
384 cp = cfg.PATHS.APPLICATIONPATH
385 src.pyconf.streamOpener = ConfigOpener(cp)
388 application_cfg = src.pyconf.Config(application + '.pyconf')
390 raise src.SatException(
391 _("%s, use 'config --list' to get the list of available applications.") % e)
392 except src.pyconf.ConfigError as e:
393 if (not ('-e' in parser.parse_args()[1])
394 or ('--edit' in parser.parse_args()[1])
395 and command == 'config'):
396 raise src.SatException(_("Error in configuration file: "
397 "%(application)s.pyconf\n "
399 { 'application': application, 'error': str(e) } )
401 sys.stdout.write(src.printcolors.printcWarning(
402 "There is an error in the file"
403 " %s.pyconf.\n" % cfg.VARS.application))
405 except Exception as e:
406 if (not ('-e' in parser.parse_args()[1])
407 or ('--edit' in parser.parse_args()[1])
408 and command == 'config'):
409 sys.stdout.write(src.printcolors.printcWarning("%s\n" % str(e)))
410 raise src.SatException(_("Error in configuration file:"
411 " %(application)s.pyconf\n") % \
412 { 'application': application} )
414 sys.stdout.write(src.printcolors.printcWarning(
415 "There is an error in the file"
416 " %s.pyconf. Opening the file with the"
417 " default viewer\n" % cfg.VARS.application))
418 sys.stdout.write("The error:"
419 " %s\n" % src.printcolors.printcWarning(
424 cfg['open_application'] = 'yes'
426 # =====================================================================
427 # Load product config files in PRODUCTS section
428 products_cfg = src.pyconf.Config()
429 products_cfg.addMapping("PRODUCTS",
430 src.pyconf.Mapping(products_cfg),
432 if application is not None:
433 src.pyconf.streamOpener = ConfigOpener(cfg.PATHS.PRODUCTPATH)
434 for product_name in application_cfg.APPLICATION.products.keys():
435 # Loop on all files that are in softsDir directory
436 # and read their config
437 product_file_name = product_name + ".pyconf"
438 product_file_path = src.find_file_in_lpath(product_file_name, cfg.PATHS.PRODUCTPATH)
439 if product_file_path:
440 products_dir = os.path.dirname(product_file_path)
442 prod_cfg = src.pyconf.Config(open(product_file_path),
443 PWD=("", products_dir))
444 prod_cfg.from_file = product_file_path
445 products_cfg.PRODUCTS[product_name] = prod_cfg
446 except Exception as e:
448 "WARNING: Error in configuration file"
449 ": %(prod)s\n %(error)s" % \
450 {'prod' : product_name, 'error': str(e) })
451 sys.stdout.write(msg)
453 merger.merge(cfg, products_cfg)
455 # apply overwrite from command line if needed
456 for rule in self.get_command_line_overrides(options, ["PRODUCTS"]):
457 exec('cfg.' + rule) # this cannot be factorized because of the exec
460 merger.merge(cfg, application_cfg)
462 # default launcher name ('salome')
463 if ('profile' in cfg.APPLICATION and
464 'launcher_name' not in cfg.APPLICATION.profile):
465 cfg.APPLICATION.profile.launcher_name = 'salome'
467 # apply overwrite from command line if needed
468 for rule in self.get_command_line_overrides(options,
470 # this cannot be factorized because of the exec
473 # =====================================================================
475 self.set_user_config_file(cfg)
476 user_cfg_file = self.get_user_config_file()
477 user_cfg = src.pyconf.Config(open(user_cfg_file))
478 merger.merge(cfg, user_cfg)
480 # apply overwrite from command line if needed
481 for rule in self.get_command_line_overrides(options, ["USER"]):
482 exec('cfg.' + rule) # this cannot be factorize because of the exec
486 def set_user_config_file(self, config):
487 '''Set the user config file name and path.
488 If necessary, build it from another one or create it from scratch.
490 :param config class 'src.pyconf.Config': The global config
491 (containing all pyconf).
493 # get the expected name and path of the file
494 self.config_file_name = 'SAT.pyconf'
495 self.user_config_file_path = os.path.join(config.VARS.personalDir,
496 self.config_file_name)
498 # if pyconf does not exist, create it from scratch
499 if not os.path.isfile(self.user_config_file_path):
500 self.create_config_file(config)
502 def create_config_file(self, config):
503 '''This method is called when there are no user config file.
504 It build it from scratch.
506 :param config class 'src.pyconf.Config': The global config.
507 :return: the config corresponding to the file created.
508 :rtype: config class 'src.pyconf.Config'
511 cfg_name = self.get_user_config_file()
513 user_cfg = src.pyconf.Config()
515 user_cfg.addMapping('USER', src.pyconf.Mapping(user_cfg), "")
517 user_cfg.USER.addMapping('cvs_user', config.VARS.user,
518 "This is the user name used to access salome cvs base.\n")
519 user_cfg.USER.addMapping('svn_user', config.VARS.user,
520 "This is the user name used to access salome svn base.\n")
521 user_cfg.USER.addMapping('output_verbose_level', 3,
522 "This is the default output_verbose_level you want."
523 " 0=>no output, 5=>debug.\n")
524 user_cfg.USER.addMapping('publish_dir',
525 os.path.join(os.path.expanduser('~'),
529 user_cfg.USER.addMapping('editor',
531 "This is the editor used to "
532 "modify configuration files\n")
533 user_cfg.USER.addMapping('browser',
535 "This is the browser used to "
536 "read html documentation\n")
537 user_cfg.USER.addMapping('pdf_viewer',
539 "This is the pdf_viewer used "
540 "to read pdf documentation\n")
541 # CNC 25/10/17 : plus nécessaire a priori
542 # user_cfg.USER.addMapping("base",
543 # src.pyconf.Reference(
546 # 'workdir + $VARS.sep + "BASE"'),
547 # "The products installation base (could be "
548 # "ignored if this key exists in the local.pyconf"
549 # " file of salomTools).\n")
552 src.ensure_path_exists(config.VARS.personalDir)
553 src.ensure_path_exists(os.path.join(config.VARS.personalDir,
556 f = open(cfg_name, 'w')
562 def get_user_config_file(self):
563 '''Get the user config file
564 :return: path to the user config file.
567 if not self.user_config_file_path:
568 raise src.SatException(_("Error in get_user_config_file: "
569 "missing user config file path"))
570 return self.user_config_file_path
572 def check_path(path, ext=[]):
573 '''Construct a text with the input path and "not found" if it does not
576 :param path Str: the path to check.
577 :param ext List: An extension. Verify that the path extension
579 :return: The string of the path with information
582 # check if file exists
583 if not os.path.exists(path):
584 return "'%s'" % path + " " + src.printcolors.printcError(_(
589 fe = os.path.splitext(path)[1].lower()
591 return "'%s'" % path + " " + src.printcolors.printcError(_(
596 def show_product_info(config, name, logger):
597 '''Display on the terminal and logger information about a product.
599 :param config Config: the global configuration.
600 :param name Str: The name of the product
601 :param logger Logger: The logger instance to use for the display
604 logger.write(_("%s is a product\n") % src.printcolors.printcLabel(name), 2)
605 pinfo = src.product.get_product_config(config, name)
607 if "depend" in pinfo:
608 src.printcolors.print_value(logger,
610 ', '.join(pinfo.depend), 2)
612 if "opt_depend" in pinfo:
613 src.printcolors.print_value(logger,
615 ', '.join(pinfo.opt_depend), 2)
617 # information on pyconf
618 logger.write("\n", 2)
619 logger.write(src.printcolors.printcLabel("configuration:") + "\n", 2)
620 if "from_file" in pinfo:
621 src.printcolors.print_value(logger,
625 if "section" in pinfo:
626 src.printcolors.print_value(logger,
631 # information on prepare
632 logger.write("\n", 2)
633 logger.write(src.printcolors.printcLabel("prepare:") + "\n", 2)
635 is_dev = src.product.product_is_dev(pinfo)
636 method = pinfo.get_source
639 src.printcolors.print_value(logger, "get method", method, 2)
642 src.printcolors.print_value(logger, "server", pinfo.cvs_info.server, 2)
643 src.printcolors.print_value(logger, "base module",
644 pinfo.cvs_info.module_base, 2)
645 src.printcolors.print_value(logger, "source", pinfo.cvs_info.source, 2)
646 src.printcolors.print_value(logger, "tag", pinfo.cvs_info.tag, 2)
648 elif method == 'svn':
649 src.printcolors.print_value(logger, "repo", pinfo.svn_info.repo, 2)
651 elif method == 'git':
652 src.printcolors.print_value(logger, "repo", pinfo.git_info.repo, 2)
653 src.printcolors.print_value(logger, "tag", pinfo.git_info.tag, 2)
655 elif method == 'archive':
656 src.printcolors.print_value(logger,
658 check_path(pinfo.archive_info.archive_name),
661 if 'patches' in pinfo:
662 for patch in pinfo.patches:
663 src.printcolors.print_value(logger, "patch", check_path(patch), 2)
665 if src.product.product_is_fixed(pinfo):
666 src.printcolors.print_value(logger, "install_dir",
667 check_path(pinfo.install_dir), 2)
669 if src.product.product_is_native(pinfo) or src.product.product_is_fixed(pinfo):
672 # information on compilation
673 if src.product.product_compiles(pinfo):
674 logger.write("\n", 2)
675 logger.write(src.printcolors.printcLabel("compile:") + "\n", 2)
676 src.printcolors.print_value(logger,
677 "compilation method",
681 if pinfo.build_source == "script" and "compil_script" in pinfo:
682 src.printcolors.print_value(logger,
683 "Compilation script",
687 if 'nb_proc' in pinfo:
688 src.printcolors.print_value(logger, "make -j", pinfo.nb_proc, 2)
690 src.printcolors.print_value(logger,
692 check_path(pinfo.source_dir),
694 if 'install_dir' in pinfo:
695 src.printcolors.print_value(logger,
697 check_path(pinfo.build_dir),
699 src.printcolors.print_value(logger,
701 check_path(pinfo.install_dir),
705 src.printcolors.printcWarning(_("no install dir")) +
708 logger.write("\n", 2)
709 msg = _("This product does not compile")
710 logger.write("%s\n" % msg, 2)
712 # information on environment
713 logger.write("\n", 2)
714 logger.write(src.printcolors.printcLabel("environ :") + "\n", 2)
715 if "environ" in pinfo and "env_script" in pinfo.environ:
716 src.printcolors.print_value(logger,
718 check_path(pinfo.environ.env_script),
721 zz = src.environment.SalomeEnviron(config,
722 src.fileEnviron.ScreenEnviron(logger),
724 zz.set_python_libdirs()
725 zz.set_a_product(name, logger)
726 logger.write("\n", 2)
729 def show_patchs(config, logger):
730 '''Prints all the used patchs in the application.
732 :param config Config: the global configuration.
733 :param logger Logger: The logger instance to use for the display
736 for product in sorted(config.APPLICATION.products):
738 product_info = src.product.get_product_config(config, product)
739 if src.product.product_has_patches(product_info):
741 logger.write("%s:\n" % product, 1)
742 for i in product_info.patches:
743 logger.write(src.printcolors.printcInfo(" %s\n" % i), 1)
744 except Exception as e:
745 msg = "problem on product %s\n%s\n" % (product, str(e))
749 logger.write("\n", 1)
751 logger.write("No patchs found\n", 1)
754 def show_properties(config, logger):
755 '''Prints all the used properties in the application.
757 :param config Config: the global configuration.
758 :param logger Logger: The logger instance to use for the display
761 for product in sorted(config.APPLICATION.products):
763 product_info = src.product.get_product_config(config, product)
766 for i in product_info.properties:
768 logger.write("%s:\n" % product, 1)
771 logger.write(src.printcolors.printcInfo(" %s\n" % i), 1)
772 except Exception as e:
774 except Exception as e:
775 # logger.write(src.printcolors.printcInfo(" %s\n" % "no properties"), 1)
776 msg = "problem on product %s\n%s\n" % (product, e)
780 logger.write("\n", 1)
782 logger.write("No properties found\n", 1)
784 def print_value(config, path, show_label, logger, level=0, show_full_path=False):
785 '''Prints a value from the configuration. Prints recursively the values
786 under the initial path.
788 :param config class 'src.pyconf.Config': The configuration
789 from which the value is displayed.
790 :param path str : the path in the configuration of the value to print.
791 :param show_label boolean: if True, do a basic display.
792 (useful for bash completion)
793 :param logger Logger: the logger instance
794 :param level int: The number of spaces to add before display.
795 :param show_full_path :
798 # Make sure that the path does not ends with a point
799 if path.endswith('.'):
802 # display all the path or not
806 vname = path.split('.')[-1]
808 # number of spaces before the display
809 tab_level = " " * level
811 # call to the function that gets the value of the path.
813 val = config.getByPath(path)
814 except Exception as e:
815 logger.write(tab_level)
816 logger.write("%s: ERROR %s\n" % (src.printcolors.printcLabel(vname),
817 src.printcolors.printcError(str(e))))
820 # in this case, display only the value
822 logger.write(tab_level)
823 logger.write("%s: " % src.printcolors.printcLabel(vname))
825 # The case where the value has under values,
826 # do a recursive call to the function
827 if dir(val).__contains__('keys'):
828 if show_label: logger.write("\n")
829 for v in sorted(val.keys()):
830 print_value(config, path + '.' + v, show_label, logger, level + 1)
831 elif val.__class__ == src.pyconf.Sequence or isinstance(val, list):
832 # in this case, value is a list (or a Sequence)
833 if show_label: logger.write("\n")
836 print_value(config, path + "[" + str(index) + "]",
837 show_label, logger, level + 1)
839 else: # case where val is just a str
840 logger.write("%s\n" % val)
842 def get_config_children(config, args):
843 '''Gets the names of the children of the given parameter.
844 Useful only for completion mechanism
846 :param config Config: The configuration where to read the values
847 :param args: The path in the config from which get the keys
850 rootkeys = config.keys()
853 # no parameter returns list of root keys
857 pos = parent.rfind('.')
859 # Case where there is only on key as parameter.
861 vals = [m for m in rootkeys if m.startswith(parent)]
863 # Case where there is a part from a key
864 # for example VARS.us (for VARS.user)
866 tail = parent[pos+1:]
868 a = config.getByPath(head)
869 if dir(a).__contains__('keys'):
870 vals = map(lambda x: head + '.' + x,
871 [m for m in a.keys() if m.startswith(tail)])
875 for v in sorted(vals):
876 sys.stdout.write("%s\n" % v)
879 '''method that is called when salomeTools is called with --help option.
881 :return: The text to display for the config command description.
884 return _("The config command allows manipulation "
885 "and operation on config files.\n\nexample:\nsat config "
886 "SALOME-master --info ParaView")
889 def run(args, runner, logger):
890 '''method that is called when salomeTools is called with config parameter.
893 (options, args) = parser.parse_args(args)
895 # Only useful for completion mechanism : print the keys of the config
897 get_config_children(runner.cfg, args)
900 # case : print a value of the config
902 if options.value == ".":
903 # if argument is ".", print all the config
904 for val in sorted(runner.cfg.keys()):
905 print_value(runner.cfg, val, not options.no_label, logger)
907 print_value(runner.cfg, options.value, not options.no_label, logger,
908 level=0, show_full_path=False)
910 # case : print a debug value of the config
912 if options.debug == ".":
913 # if argument is ".", print all the config
914 res = DBG.indent(DBG.getStrConfigDbg(runner.cfg))
915 logger.write("\nConfig of application %s:\n\n%s\n" % (runner.cfg.VARS.application, res))
917 if options.debug[0] == ".": # accept ".PRODUCT.etc" as "PRODUCT.etc"
918 od = options.debug[1:]
921 exec("a = runner.cfg.%s" % od)
922 res = DBG.indent(DBG.getStrConfigDbg(a))
923 logger.write("\nConfig.%s of application %s:\n\n%s\n" % (od, runner.cfg.VARS.application, res))
926 # case : edit user pyconf file or application file
928 editor = runner.cfg.USER.editor
929 if ('APPLICATION' not in runner.cfg and
930 'open_application' not in runner.cfg): # edit user pyconf
931 usercfg = os.path.join(runner.cfg.VARS.personalDir,
933 logger.write(_("Opening %s\n" % usercfg), 3)
934 src.system.show_in_editor(editor, usercfg, logger)
936 # search for file <application>.pyconf and open it
937 for path in runner.cfg.PATHS.APPLICATIONPATH:
938 pyconf_path = os.path.join(path,
939 runner.cfg.VARS.application + ".pyconf")
940 if os.path.exists(pyconf_path):
941 logger.write(_("Opening %s\n" % pyconf_path), 3)
942 src.system.show_in_editor(editor, pyconf_path, logger)
945 # case : give information about the product(s) in parameter
947 # DBG.write("products", sorted(runner.cfg.APPLICATION.products.keys()), True)
948 src.check_config_has_application(runner.cfg)
949 taggedProducts = src.getProductNames(runner.cfg, options.info, logger)
950 DBG.write("tagged products", sorted(taggedProducts))
951 for prod in sorted(taggedProducts):
952 if prod in runner.cfg.APPLICATION.products:
954 if len(taggedProducts) > 1:
955 logger.write("#################### ", 2)
956 show_product_info(runner.cfg, prod, logger)
957 except Exception as e:
958 msg = "problem on product %s\n%s\n" % (prod, str(e))
962 msg = _("%s is not a product of %s.\n") % \
963 (prod, runner.cfg.VARS.application)
965 #raise Exception(msg)
967 # case : copy an existing <application>.pyconf
968 # to ~/.salomeTools/Applications/LOCAL_<application>.pyconf
970 # product is required
971 src.check_config_has_application( runner.cfg )
973 # get application file path
974 source = runner.cfg.VARS.application + '.pyconf'
975 source_full_path = ""
976 for path in runner.cfg.PATHS.APPLICATIONPATH:
977 # ignore personal directory
978 if path == runner.cfg.VARS.personalDir:
980 # loop on all directories that can have pyconf applications
981 zz = os.path.join(path, source)
982 if os.path.exists(zz):
983 source_full_path = zz
986 if len(source_full_path) == 0:
987 raise src.SatException(_(
988 "Config file for product %s not found\n") % source)
991 # a name is given as parameter, use it
993 elif 'copy_prefix' in runner.cfg.INTERNAL.config:
995 dest = (runner.cfg.INTERNAL.config.copy_prefix
996 + runner.cfg.VARS.application)
998 # use same name as source
999 dest = runner.cfg.VARS.application
1002 dest_file = os.path.join(runner.cfg.VARS.personalDir,
1003 'Applications', dest + '.pyconf')
1004 if os.path.exists(dest_file):
1005 raise src.SatException(_("A personal application"
1006 " '%s' already exists") % dest)
1009 shutil.copyfile(source_full_path, dest_file)
1010 logger.write(_("%s has been created.\n") % dest_file)
1012 # case : display all the available pyconf applications
1015 # search in all directories that can have pyconf applications
1016 for path in runner.cfg.PATHS.APPLICATIONPATH:
1018 if not options.no_label:
1019 logger.write("------ %s\n" % src.printcolors.printcHeader(path))
1021 if not os.path.exists(path):
1022 logger.write(src.printcolors.printcError(_(
1023 "Directory not found")) + "\n")
1025 for f in sorted(os.listdir(path)):
1026 # ignore file that does not ends with .pyconf
1027 if not f.endswith('.pyconf'):
1030 appliname = f[:-len('.pyconf')]
1031 if appliname not in lproduct:
1032 lproduct.append(appliname)
1033 if path.startswith(runner.cfg.VARS.personalDir) \
1034 and not options.no_label:
1035 logger.write("%s*\n" % appliname)
1037 logger.write("%s\n" % appliname)
1041 # case: print all the products name of the application (internal use for completion)
1042 if options.completion:
1043 for product_name in runner.cfg.APPLICATION.products.keys():
1044 logger.write("%s\n" % product_name)
1046 # case : give a synthetic view of all patches used in the application
1047 if options.show_patchs:
1048 src.check_config_has_application(runner.cfg)
1049 # Print some informations
1050 logger.write(_('Patchs of application %s\n') %
1051 src.printcolors.printcLabel(runner.cfg.VARS.application), 3)
1052 logger.write("\n", 2, False)
1053 show_patchs(runner.cfg, logger)
1055 # case : give a synthetic view of all patches used in the application
1056 if options.show_properties:
1057 src.check_config_has_application(runner.cfg)
1058 # Print some informations
1059 logger.write(_('Properties of application %s\n') %
1060 src.printcolors.printcLabel(runner.cfg.VARS.application), 3)
1061 logger.write("\n", 2, False)
1062 show_properties(runner.cfg, logger)