Salome HOME
Ajout d'un mode ftp pour les archives de prérequis
[tools/sat.git] / commands / config.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import os
20 import platform
21 import datetime
22 import shutil
23 import gettext
24 import sys
25
26 import src
27 import src.debug as DBG
28
29 # internationalization
30 satdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
31 gettext.install('salomeTools', os.path.join(satdir, 'src', 'i18n'))
32
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('p', 'products', 'list2', 'products',
44     _("Optional: same as --info, for convenience."))
45 parser.add_option('l', 'list', 'boolean', 'list',
46     _("Optional: list all available applications."))
47 parser.add_option('', 'show_patchs', 'boolean', 'show_patchs',
48     _("Optional: synthetic list of all patches used in the application"))
49 parser.add_option('', 'show_properties', 'boolean', 'show_properties',
50     _("Optional: synthetic list of all properties used in the application"))
51 parser.add_option('c', 'copy', 'boolean', 'copy',
52     _("""Optional: copy a config file to the personal config files directory.
53 \tWARNING the included files are not copied.
54 \tIf a name is given the new config file takes the given name."""))
55 parser.add_option('n', 'no_label', 'boolean', 'no_label',
56     _("Internal use: do not print labels, Works only with --value and --list."))
57 parser.add_option('', 'completion', 'boolean', 'completion',
58     _("Internal use: print only keys, works only with --value."))
59 parser.add_option('s', 'schema', 'boolean', 'schema',
60     _("Internal use."))
61
62 class ConfigOpener:
63     '''Class that helps to find an application pyconf 
64        in all the possible directories (pathList)
65     '''
66     def __init__(self, pathList):
67         '''Initialization
68         
69         :param pathList list: The list of paths where to search a pyconf.
70         '''
71         self.pathList = pathList
72
73     def __call__(self, name):
74         if os.path.isabs(name):
75             return src.pyconf.ConfigInputStream(open(name, 'rb'))
76         else:
77             return src.pyconf.ConfigInputStream( 
78                         open(os.path.join( self.get_path(name), name ), 'rb') )
79         raise IOError(_("Configuration file '%s' not found") % name)
80
81     def get_path( self, name ):
82         '''The method that returns the entire path of the pyconf searched
83         :param name str: The name of the searched pyconf.
84         '''
85         for path in self.pathList:
86             if os.path.exists(os.path.join(path, name)):
87                 return path
88         raise IOError(_("Configuration file '%s' not found") % name)
89
90 class ConfigManager:
91     '''Class that manages the read of all the configuration files of salomeTools
92     '''
93     def __init__(self, datadir=None):
94         pass
95
96     def _create_vars(self, application=None, command=None, datadir=None):
97         '''Create a dictionary that stores all information about machine,
98            user, date, repositories, etc...
99         
100         :param application str: The application for which salomeTools is called.
101         :param command str: The command that is called.
102         :param datadir str: The repository that contain external data 
103                             for salomeTools.
104         :return: The dictionary that stores all information.
105         :rtype: dict
106         '''
107         var = {}      
108         var['user'] = src.architecture.get_user()
109         var['salometoolsway'] = os.path.dirname(
110                                     os.path.dirname(os.path.abspath(__file__)))
111         var['srcDir'] = os.path.join(var['salometoolsway'], 'src')
112         var['internal_dir'] = os.path.join(var['srcDir'], 'internal_config')
113         var['sep']= os.path.sep
114         
115         # datadir has a default location
116         var['datadir'] = os.path.join(var['salometoolsway'], 'data')
117         if datadir is not None:
118             var['datadir'] = datadir
119
120         var['personalDir'] = os.path.join(os.path.expanduser('~'),
121                                            '.salomeTools')
122         src.ensure_path_exists(var['personalDir'])
123
124         var['personal_applications_dir'] = os.path.join(var['personalDir'],
125                                                         "Applications")
126         src.ensure_path_exists(var['personal_applications_dir'])
127         
128         var['personal_products_dir'] = os.path.join(var['personalDir'],
129                                                     "products")
130         src.ensure_path_exists(var['personal_products_dir'])
131         
132         var['personal_archives_dir'] = os.path.join(var['personalDir'],
133                                                     "Archives")
134         src.ensure_path_exists(var['personal_archives_dir'])
135
136         var['personal_jobs_dir'] = os.path.join(var['personalDir'],
137                                                 "Jobs")
138         src.ensure_path_exists(var['personal_jobs_dir'])
139
140         var['personal_machines_dir'] = os.path.join(var['personalDir'],
141                                                     "Machines")
142         src.ensure_path_exists(var['personal_machines_dir'])
143
144         # read linux distributions dictionary
145         distrib_cfg = src.pyconf.Config(os.path.join(var['srcDir'],
146                                                       'internal_config',
147                                                       'distrib.pyconf'))
148         
149         # set platform parameters
150         dist_name = src.architecture.get_distribution(
151                                             codes=distrib_cfg.DISTRIBUTIONS)
152         dist_version = src.architecture.get_distrib_version(dist_name, 
153                                                     codes=distrib_cfg.VERSIONS)
154         dist = dist_name + dist_version
155         
156         var['dist_name'] = dist_name
157         var['dist_version'] = dist_version
158         var['dist'] = dist
159         var['python'] = src.architecture.get_python_version()
160
161         var['nb_proc'] = src.architecture.get_nb_proc()
162         node_name = platform.node()
163         var['node'] = node_name
164         var['hostname'] = node_name
165
166         # set date parameters
167         dt = datetime.datetime.now()
168         var['date'] = dt.strftime('%Y%m%d')
169         var['datehour'] = dt.strftime('%Y%m%d_%H%M%S')
170         var['hour'] = dt.strftime('%H%M%S')
171
172         var['command'] = str(command)
173         var['application'] = str(application)
174
175         # Root dir for temporary files 
176         var['tmp_root'] = os.sep + 'tmp' + os.sep + var['user']
177         # particular win case 
178         if src.architecture.is_windows() : 
179             var['tmp_root'] =  os.path.expanduser('~') + os.sep + 'tmp'
180         
181         return var
182
183     def get_command_line_overrides(self, options, sections):
184         '''get all the overwrites that are in the command line
185         
186         :param options: the options from salomeTools class 
187                         initialization (like -l5 or --overwrite)
188         :param sections str: The config section to overwrite.
189         :return: The list of all the overwrites to apply.
190         :rtype: list
191         '''
192         # when there are no options or not the overwrite option, 
193         # return an empty list
194         if options is None or options.overwrite is None:
195             return []
196         
197         over = []
198         for section in sections:
199             # only overwrite the sections that correspond to the option 
200             over.extend(filter(lambda l: l.startswith(section + "."), 
201                                options.overwrite))
202         return over
203
204     def get_config(self, application=None, options=None, command=None,
205                     datadir=None):
206         '''get the config from all the configuration files.
207         
208         :param application str: The application for which salomeTools is called.
209         :param options class Options: The general salomeToos
210                                       options (--overwrite or -l5, for example)
211         :param command str: The command that is called.
212         :param datadir str: The repository that contain 
213                             external data for salomeTools.
214         :return: The final config.
215         :rtype: class 'src.pyconf.Config'
216         '''        
217         
218         # create a ConfigMerger to handle merge
219         merger = src.pyconf.ConfigMerger()#MergeHandler())
220         
221         # create the configuration instance
222         cfg = src.pyconf.Config()
223         
224         # =====================================================================
225         # create VARS section
226         var = self._create_vars(application=application, command=command, 
227                                 datadir=datadir)
228         # add VARS to config
229         cfg.VARS = src.pyconf.Mapping(cfg)
230         for variable in var:
231             cfg.VARS[variable] = var[variable]
232         
233         # apply overwrite from command line if needed
234         for rule in self.get_command_line_overrides(options, ["VARS"]):
235             exec('cfg.' + rule) # this cannot be factorized because of the exec
236         
237         # =====================================================================
238         # Load INTERNAL config
239         # read src/internal_config/salomeTools.pyconf
240         src.pyconf.streamOpener = ConfigOpener([
241                             os.path.join(cfg.VARS.srcDir, 'internal_config')])
242         try:
243             internal_cfg = src.pyconf.Config(open(os.path.join(cfg.VARS.srcDir, 
244                                     'internal_config', 'salomeTools.pyconf')))
245         except src.pyconf.ConfigError as e:
246             raise src.SatException(_("Error in configuration file:"
247                                      " salomeTools.pyconf\n  %(error)s") % \
248                                    {'error': str(e) })
249         
250         merger.merge(cfg, internal_cfg)
251
252         # apply overwrite from command line if needed
253         for rule in self.get_command_line_overrides(options, ["INTERNAL"]):
254             exec('cfg.' + rule) # this cannot be factorized because of the exec        
255                
256         # =====================================================================
257         # Load LOCAL config file
258         # search only in the data directory
259         src.pyconf.streamOpener = ConfigOpener([cfg.VARS.datadir])
260         try:
261             local_cfg = src.pyconf.Config(open(os.path.join(cfg.VARS.datadir, 
262                                                            'local.pyconf')),
263                                          PWD = ('LOCAL', cfg.VARS.datadir) )
264         except src.pyconf.ConfigError as e:
265             raise src.SatException(_("Error in configuration file: "
266                                      "local.pyconf\n  %(error)s") % \
267                 {'error': str(e) })
268         except IOError as error:
269             e = str(error)
270             raise src.SatException( e );
271         merger.merge(cfg, local_cfg)
272
273         # When the key is "default", put the default value
274         if cfg.LOCAL.base == "default":
275             cfg.LOCAL.base = os.path.abspath(
276                                         os.path.join(cfg.VARS.salometoolsway,
277                                                      "..",
278                                                      "BASE"))
279         if cfg.LOCAL.workdir == "default":
280             cfg.LOCAL.workdir = os.path.abspath(
281                                         os.path.join(cfg.VARS.salometoolsway,
282                                                      ".."))
283         if cfg.LOCAL.log_dir == "default":
284             cfg.LOCAL.log_dir = os.path.abspath(
285                                         os.path.join(cfg.VARS.salometoolsway,
286                                                      "..",
287                                                      "LOGS"))
288
289         if cfg.LOCAL.archive_dir == "default":
290             cfg.LOCAL.archive_dir = os.path.abspath(
291                                         os.path.join(cfg.VARS.salometoolsway,
292                                                      "..",
293                                                      "ARCHIVES"))
294
295         # apply overwrite from command line if needed
296         for rule in self.get_command_line_overrides(options, ["LOCAL"]):
297             exec('cfg.' + rule) # this cannot be factorized because of the exec
298         
299         # =====================================================================
300         # Load the PROJECTS
301         projects_cfg = src.pyconf.Config()
302         projects_cfg.addMapping("PROJECTS",
303                                 src.pyconf.Mapping(projects_cfg),
304                                 "The projects\n")
305         projects_cfg.PROJECTS.addMapping("projects",
306                                 src.pyconf.Mapping(cfg.PROJECTS),
307                                 "The projects definition\n")
308         
309         for project_pyconf_path in cfg.PROJECTS.project_file_paths:
310             if not os.path.exists(project_pyconf_path):
311                 msg = _("WARNING: The project file %s cannot be found. "
312                         "It will be ignored\n" % project_pyconf_path)
313                 sys.stdout.write(msg)
314                 continue
315             project_name = os.path.basename(
316                                     project_pyconf_path)[:-len(".pyconf")]
317             try:
318                 project_pyconf_dir = os.path.dirname(project_pyconf_path)
319                 project_cfg = src.pyconf.Config(open(project_pyconf_path),
320                                                 PWD=("", project_pyconf_dir))
321             except Exception as e:
322                 msg = _("ERROR: Error in configuration file: "
323                                  "%(file_path)s\n  %(error)s\n") % \
324                             {'file_path' : project_pyconf_path, 'error': str(e) }
325                 sys.stdout.write(msg)
326                 continue
327             projects_cfg.PROJECTS.projects.addMapping(project_name,
328                              src.pyconf.Mapping(projects_cfg.PROJECTS.projects),
329                              "The %s project\n" % project_name)
330             projects_cfg.PROJECTS.projects[project_name]=project_cfg
331             projects_cfg.PROJECTS.projects[project_name]["file_path"] = \
332                                                         project_pyconf_path
333                    
334         merger.merge(cfg, projects_cfg)
335
336         # apply overwrite from command line if needed
337         for rule in self.get_command_line_overrides(options, ["PROJECTS"]):
338             exec('cfg.' + rule) # this cannot be factorized because of the exec
339         
340         # =====================================================================
341         # Create the paths where to search the application configurations, 
342         # the product configurations, the products archives, 
343         # the jobs configurations and the machines configurations
344         cfg.addMapping("PATHS", src.pyconf.Mapping(cfg), "The paths\n")
345         cfg.PATHS["APPLICATIONPATH"] = src.pyconf.Sequence(cfg.PATHS)
346         cfg.PATHS.APPLICATIONPATH.append(cfg.VARS.personal_applications_dir, "")
347
348         
349         cfg.PATHS["PRODUCTPATH"] = src.pyconf.Sequence(cfg.PATHS)
350         cfg.PATHS.PRODUCTPATH.append(cfg.VARS.personal_products_dir, "")
351         cfg.PATHS["ARCHIVEPATH"] = src.pyconf.Sequence(cfg.PATHS)
352         cfg.PATHS.ARCHIVEPATH.append(cfg.VARS.personal_archives_dir, "")
353         cfg.PATHS["ARCHIVEFTP"] = src.pyconf.Sequence(cfg.PATHS)
354         cfg.PATHS["JOBPATH"] = src.pyconf.Sequence(cfg.PATHS)
355         cfg.PATHS.JOBPATH.append(cfg.VARS.personal_jobs_dir, "")
356         cfg.PATHS["MACHINEPATH"] = src.pyconf.Sequence(cfg.PATHS)
357         cfg.PATHS.MACHINEPATH.append(cfg.VARS.personal_machines_dir, "")
358
359         # initialise the path with local directory
360         cfg.PATHS["ARCHIVEPATH"].append(cfg.LOCAL.archive_dir, "")
361
362         # Loop over the projects in order to complete the PATHS variables
363         # as /data/tmpsalome/salome/prerequis/archives for example ARCHIVEPATH
364         for project in cfg.PROJECTS.projects:
365             for PATH in ["APPLICATIONPATH",
366                          "PRODUCTPATH",
367                          "ARCHIVEPATH", #comment this for default archive       #8646
368                          "ARCHIVEFTP",
369                          "JOBPATH",
370                          "MACHINEPATH"]:
371                 if PATH not in cfg.PROJECTS.projects[project]:
372                     continue
373                 cfg.PATHS[PATH].append(cfg.PROJECTS.projects[project][PATH], "")
374         
375         # apply overwrite from command line if needed
376         for rule in self.get_command_line_overrides(options, ["PATHS"]):
377             exec('cfg.' + rule) # this cannot be factorized because of the exec
378
379         # AT END append APPLI_TEST directory in APPLICATIONPATH, for unittest
380         appli_test_dir = os.path.join(satdir, "test", "APPLI_TEST")
381         if appli_test_dir not in cfg.PATHS.APPLICATIONPATH:
382           cfg.PATHS.APPLICATIONPATH.append(appli_test_dir, "unittest APPLI_TEST path")
383
384         # =====================================================================
385         # Load APPLICATION config file
386         if application is not None:
387             # search APPLICATION file in all directories in configPath
388             cp = cfg.PATHS.APPLICATIONPATH
389             src.pyconf.streamOpener = ConfigOpener(cp)
390             do_merge = True
391             try:
392                 application_cfg = src.pyconf.Config(application + '.pyconf')
393             except IOError as e:
394                 raise src.SatException(
395                    _("%s, use 'config --list' to get the list of available applications.") % e)
396             except src.pyconf.ConfigError as e:
397                 if (not ('-e' in parser.parse_args()[1]) 
398                                          or ('--edit' in parser.parse_args()[1]) 
399                                          and command == 'config'):
400                     raise src.SatException(_("Error in configuration file: "
401                                              "%(application)s.pyconf\n "
402                                              " %(error)s") % \
403                         { 'application': application, 'error': str(e) } )
404                 else:
405                     sys.stdout.write(src.printcolors.printcWarning(
406                                         "There is an error in the file"
407                                         " %s.pyconf.\n" % cfg.VARS.application))
408                     do_merge = False
409             except Exception as e:
410                 if (not ('-e' in parser.parse_args()[1]) 
411                                         or ('--edit' in parser.parse_args()[1]) 
412                                         and command == 'config'):
413                     sys.stdout.write(src.printcolors.printcWarning("%s\n" % str(e)))
414                     raise src.SatException(_("Error in configuration file:"
415                                              " %(application)s.pyconf\n") % \
416                         { 'application': application} )
417                 else:
418                     sys.stdout.write(src.printcolors.printcWarning(
419                                 "There is an error in the file"
420                                 " %s.pyconf. Opening the file with the"
421                                 " default viewer\n" % cfg.VARS.application))
422                     sys.stdout.write("The error:"
423                                  " %s\n" % src.printcolors.printcWarning(
424                                                                       str(e)))
425                     do_merge = False
426         
427             else:
428                 cfg['open_application'] = 'yes'
429
430         # =====================================================================
431         # Load product config files in PRODUCTS section
432         products_cfg = src.pyconf.Config()
433         products_cfg.addMapping("PRODUCTS",
434                                 src.pyconf.Mapping(products_cfg),
435                                 "The products\n")
436         if application is not None:
437             src.pyconf.streamOpener = ConfigOpener(cfg.PATHS.PRODUCTPATH)
438             for product_name in application_cfg.APPLICATION.products.keys():
439                 # Loop on all files that are in softsDir directory
440                 # and read their config
441                 product_file_name = product_name + ".pyconf"
442                 product_file_path = src.find_file_in_lpath(product_file_name, cfg.PATHS.PRODUCTPATH)
443                 if product_file_path:
444                     products_dir = os.path.dirname(product_file_path)
445                     try:
446                         prod_cfg = src.pyconf.Config(open(product_file_path),
447                                                      PWD=("", products_dir))
448                         prod_cfg.from_file = product_file_path
449                         products_cfg.PRODUCTS[product_name] = prod_cfg
450                     except Exception as e:
451                         msg = _(
452                             "WARNING: Error in configuration file"
453                             ": %(prod)s\n  %(error)s" % \
454                             {'prod' :  product_name, 'error': str(e) })
455                         sys.stdout.write(msg)
456             
457             merger.merge(cfg, products_cfg)
458             
459             # apply overwrite from command line if needed
460             for rule in self.get_command_line_overrides(options, ["PRODUCTS"]):
461                 exec('cfg.' + rule) # this cannot be factorized because of the exec
462             
463             if do_merge:
464                 merger.merge(cfg, application_cfg)
465
466                 # default launcher name ('salome')
467                 if ('profile' in cfg.APPLICATION and 
468                     'launcher_name' not in cfg.APPLICATION.profile):
469                     cfg.APPLICATION.profile.launcher_name = 'salome'
470
471                 # apply overwrite from command line if needed
472                 for rule in self.get_command_line_overrides(options,
473                                                              ["APPLICATION"]):
474                     # this cannot be factorized because of the exec
475                     exec('cfg.' + rule)
476             
477         # =====================================================================
478         # load USER config
479         self.set_user_config_file(cfg)
480         user_cfg_file = self.get_user_config_file()
481         user_cfg = src.pyconf.Config(open(user_cfg_file))
482         merger.merge(cfg, user_cfg)
483
484         # apply overwrite from command line if needed
485         for rule in self.get_command_line_overrides(options, ["USER"]):
486             exec('cfg.' + rule) # this cannot be factorize because of the exec
487         
488         return cfg
489
490     def set_user_config_file(self, config):
491         '''Set the user config file name and path.
492         If necessary, build it from another one or create it from scratch.
493         
494         :param config class 'src.pyconf.Config': The global config 
495                                                  (containing all pyconf).
496         '''
497         # get the expected name and path of the file
498         self.config_file_name = 'SAT.pyconf'
499         self.user_config_file_path = os.path.join(config.VARS.personalDir,
500                                                    self.config_file_name)
501         
502         # if pyconf does not exist, create it from scratch
503         if not os.path.isfile(self.user_config_file_path): 
504             self.create_config_file(config)
505     
506     def create_config_file(self, config):
507         '''This method is called when there are no user config file. 
508            It build it from scratch.
509         
510         :param config class 'src.pyconf.Config': The global config.
511         :return: the config corresponding to the file created.
512         :rtype: config class 'src.pyconf.Config'
513         '''
514         
515         cfg_name = self.get_user_config_file()
516
517         user_cfg = src.pyconf.Config()
518         #
519         user_cfg.addMapping('USER', src.pyconf.Mapping(user_cfg), "")
520
521         user_cfg.USER.addMapping('cvs_user', config.VARS.user,
522             "This is the user name used to access salome cvs base.\n")
523         user_cfg.USER.addMapping('svn_user', config.VARS.user,
524             "This is the user name used to access salome svn base.\n")
525         user_cfg.USER.addMapping('output_verbose_level', 3,
526             "This is the default output_verbose_level you want."
527             " 0=>no output, 5=>debug.\n")
528         user_cfg.USER.addMapping('publish_dir', 
529                                  os.path.join(os.path.expanduser('~'),
530                                  'websupport', 
531                                  'satreport'), 
532                                  "")
533         user_cfg.USER.addMapping('editor',
534                                  'vi', 
535                                  "This is the editor used to "
536                                  "modify configuration files\n")
537         user_cfg.USER.addMapping('browser', 
538                                  'firefox', 
539                                  "This is the browser used to "
540                                  "read html documentation\n")
541         user_cfg.USER.addMapping('pdf_viewer', 
542                                  'evince', 
543                                  "This is the pdf_viewer used "
544                                  "to read pdf documentation\n")
545 # CNC 25/10/17 : plus nécessaire a priori
546 #        user_cfg.USER.addMapping("base",
547 #                                 src.pyconf.Reference(
548 #                                            user_cfg,
549 #                                            src.pyconf.DOLLAR,
550 #                                            'workdir  + $VARS.sep + "BASE"'),
551 #                                 "The products installation base (could be "
552 #                                 "ignored if this key exists in the local.pyconf"
553 #                                 " file of salomTools).\n")
554                
555         # 
556         src.ensure_path_exists(config.VARS.personalDir)
557         src.ensure_path_exists(os.path.join(config.VARS.personalDir, 
558                                             'Applications'))
559
560         f = open(cfg_name, 'w')
561         user_cfg.__save__(f)
562         f.close()
563
564         return user_cfg   
565
566     def get_user_config_file(self):
567         '''Get the user config file
568         :return: path to the user config file.
569         :rtype: str
570         '''
571         if not self.user_config_file_path:
572             raise src.SatException(_("Error in get_user_config_file: "
573                                      "missing user config file path"))
574         return self.user_config_file_path     
575
576 def check_path(path, ext=[]):
577     '''Construct a text with the input path and "not found" if it does not
578        exist.
579     
580     :param path Str: the path to check.
581     :param ext List: An extension. Verify that the path extension 
582                      is in the list
583     :return: The string of the path with information
584     :rtype: Str
585     '''
586     # check if file exists
587     if not os.path.exists(path):
588         return "'%s'" % path + " " + src.printcolors.printcError(_(
589                                                             "** not found"))
590
591     # check extension
592     if len(ext) > 0:
593         fe = os.path.splitext(path)[1].lower()
594         if fe not in ext:
595             return "'%s'" % path + " " + src.printcolors.printcError(_(
596                                                         "** bad extension"))
597
598     return path
599
600 def show_product_info(config, name, logger):
601     '''Display on the terminal and logger information about a product.
602     
603     :param config Config: the global configuration.
604     :param name Str: The name of the product
605     :param logger Logger: The logger instance to use for the display
606     '''
607     
608     logger.write(_("%s is a product\n") % src.printcolors.printcLabel(name), 2)
609     pinfo = src.product.get_product_config(config, name)
610     
611     if "depend" in pinfo:
612         src.printcolors.print_value(logger, 
613                                     "depends on", 
614                                     ', '.join(pinfo.depend), 2)
615
616     if "opt_depend" in pinfo:
617         src.printcolors.print_value(logger, 
618                                     "optional", 
619                                     ', '.join(pinfo.opt_depend), 2)
620
621     # information on pyconf
622     logger.write("\n", 2)
623     logger.write(src.printcolors.printcLabel("configuration:") + "\n", 2)
624     if "from_file" in pinfo:
625         src.printcolors.print_value(logger, 
626                                     "pyconf file path", 
627                                     pinfo.from_file, 
628                                     2)
629     if "section" in pinfo:
630         src.printcolors.print_value(logger, 
631                                     "section", 
632                                     pinfo.section, 
633                                     2)
634
635     # information on prepare
636     logger.write("\n", 2)
637     logger.write(src.printcolors.printcLabel("prepare:") + "\n", 2)
638
639     is_dev = src.product.product_is_dev(pinfo)
640     method = pinfo.get_source
641     if is_dev:
642         method += " (dev)"
643     src.printcolors.print_value(logger, "get method", method, 2)
644
645     if method == 'cvs':
646         src.printcolors.print_value(logger, "server", pinfo.cvs_info.server, 2)
647         src.printcolors.print_value(logger, "base module",
648                                     pinfo.cvs_info.module_base, 2)
649         src.printcolors.print_value(logger, "source", pinfo.cvs_info.source, 2)
650         src.printcolors.print_value(logger, "tag", pinfo.cvs_info.tag, 2)
651
652     elif method == 'svn':
653         src.printcolors.print_value(logger, "repo", pinfo.svn_info.repo, 2)
654
655     elif method == 'git':
656         src.printcolors.print_value(logger, "repo", pinfo.git_info.repo, 2)
657         src.printcolors.print_value(logger, "tag", pinfo.git_info.tag, 2)
658
659     elif method == 'archive':
660         src.printcolors.print_value(logger, 
661                                     "get from", 
662                                     check_path(pinfo.archive_info.archive_name), 
663                                     2)
664
665     if 'patches' in pinfo:
666         for patch in pinfo.patches:
667             src.printcolors.print_value(logger, "patch", check_path(patch), 2)
668
669     if src.product.product_is_fixed(pinfo):
670         src.printcolors.print_value(logger, "install_dir", 
671                                     check_path(pinfo.install_dir), 2)
672
673     if src.product.product_is_native(pinfo) or src.product.product_is_fixed(pinfo):
674         return
675     
676     # information on compilation
677     if src.product.product_compiles(pinfo):
678         logger.write("\n", 2)
679         logger.write(src.printcolors.printcLabel("compile:") + "\n", 2)
680         src.printcolors.print_value(logger, 
681                                     "compilation method", 
682                                     pinfo.build_source, 
683                                     2)
684         
685         if pinfo.build_source == "script" and "compil_script" in pinfo:
686             src.printcolors.print_value(logger, 
687                                         "Compilation script", 
688                                         pinfo.compil_script, 
689                                         2)
690         
691         if 'nb_proc' in pinfo:
692             src.printcolors.print_value(logger, "make -j", pinfo.nb_proc, 2)
693     
694         src.printcolors.print_value(logger, 
695                                     "source dir", 
696                                     check_path(pinfo.source_dir), 
697                                     2)
698         if 'install_dir' in pinfo:
699             src.printcolors.print_value(logger, 
700                                         "build dir", 
701                                         check_path(pinfo.build_dir), 
702                                         2)
703             src.printcolors.print_value(logger, 
704                                         "install dir", 
705                                         check_path(pinfo.install_dir), 
706                                         2)
707         else:
708             logger.write("  " + 
709                          src.printcolors.printcWarning(_("no install dir")) + 
710                          "\n", 2)
711     else:
712         logger.write("\n", 2)
713         msg = _("This product does not compile")
714         logger.write("%s\n" % msg, 2)
715
716     # information on environment
717     logger.write("\n", 2)
718     logger.write(src.printcolors.printcLabel("environ :") + "\n", 2)
719     if "environ" in pinfo and "env_script" in pinfo.environ:
720         src.printcolors.print_value(logger, 
721                                     "script", 
722                                     check_path(pinfo.environ.env_script), 
723                                     2)
724
725     zz = src.environment.SalomeEnviron(config, 
726                                        src.fileEnviron.ScreenEnviron(logger), 
727                                        False)
728     zz.set_python_libdirs()
729     zz.set_a_product(name, logger)
730     logger.write("\n", 2)
731
732
733 def show_patchs(config, logger):
734   '''Prints all the used patchs in the application.
735
736   :param config Config: the global configuration.
737   :param logger Logger: The logger instance to use for the display
738   '''
739   oneOrMore = False
740   for product in sorted(config.APPLICATION.products):
741     try:
742       product_info = src.product.get_product_config(config, product)
743       if src.product.product_has_patches(product_info):
744         oneOrMore = True
745         logger.write("%s:\n" % product, 1)
746         for i in product_info.patches:
747           logger.write(src.printcolors.printcInfo("    %s\n" % i), 1)
748     except Exception as e:
749       msg = "problem on product %s\n%s\n" % (product, str(e))
750       logger.error(msg)
751
752   if oneOrMore:
753     logger.write("\n", 1)
754   else:
755     logger.write("No patchs found\n", 1)
756
757
758 def show_properties(config, logger):
759   '''Prints all the used properties in the application.
760
761   :param config Config: the global configuration.
762   :param logger Logger: The logger instance to use for the display
763   '''
764   oneOrMore = False
765   for product in sorted(config.APPLICATION.products):
766     try:
767       product_info = src.product.get_product_config(config, product)
768       done = False
769       try:
770         for i in product_info.properties:
771           if not done:
772             logger.write("%s:\n" % product, 1)
773             done = True
774           oneOrMore = True
775           logger.write(src.printcolors.printcInfo("    %s\n" % i), 1)
776       except Exception as e:
777         pass
778     except Exception as e:
779       # logger.write(src.printcolors.printcInfo("    %s\n" % "no properties"), 1)
780       msg = "problem on product %s\n%s\n" % (product, e)
781       logger.error(msg)
782
783   if oneOrMore:
784     logger.write("\n", 1)
785   else:
786     logger.write("No properties found\n", 1)
787
788 def print_value(config, path, show_label, logger, level=0, show_full_path=False):
789     '''Prints a value from the configuration. Prints recursively the values 
790        under the initial path.
791     
792     :param config class 'src.pyconf.Config': The configuration 
793                                              from which the value is displayed.
794     :param path str : the path in the configuration of the value to print.
795     :param show_label boolean: if True, do a basic display. 
796                                (useful for bash completion)
797     :param logger Logger: the logger instance
798     :param level int: The number of spaces to add before display.
799     :param show_full_path :
800     '''            
801     
802     # Make sure that the path does not ends with a point
803     if path.endswith('.'):
804         path = path[:-1]
805     
806     # display all the path or not
807     if show_full_path:
808         vname = path
809     else:
810         vname = path.split('.')[-1]
811
812     # number of spaces before the display
813     tab_level = "  " * level
814     
815     # call to the function that gets the value of the path.
816     try:
817         val = config.getByPath(path)
818     except Exception as e:
819         logger.write(tab_level)
820         logger.write("%s: ERROR %s\n" % (src.printcolors.printcLabel(vname), 
821                                          src.printcolors.printcError(str(e))))
822         return
823
824     # in this case, display only the value
825     if show_label:
826         logger.write(tab_level)
827         logger.write("%s: " % src.printcolors.printcLabel(vname))
828
829     # The case where the value has under values, 
830     # do a recursive call to the function
831     if dir(val).__contains__('keys'):
832         if show_label: logger.write("\n")
833         for v in sorted(val.keys()):
834             print_value(config, path + '.' + v, show_label, logger, level + 1)
835     elif val.__class__ == src.pyconf.Sequence or isinstance(val, list): 
836         # in this case, value is a list (or a Sequence)
837         if show_label: logger.write("\n")
838         index = 0
839         for v in val:
840             print_value(config, path + "[" + str(index) + "]", 
841                         show_label, logger, level + 1)
842             index = index + 1
843     else: # case where val is just a str
844         logger.write("%s\n" % val)
845
846 def get_config_children(config, args):
847     '''Gets the names of the children of the given parameter.
848        Useful only for completion mechanism
849     
850     :param config Config: The configuration where to read the values
851     :param args: The path in the config from which get the keys
852     '''
853     vals = []
854     rootkeys = config.keys()
855     
856     if len(args) == 0:
857         # no parameter returns list of root keys
858         vals = rootkeys
859     else:
860         parent = args[0]
861         pos = parent.rfind('.')
862         if pos < 0:
863             # Case where there is only on key as parameter.
864             # For example VARS
865             vals = [m for m in rootkeys if m.startswith(parent)]
866         else:
867             # Case where there is a part from a key
868             # for example VARS.us  (for VARS.user)
869             head = parent[0:pos]
870             tail = parent[pos+1:]
871             try:
872                 a = config.getByPath(head)
873                 if dir(a).__contains__('keys'):
874                     vals = map(lambda x: head + '.' + x,
875                                [m for m in a.keys() if m.startswith(tail)])
876             except:
877                 pass
878
879     for v in sorted(vals):
880         sys.stdout.write("%s\n" % v)
881
882 def description():
883     '''method that is called when salomeTools is called with --help option.
884     
885     :return: The text to display for the config command description.
886     :rtype: str
887     '''
888     return _("The config command allows manipulation "
889              "and operation on config files.\n\nexample:\nsat config "
890              "SALOME-master --info ParaView")
891     
892
893 def run(args, runner, logger):
894     '''method that is called when salomeTools is called with config parameter.
895     '''
896     # Parse the options
897     (options, args) = parser.parse_args(args)
898
899     # Only useful for completion mechanism : print the keys of the config
900     if options.schema:
901         get_config_children(runner.cfg, args)
902         return
903
904     # case : print a value of the config
905     if options.value:
906         if options.value == ".":
907             # if argument is ".", print all the config
908             for val in sorted(runner.cfg.keys()):
909                 print_value(runner.cfg, val, not options.no_label, logger)
910         else:
911             print_value(runner.cfg, options.value, not options.no_label, logger, 
912                         level=0, show_full_path=False)
913     
914     # case : print a debug value of the config
915     if options.debug:
916         if options.debug == ".":
917             # if argument is ".", print all the config
918             res = DBG.indent(DBG.getStrConfigDbg(runner.cfg))
919             logger.write("\nConfig of application %s:\n\n%s\n" % (runner.cfg.VARS.application, res))
920         else:
921             if options.debug[0] == ".": # accept ".PRODUCT.etc" as "PRODUCT.etc"
922               od = options.debug[1:]
923             else:
924               od = options.debug
925             exec("a = runner.cfg.%s" % od)
926             res = DBG.indent(DBG.getStrConfigDbg(a))
927             logger.write("\nConfig.%s of application %s:\n\n%s\n" % (od, runner.cfg.VARS.application, res))
928
929     
930     # case : edit user pyconf file or application file
931     if options.edit:
932         editor = runner.cfg.USER.editor
933         if ('APPLICATION' not in runner.cfg and
934                        'open_application' not in runner.cfg): # edit user pyconf
935             usercfg = os.path.join(runner.cfg.VARS.personalDir, 
936                                    'SAT.pyconf')
937             logger.write(_("Opening %s\n" % usercfg), 3)
938             src.system.show_in_editor(editor, usercfg, logger)
939         else:
940             # search for file <application>.pyconf and open it
941             for path in runner.cfg.PATHS.APPLICATIONPATH:
942                 pyconf_path = os.path.join(path, 
943                                     runner.cfg.VARS.application + ".pyconf")
944                 if os.path.exists(pyconf_path):
945                     logger.write(_("Opening %s\n" % pyconf_path), 3)
946                     src.system.show_in_editor(editor, pyconf_path, logger)
947                     break
948     
949     # case : give information about the product(s) in parameter
950     if options.products:
951       if options.info is not None:
952         logger.warning('options.products %s overrides options.info %s' % (options.products, options.info))
953       options.info = options.products
954
955     if options.info:
956       # DBG.write("products", sorted(runner.cfg.APPLICATION.products.keys()), True)
957       src.check_config_has_application(runner.cfg)
958       taggedProducts = src.getProductNames(runner.cfg, options.info, logger)
959       DBG.write("tagged products", sorted(taggedProducts))
960       for prod in sorted(taggedProducts):
961         if prod in runner.cfg.APPLICATION.products:
962           try:
963             if len(taggedProducts) > 1:
964               logger.write("#################### ", 2)
965             show_product_info(runner.cfg, prod, logger)
966           except Exception as e:
967             msg = "problem on product %s\n%s\n" % (prod, str(e))
968             logger.error(msg)
969           # return
970         else:
971           msg = _("%s is not a product of %s.\n") % \
972                 (prod, runner.cfg.VARS.application)
973           logger.warning(msg)
974           #raise Exception(msg)
975     
976     # case : copy an existing <application>.pyconf 
977     # to ~/.salomeTools/Applications/LOCAL_<application>.pyconf
978     if options.copy:
979         # product is required
980         src.check_config_has_application( runner.cfg )
981
982         # get application file path 
983         source = runner.cfg.VARS.application + '.pyconf'
984         source_full_path = ""
985         for path in runner.cfg.PATHS.APPLICATIONPATH:
986             # ignore personal directory
987             if path == runner.cfg.VARS.personalDir:
988                 continue
989             # loop on all directories that can have pyconf applications
990             zz = os.path.join(path, source)
991             if os.path.exists(zz):
992                 source_full_path = zz
993                 break
994
995         if len(source_full_path) == 0:
996             raise src.SatException(_(
997                         "Config file for product %s not found\n") % source)
998         else:
999             if len(args) > 0:
1000                 # a name is given as parameter, use it
1001                 dest = args[0]
1002             elif 'copy_prefix' in runner.cfg.INTERNAL.config:
1003                 # use prefix
1004                 dest = (runner.cfg.INTERNAL.config.copy_prefix 
1005                         + runner.cfg.VARS.application)
1006             else:
1007                 # use same name as source
1008                 dest = runner.cfg.VARS.application
1009                 
1010             # the full path
1011             dest_file = os.path.join(runner.cfg.VARS.personalDir, 
1012                                      'Applications', dest + '.pyconf')
1013             if os.path.exists(dest_file):
1014                 raise src.SatException(_("A personal application"
1015                                          " '%s' already exists") % dest)
1016             
1017             # perform the copy
1018             shutil.copyfile(source_full_path, dest_file)
1019             logger.write(_("%s has been created.\n") % dest_file)
1020     
1021     # case : display all the available pyconf applications
1022     if options.list:
1023         lproduct = list()
1024         # search in all directories that can have pyconf applications
1025         for path in runner.cfg.PATHS.APPLICATIONPATH:
1026             # print a header
1027             if not options.no_label:
1028                 logger.write("------ %s\n" % src.printcolors.printcHeader(path))
1029
1030             if not os.path.exists(path):
1031                 logger.write(src.printcolors.printcError(_(
1032                                             "Directory not found")) + "\n")
1033             else:
1034                 for f in sorted(os.listdir(path)):
1035                     # ignore file that does not ends with .pyconf
1036                     if not f.endswith('.pyconf'):
1037                         continue
1038
1039                     appliname = f[:-len('.pyconf')]
1040                     if appliname not in lproduct:
1041                         lproduct.append(appliname)
1042                         if path.startswith(runner.cfg.VARS.personalDir) \
1043                                     and not options.no_label:
1044                             logger.write("%s*\n" % appliname)
1045                         else:
1046                             logger.write("%s\n" % appliname)
1047                             
1048             logger.write("\n")
1049
1050     # case: print all the products name of the application (internal use for completion)
1051     if options.completion:
1052         for product_name in runner.cfg.APPLICATION.products.keys():
1053             logger.write("%s\n" % product_name)
1054         
1055     # case : give a synthetic view of all patches used in the application
1056     if options.show_patchs:
1057         src.check_config_has_application(runner.cfg)
1058         # Print some informations
1059         logger.write(_('Patchs of application %s\n') %
1060                     src.printcolors.printcLabel(runner.cfg.VARS.application), 3)
1061         logger.write("\n", 2, False)
1062         show_patchs(runner.cfg, logger)
1063
1064     # case : give a synthetic view of all patches used in the application
1065     if options.show_properties:
1066         src.check_config_has_application(runner.cfg)
1067         # Print some informations
1068         logger.write(_('Properties of application %s\n') %
1069                     src.printcolors.printcLabel(runner.cfg.VARS.application), 3)
1070         logger.write("\n", 2, False)
1071         show_properties(runner.cfg, logger)
1072