This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hie-hackage.py
executable file
·74 lines (54 loc) · 1.77 KB
/
hie-hackage.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
#!/usr/bin/env python
# hie-hackage.py
# For generating documentation for hackage modules and the hacks necessary...
#
# Christopher Monsanto <[email protected]>
# License: GPLv3
import sys
import os, fnmatch
from glob import glob
import re
import subprocess
def hie(f, inp):
k = subprocess.Popen(["hie", f], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
data = k.communicate(inp)
return data
directory = sys.argv[1]
prefixes = sys.argv[2:] or ["Algebra", "Codec", "Control", "Data", "Database", "Debug", "Foreign", "GHC", "Graphics", "Language", "Numeric", "Network", "Prelude", "Sound", "System", "Test", "Text", "Training"]
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
files = list(find_files(".", "*.hs")) + list(find_files(".", "*.lhs"))
errs = []
for f in files:
path = os.path.abspath(f)
writeit = False
parts = []
for part in f.split("/"):
part = part.replace(".hs", "").replace(".lhs", "")
if part in prefixes:
writeit = True
elif not re.match("^[A-Z]", part):
parts = []
writeit = False
if writeit:
parts.append(part)
if not parts:
continue
module = ".".join(parts)
to = "%s/%s" % (directory, module)
with open(f) as h:
data = h.read()
with open(to, "w") as h:
print "Processing", f
(out, err) = hie(path, data)
if err:
errs.append((f, err))
h.write(out)
if errs:
print "The following files had problems:"
for (f, err) in errs:
print f, ":", err