-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv-watcher
More file actions
executable file
·82 lines (64 loc) · 2.82 KB
/
env-watcher
File metadata and controls
executable file
·82 lines (64 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
#
# This is the main executable of the env-watcher.
#
# It is called from within the shell function that passes the entire
# environment to this executable. The shell function also takes care of
# executing possible corrective changes in the shell environment afterwards.
#
# The design of this package is such that all shell-specific code is imported
# dynamically by ShellParser.GetShellParser. At the moment I only have code
# for the Bash-shell, but that may change at some point.
#
# All the managing, storing and diff-ing are done on shell-independent
# objects defined in EnvironmentObjects.
import os
import sys
# some setup paths:
session_directory = os.environ["ENV_WATCHER_SESSION"]
main_directory = os.environ["ENV_WATCHER_DIR"]
if not session_directory or not main_directory:
print >>sys.stderr, "Your EnvWatcher environment was not properly set up. Please use the included setup.sh file."
sys.exit(127)
# if this fails then there is nothing I can do:
sys.path.append(os.environ["ENV_WATCHER_DIR"]+os.sep+"python")
from Debug import log
def main():
all_input = os.environ["ENV_WATCHER_INPUT"]
if not all_input:
raise Exception("This executable needs to be called from within the shell function env-watcher.\nPlease source the relevant setup script and procceed from there.")
from env_manager import env_manager
import argparse
parser = argparse.ArgumentParser(description='Monitor the shell environment')
parser.add_argument("-d","--debug", action="store_const", const=True, default=False, help="enable debug mode")
parser.add_argument("-f","--force", action="store_const", const=True, default=False, help="no questions asked. Overwriting is default.")
parser.add_argument("action",action="store", choices=env_manager.action_names, help="action to perform. Use 'usage' for full explanations.")
parser.add_argument("name",action="store", nargs="?", default="",help="Chosen name for the recording session.")
log("Command line:",sys.argv)
args=parser.parse_args(sys.argv[1:])
log("Parsed arguments",args)
action=env_manager.action_names[args.action]
import inspect
if "name" in inspect.getargspec(action)[0] and not args.name:
raise Exception("The action '%s' requires the 'name' argument." % args.action)
from ShellParser import GetShellParser
shellParser = GetShellParser(os.environ["SHELL"])
shell = shellParser(conf_dir=main_directory+"/config", input=all_input)
manager = env_manager(shell=shell, session_dir=session_directory, main_dir=main_directory)
return action(manager,name=args.name)
if __name__ == '__main__':
res=127
try:
res=main()
except SystemExit:
log()
except Exception, e:
print >>sys.stderr, "ERROR:",e
log()
except KeyboardInterrupt:
log()
except:
import sys
print >>sys.stderr, "Unknown error:",sys.exc_value,sys.exc_type
log()
sys.exit(res)