-
Notifications
You must be signed in to change notification settings - Fork 4
/
kicad-diff-init.py
executable file
·147 lines (128 loc) · 4.82 KB
/
kicad-diff-init.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
#!/usr/bin/env python3
# Copyright (c) 2020-2022 Salvador E. Tropea
# Copyright (c) 2020-2022 Instituto Nacional de Tecnología Industrial
# License: GPL-2.0
# Project: KiCad Diff
"""
KiCad diff tool
This program initializes a repo to use the kicad-git-diff plug-in.
"""
__author__ = 'Salvador E. Tropea'
__copyright__ = 'Copyright 2020, INTI'
__credits__ = ['Salvador E. Tropea']
__license__ = 'GPL 2.0'
__version__ = '2.5.5'
__email__ = '[email protected]'
__status__ = 'beta'
__url__ = 'https://github.com/INTI-CMNB/KiDiff/'
import argparse
import logging
from os.path import isfile, isdir, basename, sep
from os import getcwd
import re
from shutil import which
from subprocess import call
from sys import exit
# Exit error codes
NO_GIT_ROOT = 1
MISSING_GIT = 2
MISSING_SCRIPTS = 3
git_attributes = '.gitattributes'
git_config = '.gitconfig'
layers_file = '.kicad-git-diff'
def CheckAttributes():
if not isfile(git_attributes):
logger.debug('No '+git_attributes)
return False
with open(git_attributes) as attr_file:
for line in attr_file:
if re.match(r'^\*.kicad_pcb\s+diff', line):
attr_file.close()
return True
return False
def CheckCommand():
if not isfile(git_config):
logger.debug('No '+git_config)
return False
with open(git_config) as cfg_file:
for line in cfg_file:
if re.match(r'^\[diff\s+\"kicad_diff\"', line):
cfg_file.close()
return True
return False
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='KiCad diff GIT repo initialization')
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--version', '-V', action='version', version='%(prog)s '+__version__+' - ' +
__copyright__+' - License: '+__license__)
args = parser.parse_args()
# Create a logger with the specified verbosity
if args.verbose >= 2:
log_level = logging.DEBUG
verb = '-vv'
elif args.verbose == 1:
log_level = logging.INFO
verb = '-v'
else:
verb = None
log_level = logging.WARNING
logging.basicConfig(level=log_level)
logger = logging.getLogger(basename(__file__))
# Check the environment
if which('git') is None:
logger.error('No git command, install it')
exit(MISSING_GIT)
if which('kicad-git-diff.py') is None:
logger.error('Please install the diff scripts first')
exit(MISSING_SCRIPTS)
# The script must be invoked from the root of the repo
dir_git = getcwd()+sep+'.git'
if not isdir(dir_git):
logger.error('Run this script from the root of your repo (no .git/ here)')
exit(NO_GIT_ROOT)
# Configure the repo to use a local .gitconfig file
logger.info('Configuring git to use ".gitconfig" as a configuration file')
command = ['git', 'config', '--local', 'include.path', '../'+git_config]
logger.debug(command)
call(command)
# Add an attribute for *.kicad_pcb files
if CheckAttributes():
logger.info('KiCad PCB files already has a diff tool associated')
else:
logger.info('Associating the KiCad PCB extension to a diff plug-in')
with open(git_attributes, "a+") as attr_file:
attr_file.write("*.kicad_pcb diff=kicad_diff\n")
attr_file.write("*.kicad_sch diff=kicad_diff\n")
attr_file.write("*.sch diff=kicad_diff\n")
# Add a command to the new attribute
if CheckCommand():
logger.info('Command already configured')
else:
logger.info("Defining a command to compute a diff between KiCad PCB/SCH files")
with open(git_config, "a+") as cfg_file:
cfg_file.write('[diff "kicad_diff"]\n')
cfg_file.write("\tcommand="+which('kicad-git-diff.py')+" -v\n")
# Add a list of layers to be excluded
if isfile(layers_file):
logger.info('Layer exclusion file already present')
else:
logger.info('Generating a list of layers to be excluded')
with open(layers_file, "w+") as layer_file:
layer_file.write("B.Adhes\n")
layer_file.write("F.Adhes\n")
layer_file.write("#B.Paste\n")
layer_file.write("#F.Paste\n")
layer_file.write("#B.SilkS\n")
layer_file.write("#F.SilkS\n")
layer_file.write("#B.Mask\n")
layer_file.write("#F.Mask\n")
layer_file.write("#Dwgs.User\n")
layer_file.write("Cmts.User\n")
layer_file.write("Eco1.User\n")
layer_file.write("Eco2.User\n")
layer_file.write("Edge.Cuts\n")
layer_file.write("Margin\n")
layer_file.write("#B.CrtYd\n")
layer_file.write("#F.CrtYd\n")
layer_file.write("#B.Fab\n")
layer_file.write("#F.Fab\n")