author | Danek Duvall <danek.duvall@sun.com> |
Wed, 09 Apr 2008 16:14:58 -0700 | |
changeset 313 | c4d18a029dfd |
parent 310 | 8fa744df663a |
child 382 | 95304bd3b365 |
permissions | -rw-r--r-- |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
2 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
3 |
# CDDL HEADER START |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
4 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
5 |
# The contents of this file are subject to the terms of the |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
6 |
# Common Development and Distribution License (the "License"). |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
7 |
# You may not use this file except in compliance with the License. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
8 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
9 |
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
10 |
# or http://www.opensolaris.org/os/licensing. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
11 |
# See the License for the specific language governing permissions |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
12 |
# and limitations under the License. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
13 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
14 |
# When distributing Covered Code, include this CDDL HEADER in each |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
15 |
# file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
16 |
# If applicable, add the following below this CDDL HEADER, with the |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
17 |
# fields enclosed by brackets "[]" replaced with your own identifying |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
18 |
# information: Portions Copyright [yyyy] [name of copyright owner] |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
19 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
20 |
# CDDL HEADER END |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
21 |
# |
260
a1b77322abb2
255 Symbolic HTTP response codes should be used
Shawn Walker <swalker@opensolaris.org>
parents:
258
diff
changeset
|
22 |
# Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
23 |
# Use is subject to license terms. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
24 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
25 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
26 |
import os |
313
c4d18a029dfd
761 'import timedanek' in subprocess_method.py's __main__
Danek Duvall <danek.duvall@sun.com>
parents:
310
diff
changeset
|
27 |
import errno |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
28 |
import re |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
29 |
import time |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
30 |
import datetime |
260
a1b77322abb2
255 Symbolic HTTP response codes should be used
Shawn Walker <swalker@opensolaris.org>
parents:
258
diff
changeset
|
31 |
import httplib |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
32 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
33 |
import pkg.fmri as fmri |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
34 |
import pkg.catalog as catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
35 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
36 |
class UpdateLogException(Exception): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
37 |
def __init__(self, args=None): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
38 |
self.args = args |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
39 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
40 |
class UpdateLog(object): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
41 |
"""The update log is a mechanism that allows clients and servers to make |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
42 |
incremental updates to their package catalogs. The server logs |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
43 |
whether it has added or removed a package, the time when the action |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
44 |
occurred, and the name of the package added or removed. The client |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
45 |
requests a list of actions that have been applied to the server's |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
46 |
catalog since a particular time in the past. The server is then able to |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
47 |
send this list of actions, allowing the client to apply these changes to |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
48 |
its catalog. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
49 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
50 |
This allows the client to obtain incremental updates to its catalog, |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
51 |
instead of having to download an entire (and largely duplicated) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
52 |
catalog each time a refresh is requested. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
53 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
54 |
The UpdateLog must have an associated catalog; however, |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
55 |
Catalogs are not required to have an UpdateLog. The UpdateLog |
220
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
56 |
allows catalogs to support incremental updates. |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
57 |
|
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
58 |
The catalog format is a + or -, an isoformat timestamp, and a catalog |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
59 |
entry in server-side format. They must be in order and separated by |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
60 |
spaces.""" |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
61 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
62 |
def __init__(self, update_root, catalog, maxfiles = 336): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
63 |
"""Create an instance of the UpdateLog. "update_root" is |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
64 |
the root directory for the update log files. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
65 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
66 |
maxfiles is the maximum number of logfiles that |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
67 |
the UpdateLog will keep. A new file is added |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
68 |
for each hour in which there is an update. The |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
69 |
default value of 336 means that we keep 336 hours, or |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
70 |
14 days worth of log history.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
71 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
72 |
self.rootdir = update_root |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
73 |
self.logfd = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
74 |
self.maxfiles = maxfiles |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
75 |
self.catalog = catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
76 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
77 |
if not os.path.exists(update_root): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
78 |
os.makedirs(update_root) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
79 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
80 |
self._setup_logfiles() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
81 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
82 |
def __del__(self): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
83 |
"""Perform any last minute cleanup.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
84 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
85 |
if self.logfd: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
86 |
try: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
87 |
self.logfd.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
88 |
except: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
89 |
pass |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
90 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
91 |
self.logfd = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
92 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
93 |
def add_package(self, fmri, critical = False): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
94 |
"""Record that the catalog has added "fmri".""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
95 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
96 |
# First add FMRI to catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
97 |
ts = self.catalog.add_fmri(fmri, critical) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
98 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
99 |
# Now add update to updatelog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
100 |
self._check_logs() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
101 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
102 |
if not self.logfd: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
103 |
self._begin_log() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
104 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
105 |
if critical: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
106 |
type = "C" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
107 |
else: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
108 |
type = "V" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
109 |
|
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
110 |
# The format for catalog C and V records is described |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
111 |
# in the docstrings for the Catalog class. |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
112 |
|
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
113 |
logstr = "+ %s %s %s\n" % \ |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
114 |
(ts.isoformat(), type, fmri.get_fmri(anarchy = True)) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
115 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
116 |
self.logfd.write(logstr) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
117 |
self.logfd.flush() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
118 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
119 |
self.last_update = ts |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
120 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
121 |
return ts |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
122 |
|
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
123 |
def rename_package(self, srcname, srcvers, destname, destvers): |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
124 |
"""Record that package oldname has been renamed to newname, |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
125 |
effective as of version vers.""" |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
126 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
127 |
# Record rename in catalog |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
128 |
ts, rr = self.catalog.rename_package(srcname, srcvers, destname, |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
129 |
destvers) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
130 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
131 |
# Now add rename record to updatelog |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
132 |
self._check_logs() |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
133 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
134 |
if not self.logfd: |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
135 |
self._begin_log() |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
136 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
137 |
# The format for a catalog rename record is described |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
138 |
# in the docstring for the RenameRecord class. |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
139 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
140 |
logstr = "+ %s %s\n" % (ts.isoformat(), rr) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
141 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
142 |
self.logfd.write(logstr) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
143 |
self.logfd.flush() |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
144 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
145 |
self.last_update = ts |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
146 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
147 |
return ts |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
148 |
|
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
149 |
def _begin_log(self): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
150 |
"""Open a log-file so that the UpdateLog can write updates |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
151 |
into it.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
152 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
153 |
filenm = time.strftime("%Y%m%d%H") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
154 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
155 |
ftime = datetime.datetime( |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
156 |
*time.strptime(filenm, "%Y%m%d%H")[0:6]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
157 |
delta = datetime.timedelta(hours=1) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
158 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
159 |
self.close_time = ftime + delta |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
160 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
161 |
self.logfd = file(os.path.join(self.rootdir, filenm), "a") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
162 |
|
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
163 |
if filenm not in self.logfiles: |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
164 |
self.logfiles.append(filenm) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
165 |
self.curfiles += 1 |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
166 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
167 |
if not self.first_update: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
168 |
self.first_update = ftime |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
169 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
170 |
def _check_logs(self): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
171 |
"""Check to see if maximum number of logfiles has been exceeded. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
172 |
If so, rotate the logs. Also, if a log is open, check to |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
173 |
see if it needs to be closed.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
174 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
175 |
if self.logfd and self.close_time < datetime.datetime.now(): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
176 |
self.logfd.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
177 |
self.logfd = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
178 |
self.close_time = 0 |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
179 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
180 |
if self.curfiles < self.maxfiles: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
181 |
return |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
182 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
183 |
excess = self.curfiles - self.maxfiles |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
184 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
185 |
to_remove = self.logfiles[0:excess] |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
186 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
187 |
for r in to_remove: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
188 |
filepath = os.path.join(self.rootdir, "%s" % r) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
189 |
os.unlink(filepath) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
190 |
self.curfiles -= 1 |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
191 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
192 |
del self.logfiles[0:excess] |
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
193 |
|
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
194 |
self.first_update = datetime.datetime(*time.strptime( |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
195 |
self.logfiles[0], "%Y%m%d%H")[0:6]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
196 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
197 |
def enough_history(self, ts): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
198 |
"""Returns true if the timestamp is so far behind the |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
199 |
update log, that there is not enough log history to bring |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
200 |
the client up to date.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
201 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
202 |
# Absence of server-side log history also counts as |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
203 |
# not enough history. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
204 |
if not self.last_update or not self.first_update: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
205 |
return False |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
206 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
207 |
if ts < self.first_update: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
208 |
return False |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
209 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
210 |
return True |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
211 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
212 |
@staticmethod |
310
8fa744df663a
975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents:
270
diff
changeset
|
213 |
def recv(c, path, ts, auth): |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
214 |
"""Take a connection object and a catalog path. This method |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
215 |
receives a catalog from the server. If it is an incremental |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
216 |
update, it is processed by the updatelog. If it is a full |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
217 |
update, we call the catalog to handle the request. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
218 |
Ts is the timestamp when the local copy of the catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
219 |
was last modified.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
220 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
221 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
222 |
update_type = c.info().getheader("X-Catalog-Type", "full") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
223 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
224 |
if update_type == 'incremental': |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
225 |
UpdateLog._recv_updates(c, path, ts) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
226 |
else: |
310
8fa744df663a
975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents:
270
diff
changeset
|
227 |
catalog.recv(c, path, auth) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
228 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
229 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
230 |
@staticmethod |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
231 |
def _recv_updates(filep, path, cts): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
232 |
"""A static method that takes a file-like object, |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
233 |
a path, and a timestamp. This is the other half of |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
234 |
send_updates(). It reads a stream as an incoming updatelog and |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
235 |
modifies the catalog on disk.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
236 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
237 |
if not os.path.exists(path): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
238 |
os.makedirs(path) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
239 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
240 |
# Build a list of FMRIs that this update would add, check to |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
241 |
# make sure that they aren't present in the catalog, then append |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
242 |
# the fmris. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
243 |
mts = catalog.ts_to_datetime(cts) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
244 |
cts = mts |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
245 |
added = 0 |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
246 |
npkgs = 0 |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
247 |
add_lines = [] |
270
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
248 |
add_pkg_names = set() |
220
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
249 |
unknown_lines = [] |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
250 |
attrs = {} |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
251 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
252 |
for s in filep: |
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
253 |
l = s.split(None, 3) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
254 |
if len(l) < 4: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
255 |
continue |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
256 |
|
220
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
257 |
elif l[2] not in catalog.known_prefixes: |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
258 |
# Add unknown line directly to catalog. |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
259 |
# This can be post-processed later, when it |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
260 |
# becomes known. |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
261 |
# |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
262 |
# XXX Notify user that unknown entry was added? |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
263 |
ts = catalog.ts_to_datetime(l[1]) |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
264 |
if ts > cts: |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
265 |
if ts > mts: |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
266 |
mts = ts |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
267 |
str = "%s %s\n" % (l[2], l[3]) |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
268 |
unknown_lines.append(str) |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
269 |
|
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
270 |
elif l[0] == "+": |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
271 |
# This is a known entry type. |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
272 |
# Create a list of FMRIs to add, since |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
273 |
# additional inspection is required |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
274 |
ts = catalog.ts_to_datetime(l[1]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
275 |
if ts > cts: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
276 |
if ts > mts: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
277 |
mts = ts |
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
278 |
|
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
279 |
# The format for C and V records |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
280 |
# is described in the Catalog's |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
281 |
# docstring. |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
282 |
if l[2] in tuple("CV"): |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
283 |
f = fmri.PkgFmri(l[3]) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
284 |
str = "%s %s %s %s\n" % \ |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
285 |
(l[2], "pkg", f.pkg_name, |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
286 |
f.version) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
287 |
add_lines.append(str) |
270
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
288 |
add_pkg_names.add(f.pkg_name) |
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
289 |
added += 1 |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
290 |
# The format for R records is |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
291 |
# described in the docstring for |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
292 |
# RenameRecords |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
293 |
elif l[2] == "R": |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
294 |
sf, sv, rf, rv = l[3].split() |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
295 |
str = "%s %s %s %s %s\n" % \ |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
296 |
(l[2], sf, sv, rf, rv) |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
297 |
add_lines.append(str) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
298 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
299 |
# Verify that they aren't already in the catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
300 |
catf = file(os.path.normpath( |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
301 |
os.path.join(path, "catalog")), "a+") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
302 |
catf.seek(0) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
303 |
for c in catf: |
258
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
304 |
if c[0] in tuple("CV"): |
b3b7592412ec
270 Need ability to rename packages
johansen <johansen@sun.com>
parents:
239
diff
changeset
|
305 |
npkgs += 1 |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
306 |
if c in add_lines: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
307 |
catf.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
308 |
raise UpdateLogException, \ |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
309 |
"Package %s is already in the catalog" % \ |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
310 |
c |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
311 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
312 |
# Write the new entries to the catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
313 |
catf.seek(0, 2) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
314 |
catf.writelines(add_lines) |
220
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
315 |
if len(unknown_lines) > 0: |
afceea12576c
320 pkg should ignore catalog lines it doesn't recognize
johansen <johansen@sun.com>
parents:
215
diff
changeset
|
316 |
catf.writelines(unknown_lines) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
317 |
catf.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
318 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
319 |
# Now re-write npkgs and Last-Modified in attributes file |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
320 |
afile = file(os.path.normpath(os.path.join(path, "attrs")), "r") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
321 |
attrre = re.compile('^S ([^:]*): (.*)') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
322 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
323 |
for entry in afile: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
324 |
m = attrre.match(entry) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
325 |
if m != None: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
326 |
attrs[m.group(1)] = m.group(2) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
327 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
328 |
afile.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
329 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
330 |
# Update the attributes we care about |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
331 |
attrs["npkgs"] = npkgs + added |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
332 |
attrs["Last-Modified"] = mts.isoformat() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
333 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
334 |
# Write attributes back out |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
335 |
afile = file(os.path.normpath(os.path.join(path, "attrs")), "w") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
336 |
for a in attrs.keys(): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
337 |
s = "S %s: %s\n" % (a, attrs[a]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
338 |
afile.write(s) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
339 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
340 |
afile.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
341 |
|
270
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
342 |
# Update list of package names. If it doesn't exist, rebuild it |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
343 |
# and then append the new packages. |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
344 |
try: |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
345 |
pkg_names = catalog.Catalog.load_pkg_names(path) |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
346 |
except IOError, e: |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
347 |
if e.errno == errno.ENOENT: |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
348 |
pkg_names = catalog.Catalog.build_pkg_names( |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
349 |
path) |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
350 |
else: |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
351 |
raise |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
352 |
|
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
353 |
pkg_names.update(add_pkg_names) |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
354 |
catalog.Catalog.save_pkg_names(path, pkg_names) |
904b204d9436
633 Improve search time by caching package names
johansen <johansen@sun.com>
parents:
260
diff
changeset
|
355 |
|
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
356 |
return True |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
357 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
358 |
def send(self, request): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
359 |
"""This method takes a http request and sends a catalog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
360 |
to the client. If the client it capable of receiving an |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
361 |
incremental update, we'll send that. Otherwise, it calls |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
362 |
into the catalog to send a full copy.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
363 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
364 |
modified = request.headers.getheader("If-Modified-Since") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
365 |
ts = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
366 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
367 |
if modified: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
368 |
try: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
369 |
ts = catalog.ts_to_datetime(modified) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
370 |
except ValueError: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
371 |
ts = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
372 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
373 |
# Incremental catalog updates |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
374 |
if ts and self.up_to_date(ts): |
260
a1b77322abb2
255 Symbolic HTTP response codes should be used
Shawn Walker <swalker@opensolaris.org>
parents:
258
diff
changeset
|
375 |
request.send_response(httplib.NOT_MODIFIED) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
376 |
request.send_header('Content-type', 'text/plain') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
377 |
request.send_header('Last-Modified', |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
378 |
self.catalog.last_modified()) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
379 |
request.send_header('X-Catalog-Type', 'incremental') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
380 |
request.end_headers() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
381 |
return |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
382 |
elif ts and self.enough_history(ts): |
260
a1b77322abb2
255 Symbolic HTTP response codes should be used
Shawn Walker <swalker@opensolaris.org>
parents:
258
diff
changeset
|
383 |
request.send_response(httplib.OK) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
384 |
request.send_header('Content-type', 'text/plain') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
385 |
request.send_header('Last-Modified', |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
386 |
self.catalog.last_modified()) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
387 |
request.send_header('X-Catalog-Type', 'incremental') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
388 |
request.end_headers() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
389 |
self._send_updates(ts, request.wfile) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
390 |
return |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
391 |
else: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
392 |
# Not enough history, or full catalog requested |
260
a1b77322abb2
255 Symbolic HTTP response codes should be used
Shawn Walker <swalker@opensolaris.org>
parents:
258
diff
changeset
|
393 |
request.send_response(httplib.OK) |
215
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
394 |
request.send_header('Content-type', 'text/plain') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
395 |
request.send_header('X-Catalog-Type', 'full') |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
396 |
request.end_headers() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
397 |
self.catalog.send(request.wfile) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
398 |
return |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
399 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
400 |
def _send_updates(self, ts, filep): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
401 |
"""Look through the logs for updates that have occurred |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
402 |
after timestamp. Write these changes to the file-like object |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
403 |
supplied in filep.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
404 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
405 |
# The files that need to be examined depend upon the timestamp |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
406 |
# supplied by the client, and the log files actually present. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
407 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
408 |
# The following cases exist: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
409 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
410 |
# 1. No updates have occurred since timestamp. Send nothing. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
411 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
412 |
# 2. Timestamp is older than oldest log record. Client needs to |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
413 |
# download full catalog. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
414 |
# |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
415 |
# 3. Timestamp falls within a range for which update records |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
416 |
# exist. If the timestamp is in the middle of a log-file, open |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
417 |
# that file, send updates newer than timestamp, and then send |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
418 |
# all newer files. Otherwise, just send updates from the newer |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
419 |
# log files. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
420 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
421 |
if self.up_to_date(ts) or not self.enough_history(ts): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
422 |
return |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
423 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
424 |
# Remove minutes, seconds, and microsec from timestamp |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
425 |
rts = datetime.datetime(ts.year, ts.month, ts.day, ts.hour) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
426 |
assert rts < ts |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
427 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
428 |
# send data from logfiles newer or equal to rts |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
429 |
for lf in self.logfiles: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
430 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
431 |
lf_time = datetime.datetime( |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
432 |
*time.strptime(lf, "%Y%m%d%H")[0:6]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
433 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
434 |
if lf_time >= rts: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
435 |
fn = "%s" % lf |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
436 |
logf = file(os.path.join(self.rootdir, fn), "r") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
437 |
for line in logf: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
438 |
filep.write(line) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
439 |
logf.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
440 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
441 |
def _setup_logfiles(self): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
442 |
"""Scans the directory containing the update log's files. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
443 |
Sets up any necessary state for the UpdateLog.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
444 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
445 |
# Store names of logfiles as integers for easier comparison |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
446 |
self.logfiles = [f for f in os.listdir(self.rootdir)] |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
447 |
self.logfiles.sort() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
448 |
self.curfiles = len(self.logfiles) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
449 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
450 |
if self.curfiles == 0: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
451 |
self.last_update = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
452 |
self.first_update = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
453 |
return |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
454 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
455 |
# Find the last update by opening the most recent logfile |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
456 |
# and finding its last entry |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
457 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
458 |
filenm = self.logfiles[self.curfiles - 1] |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
459 |
logf = file(os.path.join(self.rootdir, filenm), "r") |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
460 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
461 |
last_update = None |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
462 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
463 |
for ln in logf: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
464 |
lspl = ln.split(" ", 4) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
465 |
if len(lspl) < 4: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
466 |
continue |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
467 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
468 |
current_ts = catalog.ts_to_datetime(lspl[1]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
469 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
470 |
if not last_update or current_ts > last_update: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
471 |
last_update = current_ts |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
472 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
473 |
logf.close() |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
474 |
self.last_update = last_update |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
475 |
self.first_update = datetime.datetime( |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
476 |
*time.strptime(self.logfiles[0], "%Y%m%d%H")[0:6]) |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
477 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
478 |
def up_to_date(self, ts): |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
479 |
"""Returns true if the timestamp is up to date.""" |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
480 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
481 |
if self.last_update and ts >= self.last_update: |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
482 |
return True |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
483 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
484 |
return False |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
485 |
|
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
486 |
# Allow these methods to be invoked without explictly naming the UpdateLog |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
487 |
# class. |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
488 |
recv = UpdateLog.recv |
c10719939c6d
245 Need incremental update mechanism for the catalog
johansen <johansen@sun.com>
parents:
diff
changeset
|
489 |