Paste P6446

One implementation for T182451: qacct wrapper
ActivePublic

Authored by zhuyifei1999 on Dec 9 2017, 1:49 AM.
1#! /usr/bin/python3 -Es
2
3import io
4import os
5import subprocess
6import sys
7import tempfile
8
9sge_root = os.environ.get('SGE_ROOT', '/var/lib/gridengine')
10sge_cell = os.environ.get('SGE_CELL', 'default')
11account_file = 'common/accounting'
12
13logpath = os.path.join(sge_root, sge_cell, account_file)
14
15with open(logpath, 'rb') as logfile:
16logfile.seek(-400 * 45000 * 7, os.SEEK_END)
17# Ignore first line as it may be onlym part of a line
18logfile.readline()
19with tempfile.TemporaryDirectory() as new_sge_root:
20new_logpath = os.path.join(new_sge_root, sge_cell, account_file)
21os.makedirs(os.path.dirname(new_logpath))
22
23for fname in os.listdir(os.path.dirname(logpath)):
24os.symlink(
25os.path.join(os.path.dirname(logpath), fname),
26os.path.join(os.path.dirname(new_logpath), fname))
27
28os.remove(new_logpath)
29os.mkfifo(new_logpath)
30
31real_qacct = subprocess.Popen(
32['/usr/bin/qacct'] + sys.argv[1:],
33env=dict(os.environ, SGE_ROOT=new_sge_root))
34
35with open(new_logpath, 'wb') as new_logfile:
36while real_qacct.poll() is None:
37data = logfile.read(io.DEFAULT_BUFFER_SIZE)
38if not data:
39break
40new_logfile.write(data)
41
42sys.exit(real_qacct.wait())