public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-dev] elog file reader
@ 2005-12-22 17:08 Donnie Berkholz
  2005-12-23  6:05 ` Colin Kingsley
  0 siblings, 1 reply; 2+ messages in thread
From: Donnie Berkholz @ 2005-12-22 17:08 UTC (permalink / raw
  To: Gentoo Developers

[-- Attachment #1: Type: text/plain, Size: 393 bytes --]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I hacked up a quick script this morning to handle the new elog scripts
in 2.1. Perhaps some of you can make use of it or help me improve it.

Thanks,
Donnie
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDqt2XXVaO67S1rtsRAlzuAJwJooVqex46LHMYcYTDx40kUn6/WgCgrlgr
KUC845ELqhXHZ5ZNs6PkUbQ=
=Z5KJ
-----END PGP SIGNATURE-----

[-- Attachment #2: eread --]
[-- Type: text/plain, Size: 1269 bytes --]

#!/bin/bash

# This is a script to read portage log items from einfo, ewarn etc, new in the
# portage-2.1 series.
#
# Author: Donnie Berkholz <spyderous@gentoo.org>

# Set PORTAGE_TMPDIR
PORTAGE_TMPDIR=$(portageq envvar PORTAGE_TMPDIR)

# Set up select prompt
PS3="Choice? "

select_loop() {
	ANY_FILES=$(/usr/bin/find ${PORTAGE_TMPDIR}/elogs/ -type f)

	if [[ -z ${ANY_FILES} ]]; then
		echo "No log items to read"
		break
	fi

	echo
	echo "This is a list of portage log items. Choose a number to view that file."
	echo

	# Pick which file to read
	select FILE in ${PORTAGE_TMPDIR}/elogs/*; do
		case ${REPLY} in
			q)
				echo "Quitting"
				QUIT="yes"
				break
				;;
			*)
				/usr/bin/less ${FILE}
				read -p "Delete file? [y/N] " DELETE
				case ${DELETE} in
					q)
						echo "Quitting"
						QUIT="yes"
						break
						;;
					y|Y)
						/usr/bin/rm -f ${FILE}
						SUCCESS=$?
						if [[ ${SUCCESS} = 0 ]]; then
							echo "Deleted ${FILE}"
						else
							echo "Unable to delete ${FILE}"
						fi
						;;
					# Empty string defaults to N (save file)
					n|N|"")
						echo "Saving ${FILE}"
						;;
					*)
						echo "Invalid response. Saving ${FILE}"
						;;
				esac
				;;
		esac
		break
	done
}

until [[ -n ${QUIT} ]]; do
	select_loop
done

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [gentoo-dev] elog file reader
  2005-12-22 17:08 [gentoo-dev] elog file reader Donnie Berkholz
@ 2005-12-23  6:05 ` Colin Kingsley
  0 siblings, 0 replies; 2+ messages in thread
From: Colin Kingsley @ 2005-12-23  6:05 UTC (permalink / raw
  To: gentoo-dev

[-- Attachment #1: Type: text/plain, Size: 784 bytes --]

Donnie Berkholz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> I hacked up a quick script this morning to handle the new elog scripts
> in 2.1. Perhaps some of you can make use of it or help me improve it.
> 
> Thanks,
> Donnie

I also have a partially completed utility for this, which I had intended 
to complete when my final exams ended. I'll include it here as well in 
case anybody wants to play with it. If nobody has a preference wither 
way, I'll do some more work on this when I get home from college in a 
day or two.

Please understand that this is incomplete code that doesn't yet work 
without some tinkering, and I haven't touched it in a few months because 
of school work. I'm just sharing what I've already got in case anybody 
is interested.

Tercel

[-- Attachment #2: elogread --]
[-- Type: text/plain, Size: 2527 bytes --]

#!/usr/bin/python

"""
elogread-0.1 written by Colin Kingsley (tercel@gentoo.org)
Utility for reading messages logged by ebuilds.
"""

__revision__ = '@VER@'

import os, sys, time
from portage import settings
from getopt import GetoptError, gnu_getopt

logdir = settings['PORTAGE_TMPDIR']+'/elogs'

class Log:
	"""Abstracts log messages"""

	def __init__(self, name, index):
		self.name = name

		split = name.split(':')
		self.cat = split[0]
		self.pvr = split[1]
		self.tstr = split[2].replace('.log', '')

		self.index = index + 1
	
	def list(self):
		"""parses the name of the logfile, and prints it."""

#		get the time string from the filename, and make a gmt time tuple
		gtt = time.strptime(self.tstr, '%Y%m%d-%H%M%S')
#		turn the gtt into second since epoch, and turn it into localtime
		lts = time.mktime(gtt) - time.timezone + (3600 * time.daylight)

		print '%s) %s/%s:\t\t%s' % (self.index, self.cat, self.pvr,
				time.strftime('%a %b %d, %I:%M%p, %Y', time.localtime(lts)))
	
	def display(self):
		"""Formats and displays the contents of a log"""
		self.list()
		print '!!! display code incomplete !!!'
#TODO: implement display finctionality
	
	def remove(self):
		"""Deletes the log file referenced by the instance."""
		os.remove(logdir +'/' + self.name)

		print 'Logs removed:'
		self.list()
		

def parse_args(argv):
	"""parses arguments using gnu_getopt"""

	try:
		ops, args = gnu_getopt(argv[1:], 'hld:r:')
	except GetoptError, e:
		usage(e)

	if args:
		usage('Unexpected arguments passed.')
	
	if len(ops) == 0:
		usage()

	if len(ops) > 1:
		usage('Multipe options passed. Please oly use one.')
	
	return ops[0]


def usage(error=None):
	if error:
		print error
	print 'usage: %s [-l][-h]\n' % sys.argv[0]
	print '-l:\tlist mode: Shows a list of unread log messages.'
	print '-h:\thelp: displays this simple help text.'
	sys.exit(1)

def main(argv):
	ops = parse_args(argv)

	if ops[0] == '-h':	#if we are gonna bail, don't make 'em wait
		usage()
	
	logs = []			#create log objects
	index = 0
	for name in os.listdir(logdir):
		if name.endswith('.log'):
			logs.append(Log(name, index))
			index = index + 1


	## Start doing things ##

	#list
	if ops[0] == '-l':
		print 'Unread logs:'
		if len(logs) == 0:
			print 'None!'
			sys.exit(0)
		else:
			for log in logs:
				log.list()
			sys.exit(0)
	
	#display
	if ops[0] == '-d':
		logs[int(ops[1]) - 1].display()
		sys.exit(0)
	
	#remove
	if ops[0] == '-r':
		logs[int(ops[1]) - 1].remove()
		sys.exit(0)

if __name__ == '__main__':
	main(sys.argv)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-12-23  6:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-12-22 17:08 [gentoo-dev] elog file reader Donnie Berkholz
2005-12-23  6:05 ` Colin Kingsley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox