-
Notifications
You must be signed in to change notification settings - Fork 0
/
splot
executable file
·227 lines (190 loc) · 7.25 KB
/
splot
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
# ------------------------------------------------------------------------
# splot
#
# This is based on genlop; its output is vaguely similar to running genlop
# with -c, then -t for the same package. It doesn't attempt to guess the
# build time, by default it shows the time the last 5 attempts took.
# ------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# ------------------------------------------------------------------------
import subprocess
import os
import re
import sys
import datetime
import string
import portage
from argparse import ArgumentParser
LOGFILE = r"/var/log/emerge.log"
def format_duration(secs):
""" Turn a time difference into a reasonable string
"""
if secs<60:
mins = 0
else:
mins = int(secs/60)
secs -= (mins*60)
result = "%2d:%02d" % (mins,secs)
return result
def escape_re(s):
"""
Need to escape any characters in package name which are
valid re commands. So far, only come across "+", but
there are probably others out these.
e.g. gtk+ causes problems
"""
result = re.escape(s)
return result
def compare_package(desired,test,slashed):
""" Extract the package name by stripping the version
number of the package. Assumed to be "-[0-9]"
- desired: package name, may include category, but no version
- test: category/package-version
"""
(testcat,testpkg) = test.split("/")
testname = re.split("-[0-9]",testpkg)[0]
if not slashed:
result = (desired==testname)
else:
(cat,pkg) = desired.split("/")
result = (pkg==testname)
return result
def current_build():
"""
Get a list of the currently running emerges, and output
their names, how long they've been building, and roughly
how much longer to go.
"""
# This selects process information: pid and package
# The package version is ignored.
ebuild_re = re.compile(r"(\d+) \[(.+?)\-[0-9].*?\] .+")
# Nicked most of the following direct from genlop
cmd = "ps ax -o pid,args | tail -n +2 | sed -e's/^ *//' | grep ' sandbox ' | grep -v ' grep '"
result = subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
scanresults = result.communicate()[0].decode('utf-8').split('\n')
found = []
for line in scanresults:
check = ebuild_re.match(line)
if check:
the_pid = check.group(1)
the_ebuild = check.group(2)
found.append((the_pid,the_ebuild))
if len(found) == 0:
print("Error: no working merge found.")
print(" (the -c option only works if there is an ongoing compilation)")
sys.exit(1)
for (pid,ebuild) in found:
generate_report(ebuild)
def specified_package(ebuild):
"""
Get the build times for the package
"""
generate_report(ebuild,False)
def generate_report(ebuild,show_current=True):
"""
Find and print the durations for the given ebuild
"""
slashed = (ebuild.find('/') >= 0)
report = ""
sb = r"(\d+): >>> emerge \((\d+) of (\d+)\) ([^ ]+) to \/"
start_build_re = re.compile(sb)
cb = r"(\d+): ::: completed emerge \(\d+ of \d+\) ([^ ]+) to \/"
complete_build_re = re.compile(cb)
# print("START",sb)
# print("COMPL",cb)
e_count = 0
emlog = open(LOGFILE,'r')
for line in emlog.readlines():
look = start_build_re.match(line)
if look:
if compare_package(ebuild,look.group(4),slashed):
e_start = int(look.group(1))
e_cur_num = look.group(2)
e_end_num = look.group(3)
e_current = look.group(4)
else:
look = complete_build_re.match(line)
if look:
if compare_package(ebuild,look.group(2),slashed):
e_end = int(look.group(1))
e_end_time = datetime.datetime.fromtimestamp(e_end)
e_count += 1
report_line = "\t%s, %s, %s\n" % \
(e_end_time.isoformat(' '),format_duration(e_end-e_start),e_current)
report = report_line + report
emlog.close()
# Only show up to 5 of the most recent build times. Can't
# see any point trying to use all the history and calc the
# expected time - no chance of getting it right, so just
# show the most recent set so user can work it out.
summary = report.split('\n')
summary = summary[0:options.limit]
report = '\n'.join(summary)
if show_current:
# Need to get the difference between e_start and now - that is how long
# the current ebuild has been going.
e_start_time = datetime.datetime.fromtimestamp(e_start)
runtime = datetime.datetime.now() - e_start_time
header = "\nCurrently merging %s out of %s:\n\n" % (e_cur_num,e_end_num)
header += ("\tEbuild: %s\n\tDuration: %s\n\n\tHistory:\n" % (e_current,format_duration(runtime.seconds)))
else:
e_current = r"n/a"
header = ("\n\tEbuild: %s\n\n\tHistory:\n" % e_current)
report = header + report
print(report)
def show_remaining_builds():
"""
List the packages queued to build, including the current build task.
"""
if 'resume' in portage.mtimedb and 'mergelist' in portage.mtimedb['resume']:
plist = []
todo = portage.mtimedb['resume']['mergelist']
for item in todo:
package = item[2]
print(f"={package}")
else:
print("No builds in progress")
def check_log_access():
"""
Check user has read access to the portage log file
"""
if not os.access(LOGFILE, os.R_OK):
print("Error: denied read access to the portage log file")
print(" are you logged in to the required account?")
sys.exit(1)
if __name__ == "__main__":
showpkg = None
parser = ArgumentParser(description="List emerge times for current or specific package")
parser.add_argument('-t','--time',
dest="showpkg",
help="report the emerge times for the given package")
parser.add_argument('-l','--limit',
type=int,
dest="limit",
default=5,
help="limit the number of events to be listed")
parser.add_argument('-r','--remaining',
action='store_true',
dest="running",
default=False,
help="Show the list of remaining builds")
options = parser.parse_args()
if options.showpkg:
check_log_access()
specified_package(options.showpkg)
elif options.running:
show_remaining_builds()
else:
check_log_access()
current_build()