-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfiles.py
115 lines (88 loc) · 3.1 KB
/
files.py
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
# Copyright (c) Pypperoni
#
# Pypperoni is licensed under the MIT License; you may
# not use it except in compliance with the License.
#
# You should have received a copy of the License with
# this source code under the name "LICENSE.txt". However,
# you may obtain a copy of the License on our GitHub here:
# https://github.com/Pypperoni/pypperoni
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the
# License.
from . import config
from threading import Lock
from io import StringIO
import os
class ConditionalFile:
def __init__(self, filename, hashfunc):
self.filename = filename
self.hashfunc = hashfunc
self._buf = StringIO()
def write(self, data):
self._buf.write(data)
def read(self):
return self._buf.read()
def seek(self, *args):
self._buf.seek(*args)
def tell(self):
return self._buf.tell()
def close(self):
self._buf.seek(0)
newhash = self.hashfunc(self._buf)
if not os.path.isfile(self.filename):
self.__write()
return (self.filename, newhash, False)
f = open(self.filename, 'rb')
oldhash = self.hashfunc(f)
self.seek(0)
f.close()
modified = oldhash != newhash
if modified:
self.__write()
return (self.filename, newhash, modified)
def __write(self):
self.seek(0)
f = open(self.filename, 'wb')
f.write(self._buf.read().encode('utf-8', errors='backslashreplace'))
f.close()
class FileContainer:
def __init__(self, prefix, hashfunc, uid=None):
self.prefix = prefix.replace('.', '/')
self.hashfunc = hashfunc
if uid:
self.uid = uid
else:
self.uid = os.path.basename(prefix).replace('.', '_')
self.headername = self.prefix + '.pyp.h'
self.header = ConditionalFile(self.headername, self.hashfunc)
self.header.write('#include "pypperoni_impl.h"\n')
prefix_dir = os.path.dirname(self.prefix)
if not os.path.isdir(prefix_dir):
try:
os.makedirs(prefix_dir)
except FileExistsError:
pass
self.files = []
self.filenames = []
self.__next()
def __next(self):
self.filenames.append('%s_%d.c' % (self.prefix, len(self.filenames) + 1))
f = ConditionalFile(self.filenames[-1], self.hashfunc)
f.write('#include "%s"\n\n' % os.path.basename(self.headername))
self.files.append(f)
def write(self, *args):
self.files[-1].write(*args)
def consider_next(self):
if self.files[-1].tell() > config.MAX_FILE_SIZE:
self.__next()
def add_common_header(self, header):
self.header.write(header + '\n')
def close(self):
yield self.header.close()
for f in self.files:
yield f.close()