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