| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
"""Parser for .dirindex files.""" |
| 22 |
|
| 23 |
from .exceptions import InvalidDirIndexFile |
| 24 |
from .virtual_path import VirtualPath |
| 25 |
|
| 26 |
|
| 27 |
class DirIndex: |
| 28 |
"""Parser for .dirindex files.""" |
| 29 |
|
| 30 |
def __init__(self, dirIndexFile): |
| 31 |
self.directories = [] |
| 32 |
self.files = [] |
| 33 |
self.tarballs = [] |
| 34 |
self.version = 0 |
| 35 |
self.path = None |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
self._rawContents = None |
| 40 |
|
| 41 |
with open(dirIndexFile, "r", encoding="ascii") as f: |
| 42 |
self.readFrom(f) |
| 43 |
|
| 44 |
self._sanityCheck() |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def checkForBackslashOrLeadingSlash(cls, line, path): |
| 48 |
if '\\' in path or path.startswith('/'): |
| 49 |
raise InvalidDirIndexFile( |
| 50 |
r"invalid '\' or leading '/' in path field from line {!r}" |
| 51 |
.format(line)) |
| 52 |
|
| 53 |
@classmethod |
| 54 |
def checkForSlashBackslashOrDoubleColon(cls, line, name): |
| 55 |
if '/' in name or '\\' in name: |
| 56 |
raise InvalidDirIndexFile( |
| 57 |
r"invalid '\' or '/' in name field from line {!r}" |
| 58 |
.format(line)) |
| 59 |
|
| 60 |
if name == "..": |
| 61 |
raise InvalidDirIndexFile( |
| 62 |
r"invalid name field equal to '..' in line {!r}".format(line)) |
| 63 |
|
| 64 |
def readFrom(self, readable): |
| 65 |
self._rawContents = readable.read() |
| 66 |
|
| 67 |
for line in self._rawContents.split('\n'): |
| 68 |
line = line.strip() |
| 69 |
if line.startswith('#'): |
| 70 |
continue |
| 71 |
|
| 72 |
tokens = line.split(':') |
| 73 |
if len(tokens) == 0: |
| 74 |
continue |
| 75 |
elif tokens[0] == "version": |
| 76 |
self.version = int(tokens[1]) |
| 77 |
elif tokens[0] == "path": |
| 78 |
self.checkForBackslashOrLeadingSlash(line, tokens[1]) |
| 79 |
|
| 80 |
self.path = VirtualPath(tokens[1]) |
| 81 |
|
| 82 |
if ".." in self.path.parts: |
| 83 |
raise InvalidDirIndexFile( |
| 84 |
"'..' component found in 'path' entry {!r}" |
| 85 |
.format(self.path)) |
| 86 |
elif tokens[0] == "d": |
| 87 |
self.checkForSlashBackslashOrDoubleColon(line, tokens[1]) |
| 88 |
self.directories.append({'name': tokens[1], 'hash': tokens[2]}) |
| 89 |
elif tokens[0] == "f": |
| 90 |
self.checkForSlashBackslashOrDoubleColon(line, tokens[1]) |
| 91 |
self.files.append({'name': tokens[1], |
| 92 |
'hash': tokens[2], 'size': int(tokens[3])}) |
| 93 |
elif tokens[0] == "t": |
| 94 |
self.checkForSlashBackslashOrDoubleColon(line, tokens[1]) |
| 95 |
self.tarballs.append({'name': tokens[1], 'hash': tokens[2], |
| 96 |
'size': int(tokens[3])}) |
| 97 |
|
| 98 |
def _sanityCheck(self): |
| 99 |
if self.path is None: |
| 100 |
assert self._rawContents is not None |
| 101 |
|
| 102 |
firstLines = self._rawContents.split('\n')[:5] |
| 103 |
raise InvalidDirIndexFile( |
| 104 |
"no 'path' field found; the first lines of this .dirindex file " |
| 105 |
"follow:\n\n" + '\n'.join(firstLines)) |