Debian Codegraph @

scripts/python/TerraSync/terrasync/dirindex.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # dirindex.py --- Class used to parse .dirindex files
4 #
5 # Copyright (C) 2016 Torsten Dreyer
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 # will be a VirtualPath instance when set
36
37 # readFrom() stores the raw contents of the .dirindex file in this
38 # attribute. This is useful for troubleshooting.
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 # This is relative to the repository root
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))