6
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import os
|
|
4 import sys
|
|
5 import time
|
|
6 import shutil
|
|
7 from commons.core.checker.RepetException import RepetException
|
|
8 from commons.core.sql.TableJobAdaptator import TableJobAdaptator
|
|
9 from commons.core.sql.DbFactory import DbFactory
|
|
10 from commons.core.sql.Job import Job
|
|
11
|
|
12 try:
|
|
13 newDir = None
|
|
14 print os.uname()
|
|
15 beginTime = time.time()
|
|
16 print 'beginTime=%f' % beginTime
|
|
17 print "work in dir '@@tmpDir@@'"
|
|
18 sys.stdout.flush()
|
|
19 if not os.path.exists("@@tmpDir@@"):
|
|
20 raise IOError("ERROR: temporary directory '@@tmpDir@@' doesn't exist")
|
|
21
|
|
22 fileSize = 0
|
|
23 if not os.path.exists("@@groupId@@"):
|
|
24 @@cmdSize@@
|
|
25 freeGigaNeededInTmpDir = float(1 + fileSize)
|
|
26 freeSpace = os.statvfs("@@tmpDir@@")
|
|
27 if ((freeSpace.f_bavail * freeSpace.f_frsize) / 1073741824.0 < freeGigaNeededInTmpDir):
|
|
28 raise RepetException("ERROR: less than %.2fG of input file in '@@tmpDir@@'" % freeGigaNeededInTmpDir)
|
|
29
|
|
30 os.chdir("@@tmpDir@@")
|
|
31 if not os.path.exists("@@groupId@@"):
|
|
32 try:
|
|
33 os.mkdir("@@groupId@@")
|
|
34 except OSError, e :
|
|
35 if e.args[0] != 17:
|
|
36 raise RepetException("ERROR: can't create '@@groupId@@'")
|
|
37 os.chdir("@@groupId@@")
|
|
38 @@cmdCopy@@
|
|
39 else:
|
|
40 os.chdir("@@groupId@@")
|
|
41
|
|
42 newDir = "@@groupId@@_@@jobName@@_@@time@@"
|
|
43 if os.path.exists(newDir):
|
|
44 shutil.rmtree(newDir)
|
|
45 os.mkdir(newDir)
|
|
46 os.chdir(newDir)
|
|
47
|
|
48 iJob = Job(jobname = "@@jobName@@", groupid = "@@groupId@@", launcherFile = "@@launcher@@", node = os.getenv("HOSTNAME"))
|
|
49 iDb = DbFactory.createInstance()
|
|
50 iTJA = TableJobAdaptator(iDb, "@@jobTableName@@")
|
|
51 print "current status: %s" % iTJA.getJobStatus(iJob)
|
|
52 iTJA.changeJobStatus(iJob, "running")
|
|
53 print "updated status: %s" % iTJA.getJobStatus(iJob)
|
|
54 sys.stdout.flush()
|
|
55 iDb.close()
|
|
56
|
|
57 @@cmdStart@@
|
|
58 if log != 0:
|
|
59 raise RepetException("ERROR: job returned %i" % log)
|
|
60 else:
|
|
61 print "job finished successfully"
|
|
62 sys.stdout.flush()
|
|
63 @@cmdFinish@@
|
|
64
|
|
65 os.chdir("..")
|
|
66 shutil.rmtree(newDir)
|
|
67
|
|
68 iDb = DbFactory.createInstance()
|
|
69 iTJA = TableJobAdaptator(iDb, "@@jobTableName@@")
|
|
70 print "current status: %s" % iTJA.getJobStatus(iJob)
|
|
71 iTJA.changeJobStatus(iJob, "finished")
|
|
72 print "updated status: %s" % iTJA.getJobStatus(iJob)
|
|
73 sys.stdout.flush()
|
|
74 iDb.close()
|
|
75
|
|
76 endTime = time.time()
|
|
77 print 'endTime=%f' % endTime
|
|
78 print 'executionTime=%f' % (endTime - beginTime)
|
|
79 print os.uname()
|
|
80 sys.stdout.flush()
|
|
81
|
|
82 except IOError, e :
|
|
83 print e
|
|
84 iJob = Job(jobname = "@@jobName@@", groupid = "@@groupId@@", launcherFile = "@@launcher@@", node = os.getenv("HOSTNAME"))
|
|
85 iDb = DbFactory.createInstance()
|
|
86 iTJA = TableJobAdaptator(iDb, "@@jobTableName@@")
|
|
87 print "current status: %s" % iTJA.getJobStatus(iJob)
|
|
88 iTJA.changeJobStatus(iJob, "error")
|
|
89 print "updated status: %s" % iTJA.getJobStatus(iJob)
|
|
90 sys.stdout.flush()
|
|
91 iDb.close()
|
|
92 sys.exit(1)
|
|
93
|
|
94 except Exception, e :
|
|
95 print "tmpDir is : @@tmpDir@@"
|
|
96 print "cDir is : @@cDir@@"
|
|
97 print e
|
|
98 if newDir != None and os.path.exists("../%s" % newDir) and not os.path.exists("@@cDir@@/%s" % newDir):
|
|
99 os.chdir("..")
|
|
100 shutil.move(newDir, "@@cDir@@/%s" % newDir)
|
|
101 iJob = Job(jobname = "@@jobName@@", groupid = "@@groupId@@", launcherFile = "@@launcher@@", node = os.getenv("HOSTNAME"))
|
|
102 iDb = DbFactory.createInstance()
|
|
103 iTJA = TableJobAdaptator(iDb, "@@jobTableName@@")
|
|
104 print "current status: %s" % iTJA.getJobStatus(iJob)
|
|
105 iTJA.changeJobStatus(iJob, "error")
|
|
106 print "updated status: %s" % iTJA.getJobStatus(iJob)
|
|
107 sys.stdout.flush()
|
|
108 iDb.close()
|
|
109 sys.exit(1)
|