Cross Reference: /pkg/src/modules/catalog.py
catalog.py revision 261
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
#!/usr/bin/python
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
"""Interfaces and implementation for the Catalog object, as well as functions
that operate on lists of package FMRIs."""
import os
import re
import urllib
import errno
import dbm
import signal
import threading
import datetime
import pkg.fmri as fmri
import pkg.version as version
import pkg.manifest as manifest
from pkg.subprocess_method import Mopen, PIPE
class CatalogException(Exception):
def __init__(self, args=None):
self.args = args
class RenameException(Exception):
def __init__(self, args=None):
self.args = args
class Catalog(object):
"""A Catalog is the representation of the package FMRIs available to
this client or repository. Both purposes utilize the same storage
format.
The serialized structure of the repository is an unordered list of
available package versions, followed by an unordered list of
incorporation relationships between packages. This latter section
allows the graph to be topologically sorted by the client.
S Last-Modified: [timespec]
XXX A authority mirror-uri ...
XXX ...
V fmri
V fmri
...
C fmri
C fmri
...
I fmri fmri
I fmri fmri
...
"""
# XXX Mirroring records also need to be allowed from client
# configuration, and not just catalogs.
#
# XXX It would be nice to include available tags and package sizes,
# although this could also be calculated from the set of manifests.
#
# XXX Current code is O(N_packages) O(M_versions), should be
# O(1) O(M_versions), and possibly O(1) O(1).
#
# XXX Initial estimates suggest that the Catalog could be composed of
# 1e5 - 1e7 lines. Catalogs across these magnitudes will need to be
# spread out into chunks, and may require a delta-oriented update
# interface.
def __init__(self, cat_root, authority = None, pkg_root = None):
"""Create a catalog. If the path supplied does not exist,
this will create the required directory structure.
Otherwise, if the directories are already in place, the
existing catalog is opened. If pkg_root is specified
and no catalog is found at cat_root, the catalog will be
rebuilt. authority names the authority that
is represented by this catalog."""
self.catalog_root = cat_root
self.attrs = {}
self.auth = authority
self.renamed = None
self.searchdb_update_handle = None
self.searchdb = None
self._search_available = False
self.deferred_searchdb_updates = []
# We need to lock the search database against multiple
# simultaneous updates from separate threads closing
# publication transactions.
self.searchdb_lock = threading.Lock()
self.pkg_root = pkg_root
if self.pkg_root:
self.searchdb_file = os.path.dirname(self.pkg_root) + \
"/search"
self.attrs["npkgs"] = 0
if not os.path.exists(cat_root):
os.makedirs(cat_root)
catpath = os.path.normpath(os.path.join(cat_root, "catalog"))
if pkg_root is not None:
self.build_catalog()
self.load_attrs()
self.check_prefix()
def add_fmri(self, fmri, critical = False):
"""Add a package, named by the fmri, to the catalog.
Throws an exception if an identical package is already
present. Throws an exception if package has no version."""
if fmri.version == None:
raise CatalogException, \
"Unversioned FMRI not supported: %s" % fmri
# Callers should verify that the FMRI they're going to add is
# valid; however, this check is here in case they're
# lackadaisical
if not self.valid_new_fmri(fmri):
raise CatalogException, \
"Existing renames make adding FMRI %s invalid." \
% fmri
if critical:
pkgstr = "C %s\n" % fmri.get_fmri(anarchy = True)
else:
pkgstr = "V %s\n" % fmri.get_fmri(anarchy = True)
pathstr = os.path.normpath(os.path.join(self.catalog_root,
"catalog"))
pfile = file(pathstr, "a+")
pfile.seek(0)
for entry in pfile:
if entry == pkgstr:
pfile.close()
raise CatalogException, \
"Package %s is already in the catalog" % \
fmri
pfile.write(pkgstr)
pfile.close()
self.attrs["npkgs"] += 1
ts = datetime.datetime.now()
self.set_time(ts)
return ts
def added_prefix(self, p):
"""Perform any catalog transformations necessary if
prefix p is found in the catalog. Previously, we didn't
know how to handle this prefix and now we do. If we
need to transform the entry from server to client form,
make sure that happens here."""
# Nothing to do now.
pass
def attrs_as_lines(self):
"""Takes the list of in-memory attributes and returns
a list of strings, each string naming an attribute."""
ret = []
for k,v in self.attrs.items():
s = "S %s: %s\n" % (k, v)
ret.append(s)
return ret
def _fmri_from_path(self, pkg, vers):
"""Helper method that takes the full path to the package
directory and the name of the manifest file, and returns an FMRI
constructed from the information in those components."""
v = version.Version(urllib.unquote(vers), None)
f = fmri.PkgFmri(urllib.unquote(os.path.basename(pkg)), None)
f.version = v
return f
def check_prefix(self):
"""If this version of the catalog knows about new prefixes,
check the on disk catalog to see if we can perform any
transformations based upon previously unknown catalog formats.
This routine will add a catalog attribute if it doesn't exist,
otherwise it checks this attribute against a hard-coded
version-specific tuple to see if new methods were added.
If new methods were added, it will call an additional routine
that updates the on-disk catalog, if necessary."""
# If a prefixes attribute doesn't exist, write one and get on
# with it.
if not "prefix" in self.attrs:
self.attrs["prefix"] = "".join(known_prefixes)
self.save_attrs()
return
# Prefixes attribute does exist. Check if it has changed.
pfx_set = set(self.attrs["prefix"])
# Nothing to do if prefixes haven't changed
if pfx_set == known_prefixes:
return
# If known_prefixes contains a prefix not in pfx_set,
# add the prefix and perform a catalog transform.
new = known_prefixes.difference(pfx_set)
if new:
for p in new:
self.added_prefix(p)
pfx_set.update(new)
# Write out updated prefixes list
self.attrs["prefix"] = "".join(pfx_set)
self.save_attrs()
def build_catalog(self):
"""Walk the on-disk package data and build (or rebuild) the
package catalog and search database."""
try:
idx_mtime = \
os.stat(self.searchdb_file + ".pag").st_mtime
except OSError, e:
if e.errno != errno.ENOENT:
raise
idx_mtime = 0
try:
cat_mtime = os.stat(os.path.join(
self.catalog_root, "catalog")).st_mtime
except OSError, e:
if e.errno != errno.ENOENT:
raise
cat_mtime = 0
fmri_list = []
# XXX eschew os.walk in favor of another os.listdir here?
tree = os.walk(self.pkg_root)
for pkg in tree:
if pkg[0] == self.pkg_root:
continue
for e in os.listdir(pkg[0]):
ver_mtime = os.stat(os.path.join(
self.pkg_root, pkg[0], e)).st_mtime
# XXX force a rebuild despite mtimes?
# XXX queue this and fork later?
if ver_mtime > cat_mtime:
f = self._fmri_from_path(pkg[0], e)
self.add_fmri(f)
print f
# XXX force a rebuild despite mtimes?
# If the database doesn't exist, don't bother
# building the list; we'll just build it all.
if ver_mtime > idx_mtime > 0:
fmri_list.append((pkg[0], e))
# If we have no updates to make to the search database but it
# already exists, just make it available. If we do have updates
# to make (including possibly building it from scratch), fork it
# off into another process; when that's done, we'll mark it
# available.
if not fmri_list and idx_mtime > 0:
self.searchdb = dbm.open(self.searchdb_file, "w")
self._search_available = True
else:
signal.signal(signal.SIGCHLD, self.child_handler)
self.searchdb_update_handle = \
Mopen(self.update_searchdb, [fmri_list], {},
stderr = PIPE)
def child_handler(self, sig, frame):
"""Handler method for the SIGCLD signal. Checks to see if the
search database update child has finished, and enables searching
if it finished successfully, or logs an error if it didn't."""
if not self.searchdb_update_handle:
return
rc = self.searchdb_update_handle.poll()
if rc == 0:
self.searchdb = dbm.open(self.searchdb_file, "w")
self._search_available = True
self.searchdb_update_handle = None
if self.deferred_searchdb_updates:
self.update_searchdb(
self.deferred_searchdb_updates)
elif rc > 0:
# XXX This should be logged instead
print "ERROR building search database:"
print self.searchdb_update_handle.stderr.read()
def update_searchdb(self, fmri_list):
"""Update the search database with the FMRIs passed in via
'fmri_list'. If 'fmri_list' is empty or None, then rebuild the
database from scratch. 'fmri_list' should be a list of tuples
where the first element is the full path to the package name in
pkg_root and the second element is the version string."""
# If we're in the process of updating the database in our
# separate process, and this particular update until that's
# done.
if self.searchdb_update_handle:
self.deferred_searchdb_updates += fmri_list
return
self.searchdb_lock.acquire()
new = False
if fmri_list:
if self.searchdb is None:
self.searchdb = \
dbm.open(self.searchdb_file, "c")
if not self.searchdb.has_key("indir_num"):
self.searchdb["indir_num"] = "0"
else:
# new = True
self.searchdb = dbm.open(self.searchdb_file, "n")
self.searchdb["indir_num"] = "0"
# XXX We should probably iterate over the catalog, for
# cases where manifests have stuck around, but have been
# moved to historical and removed from the catalog.
fmri_list = (
(os.path.join(self.pkg_root, pkg), ver)
for pkg in os.listdir(self.pkg_root)
for ver in os.listdir(
os.path.join(self.pkg_root, pkg))
)
for pkg, vers in fmri_list:
mfst_path = os.path.join(pkg, vers)
mfst = manifest.Manifest()
mfst_file = file(mfst_path)
mfst.set_content(mfst_file.read())
mfst_file.close()
f = self._fmri_from_path(pkg, vers)
self.update_index(f, mfst.search_dict())
self.searchdb_lock.release()
# If we rebuilt the database from scratch ... XXX why would we
# want to do this?
# if new:
# self.searchdb.close()
# self.searchdb = None
self._search_available = True
# Five digits of a base-62 number represents a little over 900 million.
# Assuming 1 million tokens used in a WOS build (current imports use
# just short of 500k, but we don't have all the l10n packages, and may
# not have all the search tokens we want) and keeping every nightly
# build gives us 2.5 years before we run out of token space. We're
# likely to garbage collect manifests and rebuild the db before then.
#
# XXX We're eventually going to run into conflicts with real tokens
# here. This is unlikely until we hit, say "alias", which is a ways
# off, but we should still look at solving this.
idx_tok_len = 5
def next_token(self):
alphabet = "abcdefghijklmnopqrstuvwxyz"
k = "0123456789" + alphabet + alphabet.upper()
num = int(self.searchdb["indir_num"])
s = ""
for i in range(1, self.idx_tok_len + 1):
junk, tail = divmod(num, 62 ** i)
idx, junk = divmod(tail, 62 ** (i - 1))
s = k[idx] + s
# XXX Do we want to log warnings as we approach index capacity?
self.searchdb["indir_num"] = \
str(int(self.searchdb["indir_num"]) + 1)
return s
def update_index(self, fmri, search_dict):
"""Update the search database with the data from the manifest
for 'fmri', which has been collected into 'search_dict'"""
# self.searchdb: token -> (type, fmri, action)
# XXX search_dict doesn't have action info, but should
# Don't update the database if it already has this FMRI's
# indices.
if self.searchdb.has_key(str(fmri)):
return
self.searchdb[str(fmri)] = "True"
for tok_type in search_dict.keys():
for tok in search_dict[tok_type]:
# XXX The database files are so damned huge (if
# holey) because we have zillions of copies of
# the full fmri strings. We might want to
# indirect these as well.
s = "%s %s" % (tok_type, fmri)
s_ptr = self.next_token()
self.searchdb[s_ptr] = s
self.update_chain(tok, s_ptr)
def update_chain(self, token, data_token):
"""Because of the size limitations of the underlying database
records, not only do we have to store pointers to the actual
search data, but once the pointer records fill up, we have to
chain those records up to spillover records. This method adds
the pointer to the data to the end of the last link in the
chain, overflowing as necessary. The search token is passed in
as 'token', and the pointer to the actual data which should be
returned is passed in as 'data_token'."""
while True:
try:
cur = self.searchdb[token]
except KeyError:
cur = ""
l = len(cur)
# According to the ndbm man page, the total length of
# key and value must be less than 1024. Seems like the
# actual value is 1018, probably due to some padding or
# accounting bytes or something. The 2 is for the space
# separator and the plus-sign for the extension token.
# XXX The comparison should be against 1017, but that
# crahes in the if clause below trying to append the
# extension token. Dunno why.
if len(token) + l + self.idx_tok_len + 2 > 1000:
# If we're adding the first element in the next
# link of the chain, add the extension token to
# the end of this link, and put the token
# pointing to the data at the beginning of the
# next link.
if cur[-(self.idx_tok_len + 1)] != "+":
nindir_tok = "+" + self.next_token()
self.searchdb[token] += " " + nindir_tok
self.searchdb[nindir_tok] = data_token
break # from while True; we're done
# If we find an extension token, start looking
# at the next chain link.
else:
token = cur[-(self.idx_tok_len + 1):]
continue
# If we get here, it's safe to append the data token to
# the current link, and get out.
if cur:
self.searchdb[token] += " " + data_token
else:
self.searchdb[token] = data_token
break
def search(self, token):
"""Search through the search database for 'token'. Return a
list of token type / fmri pairs."""
ret = []
while True:
# For each indirect token in the search token's value,
# add its value to the return list. If we see a chain
# token, switch to its value and continue. If we fall
# out of the loop without seeing a chain token, we can
# return.
for tok in self.searchdb[token].split():
if tok[0] == "+":
token = tok
break
else:
ret.append(
self.searchdb[tok].split(" ", 1))
else:
return ret
def get_matching_fmris(self, patterns, matcher = None,
constraint = None, counthash = None):
"""Iterate through the catalog, looking for packages matching
'pattern', based on the function in 'matcher' and the versioning
constraint described by 'constraint'. If 'matcher' is None,
uses fmri subset matching as the default. Returns a sorted list
of PkgFmri objects, newest versions first. If 'counthash' is a
dictionary, instead store the number of matched fmris for each
package name which was matched."""
cat_auth = self.auth
tuples = {}
if not isinstance(patterns, list):
patterns = [ patterns ]
# 'patterns' may be partially or fully decorated fmris; we want
# to extract their names and versions to match separately
# against the catalog.
#
# XXX "5.11" here needs to be saner
for pattern in patterns:
if isinstance(pattern, fmri.PkgFmri):
tuples[pattern] = pattern.tuple()
else:
tuples[pattern] = \
fmri.PkgFmri(pattern, "5.11").tuple()
pkgs = []
try:
pfile = file(os.path.normpath(
os.path.join(self.catalog_root, "catalog")), "r")
except IOError, e:
if e.errno == errno.ENOENT:
return pkgs
else:
raise
for entry in pfile:
if not entry[1].isspace() or \
not entry[0] in known_prefixes:
continue
try:
if entry[0] not in tuple("CV"):
continue
cv, pkg, cat_name, cat_version = entry.split()
if pkg != "pkg":
continue
except ValueError:
# Handle old two-column catalog file, mostly in
# use on server.
cv, cat_fmri = entry.split()
pkgs.append(fmri.PkgFmri(cat_fmri, "5.11",
authority = self.auth))
continue
pkgs.append(fmri.PkgFmri("%s@%s" %
(cat_name, cat_version), "5.11",
authority = self.auth))
pfile.close()
ret = extract_matching_fmris(pkgs, cat_auth, patterns, matcher,
constraint, counthash)
return sorted(ret, reverse = True)
def fmris(self):
"""A generator function that produces FMRIs as it
iterates over the contents of the catalog."""
try:
pfile = file(os.path.normpath(
os.path.join(self.catalog_root, "catalog")), "r")
except IOError, e:
if e.errno == errno.ENOENT:
return
else:
raise
for entry in pfile:
if not entry[1].isspace() or \
not entry[0] in known_prefixes:
continue
try:
if entry[0] not in tuple("CV"):
continue
cv, pkg, cat_name, cat_version = entry.split()
if pkg == "pkg":
yield fmri.PkgFmri("%s@%s" %
(cat_name, cat_version),
authority = self.auth)
except ValueError:
# Handle old two-column catalog file, mostly in
# use on server.
cv, cat_fmri = entry.split()
yield fmri.PkgFmri(cat_fmri,
authority = self.auth)
pfile.close()
def fmri_renamed_dest(self, fmri):
"""Returns a list of RenameRecords where fmri is listed as the
destination package."""
# Don't bother doing this if no FMRI is present
if not fmri:
return
# Load renamed packages, if needed
if self.renamed is None:
self._load_renamed()
for rr in self.renamed:
if rr.destname == fmri.pkg_name and \
fmri.version >= rr.destversion:
yield rr
def fmri_renamed_src(self, fmri):
"""Returns a list of RenameRecords where fmri is listed as
the source package."""
# Don't bother doing this if no FMRI is present
if not fmri:
return
# Load renamed packages, if needed
if self.renamed is None:
self._load_renamed()
for rr in self.renamed:
if rr.srcname == fmri.pkg_name and \
fmri.version < rr.srcversion:
yield rr
def last_modified(self):
"""Return the time at which the catalog was last modified."""
return self.attrs.get("Last-Modified", None)
def load_attrs(self, filenm = "attrs"):
"""Load attributes from the catalog file into the in-memory
attributes dictionary"""
apath = os.path.normpath(
os.path.join(self.catalog_root, filenm))
if not os.path.exists(apath):
return
afile = file(apath, "r")
attrre = re.compile('^S ([^:]*): (.*)')
for entry in afile:
m = attrre.match(entry)
if m != None:
self.attrs[m.group(1)] = m.group(2)
afile.close()
# convert npkgs to integer value
if "npkgs" in self.attrs:
self.attrs["npkgs"] = int(self.attrs["npkgs"])
def _load_renamed(self):
"""Load the catalog's rename records into self.renamed"""
self.renamed = []
try:
pfile = file(os.path.normpath(
os.path.join(self.catalog_root, "catalog")), "r")
except IOError, e:
if e.errno == errno.ENOENT:
return
else:
raise
self.renamed = [
RenamedPackage(*entry.split()[1:]) for entry in pfile
if entry[0] == "R"
]
pfile.close()
def npkgs(self):
"""Returns the number of packages in the catalog."""
return self.attrs["npkgs"]
@staticmethod
def recv(filep, path):
"""A static method that takes a file-like object and
a path. This is the other half of catalog.send(). It
reads a stream as an incoming catalog and lays it down
on disk."""
if not os.path.exists(path):
os.makedirs(path)
attrf = file(os.path.normpath(
os.path.join(path, "attrs")), "w+")
catf = file(os.path.normpath(
os.path.join(path, "catalog")), "w+")
for s in filep:
if not s[1].isspace():
continue
elif not s[0] in known_prefixes:
catf.write(s)
elif s.startswith("S "):
attrf.write(s)
elif s.startswith("R "):
catf.write(s)
else:
# XXX Need to be able to handle old and new
# format catalogs.
f = fmri.PkgFmri(s[2:])
catf.write("%s %s %s %s\n" %
(s[0], "pkg", f.pkg_name, f.version))
attrf.close()
catf.close()
def rename_package(self, srcname, srcvers, destname, destvers):
"""Record that the name of package oldname has been changed
to newname as of version vers. Returns a timestamp
of when the catalog was modified and a RenamedPackage
object that describes the rename."""
rr = RenamedPackage(srcname, srcvers, destname, destvers)
# Check that the destination (new) package is already in the
# catalog. Also check that the old package does not exist at
# the version that is being renamed.
if rr.new_fmri():
newfm = self.get_matching_fmris(rr.new_fmri())
if len(newfm) < 1:
raise CatalogException, \
"Destination FMRI %s must be in catalog" % \
rr.new_fmri()
oldfm = self.get_matching_fmris(rr.old_fmri())
if len(oldfm) > 0:
raise CatalogException, \
"Src FMRI %s must not be in catalog" % \
rr.old_fmri()
# Load renamed packages, if needed
if self.renamed is None:
self._load_renamed()
# Check that rename record isn't already in catalog
if rr in self.renamed:
raise CatalogException, \
"Rename %s is already in the catalog" % rr
# Keep renames acyclic. Check that the destination of this
# rename isn't the source of another rename.
if rr.new_fmri() and \
self.rename_is_predecessor(rr.new_fmri(), rr.old_fmri()):
raise RenameException, \
"Can't rename %s. Causes cycle in rename graph." \
% rr.srcname
pathstr = os.path.normpath(os.path.join(self.catalog_root,
"catalog"))
pfile = file(pathstr, "a+")
pfile.write("%s\n" % rr)
pfile.close()
self.renamed.append(rr)
ts = datetime.datetime.now()
self.set_time(ts)
return (ts, rr)
def rename_is_same_pkg(self, fmri, pfmri):
"""Returns true if fmri and pfmri are the same package because
of a rename operation."""
for s in self.fmri_renamed_src(fmri):
if s.destname == pfmri.pkg_name:
return True
elif s.new_fmri() and \
self.rename_is_same_pkg(s.new_fmri(), pfmri):
return True
for d in self.fmri_renamed_dest(fmri):
if d.srcname == pfmri.pkg_name:
return True
elif self.rename_is_same_pkg(d.old_fmri(), pfmri):
return True
return False
def rename_is_successor(self, fmri, pfmri):
"""Returns true if fmri is a successor to pfmri by way
of a rename operation."""
for d in self.fmri_renamed_dest(fmri):
if d.srcname == pfmri.pkg_name and \
pfmri.version <= d.srcversion:
return True
else:
return self.rename_is_successor(d.old_fmri(),
pfmri)
return False
def rename_is_predecessor(self, fmri, pfmri):
"""Returns true if fmri is a predecessor to pfmri by
a rename operation."""
for s in self.fmri_renamed_src(fmri):
if s.destname == pfmri.pkg_name and \
s.destversion < pfmri.version:
return True
elif s.new_fmri():
return self.rename_is_predecessor(s.new_fmri(),
pfmri)
return False
def rename_newer_pkgs(self, fmri):
"""Returns a list of packages that are newer than fmri."""
pkgs = []
for s in self.fmri_renamed_src(fmri):
if s.new_fmri():
pkgs.append(s.new_fmri())
nl = self.rename_newer_pkgs(s.new_fmri())
pkgs.extend(nl)
return pkgs
def rename_older_pkgs(self, fmri):
"""Returns a list of packages that are older than fmri."""
pkgs = []
for d in self.fmri_renamed_dest(fmri):
pkgs.append(d.old_fmri())
ol = self.rename_older_pkgs(d.old_fmri())
pkgs.extend(ol)
return pkgs
def save_attrs(self, filenm = "attrs"):
"""Save attributes from the in-memory catalog to a file
specified by filenm."""
afile = file(os.path.normpath(
os.path.join(self.catalog_root, filenm)), "w+")
for a in self.attrs.keys():
s = "S %s: %s\n" % (a, self.attrs[a])
afile.write(s)
afile.close()
def send(self, filep):
"""Send the contents of this catalog out to the filep
specified as an argument."""
# Send attributes first.
filep.writelines(self.attrs_as_lines())
try:
cfile = file(os.path.normpath(
os.path.join(self.catalog_root, "catalog")), "r")
except IOError, e:
# Missing catalog is fine; other errors need to be
# reported.
if e.errno == errno.ENOENT:
return
else:
raise
for e in cfile:
filep.write(e)
cfile.close()
def set_time(self, ts = None):
"""Set time to timestamp if supplied by caller. Otherwise
use the system time."""
if ts and isinstance(ts, str):
self.attrs["Last-Modified"] = ts
elif ts and isinstance(ts, datetime.datetime):
self.attrs["Last-Modified"] = ts.isoformat()
else:
self.attrs["Last-Modified"] = timestamp()
self.save_attrs()
def search_available(self):
return self._search_available
def valid_new_fmri(self, fmri):
"""Check that the fmri supplied as an argument would be
valid to add to the catalog. This checks to make sure that
rename/freeze operations would not prohibit the caller
from adding this FMRI."""
if self.renamed is None:
self._load_renamed()
for rr in self.renamed:
if rr.srcname == fmri.pkg_name and \
fmri.version >= rr.srcversion:
return False
return True
# In order to avoid a fine from the Department of Redundancy Department,
# allow these methods to be invoked without explictly naming the Catalog class.
recv = Catalog.recv
# Prefixes that this catalog knows how to handle
known_prefixes = frozenset("CSVR")
# Method used by Catalog and UpdateLog. Since UpdateLog needs to know
# about Catalog, keep it in Catalog to avoid circular dependency problems.
def timestamp():
"""Return an integer timestamp that can be used for comparisons."""
tobj = datetime.datetime.now()
tstr = tobj.isoformat()
return tstr
def ts_to_datetime(ts):
"""Take timestamp ts in string isoformat, and convert it to a datetime
object."""
year = int(ts[0:4])
month = int(ts[5:7])
day = int(ts[8:10])
hour = int(ts[11:13])
min = int(ts[14:16])
sec = int(ts[17:19])
usec = int(ts[20:26])
dt = datetime.datetime(year, month, day, hour, min, sec, usec)
return dt
def extract_matching_fmris(pkgs, cat_auth, patterns, matcher = None,
constraint = None, counthash = None):
"""Iterate through the given list of PkgFmri objects,
looking for packages matching 'pattern', based on the function
in 'matcher' and the versioning constraint described by
'constraint'. If 'matcher' is None, uses fmri subset matching
as the default. Returns a sorted list of PkgFmri objects,
newest versions first. If 'counthash' is a dictionary, instead
store the number of matched fmris for each package name which
was matched."""
if not matcher:
matcher = fmri.fmri_match
if not isinstance(patterns, list):
patterns = [ patterns ]
# 'pattern' may be a partially or fully decorated fmri; we want
# to extract its name and version to match separately against
# the catalog.
# XXX "5.11" here needs to be saner
tuples = {}
for pattern in patterns:
if isinstance(pattern, fmri.PkgFmri):
tuples[pattern] = pattern.tuple()
else:
assert pattern != None
tuples[pattern] = \
fmri.PkgFmri(pattern, "5.11").tuple()
ret = []
for p in pkgs:
cat_auth, cat_name, cat_version = p.tuple()
for pattern in patterns:
pat_auth, pat_name, pat_version = tuples[pattern]
if (pat_auth == cat_auth or not pat_auth) and \
matcher(cat_name, pat_name):
if not pat_version or \
p.version.is_successor(
pat_version, constraint) or \
p.version == pat_version:
if counthash is not None:
if pattern in counthash:
counthash[pattern] += 1
else:
counthash[pattern] = 1
ret.append(p)
return sorted(ret, reverse = True)
class RenamedPackage(object):
"""An in-memory representation of a rename object. This object records
information about a package that has had its name changed.
Renaming a package presents a number of challenges. The packaging
system must still be able to recognize and decode dependencies on
packages with the old name. In order for this to work correctly, the
rename record must contain both the old and new name of the package. It
is also undesireable to have a renamed package receive subsequent
versions. However, it still should be possible to publish bugfixes to
the old package lineage. This means that we must also record
versioning information at the time a package is renamed.
This versioning information allows us to determine which portions
of the version and namespace are allowed to add new versions.
If a package is re-named to the NULL package at a specific version,
this is equivalent to freezing the package. No further updates to
the version history may be made under that name. (NULL is never open)
The rename catalog format is as follows:
R <srcname> <srcversion> <destname> <destversion>
"""
def __init__(self, srcname, srcversion, destname, destversion):
"""Create a RenamedPackage object. Srcname is the original
name of the package, destname is the name this package
will take after the operation is successful.
Versionstr is the version at which this change takes place. No
versions >= version of srcname will be permitted."""
if destname == "NULL":
self.destname = None
destversion = None
else:
self.destname = destname
self.srcname = srcname
if not srcversion and not destversion:
raise RenameException, \
"Must supply a source or destination version"
elif not srcversion:
self.srcversion = version.Version(destversion, None)
self.destversion = self.srcversion
elif not destversion:
self.srcversion = version.Version(srcversion, None)
self.destversion = self.srcversion
else:
self.destversion = version.Version(destversion, None)
self.srcversion = version.Version(srcversion, None)
def __str__(self):
if not self.destname:
return "R %s %s NULL NULL" % (self.srcname,
self.srcversion)
return "R %s %s %s %s" % (self.srcname, self.srcversion,
self.destname, self.destversion)
def __eq__(self, other):
"""Implementing our own == function allows us to properly
check whether a rename object is in a list of renamed
objects."""
if not isinstance(other, RenamedPackage):
return False
if self.srcname != other.srcname:
return False
if self.destname != other.destname:
return False
if self.srcversion != other.srcversion:
return False
if self.destversion != other.destversion:
return False
return True
def new_fmri(self):
"""Return a FMRI that represents the destination name and
version of the renamed package."""
if not self.destname:
return None
fmstr = "pkg:/%s@%s" % (self.destname, self.destversion)
fm = fmri.PkgFmri(fmstr, None)
return fm
def old_fmri(self):
"""Return a FMRI that represents the most recent version
of the package had it not been renamed."""
fmstr = "pkg:/%s@%s" % (self.srcname, self.srcversion)
fm = fmri.PkgFmri(fmstr, None)
return fm