Salome HOME
sat #32302 pip option --build obsolète : integration du patch fourni par Nabil
[tools/sat.git] / commands / shell.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 subprocess
20
21 import src
22
23 # Define all possible option for the shell command :  sat shell <options>
24 parser = src.options.Options()
25 parser.add_option('c', 'command', 'string', 'command',
26     _('Mandatory: The shell command to execute.'), "")
27
28 def description():
29     '''method that is called when salomeTools is called with --help option.
30     
31     :return: The text to display for the shell command description.
32     :rtype: str
33     '''
34     return _("""\
35 The shell command executes the shell commands passed as argument.
36
37 example:
38 >> sat shell --command "ls -l /tmp" """)
39   
40 def run(args, runner, logger):
41     '''method that is called when salomeTools is called with shell parameter.
42     '''
43     
44     # Parse the options
45     (options, args) = parser.parse_args(args)
46
47     # Make sure the command option has been called
48     if not options.command:
49         message = _("The option --command is required\n")      
50         logger.write(src.printcolors.printcError(message))
51         return 1
52     
53     # Print the input command
54     msg = _("Command to execute:\n%s\nExecution ... " % options.command)
55     logger.write(msg, 3)
56     
57     # Call the input command
58     res = subprocess.call(options.command,
59                           shell=True,
60                           stdout=logger.logTxtFile,
61                           stderr=subprocess.STDOUT)
62  
63     # Format the result to be 0 (success) or 1 (fail)
64     if res != 0:
65         res = 1
66         logger.write(src.printcolors.printc("KO"), 3)
67     else:
68         logger.write(src.printcolors.printc("OK"), 3)
69     
70     logger.write("\n",3)
71     
72     return res