3 # Copyright (C) 2010-2012 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 '''This file is the main entry file to salomeTools
31 # get path to salomeTools sources
32 srcdir = os.path.dirname(os.path.realpath(__file__))
34 # load resources for internationalization
35 #gettext.install('salomeTools', os.path.join(srcdir, 'common', 'i18n'))
37 es = gettext.translation('salomeTools', os.path.join(srcdir, 'common', 'i18n'))
40 def find_command_list():
42 for item in os.listdir(srcdir):
43 if item.endswith('.py') and item!='salomeTools.py':
44 cmd_list.append(item[:-len('.py')])
47 # The list of valid salomeTools commands
48 #lCommand = ['config', 'compile', 'prepare']
49 lCommand = find_command_list()
51 # Define all possible option for salomeTools command : sat <option> <args>
52 parser = common.options.Options()
53 parser.add_option('h', 'help', 'boolean', 'help', _(u"shows global help or help on a specific command."))
55 class salomeTools(object):
56 def __init__(self, opt, dataDir=None):
59 # Read the salomeTools options (the list of possible options is at the beginning of this file)
61 (options, args) = parser.parse_args(opt)
62 except Exception as exc:
69 except Exception as exc:
73 self.__dict__ = dict()
74 self.options = options
75 self.dataDir = dataDir
77 self.__setCommands__(srcdir)
79 def __getattr__(self, name):
80 if name in self.__dict__:
81 return self.__dict__[name]
83 raise AttributeError(name + _(" is not a valid command"))
85 def __setattr__(self, name, value):
86 object.__setattr__(self,name,value)
88 def __setCommands__(self, dirPath):
89 for nameCmd in lCommand:
90 (file_, pathname, description) = imp.find_module(nameCmd, [dirPath])
91 module = imp.load_module(nameCmd, file_, pathname, description)
93 def run_command(args):
94 print('Je suis dans l\'initialisation de la commande ' + __name__)
95 argv = args.split(" ")
97 # first argument is the APPLICATION
99 if len(argv) > 0 and argv[0][0] != "-":
100 appliToLoad = argv[0].rstrip('*')
103 # read the configuration from all the pyconf files
104 cfgManager = config.ConfigManager()
105 self.cfg = cfgManager.getConfig(dataDir=self.dataDir, application=appliToLoad)
107 return __module__.run(argv, self)
110 globals_up.update(run_command.__globals__)
111 globals_up.update({'__name__': nameCmd, '__module__' : module})
112 func = types.FunctionType(run_command.__code__, globals_up, run_command.__name__,run_command.__defaults__, run_command.__closure__)
114 self.__setattr__(nameCmd, func)
119 def print_help(options):
120 #from config import ConfigManager
121 #cfgManager = ConfigManager(STAND_ALONE_VERSION)
122 #cfg = cfgManager.getConfig(None, options)
126 print(common.printcolors.printcHeader( _("Usage: ") ) + "sat [sat_options] <command> [product] [command_options]\n")
130 # parse the src directory to list the available commands.
131 print(common.printcolors.printcHeader(_("Available commands are:\n")))
132 for command in lCommand:
133 print(" - %s" % (command))
135 print(common.printcolors.printcHeader(_(u"\nGetting the help for a specific command: ")) + "sat --help <command>\n")
137 def write_exception(exc):
138 sys.stderr.write("\n***** ")
139 sys.stderr.write(common.printcolors.printcError("salomeTools ERROR:"))
140 sys.stderr.write("\n" + str(exc) + "\n")
142 # ###############################
143 # MAIN : terminal command usage #
144 # ###############################
145 if __name__ == "__main__":
146 # Get the command line using sys.argv
147 cmd_line = " ".join(sys.argv)
148 # Initialize the code that will be returned by the terminal command
151 (options, args) = parser.parse_args(sys.argv[1:])
154 # no options => show help
157 sat = salomeTools(sys.argv[1:])