1 #!/usr/bin/env python 2 3 """ 4 File access. 5 6 Copyright (C) 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 from iixr.terms import * 22 from os import listdir, remove, rename # partition manipulation 23 from shutil import copy # index updating 24 from os.path import join 25 26 try: 27 set 28 except NameError: 29 from sets import Set as set 30 31 # Constants. 32 33 TERM_FILENAMES = "terms", 34 35 # Utility functions. 36 37 def get_partitions(pathname, prefix): 38 39 """ 40 Return a set of partition names for partitions residing at the given 41 'pathname' having the given 'prefix'. 42 """ 43 44 prefix_length = len(prefix) 45 46 partitions = set() 47 for filename in listdir(pathname): 48 if filename.startswith(prefix): 49 partition = filename[prefix_length:] 50 if partition.isdigit(): 51 partitions.add(int(partition)) 52 return partitions 53 54 def get_term_partitions(pathname): 55 56 """ 57 Return a set of term partition identifiers for partitions residing at the 58 given 'pathname'. 59 """ 60 61 return get_partitions(pathname, "terms-") 62 63 def get_next_partition(partitions): 64 return max(partitions or [-1]) + 1 65 66 def get_term_writer(pathname, partition): 67 68 """ 69 Return a term writer using files under the given 'pathname' labelled 70 according to the given 'partition'. 71 """ 72 73 f = open(join(pathname, "terms-%s" % partition), "wb") 74 return TermWriter(f) 75 76 def get_reader(pathname, name, partition, cls): 77 f = open(join(pathname, "%s-%s" % (name, partition)), "rb") 78 return cls(f) 79 80 def get_term_reader(pathname, partition): 81 82 """ 83 Return a term reader using files under the given 'pathname' labelled 84 according to the given 'partition'. 85 """ 86 87 return get_reader(pathname, "terms", partition, TermIterator) 88 89 def get_term_data_reader(pathname, partition): 90 91 """ 92 Return a term plus data reader using files under the given 'pathname' 93 labelled according to the given 'partition'. 94 """ 95 96 return get_reader(pathname, "terms", partition, TermDataIterator) 97 98 # Renaming. 99 100 def rename_files(pathname, names, from_partition, to_partition): 101 for name in names: 102 rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) 103 104 def rename_term_files(pathname, from_partition, to_partition): 105 rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) 106 107 # Removal/deletion. 108 109 def remove_files(pathname, names, partition): 110 for name in names: 111 remove(join(pathname, "%s-%s" % (name, partition))) 112 113 def remove_term_files(pathname, partition): 114 remove_files(pathname, TERM_FILENAMES, partition) 115 116 # Copying. 117 118 def copy_files(source, names, partition, destination, suffix): 119 for name in names: 120 filename = "%s-%s" % (name, partition) 121 copy(join(source, filename), join(destination, filename + suffix)) 122 123 def copy_term_files(source, partition, destination, suffix): 124 copy_files(source, TERM_FILENAMES, partition, destination, suffix) 125 126 # vim: tabstop=4 expandtab shiftwidth=4