Salome HOME
pip download dans sat compile, property single_install_dir, postfix nom produit pour...
[tools/sat.git] / commands / compile.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 re
21 import subprocess
22 import src
23 import src.debug as DBG
24
25 # Compatibility python 2/3 for input function
26 # input stays input for python 3 and input = raw_input for python 2
27 try: 
28     input = raw_input
29 except NameError: 
30     pass
31
32
33 # Define all possible option for the compile command :  sat compile <options>
34 parser = src.options.Options()
35 parser.add_option('p', 'products', 'list2', 'products',
36     _('Optional: products to compile. This option accepts a comma separated list.'))
37 parser.add_option('', 'with_fathers', 'boolean', 'fathers',
38     _("Optional: build all necessary products to the given product (KERNEL is "
39       "build before building GUI)."), False)
40 parser.add_option('', 'with_children', 'boolean', 'children',
41     _("Optional: build all products using the given product (all SMESH plugins"
42       " are build after SMESH)."), False)
43 parser.add_option('', 'clean_all', 'boolean', 'clean_all',
44     _("Optional: clean BUILD dir and INSTALL dir before building product."),
45     False)
46 parser.add_option('', 'clean_install', 'boolean', 'clean_install',
47     _("Optional: clean INSTALL dir before building product."), False)
48 parser.add_option('', 'make_flags', 'string', 'makeflags',
49     _("Optional: add extra options to the 'make' command."))
50 parser.add_option('', 'show', 'boolean', 'no_compile',
51     _("Optional: DO NOT COMPILE just show if products are installed or not."),
52     False)
53 parser.add_option('', 'stop_first_fail', 'boolean', 'stop_first_fail', _(
54                   "Optional: Stops the command at first product compilation"
55                   " fail."), False)
56 parser.add_option('', 'check', 'boolean', 'check', _(
57                   "Optional: execute the unit tests after compilation"), False)
58
59 parser.add_option('', 'clean_build_after', 'boolean', 'clean_build_after', 
60                   _('Optional: remove the build directory after successful compilation'), False)
61
62
63 # from sat product infos, represent the product dependencies in a simple python graph
64 # keys are nodes, the list of dependencies are values
65 def get_dependencies_graph(p_infos):
66     graph={}
67     for (p_name,p_info) in p_infos:
68         graph[p_name]=p_info.depend
69     return graph
70
71 # this recursive function calculates all the dependencies of node start
72 def depth_search_graph(graph, start, visited=[]):
73     visited= visited+ [start]
74     for node in graph[start]:  # for all nodes in start dependencies
75         if node not in visited:
76             visited=depth_search_graph(graph, node, visited)
77     return visited
78
79 # find a path from start node to end (a group of nodes)
80 def find_path_graph(graph, start, end, path=[]):
81     path = path + [start]
82     if start in end:
83         return path
84     if not graph.has_key(start):
85         return None
86     for node in graph[start]:
87         if node not in path:
88             newpath = find_path_graph(graph, node, end, path)
89             if newpath: return newpath
90     return None
91
92 # Topological sorting algo
93 # return in sorted_nodes the list of sorted nodes
94 def depth_first_topo_graph(graph, start, visited=[], sorted_nodes=[]):
95     visited = visited + [start]
96     for node in graph[start]:
97         if node not in visited:
98             visited,sorted_nodes=depth_first_topo_graph(graph, node, visited,sorted_nodes)
99         else:
100             assert node in sorted_nodes, 'Error : cycle detection for node %s and %s !' % (start,node)
101     
102     sorted_nodes = sorted_nodes + [start]
103     return visited,sorted_nodes
104
105
106 # check for p_name that all dependencies are installed
107 def check_dependencies(config, p_name_p_info, all_products_dict):
108     l_depends_not_installed = []
109     for prod in p_name_p_info[1]["depend_all"]:
110         # for each dependency, check the install
111         prod_name, prod_info=all_products_dict[prod]
112         if not(src.product.check_installation(config, prod_info)):
113             l_depends_not_installed.append(prod_name)
114     return l_depends_not_installed   # non installed deps
115
116 def log_step(logger, header, step):
117     logger.write("\r%s%s" % (header, " " * 30), 3)
118     logger.write("\r%s%s" % (header, step), 3)
119     logger.flush()
120
121 def log_res_step(logger, res):
122     if res == 0:
123         logger.write("%s \n" % src.printcolors.printcSuccess("OK"), 4)
124         logger.flush()
125     else:
126         logger.write("%s \n" % src.printcolors.printcError("KO"), 4)
127         logger.flush()
128
129 def compile_all_products(sat, config, options, products_infos, all_products_dict, logger):
130     '''Execute the proper configuration commands 
131        in each product build directory.
132
133     :param config Config: The global configuration
134     :param products_info list: List of 
135                                  (str, Config) => (product_name, product_info)
136     :param all_products_dict: Dict of all products 
137     :param logger Logger: The logger instance to use for the display and logging
138     :return: the number of failing commands.
139     :rtype: int
140     '''
141     res = 0
142     for p_name_info in products_infos:
143         
144         p_name, p_info = p_name_info
145         
146         # Logging
147         len_end_line = 30
148         header = _("Compilation of %s") % src.printcolors.printcLabel(p_name)
149         header += " %s " % ("." * (len_end_line - len(p_name)))
150         logger.write(header, 3)
151         logger.flush()
152
153         # Do nothing if the product is not compilable
154         if not src.product.product_compiles(p_info):
155             log_step(logger, header, "ignored")
156             logger.write("\n", 3, False)
157             continue
158
159         # Do nothing if the product is native
160         if src.product.product_is_native(p_info):
161             log_step(logger, header, "native")
162             logger.write("\n", 3, False)
163             continue
164
165         # Do nothing if the product is fixed (already compiled by third party)
166         if src.product.product_is_fixed(p_info):
167             log_step(logger, header, "native")
168             logger.write("\n", 3, False)
169             continue
170
171         # Clean the build and the install directories 
172         # if the corresponding options was called
173         if options.clean_all:
174             log_step(logger, header, "CLEAN BUILD AND INSTALL ")
175             sat.clean(config.VARS.application + 
176                       " --products " + p_name + 
177                       " --build --install",
178                       batch=True,
179                       verbose=0,
180                       logger_add_link = logger)
181
182
183         # Clean the the install directory 
184         # if the corresponding option was called
185         if options.clean_install and not options.clean_all:
186             log_step(logger, header, "CLEAN INSTALL ")
187             sat.clean(config.VARS.application + 
188                       " --products " + p_name + 
189                       " --install",
190                       batch=True,
191                       verbose=0,
192                       logger_add_link = logger)
193         
194         # Recompute the product information to get the right install_dir
195         # (it could change if there is a clean of the install directory)
196         p_info = src.product.get_product_config(config, p_name)
197         
198         # Check if sources was already successfully installed
199         check_source = src.product.check_source(p_info)
200         is_pip= (src.appli_test_property(config,"pip", "yes") and src.product.product_test_property(p_info,"pip", "yes"))
201         # don't check sources with option --show 
202         # or for products managed by pip (there sources are in wheels stored in LOCAL.ARCHIVE
203         if not (options.no_compile or is_pip): 
204             if not check_source:
205                 logger.write(_("Sources of product not found (try 'sat -h prepare') \n"))
206                 res += 1 # one more error
207                 continue
208         
209         if src.product.product_is_salome(p_info):
210             # For salome modules, we check if the sources of configuration modules are present
211             # configuration modules have the property "configure_dependency"
212
213             # get the list of all modules in application 
214             all_products_infos = src.product.get_products_infos(config.APPLICATION.products,
215                                                                 config)
216             check_source = True
217             # for configuration modules, check if sources are present
218             for prod in all_products_dict:
219                 product_name, product_info = all_products_dict[prod]
220                 if ("properties" in product_info and
221                     "configure_dependency" in product_info.properties and
222                     product_info.properties.configure_dependency == "yes"):
223                     check_source = check_source and src.product.check_source(product_info)
224                     if not check_source:
225                         logger.write(_("\nERROR : SOURCES of %s not found! It is required for" 
226                                        " the configuration\n" % product_name))
227                         logger.write(_("        Get it with the command : sat prepare %s -p %s \n" % 
228                                       (config.APPLICATION.name, product_name)))
229             if not check_source:
230                 # if at least one configuration module is not present, we stop compilation
231                 res += 1
232                 continue
233         
234         # Check if it was already successfully installed
235         if src.product.check_installation(config, p_info):
236             logger.write(_("Already installed"))
237             logger.write(_(" in %s" % p_info.install_dir), 4)
238             logger.write(_("\n"))
239             continue
240         
241         # If the show option was called, do not launch the compilation
242         if options.no_compile:
243             logger.write(_("Not installed in %s\n" % p_info.install_dir))
244             continue
245         
246         # Check if the dependencies are installed
247         l_depends_not_installed = check_dependencies(config, p_name_info, all_products_dict)
248         if len(l_depends_not_installed) > 0:
249             log_step(logger, header, "")
250             logger.write(src.printcolors.printcError(
251                     _("ERROR : the following mandatory product(s) is(are) not installed: ")))
252             for prod_name in l_depends_not_installed:
253                 logger.write(src.printcolors.printcError(prod_name + " "))
254             logger.write("\n")
255             continue
256         
257         # Call the function to compile the product
258         res_prod, len_end_line, error_step = compile_product(
259              sat, p_name_info, config, options, logger, header, len_end_line)
260         
261         if res_prod != 0:
262             res += 1
263             
264             if error_step != "CHECK":
265                 # Clean the install directory if there is any
266                 logger.write(_(
267                             "Cleaning the install directory if there is any\n"),
268                              5)
269                 sat.clean(config.VARS.application + 
270                           " --products " + p_name + 
271                           " --install",
272                           batch=True,
273                           verbose=0,
274                           logger_add_link = logger)
275         else:
276             # Clean the build directory if the compilation and tests succeed
277             if options.clean_build_after:
278                 log_step(logger, header, "CLEAN BUILD")
279                 sat.clean(config.VARS.application + 
280                           " --products " + p_name + 
281                           " --build",
282                           batch=True,
283                           verbose=0,
284                           logger_add_link = logger)
285
286         # Log the result
287         if res_prod > 0:
288             logger.write("\r%s%s" % (header, " " * len_end_line), 3)
289             logger.write("\r" + header + src.printcolors.printcError("KO ") + error_step)
290             logger.write("\n==== %(KO)s in compile of %(name)s \n" %
291                 { "name" : p_name , "KO" : src.printcolors.printcInfo("ERROR")}, 4)
292             if error_step == "CHECK":
293                 logger.write(_("\nINSTALL directory = %s" % 
294                            src.printcolors.printcInfo(p_info.install_dir)), 3)
295             logger.flush()
296         else:
297             logger.write("\r%s%s" % (header, " " * len_end_line), 3)
298             logger.write("\r" + header + src.printcolors.printcSuccess("OK"))
299             logger.write(_("\nINSTALL directory = %s" % 
300                            src.printcolors.printcInfo(p_info.install_dir)), 3)
301             logger.write("\n==== %s \n" % src.printcolors.printcInfo("OK"), 4)
302             logger.write("\n==== Compilation of %(name)s %(OK)s \n" %
303                 { "name" : p_name , "OK" : src.printcolors.printcInfo("OK")}, 4)
304             logger.flush()
305         logger.write("\n", 3, False)
306         
307         
308         if res_prod != 0 and options.stop_first_fail:
309             break
310         
311     return res
312
313 def compile_product(sat, p_name_info, config, options, logger, header, len_end):
314     '''Execute the proper configuration command(s) 
315        in the product build directory.
316     
317     :param p_name_info tuple: (str, Config) => (product_name, product_info)
318     :param config Config: The global configuration
319     :param logger Logger: The logger instance to use for the display 
320                           and logging
321     :param header Str: the header to display when logging
322     :param len_end Int: the lenght of the the end of line (used in display)
323     :return: 1 if it fails, else 0.
324     :rtype: int
325     '''
326     
327     p_name, p_info = p_name_info
328           
329     # Get the build procedure from the product configuration.
330     # It can be :
331     # build_sources : autotools -> build_configure, configure, make, make install
332     # build_sources : cmake     -> cmake, make, make install
333     # build_sources : script    -> script executions
334     res = 0
335
336     
337     # check if pip should be used : the application and product have pip property
338     if (src.appli_test_property(config,"pip", "yes") and 
339        src.product.product_test_property(p_info,"pip", "yes")):
340             res, len_end_line, error_step = compile_product_pip(sat,
341                                                                 p_name_info,
342                                                                 config,
343                                                                 options,
344                                                                 logger,
345                                                                 header,
346                                                                 len_end)
347     else:
348         if (src.product.product_is_autotools(p_info) or 
349                                               src.product.product_is_cmake(p_info)):
350             res, len_end_line, error_step = compile_product_cmake_autotools(sat,
351                                                                       p_name_info,
352                                                                       config,
353                                                                       options,
354                                                                       logger,
355                                                                       header,
356                                                                       len_end)
357         if src.product.product_has_script(p_info):
358             res, len_end_line, error_step = compile_product_script(sat,
359                                                                    p_name_info,
360                                                                    config,
361                                                                    options,
362                                                                    logger,
363                                                                    header,
364                                                                    len_end)
365
366     # Check that the install directory exists
367     if res==0 and not(os.path.exists(p_info.install_dir)):
368         res = 1
369         error_step = "NO INSTALL DIR"
370         msg = _("Error: despite the fact that all the steps ended successfully,"
371                 " no install directory was found !")
372         logger.write(src.printcolors.printcError(msg), 4)
373         logger.write("\n", 4)
374         return res, len_end, error_step
375     
376     # Add the config file corresponding to the dependencies/versions of the 
377     # product that have been successfully compiled
378     if res==0:       
379         logger.write(_("Add the config file in installation directory\n"), 5)
380         src.product.add_compile_config_file(p_info, config)
381         
382         if options.check:
383             # Do the unit tests (call the check command)
384             log_step(logger, header, "CHECK")
385             res_check = sat.check(
386                               config.VARS.application + " --products " + p_name,
387                               verbose = 0,
388                               logger_add_link = logger)
389             if res_check != 0:
390                 error_step = "CHECK"
391                 
392             res += res_check
393     
394     return res, len_end_line, error_step
395
396
397 def compile_product_pip(sat,
398                         p_name_info,
399                         config,
400                         options,
401                         logger,
402                         header,
403                         len_end):
404     '''Execute the proper build procedure for pip products
405     :param p_name_info tuple: (str, Config) => (product_name, product_info)
406     :param config Config: The global configuration
407     :param logger Logger: The logger instance to use for the display 
408                           and logging
409     :param header Str: the header to display when logging
410     :param len_end Int: the lenght of the the end of line (used in display)
411     :return: 1 if it fails, else 0.
412     :rtype: int
413     '''
414     # a) initialisation
415     p_name, p_info = p_name_info
416     res = 0
417     error_step = ""
418     pip_install_in_python=False
419     pip_wheels_dir=os.path.join(config.LOCAL.archive_dir,"wheels")
420     pip_install_cmd=config.INTERNAL.command.pip_install # parametrized in src/internal
421
422     # b) get the build environment (useful to get the installed python & pip3)
423     build_environ = src.environment.SalomeEnviron(config,
424                              src.environment.Environ(dict(os.environ)),
425                              True)
426     environ_info = src.product.get_product_dependencies(config,
427                                                         p_info)
428     build_environ.silent = (config.USER.output_verbose_level < 5)
429     build_environ.set_full_environ(logger, environ_info)
430
431     # c- download : check/get pip wheel in pip_wheels_dir
432     pip_download_cmd=config.INTERNAL.command.pip_download +\
433                      " --destination-directory %s --no-deps %s==%s " %\
434                      (pip_wheels_dir, p_info.name, p_info.version)
435     logger.write("\n"+pip_download_cmd+"\n", 4, False) 
436     res_pip_dwl = (subprocess.call(pip_download_cmd, 
437                                    shell=True, 
438                                    cwd=config.LOCAL.workdir,
439                                    env=build_environ.environ.environ,
440                                    stdout=logger.logTxtFile, 
441                                    stderr=subprocess.STDOUT) == 0)
442     # error is not managed at the stage. error will be handled by pip install
443     # here we just print a message
444     if not res_pip_dwl:
445         logger.write("Error in pip download\n", 4, False)
446
447
448     # d- install (in python or in separate product directory)
449     if src.appli_test_property(config,"pip_install_dir", "python"):
450         # pip will install product in python directory"
451         pip_install_cmd+=" --find-links=%s --build %s %s==%s" %\
452         (pip_wheels_dir, p_info.build_dir, p_info.name, p_info.version)
453         pip_install_in_python=True
454         
455     else: 
456         # pip will install product in product install_dir
457         pip_install_dir=os.path.join(p_info.install_dir, "lib", "python${PYTHON_VERSION:0:3}", "site-packages")
458         pip_install_cmd+=" --find-links=%s --build %s --target %s %s==%s" %\
459         (pip_wheels_dir, p_info.build_dir, pip_install_dir, p_info.name, p_info.version)
460     log_step(logger, header, "PIP")
461     logger.write("\n"+pip_install_cmd+"\n", 4)
462     len_end_line = len_end + 3
463     error_step = ""
464
465     res_pip = (subprocess.call(pip_install_cmd, 
466                                shell=True, 
467                                cwd=config.LOCAL.workdir,
468                                env=build_environ.environ.environ,
469                                stdout=logger.logTxtFile, 
470                                stderr=subprocess.STDOUT) == 0)        
471     if res_pip:
472         res=0
473         if pip_install_in_python:
474             # when product is installed in python, create install_dir 
475             # (to put inside product info and mark the installation success)
476             os.mkdir(p_info.install_dir)
477     else:
478         #log_res_step(logger, res)
479         res=1
480         error_step = "PIP"
481
482     return res, len_end_line, error_step 
483
484
485
486 def compile_product_cmake_autotools(sat,
487                                     p_name_info,
488                                     config,
489                                     options,
490                                     logger,
491                                     header,
492                                     len_end):
493     '''Execute the proper build procedure for autotools or cmake
494        in the product build directory.
495     
496     :param p_name_info tuple: (str, Config) => (product_name, product_info)
497     :param config Config: The global configuration
498     :param logger Logger: The logger instance to use for the display 
499                           and logging
500     :param header Str: the header to display when logging
501     :param len_end Int: the lenght of the the end of line (used in display)
502     :return: 1 if it fails, else 0.
503     :rtype: int
504     '''
505     p_name, p_info = p_name_info
506     
507     # Execute "sat configure", "sat make" and "sat install"
508     res = 0
509     error_step = ""
510     
511     # Logging and sat command call for configure step
512     len_end_line = len_end
513     log_step(logger, header, "CONFIGURE")
514     res_c = sat.configure(config.VARS.application + " --products " + p_name,
515                           verbose = 0,
516                           logger_add_link = logger)
517     log_res_step(logger, res_c)
518     res += res_c
519     
520     if res_c > 0:
521         error_step = "CONFIGURE"
522     else:
523         # Logging and sat command call for make step
524         # Logging take account of the fact that the product has a compilation 
525         # script or not
526         if src.product.product_has_script(p_info):
527             # if the product has a compilation script, 
528             # it is executed during make step
529             scrit_path_display = src.printcolors.printcLabel(
530                                                         p_info.compil_script)
531             log_step(logger, header, "SCRIPT " + scrit_path_display)
532             len_end_line = len(scrit_path_display)
533         else:
534             log_step(logger, header, "MAKE")
535         make_arguments = config.VARS.application + " --products " + p_name
536         # Get the make_flags option if there is any
537         if options.makeflags:
538             make_arguments += " --option -j" + options.makeflags
539         res_m = sat.make(make_arguments,
540                          verbose = 0,
541                          logger_add_link = logger)
542         log_res_step(logger, res_m)
543         res += res_m
544         
545         if res_m > 0:
546             error_step = "MAKE"
547         else: 
548             # Logging and sat command call for make install step
549             log_step(logger, header, "MAKE INSTALL")
550             res_mi = sat.makeinstall(config.VARS.application + 
551                                      " --products " + 
552                                      p_name,
553                                     verbose = 0,
554                                     logger_add_link = logger)
555
556             log_res_step(logger, res_mi)
557             res += res_mi
558             
559             if res_mi > 0:
560                 error_step = "MAKE INSTALL"
561                 
562     return res, len_end_line, error_step 
563
564 def compile_product_script(sat,
565                            p_name_info,
566                            config,
567                            options,
568                            logger,
569                            header,
570                            len_end):
571     '''Execute the script build procedure in the product build directory.
572     
573     :param p_name_info tuple: (str, Config) => (product_name, product_info)
574     :param config Config: The global configuration
575     :param logger Logger: The logger instance to use for the display 
576                           and logging
577     :param header Str: the header to display when logging
578     :param len_end Int: the lenght of the the end of line (used in display)
579     :return: 1 if it fails, else 0.
580     :rtype: int
581     '''
582     p_name, p_info = p_name_info
583     
584     # Execute "sat configure", "sat make" and "sat install"
585     error_step = ""
586     
587     # Logging and sat command call for the script step
588     scrit_path_display = src.printcolors.printcLabel(p_info.compil_script)
589     log_step(logger, header, "SCRIPT " + scrit_path_display)
590     len_end_line = len_end + len(scrit_path_display)
591     res = sat.script(config.VARS.application + " --products " + p_name,
592                      verbose = 0,
593                      logger_add_link = logger)
594     log_res_step(logger, res)
595               
596     return res, len_end_line, error_step 
597
598     
599 def description():
600     '''method that is called when salomeTools is called with --help option.
601     
602     :return: The text to display for the compile command description.
603     :rtype: str
604     '''
605     return _("The compile command constructs the products of the application"
606              "\n\nexample:\nsat compile SALOME-master --products KERNEL,GUI,"
607              "MEDCOUPLING --clean_all")
608   
609 def run(args, runner, logger):
610     '''method that is called when salomeTools is called with compile parameter.
611     '''
612     # DBG.write("compile runner.cfg", runner.cfg, True)
613     # Parse the options
614     (options, args) = parser.parse_args(args)
615
616     # Warn the user if he invoked the clean_all option 
617     # without --products option
618     if (options.clean_all and 
619         options.products is None and 
620         not runner.options.batch):
621         rep = input(_("You used --clean_all without specifying a product"
622                           " are you sure you want to continue? [Yes/No] "))
623         if rep.upper() != _("YES").upper():
624             return 0
625         
626     # check that the command has been called with an application
627     src.check_config_has_application( runner.cfg )
628
629     # Print some informations
630     logger.write(_('Executing the compile commands in the build '
631                                 'directories of the products of '
632                                 'the application %s\n') % 
633                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
634     
635     info = [
636             (_("SOURCE directory"),
637              os.path.join(runner.cfg.APPLICATION.workdir, 'SOURCES')),
638             (_("BUILD directory"),
639              os.path.join(runner.cfg.APPLICATION.workdir, 'BUILD'))
640             ]
641     src.print_info(logger, info)
642
643     # Get the list of all application products, and create its dependency graph
644     all_products_infos = src.product.get_products_infos(runner.cfg.APPLICATION.products,
645                                                         runner.cfg)
646     all_products_graph=get_dependencies_graph(all_products_infos)
647     logger.write("Dependency graph of all application products : %s\n" % all_products_graph, 6)
648
649     # Get the list of products we have to compile
650     products_infos = src.product.get_products_list(options, runner.cfg, logger)
651     products_list = [pi[0] for pi in products_infos]
652
653     logger.write("Product we have to compile (as specified by user) : %s\n" % products_list, 5)
654     if options.fathers:
655         # Extend the list with all recursive dependencies of the given products
656         visited=[]
657         for p_name in products_list:
658             visited=depth_search_graph(all_products_graph, p_name, visited)
659         products_list = visited
660
661     logger.write("Product list to compile with fathers : %s\n" % products_list, 5)
662     if options.children:
663         # Extend the list with all products that depends upon the given products
664         children=[]
665         for n in all_products_graph:
666             # for all products (that are not in products_list):
667             # if we we find a path from the product to the product list,
668             # then we product is a child and we add it to the children list 
669             if (n not in children) and (n not in products_list):
670                 if find_path_graph(all_products_graph, n, products_list):
671                     children = children + [n]
672         # complete products_list (the products we have to compile) with the list of children
673         products_list = products_list + children
674         logger.write("Product list to compile with children : %s\n" % products_list, 5)
675
676     # Sort the list of all products (topological sort).
677     # the products listed first do not depend upon products listed after
678     visited_nodes=[]
679     sorted_nodes=[]
680     for n in all_products_graph:
681         if n not in visited_nodes:
682             visited_nodes,sorted_nodes=depth_first_topo_graph(all_products_graph, n, visited_nodes,sorted_nodes)
683     logger.write("Complete depndency graph topological search (sorting): %s\n" % sorted_nodes, 6)
684
685 #   use the sorted list of all products to sort the list of products we have to compile
686     sorted_product_list=[]
687     for n in sorted_nodes:
688         if n in products_list:
689             sorted_product_list.append(n)
690     logger.write("Sorted list of products to compile : %s\n" % sorted_product_list, 5)
691
692     
693     # from the sorted list of products to compile, build a sorted list of products infos
694     #  a- create a dict to facilitate products_infos sorting
695     all_products_dict={}
696     for (pname,pinfo) in all_products_infos:
697         all_products_dict[pname]=(pname,pinfo)
698     #  b- build a sorted list of products infos in products_infos
699     products_infos=[]
700     for product in sorted_product_list:
701         products_infos.append(all_products_dict[product])
702
703     # for all products to compile, store in "depend_all" field the complete dependencies (recursive) 
704     # (will be used by check_dependencies funvtion)
705     for pi in products_infos:
706         dep_prod=[]
707         dep_prod=depth_search_graph(all_products_graph,pi[0], dep_prod)
708         pi[1]["depend_all"]=dep_prod[1:]
709         
710
711     # Call the function that will loop over all the products and execute
712     # the right command(s)
713     res = compile_all_products(runner, runner.cfg, options, products_infos, all_products_dict, logger)
714     
715     # Print the final state
716     nb_products = len(products_infos)
717     if res == 0:
718         final_status = "OK"
719     else:
720         final_status = "KO"
721    
722     logger.write(_("\nCompilation: %(status)s (%(valid_result)d/%(nb_products)d)\n") % \
723         { 'status': src.printcolors.printc(final_status), 
724           'valid_result': nb_products - res,
725           'nb_products': nb_products }, 1)    
726     
727     code = res
728     if code != 0:
729         code = 1
730     return code