-
Notifications
You must be signed in to change notification settings - Fork 7
/
coffee-deps-check
executable file
·133 lines (103 loc) · 3.52 KB
/
coffee-deps-check
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
#! /usr/bin/python
#------------------------------------------------------------------------------#
# coffee-deps-check
# by Remi Attab on 30-10-2012
#
# Checks the require statements in coffee scripts to see if they are also
# present in its build dependency.
#
# If not arguments are provided then this script acts like a git pre-commit hook
# and will fetch the list of files to check from git (git diff
# --cached).
#
# To run the script as batch process, just provide a list of coffee scripts as
# arguments. The script will then check of the coffee scripts in turn.
#
#------------------------------------------------------------------------------#
import sys
import re
import subprocess
import jmlbuild
import os
def git_files():
"""
Returns all the .coffee file that are about to be committed.
"""
process = subprocess.Popen(
["git", "diff", "--cached", "--name-only"],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
while True:
ret = process.poll()
line = process.stdout.readline()
yield line
if ret is not None: return
def parse_script(path):
"""
finds all the require statement in a coffeescript file and returns a list of
the required modules.
"""
modules = []
with open(path, 'r') as f:
for line in f:
m = re.match(r".*\brequire\W*\(?\W*[\"'](\w+)[\"']\)?\W*$", line)
if not m: continue
if re.match(r".*#.*require.*", line): continue
modules.append(m.group(1))
return modules
def check_deps(path, graph, module, requires):
"""
Checks the given set of requires against a edges defined in the dependency
graph.
"""
if module + jmlbuild.Ext.NODEJS_MODULE in graph.edges:
module = module + jmlbuild.Ext.NODEJS_MODULE
elif module + jmlbuild.Ext.NODEJS_TEST in graph.edges:
module = module + jmlbuild.Ext.NODEJS_TEST
if False:
print "MODULE: ", module
print "REQUIRES: ", requires
print "EDGES: ", graph.edges[module]
if not module in graph.edges:
print "%s: no target defined for %s" % (path, module)
return 1
deps = []
for d in requires:
if d + jmlbuild.Ext.NODEJS_MODULE in graph.edges:
deps.append(d + jmlbuild.Ext.NODEJS_MODULE)
elif d + jmlbuild.Ext.NODEJS_ADDON in graph.edges:
deps.append(d + jmlbuild.Ext.NODEJS_ADDON)
error = 0
for d in deps:
if not d in graph.edges[module]:
print "%s: %s missing dependency on %s" % (path, module, d)
error = 1
for d in graph.edges[module]:
if not d in deps:
print "%s: %s extra dependency on %s" % (path, module, d)
return error
def check_script(path):
"""
Runs the script for a given filename.
"""
if not path.endswith(".coffee"): return
if not os.path.exists(path): return
requires = parse_script(path)
script = os.path.split(path)[1]
module = script.split(".")[0]
return check_deps(path, graph, module, requires)
build_folder = jmlbuild.find_dotgit(os.getcwd())
graph = jmlbuild.parse_makefile("Makefile", build_folder)
error = 0
if len(sys.argv) > 1:
for path in sys.argv[1:]:
error = check_script(path) or error
else:
for line in git_files():
line = line[:len(line) -1]
error = check_script(line) or error
if error != 0:
print "ERROR: Add the missing coffee script dependencies and try again."
else:
print "No missing coffee script dependencies detected."
sys.exit(error)