This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkicad-cleanup-sheets
executable file
·120 lines (102 loc) · 4.45 KB
/
kicad-cleanup-sheets
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
#!/usr/bin/python
"""
Finds unused references in the project, allowing unnecessary/erroneous "fix-refs"
MUST be run before `kicad-fix-refs`
Algorithm:
* open every schema one by one.
* find all references
* walk on that path starting from project schema to see if it really exists
Example output:
--- Examining digital-input.sch
Project schema: Found R5 in input-1 (instance of digital-input.sch)
Project schema: Found R6 in input-2 (instance of digital-input.sch)
Project schema: Found C8 in input-1 (instance of digital-input.sch)
Project schema: Found C9 in input-2 (instance of digital-input.sch)
--- Examining led-layer.sch
--- Examining led-driver.sch
Project schema: Found R28 in front-leds (instance of led-layer.sch)
Project schema: Found R11 in front-leds (instance of led-layer.sch)
Project schema: Found R13 in front-leds (instance of led-layer.sch)
Project schema: Found R24 in front-leds (instance of led-layer.sch)
Project schema: Found R25 in front-leds (instance of led-layer.sch)
Project schema: Found R26 in front-leds (instance of led-layer.sch)
Project schema: Found R27 in front-leds (instance of led-layer.sch)
Project schema: Found R29 in front-leds (instance of led-layer.sch)
Project schema: Found R30 in front-leds (instance of led-layer.sch)
Project schema: Found R31 in front-leds (instance of led-layer.sch)
Project schema: Found R32 in front-leds (instance of led-layer.sch)
Project schema: Found R43 in mb_reset (instance of led-driver.sch)
Project schema: Found R35 in front-leds (instance of led-layer.sch)
Project schema: Found Q10 in front-leds (instance of led-layer.sch)
Project schema: Found Q4 in front-leds (instance of led-layer.sch)
Project schema: Found Q5 in front-leds (instance of led-layer.sch)
"""
import sys, re
import argparse
if len(sys.argv) > 1:
project_name = sys.argv[1]
else:
print "Project name is missing."
exit()
write_to_file = False
if len(sys.argv) > 2 and sys.argv[2] == "--write":
print "*** Writing to file..."
write_to_file = True
if write_to_file:
ans = raw_input("Did you close the Schematic Editor? [y/n] ")
if ans != "y":
print "Schematic Editor should be closed before running this."
exit()
import glob, os
#os.chdir("/mydir")
all_clean = True
for filename in glob.glob("*.sch"):
if filename.startswith('_'):
continue
print "--- Examining {0}".format(filename)
with open(filename, 'r+b') as f:
lines = []
widlist = []
pattern = re.compile('(^AR Path="/)([A-Z0-9/]+)(" )(Ref=")([A-Z]+[0-9]+)(".*)')
for line in f:
ref = pattern.search(line)
if ref:
path = ref.group(2)
reference = ref.group(5)
#print "detected reference: ", path
paths = path.split('/')
#
# Walk on the paths
instance_found = False
with open(project_name + '.sch', 'r') as p:
for pline in p:
# search for this path in project schema
if pline == "U {0}\n".format(paths[0]):
# next line is instance name
instance_name = re.search('[^"]+"([^"]+).*', p.next()).group(1)
# next line is class name (.sch file name)
class_name = re.search('[^"]+"([^"]+).*', p.next()).group(1)
print "\t\tINFO: Project schema: Found {1} in {2} (instance of {3})".format(
paths[0], reference, instance_name, class_name)
instance_found = True
if instance_found:
lines.append(line)
else:
sys.stdout.write("This instance is not found: " + line)
all_clean = False
else:
# some other line, just append
lines.append(line)
new_content = ''.join(lines)
if write_to_file:
f.seek(0)
f.write(new_content)
f.truncate()
if all_clean:
print
print "----------------------------------------------"
print "All clean. Nothing to do here."
print "----------------------------------------------"
print
if not all_clean and not write_to_file:
print "**** This was a dry run, use with '--write' parameter to write to the file."