This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
/
hyerctl.py
executable file
·173 lines (153 loc) · 3.92 KB
/
hyerctl.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/local/bin/python.stackless
# -*- coding: utf-8 -*-
#================================================
import sys
sys.path.append('/usr/lib/python2.5/site-packages/')
sys.path.append('/usr/lib/python2.6/dist-packages/')
sys.path.append("/var/lib/python-support/python2.5/")
sys.path.append("/var/lib/python-support/python2.6/")
sys.path.append("/usr/share/pyshared/")
sys.path.append("/usr/lib/pymodules/python2.6/")
import stackless,sys, os,atexit
import sys,getopt
import json
import threading
import signal, os,time,re
import imp
import shutil
import hyer.document
import hyer.browser
import hyer.rules_monster
import hyer.event
import hyer.tool
import hyer.helper
import hyer.dbwriter
import hyer.singleton
import hyer.log
import hyer.pcolor
import hyer.sl
"""
"""
__author__ = "xurenlu"
__version__ = "0.1"
__copyright__ = "Copyright (c) 2008 xurenlu"
__license__ = "LGPL"
def sig_exit():
""" handle the exit signal
"""
print "[end time]:"+str(time.time())
print hyer.pcolor.pcolorstr("CAUGHT SIG_EXIT signal,exiting...",hyer.pcolor.PHIGHLIGHT,hyer.pcolor.PRED,hyer.pcolor.PBLACK)
sys.exit()
def handler(signum, frame):
"""
handle signals
"""
sig_exit()
if signum == 3:
sig_exit()
if signum == 2:
sig_exit()
if signum == 9:
sig_exit()
return None
def prepare_taskfile(taskfile):
"""Attempt to load the taskfile as a module.
"""
path = os.path.dirname(taskfile)
taskmodulename = os.path.splitext(os.path.basename(taskfile))[0]
fp, pathname, description = imp.find_module(taskmodulename, [path])
print "fp:",fp,",pathname:",pathname,",desc:",description
try:
return imp.load_module(taskmodulename, fp, pathname, description)
finally:
if fp:
fp.close()
def inittask(task,type="web"):
"""init a task and create empty files for you
"""
try:
os.mkdir(task,0755)
except Exception,e:
print "exception:",e
pass
try:
shutil.copyfile("share/templates/project-%s.py" % type,"%s/%s.py" % (task,task) )
except Exception,e:
print "exception:",e
pass
try:
shutil.copyfile("share/templates/config-%s.py" % type,"%s/config.py" % task )
except:
pass
def handle_pid():
"""
handle pid files
"""
pid=os.getpid()
pidfile= "/tmp/run.generic.pid"
try:
lastpid=int(open(pidfile).read())
except:
lastpid=0
pass
try:
if lastpid>0:
os.kill(lastpid,9)
except:
pass
fp=open(pidfile,"w")
fp.write(str(pid))
fp.close()
def at_exit():
"""
hook of exit
"""
end_time=time.time()
print "[end time]:"+str(end_time)
print "[cost time]:"+str(end_time-start_time)
print "\n=========================\n"
def usage():
print "usage:\t",sys.argv[0],' init task'
print "usage:\t",sys.argv[0],' run taskfile'
print "usage:\t",sys.argv[0],' help'
signal.signal(signal.SIGINT,handler)
signal.signal(signal.SIGTERM,handler)
signal.signal(3,handler)
#如果子进程退出时主进程不需要处理资源回收等问题
#这样可以避免僵尸进程
signal.signal(signal.SIGCHLD,signal.SIG_IGN)
sys.getdefaultencoding()
reload(sys)
sys.setdefaultencoding("utf-8")
start_time=time.time()
if len(sys.argv)<2:
usage()
sys.exit()
cmd=sys.argv[1].lower()
if cmd == "help" :
usage()
sys.exit()
elif cmd == "runfile":
'''run a single file'''
print "[start time]:"+str(start_time)
atexit.register(at_exit)
taskfile=sys.argv[-1]
k=prepare_taskfile(taskfile)
k.run({})
stackless.run()
elif cmd == "run":
'''run a project '''
print "[start time]:"+str(start_time)
atexit.register(at_exit)
taskfile=sys.argv[-1]
elif cmd == "init":
task=sys.argv[2]
if len(sys.argv)>3:
task=sys.argv[3]
else:
task="web"
type="vertical"
inittask(task,type)
else:
usage()
sys.exit()