aboutsummaryrefslogtreecommitdiffstats
path: root/pym/portage/emaint/modules/logs/logs.py
blob: 32c8508f70f7ecfd7c240b687afa61b039b58310 (plain) (blame)
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
# Copyright 2005-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import portage
from portage import os
from portage.util import shlex_split, varexpand

## default clean command from make.globals
## PORT_LOGDIR_CLEAN = 'find "${PORT_LOGDIR}" -type f ! -name "summary.log*" -mtime +7 -delete'

class CleanLogs(object):

	short_desc = "Clean PORT_LOGDIR logs"

	def name():
		return "logs"
	name = staticmethod(name)


	def can_progressbar(self, func):
		return False


	def check(self, **kwargs):
		if kwargs:
			options = kwargs.get('options', None)
			if options:
				options['pretend'] = True
		return self.clean(**kwargs)


	def clean(self, **kwargs):
		"""Log directory cleaning function
		
		@param **kwargs: optional dictionary of values used in this function are:
			settings: portage settings instance: defaults to portage.settings
				"PORT_LOGDIR": directory to clean
				"PORT_LOGDIR_CLEAN": command for cleaning the logs.
			options: dict: 
				'NUM': int: number of days
				'pretend': boolean
				'eerror': defaults to None, optional output module to output errors.
				'einfo': defaults to None, optional output module to output info msgs.
		"""
		messages = []
		num_of_days = None
		if kwargs:
			# convuluted, I know, but portage.settings does not exist in
			# kwargs.get() when called from _emerge.main.clean_logs()
			settings = kwargs.get('settings', None)
			if not settings:
				settings = portage.settings
			options = kwargs.get('options', None)
			if options:
				num_of_days = options.get('NUM', None)
				pretend = options.get('pretend', False)
				eerror = options.get('eerror', None)
				einfo = options.get('einfo', None)

		clean_cmd = settings.get("PORT_LOGDIR_CLEAN")
		if clean_cmd:
			clean_cmd = shlex_split(clean_cmd)
			if '-mtime' in clean_cmd and num_of_days is not None:
				if num_of_days == 0:
					i = clean_cmd.index('-mtime')
					clean_cmd.remove('-mtime')
					clean_cmd.pop(i)
				else:
					clean_cmd[clean_cmd.index('-mtime') +1] = \
						'+%s' % str(num_of_days)
			if pretend:
				if "-delete" in clean_cmd:
					clean_cmd.remove("-delete")

		if not clean_cmd:
			return []
		rval = self._clean_logs(clean_cmd, settings)
		messages += self._convert_errors(rval, eerror, einfo)
		return messages


	@staticmethod
	def _clean_logs(clean_cmd, settings):
		logdir = settings.get("PORT_LOGDIR")
		if logdir is None or not os.path.isdir(logdir):
			return

		variables = {"PORT_LOGDIR" : logdir}
		cmd = [varexpand(x, mydict=variables) for x in clean_cmd]

		try:
			rval = portage.process.spawn(cmd, env=os.environ)
		except portage.exception.CommandNotFound:
			rval = 127
		return rval


	@staticmethod
	def _convert_errors(rval, eerror=None, einfo=None):
		msg = []
		if rval != os.EX_OK:
			msg.append("PORT_LOGDIR_CLEAN command returned %s"
				% ("%d" % rval if rval else "None"))
			msg.append("See the make.conf(5) man page for "
				"PORT_LOGDIR_CLEAN usage instructions.")
			if eerror:
				for m in msg:
					eerror(m)
		else:
			msg.append("PORT_LOGDIR_CLEAN command succeeded")
			if einfo:
				for m in msg:
					einfo(m)
		return msg