2020-10-01 17:32:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# dirindex.py --- Class used to parse .dirindex files
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Torsten Dreyer
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 2 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
"""Parser for .dirindex files."""
|
|
|
|
|
|
|
|
from .exceptions import InvalidDirIndexFile
|
|
|
|
from .virtual_path import VirtualPath
|
|
|
|
|
|
|
|
|
|
|
|
class DirIndex:
|
|
|
|
"""Parser for .dirindex files."""
|
|
|
|
|
|
|
|
def __init__(self, dirIndexFile):
|
2020-10-02 14:19:18 +00:00
|
|
|
self.directories = []
|
|
|
|
self.files = []
|
|
|
|
self.tarballs = []
|
2020-10-01 17:32:15 +00:00
|
|
|
self.version = 0
|
|
|
|
self.path = None # will be a VirtualPath instance when set
|
|
|
|
|
|
|
|
# readFrom() stores the raw contents of the .dirindex file in this
|
|
|
|
# attribute. This is useful for troubleshooting.
|
|
|
|
self._rawContents = None
|
|
|
|
|
|
|
|
with open(dirIndexFile, "r", encoding="utf-8") as f:
|
|
|
|
self.readFrom(f)
|
|
|
|
|
|
|
|
self._sanityCheck()
|
|
|
|
|
|
|
|
def readFrom(self, readable):
|
|
|
|
self._rawContents = readable.read()
|
|
|
|
|
|
|
|
for line in self._rawContents.split('\n'):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('#'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
tokens = line.split(':')
|
|
|
|
if len(tokens) == 0:
|
|
|
|
continue
|
2020-10-02 14:19:18 +00:00
|
|
|
elif tokens[0] == "version":
|
2020-10-01 17:32:15 +00:00
|
|
|
self.version = int(tokens[1])
|
|
|
|
elif tokens[0] == "path":
|
|
|
|
# This is relative to the repository root
|
|
|
|
self.path = VirtualPath(tokens[1])
|
|
|
|
elif tokens[0] == "d":
|
2020-10-02 14:19:18 +00:00
|
|
|
self.directories.append({'name': tokens[1], 'hash': tokens[2]})
|
2020-10-01 17:32:15 +00:00
|
|
|
elif tokens[0] == "f":
|
2020-10-02 14:19:18 +00:00
|
|
|
self.files.append({'name': tokens[1],
|
|
|
|
'hash': tokens[2], 'size': int(tokens[3])})
|
2020-10-01 17:32:15 +00:00
|
|
|
elif tokens[0] == "t":
|
2020-10-02 14:19:18 +00:00
|
|
|
self.tarballs.append({'name': tokens[1], 'hash': tokens[2],
|
|
|
|
'size': int(tokens[3])})
|
2020-10-01 17:32:15 +00:00
|
|
|
|
|
|
|
def _sanityCheck(self):
|
|
|
|
if self.path is None:
|
|
|
|
assert self._rawContents is not None
|
|
|
|
|
|
|
|
firstLines = self._rawContents.split('\n')[:5]
|
|
|
|
raise InvalidDirIndexFile(
|
|
|
|
"no 'path' field found; the first lines of this .dirindex file "
|
|
|
|
"follow:\n\n" + '\n'.join(firstLines))
|