Salome HOME
sat #18868 : complément pour pouvoir produite et utiliser des archives produites...
[tools/sat.git] / commands / package.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 stat
21 import shutil
22 import datetime
23 import tarfile
24 import codecs
25 import string
26 import glob
27 import pprint as PP
28 import sys
29 import src
30
31 from application import get_SALOME_modules
32 import src.debug as DBG
33
34 old_python = sys.version_info[0] == 2 and sys.version_info[1] <= 6
35
36 BINARY = "binary"
37 SOURCE = "Source"
38 PROJECT = "Project"
39 SAT = "Sat"
40
41 ARCHIVE_DIR = "ARCHIVES"
42 PROJECT_DIR = "PROJECT"
43
44 IGNORED_DIRS = [".git", ".svn"]
45 IGNORED_EXTENSIONS = []
46
47 PACKAGE_EXT=".tar.gz" # the extension we use for the packages
48
49 PROJECT_TEMPLATE = """#!/usr/bin/env python
50 #-*- coding:utf-8 -*-
51
52 # The path to the archive root directory
53 root_path : $PWD + "/../"
54 # path to the PROJECT
55 project_path : $PWD + "/"
56
57 # Where to search the archives of the products
58 ARCHIVEPATH : $root_path + "ARCHIVES"
59 # Where to search the pyconf of the applications
60 APPLICATIONPATH : $project_path + "applications/"
61 # Where to search the pyconf of the products
62 PRODUCTPATH : $project_path + "products/"
63 # Where to search the pyconf of the jobs of the project
64 JOBPATH : $project_path + "jobs/"
65 # Where to search the pyconf of the machines of the project
66 MACHINEPATH : $project_path + "machines/"
67 """
68
69 LOCAL_TEMPLATE = ("""#!/usr/bin/env python
70 #-*- coding:utf-8 -*-
71
72   LOCAL :
73   {
74     base : 'default'
75     workdir : 'default'
76     log_dir : 'default'
77     archive_dir : 'default'
78     VCS : 'unknown'
79     tag : 'unknown'
80   }
81
82 PROJECTS :
83 {
84 project_file_paths : [$VARS.salometoolsway + $VARS.sep + \"..\" + $VARS.sep"""
85 """ + \"""" + PROJECT_DIR + """\" + $VARS.sep + "project.pyconf"]
86 }
87 """)
88
89 # Define all possible option for the package command :  sat package <options>
90 parser = src.options.Options()
91 parser.add_option('b', 'binaries', 'boolean', 'binaries',
92     _('Optional: Produce a binary package.'), False)
93 parser.add_option('f', 'force_creation', 'boolean', 'force_creation',
94     _('Optional: Only binary package: produce the archive even if '
95       'there are some missing products.'), False)
96 parser.add_option('s', 'sources', 'boolean', 'sources',
97     _('Optional: Produce a compilable archive of the sources of the '
98       'application.'), False)
99 parser.add_option('', 'with_vcs', 'boolean', 'with_vcs',
100     _('Optional: Do not make archive for products in VCS mode (git, cvs, svn). ' 
101       'Sat prepare will use VCS mode instead to retrieve them'),
102     False)
103 parser.add_option('', 'ftp', 'boolean', 'ftp',
104     _('Optional: Do not embed archives for products in archive mode.' 
105     'Sat prepare will use ftp instead to retrieve them'),
106     False)
107 parser.add_option('e', 'exe', 'string', 'exe',
108     _('Optional: Produce an extra launcher based upon the exe given as argument.'), "")
109 parser.add_option('p', 'project', 'string', 'project',
110     _('Optional: Produce an archive that contains a project.'), "")
111 parser.add_option('t', 'salometools', 'boolean', 'sat',
112     _('Optional: Produce an archive that contains salomeTools.'), False)
113 parser.add_option('n', 'name', 'string', 'name',
114     _('Optional: The name or full path of the archive.'), None)
115 parser.add_option('', 'add_files', 'list2', 'add_files',
116     _('Optional: The list of additional files to add to the archive.'), [])
117 parser.add_option('', 'without_properties', 'properties', 'without_properties',
118     _('Optional: Filter the products by their properties.\n\tSyntax: '
119       '--without_properties <property>:<value>'))
120
121
122 def add_files(tar, name_archive, d_content, logger, f_exclude=None):
123     '''Create an archive containing all directories and files that are given in
124        the d_content argument.
125     
126     :param tar tarfile: The tarfile instance used to make the archive.
127     :param name_archive str: The name of the archive to make.
128     :param d_content dict: The dictionary that contain all directories and files
129                            to add in the archive.
130                            d_content[label] = 
131                                         (path_on_local_machine, path_in_archive)
132     :param logger Logger: the logging instance
133     :param f_exclude Function: the function that filters
134     :return: 0 if success, 1 if not.
135     :rtype: int
136     '''
137     # get the max length of the messages in order to make the display
138     max_len = len(max(d_content.keys(), key=len))
139     
140     success = 0
141     # loop over each directory or file stored in the d_content dictionary
142     names = sorted(d_content.keys())
143     DBG.write("add tar names", names)
144
145     # used to avoid duplications (for pip install in python, or single_install_dir cases)
146     already_added=set() 
147     for name in names:
148         # display information
149         len_points = max_len - len(name) + 3
150         local_path, archive_path = d_content[name]
151         in_archive = os.path.join(name_archive, archive_path)
152         logger.write(name + " " + len_points * "." + " "+ in_archive + " ", 3)
153         # Get the local path and the path in archive 
154         # of the directory or file to add
155         # Add it in the archive
156         try:
157             key=local_path+"->"+in_archive
158             if key not in already_added:
159                 if old_python:
160                     tar.add(local_path,
161                                  arcname=in_archive,
162                                  exclude=exclude_VCS_and_extensions_26)
163                 else:
164                     tar.add(local_path,
165                                  arcname=in_archive,
166                                  filter=exclude_VCS_and_extensions)
167                 already_added.add(key)
168             logger.write(src.printcolors.printcSuccess(_("OK")), 3)
169         except Exception as e:
170             logger.write(src.printcolors.printcError(_("KO ")), 3)
171             logger.write(str(e), 3)
172             success = 1
173         logger.write("\n", 3)
174     return success
175
176
177 def exclude_VCS_and_extensions_26(filename):
178     ''' The function that is used to exclude from package the link to the 
179         VCS repositories (like .git) (only for python 2.6)
180
181     :param filename Str: The filname to exclude (or not).
182     :return: True if the file has to be exclude
183     :rtype: Boolean
184     '''
185     for dir_name in IGNORED_DIRS:
186         if dir_name in filename:
187             return True
188     for extension in IGNORED_EXTENSIONS:
189         if filename.endswith(extension):
190             return True
191     return False
192
193 def exclude_VCS_and_extensions(tarinfo):
194     ''' The function that is used to exclude from package the link to the 
195         VCS repositories (like .git)
196
197     :param filename Str: The filname to exclude (or not).
198     :return: None if the file has to be exclude
199     :rtype: tarinfo or None
200     '''
201     filename = tarinfo.name
202     for dir_name in IGNORED_DIRS:
203         if dir_name in filename:
204             return None
205     for extension in IGNORED_EXTENSIONS:
206         if filename.endswith(extension):
207             return None
208     return tarinfo
209
210 def produce_relative_launcher(config,
211                               logger,
212                               file_dir,
213                               file_name,
214                               binaries_dir_name):
215     '''Create a specific SALOME launcher for the binary package. This launcher 
216        uses relative paths.
217     
218     :param config Config: The global configuration.
219     :param logger Logger: the logging instance
220     :param file_dir str: the directory where to put the launcher
221     :param file_name str: The launcher name
222     :param binaries_dir_name str: the name of the repository where the binaries
223                                   are, in the archive.
224     :return: the path of the produced launcher
225     :rtype: str
226     '''
227     
228     # set base mode to "no" for the archive - save current mode to restore it at the end
229     if "base" in config.APPLICATION:
230         base_setting=config.APPLICATION.base 
231     else:
232         base_setting="maybe"
233     config.APPLICATION.base="no"
234
235     # get KERNEL installation path 
236     kernel_info = src.product.get_product_config(config, "KERNEL")
237     kernel_base_name=os.path.basename(kernel_info.install_dir)
238     if kernel_info.install_mode == "base":
239         # case of kernel installed in base. the kernel install dir name is different in the archive
240         kernel_base_name=os.path.basename(os.path.dirname(kernel_info.install_dir))
241     
242     kernel_root_dir = os.path.join(binaries_dir_name, kernel_base_name)
243
244     # set kernel bin dir (considering fhs property)
245     kernel_cfg = src.product.get_product_config(config, "KERNEL")
246     if src.get_property_in_product_cfg(kernel_cfg, "fhs"):
247         bin_kernel_install_dir = os.path.join(kernel_root_dir,"bin") 
248     else:
249         bin_kernel_install_dir = os.path.join(kernel_root_dir,"bin","salome") 
250
251     # check if the application contains an application module
252     # check also if the application has a distene product, 
253     # in this case get its licence file name
254     l_product_info = src.product.get_products_infos(config.APPLICATION.products.keys(), config)
255     salome_application_name="Not defined" 
256     distene_licence_file_name=False
257     for prod_name, prod_info in l_product_info:
258         # look for a "salome application" and a distene product
259         if src.get_property_in_product_cfg(prod_info, "is_distene") == "yes":
260             distene_licence_file_name = src.product.product_has_licence(prod_info, 
261                                             config.PATHS.LICENCEPATH) 
262         if src.get_property_in_product_cfg(prod_info, "is_salome_application") == "yes":
263             salome_application_name=prod_info.name
264
265     # if the application contains an application module, we set ABSOLUTE_APPLI_PATH to it
266     # if not we set it to KERNEL_INSTALL_DIR, which is sufficient, except for salome test
267     if salome_application_name == "Not defined":
268         app_root_dir=kernel_root_dir
269     else:
270         app_root_dir=os.path.join(binaries_dir_name, salome_application_name)
271
272     additional_env={}
273     additional_env['sat_bin_kernel_install_dir'] = "out_dir_Path + " +\
274                                                    config.VARS.sep + bin_kernel_install_dir
275     if "python3" in config.APPLICATION and config.APPLICATION.python3 == "yes":
276         additional_env['sat_python_version'] = 3
277     else:
278         additional_env['sat_python_version'] = 2
279
280     additional_env['ABSOLUTE_APPLI_PATH'] = "out_dir_Path" + config.VARS.sep + app_root_dir
281
282     # create an environment file writer
283     writer = src.environment.FileEnvWriter(config,
284                                            logger,
285                                            file_dir,
286                                            src_root=None,
287                                            env_info=None)
288     
289     filepath = os.path.join(file_dir, file_name)
290     # Write
291     writer.write_env_file(filepath,
292                           False,  # for launch
293                           "cfgForPy",
294                           additional_env=additional_env,
295                           no_path_init="False",
296                           for_package = binaries_dir_name)
297     
298     # Little hack to put out_dir_Path outside the strings
299     src.replace_in_file(filepath, 'r"out_dir_Path', 'out_dir_Path + r"' )
300     src.replace_in_file(filepath, "r'out_dir_Path + ", "out_dir_Path + r'" )
301     
302     # A hack to put a call to a file for distene licence.
303     # It does nothing to an application that has no distene product
304     if distene_licence_file_name:
305         logger.write("Application has a distene licence file! We use it in package launcher", 5)
306         hack_for_distene_licence(filepath, distene_licence_file_name)
307        
308     # change the rights in order to make the file executable for everybody
309     os.chmod(filepath,
310              stat.S_IRUSR |
311              stat.S_IRGRP |
312              stat.S_IROTH |
313              stat.S_IWUSR |
314              stat.S_IXUSR |
315              stat.S_IXGRP |
316              stat.S_IXOTH)
317
318     # restore modified setting by its initial value
319     config.APPLICATION.base=base_setting
320
321     return filepath
322
323 def hack_for_distene_licence(filepath, licence_file):
324     '''Replace the distene licence env variable by a call to a file.
325     
326     :param filepath Str: The path to the launcher to modify.
327     '''  
328     shutil.move(filepath, filepath + "_old")
329     fileout= filepath
330     filein = filepath + "_old"
331     fin = open(filein, "r")
332     fout = open(fileout, "w")
333     text = fin.readlines()
334     # Find the Distene section
335     num_line = -1
336     for i,line in enumerate(text):
337         if "# Set DISTENE License" in line:
338             num_line = i
339             break
340     if num_line == -1:
341         # No distene product, there is nothing to do
342         fin.close()
343         for line in text:
344             fout.write(line)
345         fout.close()
346         return
347     del text[num_line +1]
348     del text[num_line +1]
349     text_to_insert ="""    try:
350         distene_licence_file=r"%s"
351         if sys.version_info[0] >= 3 and sys.version_info[1] >= 5:
352             import importlib.util
353             spec_dist = importlib.util.spec_from_file_location("distene_licence", distene_licence_file)
354             distene=importlib.util.module_from_spec(spec_dist)
355             spec_dist.loader.exec_module(distene)
356         else:
357             import imp
358             distene = imp.load_source('distene_licence', distene_licence_file)
359         distene.set_distene_variables(context)
360     except:
361         pass\n"""  % licence_file
362     text.insert(num_line + 1, text_to_insert)
363     for line in text:
364         fout.write(line)
365     fin.close()    
366     fout.close()
367     return
368     
369 def produce_relative_env_files(config,
370                               logger,
371                               file_dir,
372                               binaries_dir_name,
373                               exe_name=None):
374     '''Create some specific environment files for the binary package. These 
375        files use relative paths.
376     
377     :param config Config: The global configuration.
378     :param logger Logger: the logging instance
379     :param file_dir str: the directory where to put the files
380     :param binaries_dir_name str: the name of the repository where the binaries
381                                   are, in the archive.
382     :param exe_name str: if given generate a launcher executing exe_name
383     :return: the list of path of the produced environment files
384     :rtype: List
385     '''  
386
387     # set base mode to "no" for the archive - save current mode to restore it at the end
388     if "base" in config.APPLICATION:
389         base_setting=config.APPLICATION.base 
390     else:
391         base_setting="maybe"
392     config.APPLICATION.base="no"
393
394     # create an environment file writer
395     writer = src.environment.FileEnvWriter(config,
396                                            logger,
397                                            file_dir,
398                                            src_root=None)
399     
400     if src.architecture.is_windows():
401       shell = "bat"
402       filename  = "env_launch.bat"
403     else:
404       shell = "bash"
405       filename  = "env_launch.sh"
406
407     if exe_name:
408         filename=os.path.basename(exe_name)
409
410     # Write
411     filepath = writer.write_env_file(filename,
412                           False, # for launch
413                           shell,
414                           for_package = binaries_dir_name)
415
416     # Little hack to put out_dir_Path as environment variable
417     if src.architecture.is_windows() :
418       src.replace_in_file(filepath, '"out_dir_Path', '"%out_dir_Path%' )
419       src.replace_in_file(filepath, '=out_dir_Path', '=%out_dir_Path%' )
420       src.replace_in_file(filepath, ';out_dir_Path', ';%out_dir_Path%' )
421     else:
422       src.replace_in_file(filepath, '"out_dir_Path', '"${out_dir_Path}' )
423       src.replace_in_file(filepath, ':out_dir_Path', ':${out_dir_Path}' )
424
425     if exe_name:
426         if src.architecture.is_windows():
427             cmd="\n\nrem Launch exe with user arguments\n%s " % exe_name + "%*"
428         else:
429             cmd='\n\n# Launch exe with user arguments\n%s "$*"' % exe_name
430         with open(filepath, "a") as exe_launcher:
431             exe_launcher.write(cmd)
432
433     # change the rights in order to make the file executable for everybody
434     os.chmod(filepath,
435              stat.S_IRUSR |
436              stat.S_IRGRP |
437              stat.S_IROTH |
438              stat.S_IWUSR |
439              stat.S_IXUSR |
440              stat.S_IXGRP |
441              stat.S_IXOTH)
442     
443     # restore modified setting by its initial value
444     config.APPLICATION.base=base_setting
445
446     return filepath
447
448 def produce_install_bin_file(config,
449                              logger,
450                              file_dir,
451                              d_sub,
452                              file_name):
453     '''Create a bash shell script which do substitutions in BIRARIES dir 
454        in order to use it for extra compilations.
455     
456     :param config Config: The global configuration.
457     :param logger Logger: the logging instance
458     :param file_dir str: the directory where to put the files
459     :param d_sub, dict: the dictionnary that contains the substitutions to be done
460     :param file_name str: the name of the install script file
461     :return: the produced file
462     :rtype: str
463     '''  
464     # Write
465     filepath = os.path.join(file_dir, file_name)
466     # open the file and write into it
467     # use codec utf-8 as sat variables are in unicode
468     with codecs.open(filepath, "w", 'utf-8') as installbin_file:
469         installbin_template_path = os.path.join(config.VARS.internal_dir,
470                                         "INSTALL_BIN.template")
471         
472         # build the name of the directory that will contain the binaries
473         binaries_dir_name = config.INTERNAL.config.binary_dir + config.VARS.dist
474         # build the substitution loop
475         loop_cmd = "for f in $(grep -RIl"
476         for key in d_sub:
477             loop_cmd += " -e "+ key
478         loop_cmd += ' ' + config.INTERNAL.config.install_dir +\
479                     '); do\n     sed -i "\n'
480         for key in d_sub:
481             loop_cmd += "        s?" + key + "?$(pwd)/" + d_sub[key] + "?g\n"
482         loop_cmd += '            " $f\ndone'
483
484         d={}
485         d["BINARIES_DIR"] = binaries_dir_name
486         d["SUBSTITUTION_LOOP"]=loop_cmd
487         d["INSTALL_DIR"]=config.INTERNAL.config.install_dir
488         
489         # substitute the template and write it in file
490         content=src.template.substitute(installbin_template_path, d)
491         installbin_file.write(content)
492         # change the rights in order to make the file executable for everybody
493         os.chmod(filepath,
494                  stat.S_IRUSR |
495                  stat.S_IRGRP |
496                  stat.S_IROTH |
497                  stat.S_IWUSR |
498                  stat.S_IXUSR |
499                  stat.S_IXGRP |
500                  stat.S_IXOTH)
501     
502     return filepath
503
504 def product_appli_creation_script(config,
505                                   logger,
506                                   file_dir,
507                                   binaries_dir_name):
508     '''Create a script that can produce an application (EDF style) in the binary
509        package.
510     
511     :param config Config: The global configuration.
512     :param logger Logger: the logging instance
513     :param file_dir str: the directory where to put the file
514     :param binaries_dir_name str: the name of the repository where the binaries
515                                   are, in the archive.
516     :return: the path of the produced script file
517     :rtype: Str
518     '''
519     template_name = "create_appli.py.for_bin_packages.template"
520     template_path = os.path.join(config.VARS.internal_dir, template_name)
521     text_to_fill = open(template_path, "r").read()
522     text_to_fill = text_to_fill.replace("TO BE FILLED 1",
523                                         '"' + binaries_dir_name + '"')
524     
525     text_to_add = ""
526     for product_name in get_SALOME_modules(config):
527         product_info = src.product.get_product_config(config, product_name)
528        
529         if src.product.product_is_smesh_plugin(product_info):
530             continue
531
532         if 'install_dir' in product_info and bool(product_info.install_dir):
533             if src.product.product_is_cpp(product_info):
534                 # cpp module
535                 for cpp_name in src.product.get_product_components(product_info):
536                     line_to_add = ("<module name=\"" + 
537                                    cpp_name + 
538                                    "\" gui=\"yes\" path=\"''' + "
539                                    "os.path.join(dir_bin_name, \"" + 
540                                    cpp_name + "\") + '''\"/>")
541             else:
542                 # regular module
543                 line_to_add = ("<module name=\"" + 
544                                product_name + 
545                                "\" gui=\"yes\" path=\"''' + "
546                                "os.path.join(dir_bin_name, \"" + 
547                                product_name + "\") + '''\"/>")
548             text_to_add += line_to_add + "\n"
549     
550     filled_text = text_to_fill.replace("TO BE FILLED 2", text_to_add)
551     
552     tmp_file_path = os.path.join(file_dir, "create_appli.py")
553     ff = open(tmp_file_path, "w")
554     ff.write(filled_text)
555     ff.close()
556     
557     # change the rights in order to make the file executable for everybody
558     os.chmod(tmp_file_path,
559              stat.S_IRUSR |
560              stat.S_IRGRP |
561              stat.S_IROTH |
562              stat.S_IWUSR |
563              stat.S_IXUSR |
564              stat.S_IXGRP |
565              stat.S_IXOTH)
566     
567     return tmp_file_path
568
569 def binary_package(config, logger, options, tmp_working_dir):
570     '''Prepare a dictionary that stores all the needed directories and files to
571        add in a binary package.
572     
573     :param config Config: The global configuration.
574     :param logger Logger: the logging instance
575     :param options OptResult: the options of the launched command
576     :param tmp_working_dir str: The temporary local directory containing some 
577                                 specific directories or files needed in the 
578                                 binary package
579     :return: the dictionary that stores all the needed directories and files to
580              add in a binary package.
581              {label : (path_on_local_machine, path_in_archive)}
582     :rtype: dict
583     '''
584
585     # Get the list of product installation to add to the archive
586     l_products_name = sorted(config.APPLICATION.products.keys())
587     l_product_info = src.product.get_products_infos(l_products_name,
588                                                     config)
589
590     # suppress compile time products for binaries-only archives
591     if not options.sources:
592         update_config(config, logger, "compile_time", "yes")
593
594     l_install_dir = []
595     l_source_dir = []
596     l_not_installed = []
597     l_sources_not_present = []
598     generate_mesa_launcher = False  # a flag to know if we generate a mesa launcher
599     if ("APPLICATION" in config  and
600         "properties"  in config.APPLICATION  and
601         "mesa_launcher_in_package"    in config.APPLICATION.properties  and
602         config.APPLICATION.properties.mesa_launcher_in_package == "yes") :
603             generate_mesa_launcher=True
604
605     # first loop on products : filter products, analyse properties,
606     # and store the information that will be used to create the archive in the second loop 
607     for prod_name, prod_info in l_product_info:
608         # skip product with property not_in_package set to yes
609         if src.get_property_in_product_cfg(prod_info, "not_in_package") == "yes":
610             continue  
611
612         # Add the sources of the products that have the property 
613         # sources_in_package : "yes"
614         if src.get_property_in_product_cfg(prod_info,
615                                            "sources_in_package") == "yes":
616             if os.path.exists(prod_info.source_dir):
617                 l_source_dir.append((prod_name, prod_info.source_dir))
618             else:
619                 l_sources_not_present.append(prod_name)
620
621         # ignore the native and fixed products for install directories
622         if (src.product.product_is_native(prod_info) 
623                 or src.product.product_is_fixed(prod_info)
624                 or not src.product.product_compiles(prod_info)):
625             continue
626         # 
627         # products with single_dir property will be installed in the PRODUCTS directory of the archive
628         is_single_dir=(src.appli_test_property(config,"single_install_dir", "yes") and \
629                        src.product.product_test_property(prod_info,"single_install_dir", "yes"))
630         if src.product.check_installation(config, prod_info):
631             l_install_dir.append((prod_name, prod_info.name, prod_info.install_dir,
632                                   is_single_dir, prod_info.install_mode))
633         else:
634             l_not_installed.append(prod_name)
635         
636         # Add also the cpp generated modules (if any)
637         if src.product.product_is_cpp(prod_info):
638             # cpp module
639             for name_cpp in src.product.get_product_components(prod_info):
640                 install_dir = os.path.join(config.APPLICATION.workdir,
641                                            config.INTERNAL.config.install_dir,
642                                            name_cpp) 
643                 if os.path.exists(install_dir):
644                     l_install_dir.append((name_cpp, name_cpp, install_dir, False, "value"))
645                 else:
646                     l_not_installed.append(name_cpp)
647         
648     # check the name of the directory that (could) contains the binaries 
649     # from previous detar
650     binaries_from_detar = os.path.join(
651                               config.APPLICATION.workdir,
652                               config.INTERNAL.config.binary_dir + config.VARS.dist)
653     if os.path.exists(binaries_from_detar):
654          logger.write("""
655 WARNING: existing binaries directory from previous detar installation:
656          %s
657          To make new package from this, you have to: 
658          1) install binaries in INSTALL directory with the script "install_bin.sh" 
659             see README file for more details
660          2) or recompile everything in INSTALL with "sat compile" command 
661             this step is long, and requires some linux packages to be installed 
662             on your system\n
663 """ % binaries_from_detar)
664     
665     # Print warning or error if there are some missing products
666     if len(l_not_installed) > 0:
667         text_missing_prods = ""
668         for p_name in l_not_installed:
669             text_missing_prods += " - " + p_name + "\n"
670         if not options.force_creation:
671             msg = _("ERROR: there are missing product installations:")
672             logger.write("%s\n%s" % (src.printcolors.printcError(msg),
673                                      text_missing_prods),
674                          1)
675             raise src.SatException(msg)
676         else:
677             msg = _("WARNING: there are missing products installations:")
678             logger.write("%s\n%s" % (src.printcolors.printcWarning(msg),
679                                      text_missing_prods),
680                          1)
681
682     # Do the same for sources
683     if len(l_sources_not_present) > 0:
684         text_missing_prods = ""
685         for p_name in l_sources_not_present:
686             text_missing_prods += "-" + p_name + "\n"
687         if not options.force_creation:
688             msg = _("ERROR: there are missing product sources:")
689             logger.write("%s\n%s" % (src.printcolors.printcError(msg),
690                                      text_missing_prods),
691                          1)
692             raise src.SatException(msg)
693         else:
694             msg = _("WARNING: there are missing products sources:")
695             logger.write("%s\n%s" % (src.printcolors.printcWarning(msg),
696                                      text_missing_prods),
697                          1)
698  
699     # construct the name of the directory that will contain the binaries
700     if src.architecture.is_windows():
701         binaries_dir_name = config.INTERNAL.config.binary_dir
702     else:
703         binaries_dir_name = config.INTERNAL.config.binary_dir + config.VARS.dist
704     # construct the correlation table between the product names, there 
705     # actual install directories and there install directory in archive
706     d_products = {}
707     for prod_name, prod_info_name, install_dir, is_single_dir, install_mode in l_install_dir:
708         prod_base_name=os.path.basename(install_dir)
709         if install_mode == "base":
710             # case of a products installed in base. 
711             # because the archive is in base:no mode, the name of the install dir is different inside archive
712             # we set it to the product name or by PRODUCTS if single-dir
713             if is_single_dir:
714                 prod_base_name=config.INTERNAL.config.single_install_dir
715             else:
716                 prod_base_name=prod_info_name
717         path_in_archive = os.path.join(binaries_dir_name, prod_base_name)
718         d_products[prod_name + " (bin)"] = (install_dir, path_in_archive)
719         
720     for prod_name, source_dir in l_source_dir:
721         path_in_archive = os.path.join("SOURCES", prod_name)
722         d_products[prod_name + " (sources)"] = (source_dir, path_in_archive)
723
724     # for packages of SALOME applications including KERNEL, 
725     # we produce a salome launcher or a virtual application (depending on salome version)
726     if 'KERNEL' in config.APPLICATION.products:
727         VersionSalome = src.get_salome_version(config)
728         # Case where SALOME has the launcher that uses the SalomeContext API
729         if VersionSalome >= 730:
730             # create the relative launcher and add it to the files to add
731             launcher_name = src.get_launcher_name(config)
732             launcher_package = produce_relative_launcher(config,
733                                                  logger,
734                                                  tmp_working_dir,
735                                                  launcher_name,
736                                                  binaries_dir_name)
737             d_products["launcher"] = (launcher_package, launcher_name)
738
739             # if the application contains mesa products, we generate in addition to the 
740             # classical salome launcher a launcher using mesa and called mesa_salome 
741             # (the mesa launcher will be used for remote usage through ssh).
742             if generate_mesa_launcher:
743                 #if there is one : store the use_mesa property
744                 restore_use_mesa_option=None
745                 if ('properties' in config.APPLICATION and 
746                     'use_mesa' in config.APPLICATION.properties):
747                     restore_use_mesa_option = config.APPLICATION.properties.use_mesa
748
749                 # activate mesa property, and generate a mesa launcher
750                 src.activate_mesa_property(config)  #activate use_mesa property
751                 launcher_mesa_name="mesa_"+launcher_name
752                 launcher_package_mesa = produce_relative_launcher(config,
753                                                      logger,
754                                                      tmp_working_dir,
755                                                      launcher_mesa_name,
756                                                      binaries_dir_name)
757                 d_products["launcher (mesa)"] = (launcher_package_mesa, launcher_mesa_name)
758
759                 # if there was a use_mesa value, we restore it
760                 # else we set it to the default value "no"
761                 if restore_use_mesa_option != None:
762                     config.APPLICATION.properties.use_mesa=restore_use_mesa_option
763                 else:
764                     config.APPLICATION.properties.use_mesa="no"
765
766             if options.sources:
767                 # if we mix binaries and sources, we add a copy of the launcher, 
768                 # prefixed  with "bin",in order to avoid clashes
769                 d_products["launcher (copy)"] = (launcher_package, "bin"+launcher_name)
770         else:
771             # Provide a script for the creation of an application EDF style
772             appli_script = product_appli_creation_script(config,
773                                                         logger,
774                                                         tmp_working_dir,
775                                                         binaries_dir_name)
776             
777             d_products["appli script"] = (appli_script, "create_appli.py")
778
779     # Put also the environment file
780     env_file = produce_relative_env_files(config,
781                                            logger,
782                                            tmp_working_dir,
783                                            binaries_dir_name)
784
785     if src.architecture.is_windows():
786       filename  = "env_launch.bat"
787     else:
788       filename  = "env_launch.sh"
789     d_products["environment file"] = (env_file, filename)      
790
791     # If option exe, produce an extra launcher based on specified exe
792     if options.exe:
793         exe_file = produce_relative_env_files(config,
794                                               logger,
795                                               tmp_working_dir,
796                                               binaries_dir_name,
797                                               options.exe)
798             
799         if src.architecture.is_windows():
800           filename  = os.path.basename(options.exe) + ".bat"
801         else:
802           filename  = os.path.basename(options.exe) + ".sh"
803         d_products["exe file"] = (exe_file, filename)      
804     
805
806     return d_products
807
808 def source_package(sat, config, logger, options, tmp_working_dir):
809     '''Prepare a dictionary that stores all the needed directories and files to
810        add in a source package.
811     
812     :param config Config: The global configuration.
813     :param logger Logger: the logging instance
814     :param options OptResult: the options of the launched command
815     :param tmp_working_dir str: The temporary local directory containing some 
816                                 specific directories or files needed in the 
817                                 binary package
818     :return: the dictionary that stores all the needed directories and files to
819              add in a source package.
820              {label : (path_on_local_machine, path_in_archive)}
821     :rtype: dict
822     '''
823     
824     d_archives={}
825     # Get all the products that are prepared using an archive
826     # unless ftp mode is specified (in this case the user of the
827     # archive will get the sources through the ftp mode of sat prepare
828     if not options.ftp:
829         logger.write("Find archive products ... ")
830         d_archives, l_pinfo_vcs = get_archives(config, logger)
831         logger.write("Done\n")
832
833     d_archives_vcs = {}
834     if not options.with_vcs and len(l_pinfo_vcs) > 0:
835         # Make archives with the products that are not prepared using an archive
836         # (git, cvs, svn, etc)
837         logger.write("Construct archives for vcs products ... ")
838         d_archives_vcs = get_archives_vcs(l_pinfo_vcs,
839                                           sat,
840                                           config,
841                                           logger,
842                                           tmp_working_dir)
843         logger.write("Done\n")
844
845     # Create a project
846     logger.write("Create the project ... ")
847     d_project = create_project_for_src_package(config,
848                                                tmp_working_dir,
849                                                options.with_vcs,
850                                                options.ftp)
851     logger.write("Done\n")
852     
853     # Add salomeTools
854     tmp_sat = add_salomeTools(config, tmp_working_dir)
855     d_sat = {"salomeTools" : (tmp_sat, "salomeTools")}
856     
857     # Add a sat symbolic link if not win
858     if not src.architecture.is_windows():
859         tmp_satlink_path = os.path.join(tmp_working_dir, 'sat')
860         try:
861             t = os.getcwd()
862         except:
863             # In the jobs, os.getcwd() can fail
864             t = config.LOCAL.workdir
865         os.chdir(tmp_working_dir)
866         if os.path.lexists(tmp_satlink_path):
867             os.remove(tmp_satlink_path)
868         os.symlink(os.path.join('salomeTools', 'sat'), 'sat')
869         os.chdir(t)
870         
871         d_sat["sat link"] = (tmp_satlink_path, "sat")
872     
873     d_source = src.merge_dicts(d_archives, d_archives_vcs, d_project, d_sat)
874     return d_source
875
876 def get_archives(config, logger):
877     '''Find all the products that are get using an archive and all the products
878        that are get using a vcs (git, cvs, svn) repository.
879     
880     :param config Config: The global configuration.
881     :param logger Logger: the logging instance
882     :return: the dictionary {name_product : 
883              (local path of its archive, path in the package of its archive )}
884              and the list of specific configuration corresponding to the vcs 
885              products
886     :rtype: (Dict, List)
887     '''
888     # Get the list of product informations
889     l_products_name = config.APPLICATION.products.keys()
890     l_product_info = src.product.get_products_infos(l_products_name,
891                                                     config)
892     d_archives = {}
893     l_pinfo_vcs = []
894     for p_name, p_info in l_product_info:
895         # skip product with property not_in_package set to yes
896         if src.get_property_in_product_cfg(p_info, "not_in_package") == "yes":
897             continue  
898         # ignore the native and fixed products
899         if (src.product.product_is_native(p_info) 
900                 or src.product.product_is_fixed(p_info)):
901             continue
902         if p_info.get_source == "archive":
903             archive_path = p_info.archive_info.archive_name
904             archive_name = os.path.basename(archive_path)
905             d_archives[p_name] = (archive_path,
906                                   os.path.join(ARCHIVE_DIR, archive_name))
907             if (src.appli_test_property(config,"pip", "yes") and 
908                 src.product.product_test_property(p_info,"pip", "yes")):
909                 # if pip mode is activated, and product is managed by pip
910                 pip_wheels_dir=os.path.join(config.LOCAL.archive_dir,"wheels")
911                 pip_wheel_pattern=os.path.join(pip_wheels_dir, 
912                     "%s-%s*" % (p_info.name, p_info.version))
913                 pip_wheel_path=glob.glob(pip_wheel_pattern)
914                 msg_pip_not_found="Error in get_archive, pip wheel for "\
915                                   "product %s-%s was not found in %s directory"
916                 msg_pip_two_or_more="Error in get_archive, several pip wheels for "\
917                                   "product %s-%s were found in %s directory"
918                 if len(pip_wheel_path)==0:
919                     raise src.SatException(msg_pip_not_found %\
920                         (p_info.name, p_info.version, pip_wheels_dir))
921                 if len(pip_wheel_path)>1:
922                     raise src.SatException(msg_pip_two_or_more %\
923                         (p_info.name, p_info.version, pip_wheels_dir))
924
925                 pip_wheel_name=os.path.basename(pip_wheel_path[0])
926                 d_archives[p_name+" (pip wheel)"]=(pip_wheel_path[0], 
927                     os.path.join(ARCHIVE_DIR, "wheels", pip_wheel_name))
928         else:
929             # this product is not managed by archive, 
930             # an archive of the vcs directory will be created by get_archive_vcs
931             l_pinfo_vcs.append((p_name, p_info)) 
932             
933     return d_archives, l_pinfo_vcs
934
935 def add_salomeTools(config, tmp_working_dir):
936     '''Prepare a version of salomeTools that has a specific local.pyconf file 
937        configured for a source package.
938
939     :param config Config: The global configuration.
940     :param tmp_working_dir str: The temporary local directory containing some 
941                                 specific directories or files needed in the 
942                                 source package
943     :return: The path to the local salomeTools directory to add in the package
944     :rtype: str
945     '''
946     # Copy sat in the temporary working directory
947     sat_tmp_path = src.Path(os.path.join(tmp_working_dir, "salomeTools"))
948     sat_running_path = src.Path(config.VARS.salometoolsway)
949     sat_running_path.copy(sat_tmp_path)
950     
951     # Update the local.pyconf file that contains the path to the project
952     local_pyconf_name = "local.pyconf"
953     local_pyconf_dir = os.path.join(tmp_working_dir, "salomeTools", "data")
954     local_pyconf_file = os.path.join(local_pyconf_dir, local_pyconf_name)
955     # Remove the .pyconf file in the root directory of salomeTools if there is
956     # any. (For example when launching jobs, a pyconf file describing the jobs 
957     # can be here and is not useful) 
958     files_or_dir_SAT = os.listdir(os.path.join(tmp_working_dir, "salomeTools"))
959     for file_or_dir in files_or_dir_SAT:
960         if file_or_dir.endswith(".pyconf") or file_or_dir.endswith(".txt"):
961             file_path = os.path.join(tmp_working_dir,
962                                      "salomeTools",
963                                      file_or_dir)
964             os.remove(file_path)
965     
966     ff = open(local_pyconf_file, "w")
967     ff.write(LOCAL_TEMPLATE)
968     ff.close()
969     
970     return sat_tmp_path.path
971
972 def get_archives_vcs(l_pinfo_vcs, sat, config, logger, tmp_working_dir):
973     '''For sources package that require that all products are get using an 
974        archive, one has to create some archive for the vcs products.
975        So this method calls the clean and source command of sat and then create
976        the archives.
977
978     :param l_pinfo_vcs List: The list of specific configuration corresponding to
979                              each vcs product
980     :param sat Sat: The Sat instance that can be called to clean and source the
981                     products
982     :param config Config: The global configuration.
983     :param logger Logger: the logging instance
984     :param tmp_working_dir str: The temporary local directory containing some 
985                                 specific directories or files needed in the 
986                                 source package
987     :return: the dictionary that stores all the archives to add in the source 
988              package. {label : (path_on_local_machine, path_in_archive)}
989     :rtype: dict
990     '''
991     # clean the source directory of all the vcs products, then use the source 
992     # command and thus construct an archive that will not contain the patches
993     l_prod_names = [pn for pn, __ in l_pinfo_vcs]
994     if False: # clean is dangerous in user/SOURCES, fixed in tmp_local_working_dir
995       logger.write(_("\nclean sources\n"))
996       args_clean = config.VARS.application
997       args_clean += " --sources --products "
998       args_clean += ",".join(l_prod_names)
999       logger.write("WARNING: get_archives_vcs clean\n         '%s'\n" % args_clean, 1)
1000       sat.clean(args_clean, batch=True, verbose=0, logger_add_link = logger)
1001     if True:
1002       # source
1003       logger.write(_("get sources\n"))
1004       args_source = config.VARS.application
1005       args_source += " --products "
1006       args_source += ",".join(l_prod_names)
1007       svgDir = sat.cfg.APPLICATION.workdir
1008       tmp_local_working_dir = os.path.join(sat.cfg.APPLICATION.workdir, "tmp_package")  # to avoid too much big files in /tmp
1009       sat.cfg.APPLICATION.workdir = tmp_local_working_dir
1010       # DBG.write("SSS sat config.APPLICATION.workdir", sat.cfg.APPLICATION, True)
1011       # DBG.write("sat config id", id(sat.cfg), True)
1012       # shit as config is not same id() as for sat.source()
1013       # sat.source(args_source, batch=True, verbose=5, logger_add_link = logger)
1014       import source
1015       source.run(args_source, sat, logger) #use this mode as runner.cfg reference
1016       
1017       # make the new archives
1018       d_archives_vcs = {}
1019       for pn, pinfo in l_pinfo_vcs:
1020           path_archive = make_archive(pn, pinfo, tmp_local_working_dir)
1021           logger.write("make archive vcs '%s'\n" % path_archive)
1022           d_archives_vcs[pn] = (path_archive,
1023                                 os.path.join(ARCHIVE_DIR, pn + ".tgz"))
1024       sat.cfg.APPLICATION.workdir = svgDir
1025       # DBG.write("END sat config", sat.cfg.APPLICATION, True)
1026     return d_archives_vcs
1027
1028 def make_archive(prod_name, prod_info, where):
1029     '''Create an archive of a product by searching its source directory.
1030
1031     :param prod_name str: The name of the product.
1032     :param prod_info Config: The specific configuration corresponding to the 
1033                              product
1034     :param where str: The path of the repository where to put the resulting 
1035                       archive
1036     :return: The path of the resulting archive
1037     :rtype: str
1038     '''
1039     path_targz_prod = os.path.join(where, prod_name + PACKAGE_EXT)
1040     tar_prod = tarfile.open(path_targz_prod, mode='w:gz')
1041     local_path = prod_info.source_dir
1042     if old_python:
1043         tar_prod.add(local_path,
1044                      arcname=prod_name,
1045                      exclude=exclude_VCS_and_extensions_26)
1046     else:
1047         tar_prod.add(local_path,
1048                      arcname=prod_name,
1049                      filter=exclude_VCS_and_extensions)
1050     tar_prod.close()
1051     return path_targz_prod       
1052
1053 def create_project_for_src_package(config, tmp_working_dir, with_vcs, with_ftp):
1054     '''Create a specific project for a source package.
1055
1056     :param config Config: The global configuration.
1057     :param tmp_working_dir str: The temporary local directory containing some 
1058                                 specific directories or files needed in the 
1059                                 source package
1060     :param with_vcs boolean: True if the package is with vcs products (not 
1061                              transformed into archive products)
1062     :param with_ftp boolean: True if the package use ftp servers to get archives
1063     :return: The dictionary 
1064              {"project" : (produced project, project path in the archive)}
1065     :rtype: Dict
1066     '''
1067
1068     # Create in the working temporary directory the full project tree
1069     project_tmp_dir = os.path.join(tmp_working_dir, PROJECT_DIR)
1070     products_pyconf_tmp_dir = os.path.join(project_tmp_dir,
1071                                          "products")
1072     compil_scripts_tmp_dir = os.path.join(project_tmp_dir,
1073                                          "products",
1074                                          "compil_scripts")
1075     env_scripts_tmp_dir = os.path.join(project_tmp_dir,
1076                                          "products",
1077                                          "env_scripts")
1078     patches_tmp_dir = os.path.join(project_tmp_dir,
1079                                          "products",
1080                                          "patches")
1081     application_tmp_dir = os.path.join(project_tmp_dir,
1082                                          "applications")
1083     for directory in [project_tmp_dir,
1084                       compil_scripts_tmp_dir,
1085                       env_scripts_tmp_dir,
1086                       patches_tmp_dir,
1087                       application_tmp_dir]:
1088         src.ensure_path_exists(directory)
1089
1090     # Create the pyconf that contains the information of the project
1091     project_pyconf_name = "project.pyconf"        
1092     project_pyconf_file = os.path.join(project_tmp_dir, project_pyconf_name)
1093     ff = open(project_pyconf_file, "w")
1094     ff.write(PROJECT_TEMPLATE)
1095     if with_ftp and len(config.PATHS.ARCHIVEFTP) > 0:
1096         ftp_path='ARCHIVEFTP : "'+config.PATHS.ARCHIVEFTP[0]
1097         for ftpserver in config.PATHS.ARCHIVEFTP[1:]:
1098             ftp_path=ftp_path+":"+ftpserver
1099         ftp_path+='"'
1100         ff.write("# ftp servers where to search for prerequisite archives\n")
1101         ff.write(ftp_path)
1102     # add licence paths if any
1103     if len(config.PATHS.LICENCEPATH) > 0:  
1104         licence_path='LICENCEPATH : "'+config.PATHS.LICENCEPATH[0]
1105         for path in config.PATHS.LICENCEPATH[1:]:
1106             licence_path=licence_path+":"+path
1107         licence_path+='"'
1108         ff.write("\n# Where to search for licences\n")
1109         ff.write(licence_path)
1110         
1111
1112     ff.close()
1113     
1114     # Loop over the products to get there pyconf and all the scripts 
1115     # (compilation, environment, patches)
1116     # and create the pyconf file to add to the project
1117     lproducts_name = config.APPLICATION.products.keys()
1118     l_products = src.product.get_products_infos(lproducts_name, config)
1119     for p_name, p_info in l_products:
1120         # skip product with property not_in_package set to yes
1121         if src.get_property_in_product_cfg(p_info, "not_in_package") == "yes":
1122             continue  
1123         find_product_scripts_and_pyconf(p_name,
1124                                         p_info,
1125                                         config,
1126                                         with_vcs,
1127                                         compil_scripts_tmp_dir,
1128                                         env_scripts_tmp_dir,
1129                                         patches_tmp_dir,
1130                                         products_pyconf_tmp_dir)
1131     
1132     # for the application pyconf, we write directly the config
1133     # don't search for the original pyconf file
1134     # to avoid problems with overwrite sections and rm_products key
1135     write_application_pyconf(config, application_tmp_dir)
1136     
1137     d_project = {"project" : (project_tmp_dir, PROJECT_DIR )}
1138     return d_project
1139
1140 def find_product_scripts_and_pyconf(p_name,
1141                                     p_info,
1142                                     config,
1143                                     with_vcs,
1144                                     compil_scripts_tmp_dir,
1145                                     env_scripts_tmp_dir,
1146                                     patches_tmp_dir,
1147                                     products_pyconf_tmp_dir):
1148     '''Create a specific pyconf file for a given product. Get its environment 
1149        script, its compilation script and patches and put it in the temporary
1150        working directory. This method is used in the source package in order to
1151        construct the specific project.
1152
1153     :param p_name str: The name of the product.
1154     :param p_info Config: The specific configuration corresponding to the 
1155                              product
1156     :param config Config: The global configuration.
1157     :param with_vcs boolean: True if the package is with vcs products (not 
1158                              transformed into archive products)
1159     :param compil_scripts_tmp_dir str: The path to the temporary compilation 
1160                                        scripts directory of the project.
1161     :param env_scripts_tmp_dir str: The path to the temporary environment script 
1162                                     directory of the project.
1163     :param patches_tmp_dir str: The path to the temporary patch scripts 
1164                                 directory of the project.
1165     :param products_pyconf_tmp_dir str: The path to the temporary product 
1166                                         scripts directory of the project.
1167     '''
1168     
1169     # read the pyconf of the product
1170     product_pyconf_cfg = src.pyconf.Config(p_info.from_file)
1171
1172     # find the compilation script if any
1173     if src.product.product_has_script(p_info):
1174         compil_script_path = src.Path(p_info.compil_script)
1175         compil_script_path.copy(compil_scripts_tmp_dir)
1176
1177     # find the environment script if any
1178     if src.product.product_has_env_script(p_info):
1179         env_script_path = src.Path(p_info.environ.env_script)
1180         env_script_path.copy(env_scripts_tmp_dir)
1181
1182     # find the patches if any
1183     if src.product.product_has_patches(p_info):
1184         patches = src.pyconf.Sequence()
1185         for patch_path in p_info.patches:
1186             p_path = src.Path(patch_path)
1187             p_path.copy(patches_tmp_dir)
1188             patches.append(os.path.basename(patch_path), "")
1189
1190     if (not with_vcs) and src.product.product_is_vcs(p_info):
1191         # in non vcs mode, if the product is not archive, then make it become archive.
1192
1193         # depending upon the incremental mode, select impacted sections
1194         if "properties" in p_info and "incremental" in p_info.properties and\
1195             p_info.properties.incremental == "yes":
1196             sections = ["default", "default_win", p_info.section, p_info.section+"_win"]
1197         else:
1198             sections = [p_info.section]
1199         for section in sections:
1200             if section in product_pyconf_cfg and "get_source" in product_pyconf_cfg[section]:
1201                 DBG.write("sat package set archive mode to archive for product %s and section %s" %\
1202                           (p_name,section))
1203                 product_pyconf_cfg[section].get_source = "archive"
1204                 if not "archive_info" in product_pyconf_cfg[section]:
1205                     product_pyconf_cfg[section].addMapping("archive_info",
1206                                         src.pyconf.Mapping(product_pyconf_cfg),
1207                                         "")
1208                     product_pyconf_cfg[section].archive_info.archive_name =\
1209                         p_info.name + ".tgz"
1210     
1211     if (with_vcs) and src.product.product_is_vcs(p_info):
1212         # in vcs mode we must replace explicitely the git server url
1213         # (or it will not be found later because project files are not exported in archives)
1214         for section in product_pyconf_cfg:
1215             # replace in all sections of the product pyconf the git repo definition by its substitued value (found in p_info)
1216             if "git_info" in product_pyconf_cfg[section]:
1217                 for repo in product_pyconf_cfg[section].git_info:
1218                     if repo in p_info.git_info:
1219                         product_pyconf_cfg[section].git_info[repo] =  p_info.git_info[repo]
1220
1221     # write the pyconf file to the temporary project location
1222     product_tmp_pyconf_path = os.path.join(products_pyconf_tmp_dir,
1223                                            p_name + ".pyconf")
1224     ff = open(product_tmp_pyconf_path, 'w')
1225     ff.write("#!/usr/bin/env python\n#-*- coding:utf-8 -*-\n\n")
1226     product_pyconf_cfg.__save__(ff, 1)
1227     ff.close()
1228
1229
1230 def write_application_pyconf(config, application_tmp_dir):
1231     '''Write the application pyconf file in the specific temporary 
1232        directory containing the specific project of a source package.
1233
1234     :param config Config: The global configuration.
1235     :param application_tmp_dir str: The path to the temporary application 
1236                                     scripts directory of the project.
1237     '''
1238     application_name = config.VARS.application
1239     # write the pyconf file to the temporary application location
1240     application_tmp_pyconf_path = os.path.join(application_tmp_dir,
1241                                                application_name + ".pyconf")
1242     with open(application_tmp_pyconf_path, 'w') as f:
1243         f.write("#!/usr/bin/env python\n#-*- coding:utf-8 -*-\n\n")
1244         res = src.pyconf.Config()
1245         app = src.pyconf.deepCopyMapping(config.APPLICATION)
1246
1247         # set base mode to "no" for the archive
1248         app.base = "no"
1249
1250         # Change the workdir
1251         app.workdir = src.pyconf.Reference(
1252                                  app,
1253                                  src.pyconf.DOLLAR,
1254                                  'VARS.salometoolsway + $VARS.sep + ".."')
1255         res.addMapping("APPLICATION", app, "")
1256         res.__save__(f, evaluated=False)
1257     
1258
1259 def sat_package(config, tmp_working_dir, options, logger):
1260     '''Prepare a dictionary that stores all the needed directories and files to
1261        add in a salomeTool package.
1262     
1263     :param tmp_working_dir str: The temporary local working directory 
1264     :param options OptResult: the options of the launched command
1265     :return: the dictionary that stores all the needed directories and files to
1266              add in a salomeTool package.
1267              {label : (path_on_local_machine, path_in_archive)}
1268     :rtype: dict
1269     '''
1270     d_project = {}
1271
1272     # we include sat himself
1273     d_project["all_sat"]=(config.VARS.salometoolsway, "")
1274
1275     # and we overwrite local.pyconf with a clean wersion.
1276     local_pyconf_tmp_path = os.path.join(tmp_working_dir, "local.pyconf")
1277     local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
1278     local_cfg = src.pyconf.Config(local_file_path)
1279     local_cfg.PROJECTS.project_file_paths=src.pyconf.Sequence(local_cfg.PROJECTS)
1280     local_cfg.LOCAL["base"] = "default"
1281     local_cfg.LOCAL["workdir"] = "default"
1282     local_cfg.LOCAL["log_dir"] = "default"
1283     local_cfg.LOCAL["archive_dir"] = "default"
1284     local_cfg.LOCAL["VCS"] = "None"
1285     local_cfg.LOCAL["tag"] = src.get_salometool_version(config)
1286
1287     # if the archive contains a project, we write its relative path in local.pyconf
1288     if options.project:
1289         project_arch_path = os.path.join("projects", options.project, 
1290                                          os.path.basename(options.project_file_path))
1291         local_cfg.PROJECTS.project_file_paths.append(project_arch_path, "")
1292
1293     ff = open(local_pyconf_tmp_path, 'w')
1294     local_cfg.__save__(ff, 1)
1295     ff.close()
1296     d_project["local.pyconf"]=(local_pyconf_tmp_path, "data/local.pyconf")
1297     return d_project
1298     
1299
1300 def project_package(config, name_project, project_file_path, ftp_mode, tmp_working_dir, embedded_in_sat, logger):
1301     '''Prepare a dictionary that stores all the needed directories and files to
1302        add in a project package.
1303     
1304     :param project_file_path str: The path to the local project.
1305     :param ftp_mode boolean: Do not embed archives, the archive will rely on ftp mode to retrieve them.
1306     :param tmp_working_dir str: The temporary local directory containing some 
1307                                 specific directories or files needed in the 
1308                                 project package
1309     :param embedded_in_sat boolean : the project package is embedded in a sat package
1310     :return: the dictionary that stores all the needed directories and files to
1311              add in a project package.
1312              {label : (path_on_local_machine, path_in_archive)}
1313     :rtype: dict
1314     '''
1315     d_project = {}
1316     # Read the project file and get the directories to add to the package
1317     
1318     try: 
1319       project_pyconf_cfg = config.PROJECTS.projects.__getattr__(name_project)
1320     except:
1321       logger.write("""
1322 WARNING: inexisting config.PROJECTS.projects.%s, try to read now from:\n%s\n""" % (name_project, project_file_path))
1323       project_pyconf_cfg = src.pyconf.Config(project_file_path)
1324       project_pyconf_cfg.PWD = os.path.dirname(project_file_path)
1325     
1326     paths = {"APPLICATIONPATH" : "applications",
1327              "PRODUCTPATH" : "products",
1328              "JOBPATH" : "jobs",
1329              "MACHINEPATH" : "machines"}
1330     if not ftp_mode:
1331         paths["ARCHIVEPATH"] = "archives"
1332
1333     # Loop over the project paths and add it
1334     project_file_name = os.path.basename(project_file_path)
1335     for path in paths:
1336         if path not in project_pyconf_cfg:
1337             continue
1338         if embedded_in_sat:
1339             dest_path = os.path.join("projects", name_project, paths[path])
1340             project_file_dest = os.path.join("projects", name_project, project_file_name)
1341         else:
1342             dest_path = paths[path]
1343             project_file_dest = project_file_name
1344
1345         # Add the directory to the files to add in the package
1346         d_project[path] = (project_pyconf_cfg[path], dest_path)
1347
1348         # Modify the value of the path in the package
1349         project_pyconf_cfg[path] = src.pyconf.Reference(
1350                                     project_pyconf_cfg,
1351                                     src.pyconf.DOLLAR,
1352                                     'project_path + "/' + paths[path] + '"')
1353     
1354     # Modify some values
1355     if "project_path" not in project_pyconf_cfg:
1356         project_pyconf_cfg.addMapping("project_path",
1357                                       src.pyconf.Mapping(project_pyconf_cfg),
1358                                       "")
1359     project_pyconf_cfg.project_path = src.pyconf.Reference(project_pyconf_cfg,
1360                                                            src.pyconf.DOLLAR,
1361                                                            'PWD')
1362     # we don't want to export these two fields
1363     project_pyconf_cfg.__delitem__("file_path")
1364     project_pyconf_cfg.__delitem__("PWD")
1365     if ftp_mode:
1366         project_pyconf_cfg.__delitem__("ARCHIVEPATH")
1367     
1368     # Write the project pyconf file
1369     project_pyconf_tmp_path = os.path.join(tmp_working_dir, project_file_name)
1370     ff = open(project_pyconf_tmp_path, 'w')
1371     ff.write("#!/usr/bin/env python\n#-*- coding:utf-8 -*-\n\n")
1372     project_pyconf_cfg.__save__(ff, 1)
1373     ff.close()
1374     d_project["Project hat file"] = (project_pyconf_tmp_path, project_file_dest)
1375     
1376     return d_project
1377
1378 def add_readme(config, options, where):
1379     readme_path = os.path.join(where, "README")
1380     with codecs.open(readme_path, "w", 'utf-8') as f:
1381
1382     # templates for building the header
1383         readme_header="""
1384 # This package was generated with sat $version
1385 # Date: $date
1386 # User: $user
1387 # Distribution : $dist
1388
1389 In the following, $$ROOT represents the directory where you have installed 
1390 SALOME (the directory where this file is located).
1391
1392 """
1393         if src.architecture.is_windows():
1394             readme_header = readme_header.replace('$$ROOT','%ROOT%')
1395         readme_compilation_with_binaries="""
1396
1397 compilation based on the binaries used as prerequisites
1398 =======================================================
1399
1400 If you fail to compile the complete application (for example because
1401 you are not root on your system and cannot install missing packages), you
1402 may try a partial compilation based on the binaries.
1403 For that it is necessary to copy the binaries from BINARIES to INSTALL,
1404 and do some substitutions on cmake and .la files (replace the build directories
1405 with local paths).
1406 The procedure to do it is:
1407  1) Remove or rename INSTALL directory if it exists
1408  2) Execute the shell script install_bin.sh:
1409  > cd $ROOT
1410  > ./install_bin.sh
1411  3) Use SalomeTool (as explained in Sources section) and compile only the 
1412     modules you need to (with -p option)
1413
1414 """
1415         readme_header_tpl=string.Template(readme_header)
1416         readme_template_path_bin = os.path.join(config.VARS.internal_dir,
1417                 "README_BIN.template")
1418         readme_template_path_bin_launcher = os.path.join(config.VARS.internal_dir,
1419                 "README_LAUNCHER.template")
1420         readme_template_path_bin_virtapp = os.path.join(config.VARS.internal_dir,
1421                 "README_BIN_VIRTUAL_APP.template")
1422         readme_template_path_src = os.path.join(config.VARS.internal_dir,
1423                 "README_SRC.template")
1424         readme_template_path_pro = os.path.join(config.VARS.internal_dir,
1425                 "README_PROJECT.template")
1426         readme_template_path_sat = os.path.join(config.VARS.internal_dir,
1427                 "README_SAT.template")
1428
1429         # prepare substitution dictionary
1430         d = dict()
1431         d['user'] = config.VARS.user
1432         d['date'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
1433         d['version'] = src.get_salometool_version(config)
1434         d['dist'] = config.VARS.dist
1435         f.write(readme_header_tpl.substitute(d)) # write the general header (common)
1436
1437         if options.binaries or options.sources:
1438             d['application'] = config.VARS.application
1439             d['BINARIES']    = config.INTERNAL.config.binary_dir
1440             d['SEPARATOR'] = config.VARS.sep
1441             if src.architecture.is_windows():
1442                 d['operatingSystem'] = 'Windows'
1443                 d['PYTHON3'] = 'python3'
1444                 d['ROOT']    = '%ROOT%'
1445             else:
1446                 d['operatingSystem'] = 'Linux'
1447                 d['PYTHON3'] = ''
1448                 d['ROOT']    = '$ROOT'
1449             f.write("# Application: " + d['application'] + "\n")
1450             if 'KERNEL' in config.APPLICATION.products:
1451                 VersionSalome = src.get_salome_version(config)
1452                 # Case where SALOME has the launcher that uses the SalomeContext API
1453                 if VersionSalome >= 730:
1454                     d['launcher'] = config.APPLICATION.profile.launcher_name
1455                 else:
1456                     d['virtual_app'] = 'runAppli' # this info is not used now)
1457
1458         # write the specific sections
1459         if options.binaries:
1460             f.write(src.template.substitute(readme_template_path_bin, d))
1461             if "virtual_app" in d:
1462                 f.write(src.template.substitute(readme_template_path_bin_virtapp, d))
1463             if "launcher" in d:
1464                 f.write(src.template.substitute(readme_template_path_bin_launcher, d))
1465
1466         if options.sources:
1467             f.write(src.template.substitute(readme_template_path_src, d))
1468
1469         if options.binaries and options.sources and not src.architecture.is_windows():
1470             f.write(readme_compilation_with_binaries)
1471
1472         if options.project:
1473             f.write(src.template.substitute(readme_template_path_pro, d))
1474
1475         if options.sat:
1476             f.write(src.template.substitute(readme_template_path_sat, d))
1477     
1478     return readme_path
1479
1480 def update_config(config, logger,  prop, value):
1481     '''Remove from config.APPLICATION.products the products that have the property given as input.
1482     
1483     :param config Config: The global config.
1484     :param prop str: The property to filter
1485     :param value str: The value of the property to filter
1486     '''
1487     # if there is no APPLICATION (ex sat package -t) : nothing to do
1488     if "APPLICATION" in config:
1489         l_product_to_remove = []
1490         for product_name in config.APPLICATION.products.keys():
1491             prod_cfg = src.product.get_product_config(config, product_name)
1492             if src.get_property_in_product_cfg(prod_cfg, prop) == value:
1493                 l_product_to_remove.append(product_name)
1494         for product_name in l_product_to_remove:
1495             config.APPLICATION.products.__delitem__(product_name)
1496             logger.write("Remove product %s with property %s\n" % (product_name, prop), 5)
1497
1498 def description():
1499     '''method that is called when salomeTools is called with --help option.
1500     
1501     :return: The text to display for the package command description.
1502     :rtype: str
1503     '''
1504     return _("""
1505 The package command creates a tar file archive of a product.
1506 There are four kinds of archive, which can be mixed:
1507
1508  1 - The binary archive. 
1509      It contains the product installation directories plus a launcher.
1510  2 - The sources archive. 
1511      It contains the product archives, a project (the application plus salomeTools).
1512  3 - The project archive. 
1513      It contains a project (give the project file path as argument).
1514  4 - The salomeTools archive. 
1515      It contains code utility salomeTools.
1516
1517 example:
1518  >> sat package SALOME-master --binaries --sources""")
1519   
1520 def run(args, runner, logger):
1521     '''method that is called when salomeTools is called with package parameter.
1522     '''
1523     
1524     # Parse the options
1525     (options, args) = parser.parse_args(args)
1526
1527     # Check that a type of package is called, and only one
1528     all_option_types = (options.binaries,
1529                         options.sources,
1530                         options.project not in ["", None],
1531                         options.sat)
1532
1533     # Check if no option for package type
1534     if all_option_types.count(True) == 0:
1535         msg = _("Error: Precise a type for the package\nUse one of the "
1536                 "following options: --binaries, --sources, --project or"
1537                 " --salometools")
1538         logger.write(src.printcolors.printcError(msg), 1)
1539         logger.write("\n", 1)
1540         return 1
1541     
1542     # The repository where to put the package if not Binary or Source
1543     package_default_path = runner.cfg.LOCAL.workdir
1544     
1545     # if the package contains binaries or sources:
1546     if options.binaries or options.sources:
1547         # Check that the command has been called with an application
1548         src.check_config_has_application(runner.cfg)
1549
1550         # Display information
1551         logger.write(_("Packaging application %s\n") % src.printcolors.printcLabel(
1552                                                     runner.cfg.VARS.application), 1)
1553         
1554         # Get the default directory where to put the packages
1555         package_default_path = os.path.join(runner.cfg.APPLICATION.workdir, "PACKAGE")
1556         src.ensure_path_exists(package_default_path)
1557         
1558     # if the package contains a project:
1559     if options.project:
1560         # check that the project is visible by SAT
1561         projectNameFile = options.project + ".pyconf"
1562         foundProject = None
1563         for i in runner.cfg.PROJECTS.project_file_paths:
1564             baseName = os.path.basename(i)
1565             if baseName == projectNameFile:
1566                 foundProject = i
1567                 break
1568
1569         if foundProject is None:
1570             local_path = os.path.join(runner.cfg.VARS.salometoolsway, "data", "local.pyconf")
1571             msg = _("""ERROR: the project %(1)s is not visible by salomeTools.
1572 known projects are:
1573 %(2)s
1574
1575 Please add it in file:
1576 %(3)s""" % \
1577                     {"1": options.project, "2": "\n  ".join(runner.cfg.PROJECTS.project_file_paths), "3": local_path})
1578             logger.write(src.printcolors.printcError(msg), 1)
1579             logger.write("\n", 1)
1580             return 1
1581         else:
1582             options.project_file_path = foundProject
1583             src.printcolors.print_value(logger, "Project path", options.project_file_path, 2)
1584     
1585     # Remove the products that are filtered by the --without_properties option
1586     if options.without_properties:
1587         prop, value = options.without_properties
1588         update_config(runner.cfg, logger, prop, value)
1589
1590     # Remove from config the products that have the not_in_package property
1591     update_config(runner.cfg, logger, "not_in_package", "yes")
1592
1593     # get the name of the archive or build it
1594     if options.name:
1595         if os.path.basename(options.name) == options.name:
1596             # only a name (not a path)
1597             archive_name = options.name           
1598             dir_name = package_default_path
1599         else:
1600             archive_name = os.path.basename(options.name)
1601             dir_name = os.path.dirname(options.name)
1602         
1603         # suppress extension
1604         if archive_name[-len(".tgz"):] == ".tgz":
1605             archive_name = archive_name[:-len(".tgz")]
1606         if archive_name[-len(".tar.gz"):] == ".tar.gz":
1607             archive_name = archive_name[:-len(".tar.gz")]
1608         
1609     else:
1610         archive_name=""
1611         dir_name = package_default_path
1612         if options.binaries or options.sources:
1613             archive_name = runner.cfg.APPLICATION.name
1614
1615         if options.binaries:
1616             archive_name += "-"+runner.cfg.VARS.dist
1617             
1618         if options.sources:
1619             archive_name += "-SRC"
1620             if options.with_vcs:
1621                 archive_name += "-VCS"
1622
1623         if options.sat:
1624             archive_name += ("salomeTools_" + src.get_salometool_version(runner.cfg))
1625
1626         if options.project:
1627             if options.sat:
1628                 archive_name += "_" 
1629             archive_name += ("satproject_" + options.project)
1630  
1631         if len(archive_name)==0: # no option worked 
1632             msg = _("Error: Cannot name the archive\n"
1633                     " check if at least one of the following options was "
1634                     "selected : --binaries, --sources, --project or"
1635                     " --salometools")
1636             logger.write(src.printcolors.printcError(msg), 1)
1637             logger.write("\n", 1)
1638             return 1
1639  
1640     path_targz = os.path.join(dir_name, archive_name + PACKAGE_EXT)
1641     
1642     src.printcolors.print_value(logger, "Package path", path_targz, 2)
1643
1644     # Create a working directory for all files that are produced during the
1645     # package creation and that will be removed at the end of the command
1646     tmp_working_dir = os.path.join(runner.cfg.VARS.tmp_root, runner.cfg.VARS.datehour)
1647     src.ensure_path_exists(tmp_working_dir)
1648     logger.write("\n", 5)
1649     logger.write(_("The temporary working directory: %s\n" % tmp_working_dir),5)
1650     
1651     logger.write("\n", 3)
1652
1653     msg = _("Preparation of files to add to the archive")
1654     logger.write(src.printcolors.printcLabel(msg), 2)
1655     logger.write("\n", 2)
1656     
1657     d_files_to_add={}  # content of the archive
1658
1659     # a dict to hold paths that will need to be substitute for users recompilations
1660     d_paths_to_substitute={}  
1661
1662     if options.binaries:
1663         d_bin_files_to_add = binary_package(runner.cfg,
1664                                             logger,
1665                                             options,
1666                                             tmp_working_dir)
1667         # for all binaries dir, store the substitution that will be required 
1668         # for extra compilations
1669         for key in d_bin_files_to_add:
1670             if key.endswith("(bin)"):
1671                 source_dir = d_bin_files_to_add[key][0]
1672                 path_in_archive = d_bin_files_to_add[key][1].replace(
1673                    runner.cfg.INTERNAL.config.binary_dir + runner.cfg.VARS.dist,
1674                    runner.cfg.INTERNAL.config.install_dir)
1675                 if os.path.basename(source_dir)==os.path.basename(path_in_archive):
1676                     # if basename is the same we will just substitute the dirname 
1677                     d_paths_to_substitute[os.path.dirname(source_dir)]=\
1678                         os.path.dirname(path_in_archive)
1679                 else:
1680                     d_paths_to_substitute[source_dir]=path_in_archive
1681
1682         d_files_to_add.update(d_bin_files_to_add)
1683     if options.sources:
1684         d_files_to_add.update(source_package(runner,
1685                                         runner.cfg,
1686                                         logger, 
1687                                         options,
1688                                         tmp_working_dir))
1689         if options.binaries:
1690             # for archives with bin and sources we provide a shell script able to 
1691             # install binaries for compilation
1692             file_install_bin=produce_install_bin_file(runner.cfg,logger,
1693                                                       tmp_working_dir,
1694                                                       d_paths_to_substitute,
1695                                                       "install_bin.sh")
1696             d_files_to_add.update({"install_bin" : (file_install_bin, "install_bin.sh")})
1697             logger.write("substitutions that need to be done later : \n", 5)
1698             logger.write(str(d_paths_to_substitute), 5)
1699             logger.write("\n", 5)
1700     else:
1701         # --salomeTool option is not considered when --sources is selected, as this option
1702         # already brings salomeTool!
1703         if options.sat:
1704             d_files_to_add.update(sat_package(runner.cfg, tmp_working_dir, 
1705                                   options, logger))
1706         
1707     if options.project:
1708         DBG.write("config for package %s" % options.project, runner.cfg)
1709         d_files_to_add.update(project_package(runner.cfg, options.project, options.project_file_path, options.ftp, tmp_working_dir, options.sat, logger))
1710
1711     if not(d_files_to_add):
1712         msg = _("Error: Empty dictionnary to build the archive!\n")
1713         logger.write(src.printcolors.printcError(msg), 1)
1714         logger.write("\n", 1)
1715         return 1
1716
1717     # Add the README file in the package
1718     local_readme_tmp_path = add_readme(runner.cfg, options, tmp_working_dir)
1719     d_files_to_add["README"] = (local_readme_tmp_path, "README")
1720
1721     # Add the additional files of option add_files
1722     if options.add_files:
1723         for file_path in options.add_files:
1724             if not os.path.exists(file_path):
1725                 msg = _("WARNING: the file %s is not accessible.\n" % file_path)
1726                 continue
1727             file_name = os.path.basename(file_path)
1728             d_files_to_add[file_name] = (file_path, file_name)
1729
1730     logger.write("\n", 2)
1731     logger.write(src.printcolors.printcLabel(_("Actually do the package")), 2)
1732     logger.write("\n", 2)
1733     logger.write("\nfiles and directories to add:\n%s\n\n" % PP.pformat(d_files_to_add), 5)
1734
1735     res = 0
1736     try:
1737         # Creating the object tarfile
1738         tar = tarfile.open(path_targz, mode='w:gz')
1739         
1740         # get the filtering function if needed
1741         if old_python:
1742             filter_function = exclude_VCS_and_extensions_26
1743         else:
1744             filter_function = exclude_VCS_and_extensions
1745
1746         # Add the files to the tarfile object
1747         res = add_files(tar, archive_name, d_files_to_add, logger, f_exclude=filter_function)
1748         tar.close()
1749     except KeyboardInterrupt:
1750         logger.write(src.printcolors.printcError("\nERROR: forced interruption\n"), 1)
1751         logger.write(_("Removing the temporary working directory '%s'... ") % tmp_working_dir, 1)
1752         # remove the working directory
1753         shutil.rmtree(tmp_working_dir)
1754         logger.write(_("OK"), 1)
1755         logger.write(_("\n"), 1)
1756         return 1
1757     
1758     # case if no application, only package sat as 'sat package -t'
1759     try:
1760         app = runner.cfg.APPLICATION
1761     except:
1762         app = None
1763
1764     # unconditionaly remove the tmp_local_working_dir
1765     if app is not None:
1766         tmp_local_working_dir = os.path.join(app.workdir, "tmp_package")
1767         if os.path.isdir(tmp_local_working_dir):
1768             shutil.rmtree(tmp_local_working_dir)
1769
1770     # remove the tmp directory, unless user has registered as developer
1771     if os.path.isdir(tmp_working_dir) and (not DBG.isDeveloper()):
1772         shutil.rmtree(tmp_working_dir)
1773     
1774     # Print again the path of the package
1775     logger.write("\n", 2)
1776     src.printcolors.print_value(logger, "Package path", path_targz, 2)
1777     
1778     return res