summaryrefslogtreecommitdiff
blob: 736ce3fdcaf0a2b14c7cde83bf37b1ce040e427b (plain)
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
228
229
230
231
232
233
234
# portage: news management code
# Copyright 2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

from portage_const import PRIVATE_PATH, INCREMENTALS, PROFILE_PATH
from portage import config, vartree, vardbapi, portdbapi
from portage_util import ensure_dirs
from portage_data import portage_gid
from portage_locks import lockfile, unlockfile

import os, re

class NewsManager(object):
	"""
	This object manages GLEP 42 style news items.  It will cache news items
	that have previously shown up and notify users when there are relevant news
	items that apply to their packages that the user has not previously read.
	
	Creating a news manager requires:
	root - typically ${ROOT} see man make.conf and man emerge for details
	NEWS_PATH - path to news items; usually $REPODIR/metadata/news
	UNREAD_PATH - path to the news.repoid.unread file; this helps us track news items
	
	"""

	TIMESTAMP_FILE = "news-timestamp"

	def __init__( self, root, NEWS_PATH, UNREAD_PATH, LANGUAGE_ID='en' ):
		self.NEWS_PATH = NEWS_PATH
		self.UNREAD_PATH = UNREAD_PATH
		self.TIMESTAMP_PATH = os.path.join( root, PRIVATE_PATH, NewsManager.TIMESTAMP_FILE )
		self.target_root = root
		self.LANGUAGE_ID = LANGUAGE_ID
		self.config = config( config_root = os.environ.get("PORTAGE_CONFIGROOT", "/"),
				target_root = root, config_incrementals = INCREMENTALS)
		self.vdb = vardbapi( settings = self.config, root = root,
			vartree = vartree( root = root, settings = self.config ) )
		self.portdb = portdbapi( porttree_root = self.config["PORTDIR"], mysettings = self.config )

		# Ensure that the unread path exists and is writable.
		dirmode  = 02070
		modemask =    02
		ensure_dirs(self.UNREAD_PATH, mode=dirmode, mask=modemask, gid=portage_gid)

	def updateItems( self, repoid ):
		"""
		Figure out which news items from NEWS_PATH are both unread and relevant to
		the user (according to the GLEP 42 standards of relevancy).  Then add these
		items into the news.repoid.unread file.
		"""

		repos = self.portdb.getRepositories()
		if repoid not in repos:
			raise ValueError("Invalid repoID: %s" % repoid)

		if os.path.exists(self.TIMESTAMP_PATH):
			timestamp = os.stat(self.TIMESTAMP_PATH).st_mtime
		else:
			timestamp = 0

		path = os.path.join( self.portdb.getRepositoryPath( repoid ), self.NEWS_PATH )
		news = os.listdir( path )
		updates = []
		for item in news:
			try:
				file = os.path.join( path, item, item + "." + self.LANGUAGE_ID + ".txt")
				tmp = NewsItem( file , timestamp )
			except TypeError:
				continue

			if tmp.isRelevant( profile=os.readlink(PROFILE_PATH), config=config, vardb=self.vdb):
				updates.append( tmp )
		del path
		
		path = os.path.join( self.UNREAD_PATH, "news-" + repoid + ".unread" )
		unread_lock = lockfile( path )
		unread_file = open( path, "a" )
		for item in updates:
			unread_file.write( item.path + "\n" )

		unread_file.close()
		unlockfile(unread_lock)
		
		# Touch the timestamp file
		f = open(self.TIMESTAMP_PATH, "w")
		f.close()

	def getUnreadItems( self, repoid, update=False ):
		"""
		Determine if there are unread relevant items in news.repoid.unread.
		If there are unread items return their number.
		If update is specified, updateNewsItems( repoid ) will be called to
		check for new items.
		"""
		
		if update:
			self.updateItems( repoid )

		unreadfile = os.path.join( self.UNREAD_PATH, "news-"+ repoid +".unread" )
		unread_lock = lockfile(unreadfile)
		if os.path.exists( unreadfile ):
			unread = open( unreadfile ).readlines()
			if len(unread):
				return len(unread)
		unlockfile(unread_lock)

_installedRE = re.compile("Display-If-Installed:(.*)\n")
_profileRE = re.compile("Display-If-Profile:(.*)\n")
_keywordRE = re.compile("Display-If-Keyword:(.*)\n")

class NewsItem(object):
	"""
	This class encapsulates a GLEP 42 style news item.
	It's purpose is to wrap parsing of these news items such that portage can determine
	whether a particular item is 'relevant' or not.  This requires parsing the item
	and determining 'relevancy restrictions'; these include "Display if Installed" or
	"display if arch: x86" and so forth.

	Creation of a news item involves passing in the path to the particular news item.

	"""
	
	def __init__( self, path, cache_mtime = 0 ):
		""" 
		For a given news item we only want if it path is a file and it's 
		mtime is newer than the cache'd timestamp.
		"""
		if not os.path.isfile( path ):
			raise TypeError
		if not os.stat( path ).st_mtime > cache_mtime:
			raise TypeError
		self.path = path
		self._parsed = False

	def isRelevant( self, vardb, config, profile ):
		"""
		This function takes a dict of keyword arguments; one should pass in any
		objects need to do to lookups (like what keywords we are on, what profile,
		and a vardb so we can look at installed packages).
		Each restriction will pluck out the items that are required for it to match
		or raise a ValueError exception if the required object is not present.
		"""

		if not len(self.restrictions):
			return True # no restrictions to match means everyone should see it
		
		kwargs = { 'vardb' : vardb,
			   'config' : config,
			   'profile' : profile }

		for restriction in self.restrictions:
			if restriction.checkRestriction( **kwargs ):
				return True
			
		return False # No restrictions were met; thus we aren't relevant :(

	def parse( self ):
		lines = open(self.path).readlines()
		self.restrictions = []
		for line in lines:
			#Optimization to ignore regex matchines on lines that
			#will never match
			if not line.startswith("D"):
				continue
			restricts = {  _installedRE : DisplayInstalledRestriction,
					_profileRE : DisplayProfileRestriction,
					_keywordRE : DisplayKeywordRestriction }
			for regex, restriction in restricts.iteritems():
				match = regex.match(line)
				if match:
					self.restrictions.append( restriction( match.groups()[0].strip() ) )
					continue
		self._parsed = True

	def __getattr__( self, attr ):
		if not self._parsed:
			self.parse()
		return self.__dict__[attr]

class DisplayRestriction(object):
	"""
	A base restriction object representing a restriction of display.
	news items may have 'relevancy restrictions' preventing them from
	being important.  In this case we need a manner of figuring out if
	a particular item is relevant or not.  If any of it's restrictions
	are met, then it is displayed
	"""

	def checkRestriction( self, **kwargs ):
		raise NotImplementedError("Derived class should over-ride this method")

class DisplayProfileRestriction(DisplayRestriction):
	"""
	A profile restriction where a particular item shall only be displayed
	if the user is running a specific profile.
	"""

	def __init__( self, profile ):
		self.profile = profile

	def checkRestriction( self, **kwargs ):
		if self.profile == kwargs['profile']:
			return True
		return False

class DisplayKeywordRestriction(DisplayRestriction):
	"""
	A keyword restriction where a particular item shall only be displayed
	if the user is running a specific keyword.
	"""

	def __init__( self, keyword ):
		self.keyword = keyword

	def checkRestriction( self, **kwargs ):
		if kwargs['config']["ARCH"] == self.keyword:
			return True
		return False

class DisplayInstalledRestriction(DisplayRestriction):
	"""
	An Installation restriction where a particular item shall only be displayed
	if the user has that item installed.
	"""
	
	def __init__( self, cpv ):
		self.cpv = cpv

	def checkRestriction( self, **kwargs ):
		vdb = kwargs['vardb']
		if vdb.match( self.cpv ):
			return True
		return False