-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
288 lines (219 loc) · 7.77 KB
/
__main__.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
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
#!/usr/local/bin/python3
"""A lightweight Javascript concatenator.
Lightning is an fast and efficient Javascript concatenator that
supports live builds.
"""
import watchdog.events
import watchdog.observers
import os
import glob
import time
INDEX = os.path.join(os.getcwd(), ".lightning.conf")
HEAD = "@head"
REQUIRE = "@require"
PROVIDE = "@provide"
class Source:
"""Javascript source file container."""
def __init__(self, path: str):
"""Create a source file container."""
self.path = os.path.abspath(path)
self.code = None
self.head = False
self.require = []
self.provide = []
self.search()
def __getitem__(self, target: str):
"""Get the require or provide lists."""
return {REQUIRE: self.require, PROVIDE: self.provide}[target]
def __repr__(self):
"""Represent the source file as a string."""
return "Source[" + os.path.basename(self.path) + "]"
def search(self, cache=True):
"""Search for require and provide statements."""
# Read the file
self.read(cache=cache)
# Check if this is the head
if self.code.find(HEAD) > -1:
self.head = True
return
# Iterate search items
for target in (REQUIRE, PROVIDE):
start = 0
out = set()
length = len(target)
while True:
index = self.code.find(target, start)
if index < 0:
break
newline = self.code.find("\n", index)
separated = self.code[index+length+1:newline].split(",")
out |= set(map(lambda x: x.strip(), separated))
start = newline + 1
# Clear and set the search
self[target].clear()
self[target].extend(out)
def read(self, cache=False):
"""Read the contents of the file."""
# Read if not cached
if self.code is None:
with open(self.path) as file:
code = file.read()
self.code = code
return code
return self.code
def common(paths: [str]) -> str:
"""Return the common path of a list with wildcard."""
path = os.path.abspath(os.path.commonpath(paths))
parts = path.split(os.sep)
for i in range(len(parts)):
if "*" in parts[i]:
return os.path.sep.join(parts[:i])
return path
def find(require, sources):
"""Find a source that provides a requirement from a list."""
for source in sources:
if require in source.provide:
return source
return None
class Target:
"""A set of sources defined by an index file."""
def __init__(self, target: str, include: [str], exclude: [str]):
"""Initialize a build with its target and source paths."""
self.target = target
self.include = include
self.exclude = exclude
self.sources = []
self.common = common(include)
def __repr__(self):
"""Represent the build as a string."""
return "Target[{}:{}]".format(
self.target, os.path.basename(self.common))
def index(self):
"""Index the files defined by paths."""
self.sources.clear()
files = set()
for path in self.include:
files |= set(glob.glob(path, recursive=True))
for path in self.exclude:
files -= set(glob.glob(path, recursive=True))
self.sources.extend(map(Source, files))
def includes(self, path: str) -> bool:
"""Check if a source file is in the build include."""
path = os.path.abspath(path)
for source in self.sources:
if source.path == path:
return True
return False
def sort(self):
"""Sort the sources topologically."""
correct = []
# Put head in the front of the correct list
for source in self.sources:
if source.head:
self.sources.remove(source)
correct.append(source)
# Do the algorithm
while self.sources:
cyclic = True
for source in self.sources:
for require in source.require:
if find(require, self.sources):
break
else:
cyclic = False
self.sources.remove(source)
correct.append(source)
# Break if there are cyclic dependencies
if cyclic:
raise RuntimeError(
"Circular dependencies found in {}.".format(
os.path.basename(source.path)))
# Save correct ordering
self.sources = correct
def build(self):
"""Build and write the concatenated source files."""
self.index()
self.sort()
print(self.sources)
with open(self.target, "w") as file:
for source in self.sources:
file.write(source.read().rstrip() + "\n\n\n")
print("Built {}".format(repr(self)))
def sep(path: str) -> str:
"""Fix a path according to the operating system."""
return path.replace("/", os.sep)
def index(path: str=INDEX) -> [Target]:
"""Get the build sources from the index file."""
out = []
with open(path) as file:
target = None
include = None
exclude = None
# Iterate the file
for line in file:
line = line.lstrip()
first = line[0]
# Check command
if line == "" or first in "#;":
continue
elif first in "+-" and target is None:
raise SyntaxWarning("Ignoring files not under build target.")
elif first == "+":
include.append(sep(line[1:].strip()))
elif first == "-":
exclude.append(sep(line[1:].strip()))
# Add as a new target
else:
if include or exclude:
out.append(Target(target, include, exclude))
target = line.strip()
include = []
exclude = []
# Add the last target since this is like do while
out.append(Target(target, include, exclude))
# Return
return out
class NaiveDeltaHandler(watchdog.events.FileSystemEventHandler):
"""Calls a full sort and concatenate when the filesystem changes."""
def __init__(self, target: [Target]):
"""Initialize the file handler with its config path."""
self.target = target
self.cache = {}
self.build()
def build(self):
"""Concatenate the source files."""
self.target.index()
self.target.build()
def on_any_event(self, event: watchdog.events.FileSystemMovedEvent):
"""Called when any event is fired."""
path = os.path.realpath(event.src_path)
self.target.index()
if not self.target.includes(path):
return
try:
mtime = os.path.getmtime(path)
except FileNotFoundError:
mtime = -1
if self.cache.get(path, 0) >= mtime:
return
self.cache[path] = mtime
target.build()
if __name__ == "__main__":
"""This is run if someone calls lightning from the command line."""
import sys
path = INDEX
if len(sys.argv) > 1:
path = sys.argv[1]
# Create the observer
observer = watchdog.observers.Observer()
for target in index(path=path):
handler = NaiveDeltaHandler(target)
observer.schedule(handler, target.common, recursive=True)
observer.start()
# Wait for kill command
while True:
try:
time.sleep(0.5)
except KeyboardInterrupt:
observer.stop()
break