OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ost_startup.py
Go to the documentation of this file.
1 import sys, os, platform, glob
2 import optparse
3 
4 def show_help(option, opt, value, parser):
5  parser.print_help()
6  sys.exit(-1)
7 
8 def interactive_flag(option, opt, value, parser):
9  pass
10 
11 def stop():
12  sys.exit(0)
13 
14 usage = """
15 
16  ost [ost options] [script to execute] [script parameters]
17 
18 or
19  ost [action name] [action options]
20 
21 """
22 
23 action_path = os.path.abspath(os.environ.get("OST_EXEC_DIR", ""))
24 
25 usage += 'Following actions are available:\n'
26 for action in sorted(glob.glob(os.path.join(action_path, 'ost-*'))):
27  usage += " %s\n" % action[len(action_path)+5:]
28 usage += '\nEach action should respond to "--help".\n'
29 
30 class OstOptionParser(optparse.OptionParser):
31  def __init__(self, **kwargs):
32  optparse.OptionParser.__init__(self, **kwargs)
33  def exit(self, status_code, error_message):
34  print(error_message, end=' ')
35  sys.exit(-1)
36 
37 parser=OstOptionParser(usage=usage,conflict_handler="resolve", prog='ost''')
38 parser.add_option("-i", "--interactive", action="callback", callback=interactive_flag, help="start interpreter interactively (must be first parameter, ignored otherwise)")
39 parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
40 parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2, help="sets the verbosity level [default: %default]")
41 parser.disable_interspersed_args()
42 (options, args) = parser.parse_args()
43 
44 _site_packs='python%d.%d/site-packages' % sys.version_info[0:2]
45 _base_dir=os.getenv('DNG_ROOT')
46 sys.path.insert(0, os.path.join(_base_dir, 'lib64', _site_packs))
47 
48 from ost import *
49 import ost
50 
51 HistoryFile=os.path.expanduser('~/.ost_history')
52 
53 # we are not in GUI mode.
54 gui_mode=False
55 
56 sys.ps1='ost> '
57 sys.ps2='..... '
58 sys.argv=sys.argv[1:]
59 home = os.getenv('HOME') or os.getenv('USERPROFILE')
60 _ostrc=os.path.join(home, '.ostrc')
61 if os.path.exists(_ostrc):
62  try:
63  exec(compile(open(_ostrc).read(), _ostrc, 'exec'))
64  except Exception as e:
65  print(e)
66 PushVerbosityLevel(options.vlevel)
67 
68 # this should probably only be added when running an interactive shell
69 sys.path.append(".")
70 
71 if len(parser.rargs)>0 :
72  script=parser.rargs[0]
73  sys_argv_backup=sys.argv
74  sys.argv=parser.rargs
75  try:
76  exec(compile(open(script,'rb').read(), script, 'exec'))
77  finally:
78  sys.argv=sys_argv_backup
79 
def interactive_flag
Definition: ost_startup.py:8