-
Notifications
You must be signed in to change notification settings - Fork 4
/
kicad-git-diff.py
executable file
·108 lines (95 loc) · 3.77 KB
/
kicad-git-diff.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
#!/usr/bin/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
# Adapted from: https://github.com/obra/kicad-tools
"""
KiCad diff tool
This program is a git plug-in to generate diffs between two KiCad PCBs.
It relies on kicad-diff.py script (where the real work is done).
"""
__author__ = 'Salvador E. Tropea'
__copyright__ = 'Copyright 2020, INTI'
__credits__ = ['Salvador E. Tropea', 'Jesse Vincent']
__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, dirname, realpath
from os import getcwd, mkdir
from subprocess import call
from sys import exit
# Exit error codes
OLD_PCB_INVALID = 1
NEW_PCB_INVALID = 2
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='KiCad diff wrapper for Git')
parser.add_argument('old_file_name', help='PCB/SCH name')
parser.add_argument('old_file_file', help='Original PCB/SCH file')
parser.add_argument('old_file_hash', help='Original PCB/SCH hash')
parser.add_argument('old_file_perm', help='Original PCB/SCH perms')
parser.add_argument('new_file_file', help='New PCB/SCH file')
parser.add_argument('new_file_hash', help='New PCB/SCH hash')
parser.add_argument('new_file_perm', help='New PCB/SCH perms')
parser.add_argument('--resolution', nargs=1, help='Image resolution in DPIs [150]', default=['150'])
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--version', 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 arguments
old_file = args.old_file_file
if not isfile(old_file):
logger.error('%s isn\'t a valid file name' % old_file)
exit(OLD_PCB_INVALID)
new_file = args.new_file_file
if not isfile(new_file):
logger.error('%s isn\'t a valid file name' % new_file)
exit(NEW_PCB_INVALID)
resolution = int(args.resolution[0])
# The script is invoked by git from the root of the repo
dir_git = getcwd()+sep+'.git'
dir_cache = None
if not isdir(dir_git):
logger.error('can\'t find the git repo (no '+dir_git+')')
else:
dir_cache = dir_git+sep+'kicad-git-cache'
if not isdir(dir_cache):
try:
mkdir(dir_cache)
except OSError:
logger.error('can\'t create cache dir ('+dir_cache+')')
dir_cache = None
command = [dirname(realpath(__file__))+sep+'kicad-diff.py', '--resolution', str(resolution),
'--old_file_hash', args.old_file_hash]
if int(args.new_file_hash, 16):
# When we compare to the currently modified file git uses 0 as hash
# If we use it all files that we didn't yet commit become the same
command += ['--new_file_hash', args.new_file_hash]
if verb is not None:
command.append(verb)
if dir_cache is not None:
command.append('--cache_dir')
command.append(dir_cache)
if isfile('.kicad-git-diff'):
command.append('--exclude')
command.append('.kicad-git-diff')
command.append(old_file)
command.append(new_file)
logger.debug(command)
call(command)