OpenStructure
ost_startup.py
Go to the documentation of this file.
1 import sys, os, glob
2 import optparse
3 
4 
5 def show_help(option, opt, value, parser):
6  parser.print_help()
7  sys.exit(-1)
8 
9 
10 def show_version(option, opt, value, parser):
11  print("OpenStructure " + ost.__version__)
12  sys.exit(0)
13 
14 
15 def interactive_flag(option, opt, value, parser):
16  pass
17 
18 
19 def stop():
20  sys.exit(0)
21 
22 
23 def get_options_parser(usage):
24  parser = OstOptionParser(usage=usage, conflict_handler="resolve", prog="ost")
25  parser.add_option(
26  "-i",
27  "--interactive",
28  action="callback",
29  callback=interactive_flag,
30  help="start interpreter interactively (must be first parameter, ignored otherwise)",
31  )
32  parser.add_option(
33  "-h",
34  "--help",
35  action="callback",
36  callback=show_help,
37  help="show this help message and exit",
38  )
39  parser.add_option(
40  "-V",
41  "--version",
42  action="callback",
43  callback=show_version,
44  help="show OST version and exit",
45  )
46  parser.add_option(
47  "-v",
48  "--verbosity_level",
49  action="store",
50  type="int",
51  dest="vlevel",
52  default=2,
53  help="sets the verbosity level [default: %default]",
54  )
55  parser.disable_interspersed_args()
56  return parser
57 
58 
59 usage = """
60  ost [ost options] [script to execute] [script parameters]
61  ost [action name] [action options]
62 
63 The following actions are available:
64 """
65 
66 action_path = os.path.abspath(os.environ.get("OST_EXEC_DIR", ""))
67 for action in sorted(glob.glob(os.path.join(action_path, "ost-*"))):
68  usage += " %s\n" % action[len(action_path) + 5 :]
69 
70 usage += '\nEach action should respond to "--help".'
71 
72 
73 class OstOptionParser(optparse.OptionParser):
74  def __init__(self, **kwargs):
75  optparse.OptionParser.__init__(self, **kwargs)
76 
77  def exit(self, status_code, error_message):
78  print(error_message, end=" ")
79  sys.exit(-1)
80 
81 
82 _site_packs = "python%d.%d/site-packages" % sys.version_info[0:2]
83 _base_dir = os.getenv("DNG_ROOT")
84 sys.path.insert(0, os.path.join(_base_dir, "lib64", _site_packs))
85 
86 from ost import *
87 import ost
88 
89 parser = get_options_parser(usage)
90 (options, args) = parser.parse_args()
91 
92 HistoryFile = os.path.expanduser("~/.ost_history")
93 
94 # we are not in GUI mode.
95 gui_mode = False
96 
97 sys.ps1 = "ost> "
98 sys.ps2 = "..... "
99 sys.argv = sys.argv[1:]
100 home = os.getenv("HOME") or os.getenv("USERPROFILE")
101 _ostrc = os.path.join(home, ".ostrc")
102 if os.path.exists(_ostrc):
103  try:
104  exec(compile(open(_ostrc).read(), _ostrc, "exec"))
105  except Exception as e:
106  print(e)
107 
108 PushVerbosityLevel(options.vlevel)
109 
110 # this should probably only be added when running an interactive shell
111 sys.path.append(".")
112 
113 if len(parser.rargs) > 0:
114  script = parser.rargs[0]
115  sys_argv_backup = sys.argv
116  sys.argv = parser.rargs
117  try:
118  exec(compile(open(script, "rb").read(), script, "exec"))
119  finally:
120  sys.argv = sys_argv_backup
def __init__(self, **kwargs)
Definition: ost_startup.py:74
def exit(self, status_code, error_message)
Definition: ost_startup.py:77
def PushVerbosityLevel(value)
Definition: __init__.py:229
def show_version(option, opt, value, parser)
Definition: ost_startup.py:10
def get_options_parser(usage)
Definition: ost_startup.py:23
def show_help(option, opt, value, parser)
Definition: ost_startup.py:5
def interactive_flag(option, opt, value, parser)
Definition: ost_startup.py:15