Mercurial > repos > yufei-luo > s_mart
comparison commons/pyRepetUnit/components/AbstractProgramLauncher.py @ 18:94ab73e8a190
Uploaded
author | m-zytnicki |
---|---|
date | Mon, 29 Apr 2013 03:20:15 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
17:b0e8584489e6 | 18:94ab73e8a190 |
---|---|
1 #!/usr/bin/env python | |
2 ##@file | |
3 # Abstract class to launch a program. | |
4 | |
5 import getopt | |
6 import sys | |
7 import os | |
8 import pyRepet.launcher.AbstractProgramLauncher | |
9 | |
10 class AbstractProgramLauncher( pyRepet.launcher.AbstractProgramLauncher.AbstractProgramLauncher ): #( IProgramLauncher ) | |
11 | |
12 def getHelpAsString( self ): | |
13 """ | |
14 Return the generic help as a string. | |
15 """ | |
16 string = "" | |
17 string += "usage: %s.py [options]" % ( type(self).__name__ ) | |
18 string += "\ngeneric options:" | |
19 string += "\n -h: this help" | |
20 string += "\n -i: name of the input file (format='%s')" % ( self.getFormatInputFile() ) | |
21 string += "\n -c: clean" | |
22 string += "\n -v: verbosity level (default=0/1)" | |
23 return string | |
24 | |
25 def setAttributesFromCmdLine( self, o, a="" ): | |
26 """ | |
27 Set a generic attribute from the command-line arguments. | |
28 """ | |
29 if o == "-h": | |
30 print self.getHelpAsString() | |
31 sys.exit(0) | |
32 elif o == "-i": | |
33 self.setInputFile( a ) | |
34 elif o == "-c": | |
35 self.setClean() | |
36 elif o == "-v": | |
37 self.setVerbosityLevel( a ) | |
38 | |
39 def checkAttributesFromCmdLine( self ): | |
40 """ | |
41 Set the attributes from the command-line arguments. | |
42 """ | |
43 try: | |
44 opts, args = getopt.getopt( sys.argv[1:], "%s" % (self.getCmdLineOptions()) ) | |
45 except getopt.GetoptError, err: | |
46 print str(err); | |
47 print self.getHelpAsString() | |
48 sys.exit(1) | |
49 for o, a in opts: | |
50 self.setAttributesFromCmdLine( o, a ) | |
51 | |
52 def getCmdLineOptions(self): | |
53 return self._cmdLineGenericOptions | |
54 | |
55 def check( self ): | |
56 """ | |
57 Check the generic attributes before running the program. | |
58 """ | |
59 self._checkProgramName() | |
60 self.checkInput() | |
61 | |
62 def checkInput(self): | |
63 if self.getInputFile() == "": | |
64 string = "ERROR: missing input file" | |
65 print string | |
66 print self.getHelpAsString() | |
67 sys.exit(1) | |
68 | |
69 if not os.path.exists(self.getInputFile()): | |
70 string = "ERROR: input file '%s' doesn't exist" % (self.getInputFile()) | |
71 print string | |
72 print self.getHelpAsString() | |
73 sys.exit(1) |