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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
db_layout.py
~~~~~~~~~~~~
package database file layout
:copyright: (c) 2013-2015 by Jauhien Piatlicki
:license: GPL-2, see LICENSE for more details.
"""
import hashlib
import os
import shutil
from .exceptions import DBLayoutError, DBStructureError, FileJSONError, IntegrityError
from .fileutils import FileJSON, hash_file
CATEGORIES_FILE_NAME = 'categories'
MANIFEST_FILE_NAME = 'manifest'
METADATA_FILE_NAME = 'metadata'
PACKAGES_FILE_NAME = 'packages'
JSON_FILE_SUFFIX = 'json'
BSON_FILE_SUFFIX = 'bson'
SUPPORTED_DB_LAYOUTS=[0, 1]
class CategoryJSON(FileJSON):
"""
Category file in JSON format.
"""
def __init__(self, directory, category):
super(CategoryJSON, self).__init__(os.path.join(os.path.abspath(directory), category),
file_name(PACKAGES_FILE_NAME, JSON_FILE_SUFFIX))
SUPPORTED_FILE_FORMATS = {JSON_FILE_SUFFIX: CategoryJSON}
# bson module is optional, we should check if it is installed
try:
from .file_bson.file_bson import FileBSON
class CategoryBSON(FileBSON):
"""
Category file in BSON format.
"""
def __init__(self, directory, category):
super(CategoryBSON, self).__init__(os.path.join(os.path.abspath(directory), category),
file_name(PACKAGES_FILE_NAME, BSON_FILE_SUFFIX))
SUPPORTED_FILE_FORMATS[BSON_FILE_SUFFIX] = CategoryBSON
except ImportError as e:
pass
def file_name(name, suffix=JSON_FILE_SUFFIX):
"""
Return file name based on name and suffix.
"""
return name + '.' + suffix
class Manifest(FileJSON):
"""
Manifest file.
"""
def __init__(self, directory):
super(Manifest, self).__init__(os.path.abspath(directory), file_name(MANIFEST_FILE_NAME))
def check(self):
"""
Check manifest.
"""
manifest = self.read()
result = True
errors = []
names = [file_name(CATEGORIES_FILE_NAME)]
for name in names:
if not name in manifest:
raise DBLayoutError('Bad manifest: no ' + name + ' entry')
for name, value in manifest.items():
if hash_file(os.path.join(self.directory, name), hashlib.md5()) != \
value:
errors.append(name)
if errors:
result = False
return (result, errors)
def digest(self, mandatory_files):
"""
Generate manifest.
"""
if not file_name(CATEGORIES_FILE_NAME) in mandatory_files:
raise DBLayoutError('Categories file: ' + file_name(CATEGORIES_FILE_NAME) \
+ ' is not in the list of mandatory files')
categories = Categories(self.directory)
categories = categories.read()
manifest = {}
for name in mandatory_files:
manifest[name] = hash_file(os.path.join(self.directory, name),
hashlib.md5())
for category in categories:
category_path = os.path.join(self.directory, category)
if not os.path.isdir(category_path):
raise DBStructureError('Empty category: ' + category)
for root, _, files in os.walk(category_path):
for f in files:
manifest[os.path.join(root[len(self.directory)+1:], f)] = \
hash_file(os.path.join(root, f), hashlib.md5())
self.write(manifest)
class Metadata(FileJSON):
"""
Metadata file.
"""
def __init__(self, directory):
super(Metadata, self).__init__(os.path.abspath(directory),
file_name(METADATA_FILE_NAME),
['db_version', 'layout_version', 'category_format'])
def read(self):
"""
Read metadata file.
If file doesn't exist, we have a legacy DB
with DB layout v. 0. Fill metadata appropriately.
"""
if not os.path.exists(self.directory):
os.makedirs(self.directory)
content = {}
if not os.path.isfile(self.path):
content = {'db_version': 0, 'layout_version': 0, 'category_format': JSON_FILE_SUFFIX}
else:
content = self.read_content()
for key in self.mandatories:
if not key in content:
raise FileJSONError('lack of mandatory key: ' + key)
return content
class Categories(FileJSON):
"""
Categories file.
"""
def __init__(self, directory):
super(Categories, self).__init__(os.path.abspath(directory),
file_name(CATEGORIES_FILE_NAME))
def get_layout(metadata):
"""
Get layout parameters based on metadata.
"""
layout_version = metadata['layout_version']
if layout_version == 0:
return (CategoryJSON, [file_name(CATEGORIES_FILE_NAME)])
elif layout_version == 1:
category_format = metadata['category_format']
wrong_fmt = True
try:
category_cls = SUPPORTED_FILE_FORMATS[category_format]
wrong_fmt = False
except KeyError:
pass
if wrong_fmt:
raise DBLayoutError("unsupported packages file format: " + category_format)
return (category_cls, [file_name(CATEGORIES_FILE_NAME), file_name(METADATA_FILE_NAME)])
else:
raise DBLayoutError("unsupported DB layout version: " + str(layout_version))
class DBLayout(object):
"""
Filesystem DB layout.
Directory layout.
~~~~~~~~~~~~~~~~~
For legacy DB layout v. 0:
db dir
manifest.json: database manifest
categories.json: information about categories
category1
packages.json: information about available packages
category2
...
For DB layout v. 1:
db dir
manifest.json: database manifest
categories.json: information about categories
metadata.json: DB metadata
category1
packages.[b|j]son: information about available packages
category2
...
Packages file can be in json or bson formats.
"""
def __init__(self, directory):
self.directory = os.path.abspath(directory)
self.manifest = Manifest(self.directory)
def check_manifest(self):
"""
Check manifest.
"""
sane, errors = self.manifest.check()
if not sane:
raise IntegrityError('Manifest error: ' + str(errors))
def clean(self):
"""
Remove DB files.
"""
if os.path.exists(self.directory):
shutil.rmtree(self.directory)
os.makedirs(self.directory)
def read(self):
"""
Read DB files.
Returns a tuple with metadata, list of categories
and categories dictionary.
"""
self.check_manifest()
metadata_f = Metadata(self.directory)
metadata = metadata_f.read()
category_cls, _ = get_layout(metadata)
categories_f = Categories(self.directory)
categories = categories_f.read()
packages = {}
for category in categories:
category_path = os.path.join(self.directory, category)
if not os.path.isdir(category_path):
raise DBLayoutError('Empty category: ' + category)
category_f = category_cls(self.directory, category)
pkgs = category_f.read()
if not pkgs:
raise DBLayoutError('Empty category: ' + category)
packages[category] = pkgs
return (metadata, categories, packages)
def write(self, metadata, categories, packages):
"""
Write DB files.
"""
category_cls, mandatory_files = get_layout(metadata)
self.clean()
if file_name(METADATA_FILE_NAME) in mandatory_files:
metadata_f = Metadata(self.directory)
metadata_f.write(metadata)
categories_f = Categories(self.directory)
categories_f.write(categories)
for category in categories:
category_f = category_cls(self.directory, category)
category_f.write(packages[category])
self.manifest.digest(mandatory_files)
|