From b3a681f76ea22f1e7a0eac7870bb9baf9456f191 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Tue, 3 Mar 2020 09:21:20 -0300 Subject: [PATCH] Added script to init the repo. Changed version to 1.1.0. --- .gitattributes | 1 - .gitconfig | 3 - .kicad_pcb-git-diff | 7 -- Makefile | 2 + README.md | 34 +++++----- debian/changelog | 6 ++ kicad_pcb-diff-init.py | 142 +++++++++++++++++++++++++++++++++++++++++ kicad_pcb-diff.py | 2 +- kicad_pcb-git-diff.py | 2 +- 9 files changed, 168 insertions(+), 31 deletions(-) delete mode 100644 .gitattributes delete mode 100644 .gitconfig delete mode 100644 .kicad_pcb-git-diff create mode 100755 kicad_pcb-diff-init.py diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 30bc254..0000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -*.kicad_pcb diff=kicad_pcb diff --git a/.gitconfig b/.gitconfig deleted file mode 100644 index 621374f..0000000 --- a/.gitconfig +++ /dev/null @@ -1,3 +0,0 @@ -[diff "kicad_pcb"] - command=/usr/bin/kicad_pcb-git-diff.py -v - diff --git a/.kicad_pcb-git-diff b/.kicad_pcb-git-diff deleted file mode 100644 index bd95f0c..0000000 --- a/.kicad_pcb-git-diff +++ /dev/null @@ -1,7 +0,0 @@ -B.Adhes -F.Adhes -Cmts.User -Eco1.User -Eco2.User -Edge.Cuts -Margin diff --git a/Makefile b/Makefile index 02b0fdb..8647184 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ all: install: install -D kicad_pcb-diff.py $(DESTDIR)$(prefix)/bin/kicad_pcb-diff.py install -D kicad_pcb-git-diff.py $(DESTDIR)$(prefix)/bin/kicad_pcb-git-diff.py + install -D kicad_pcb-diff-init.py $(DESTDIR)$(prefix)/bin/kicad_pcb-diff-init.py clean: @@ -14,6 +15,7 @@ distclean: clean uninstall: -rm -f $(DESTDIR)$(prefix)/bin/kicad_pcb-diff.py -rm -f $(DESTDIR)$(prefix)/bin/kicad_pcb-git-diff.py + -rm -f $(DESTDIR)$(prefix)/bin/kicad_pcb-diff-init.py .PHONY: all install clean distclean uninstall diff --git a/README.md b/README.md index 8fa6364..128219c 100644 --- a/README.md +++ b/README.md @@ -36,29 +36,27 @@ In a Debian/Ubuntu system you'll get them running: ## Standalone use -1. Copy the python scripts to some executable directory in your path (i.e. to /usr/local/bin/) -2. Well ... that's all - -## Git plug-in - -1. Copy the python scripts to some executable directory in your path (i.e. to /usr/local/bin/) -2. To force git to look for a plug-in to show diffs between KiCad PCBs create a *.gitattributes* file at the root of your repo containing: +1. As root run: ``` -*.kicad_pcb diff=kicad_pcb +# make install ``` -3. Now we have to define the *kicad_pcb* plug-in. This is done in the *gitconfig* file. In order to make it local to this repo you have to configure git to include the local file *.gitconfig*: - ``` - $ git config --local include.path ../.gitconfig - ``` -4. Now we create a *.gitconfig* file at the root of the repo defining the plug-in action: + +The scripts will be copied to */usr/local/bin*. If you want to install the scripts in */usr/bin* run + ``` -[diff "kicad_pcb"] - command=/usr/local/bin/kicad_pcb-git-diff.py -v +# make prefix=/usr install ``` -5. If you want to exclude some layers from the diff create a file named *.kicad_pcb-git-diff* at the root of your repo containing the names of the layer to be excluded. You have to use line for each layer. -An example of *.gitconfig*, *gitconfig* and *.kicad_pcb-git-diff* can be found in this repo. -Once configured the tool will be used every time you do a git diff. +## Git plug-in + +1. Install the scripts +2. To initialize a repo just run the *kicad_pcb-diff-init.py* script from the root of the repo. +This will configure the repo to read extra configuration from the *.gitconfig* file. +It will also associate the *kicad_pcb* file extension with the *kicad_pcb-git-diff.py* script. +3. The initialization script will create a list of layers to be excluded in the *.kicad_pcb-git-diff* file. +Review this file and adjust it to your needs. Lines starting with *#* will be ignored. + +Once configured the tool will be used every time you do a diff using *git*. # Usage diff --git a/debian/changelog b/debian/changelog index 6c1cc3f..8c673fa 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +kicad-pcb-diff (1.1.0-1) testing; urgency=medium + + * Added script to init the repo. + + -- Salvador Eduardo Tropea Tue, 03 Mar 2020 09:17:18 -0300 + kicad-pcb-diff (1.0.0-1) unstable; urgency=low * Initial release. diff --git a/kicad_pcb-diff-init.py b/kicad_pcb-diff-init.py new file mode 100755 index 0000000..95cbad4 --- /dev/null +++ b/kicad_pcb-diff-init.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 +"""KiCad PCB diff tool + +This program initializes a repo to use the kicad_pcb-git-diff plug-in. +""" +__author__ ='Salvador E. Tropea' +__copyright__='Copyright 2020, INTI' +__credits__ =['Salvador E. Tropea'] +__license__ ='GPL 2.0' +__version__ ='1.1.0' +__email__ ='salvador@inti.gob.ar' +__status__ ='beta' + +from os.path import (isfile,isdir,basename,sep,dirname,realpath) +from os import (getcwd) +from shutil import (which) +from sys import (exit) +from subprocess import (call) +import argparse +import logging +import re + +# Exit error codes +NO_GIT_ROOT=1 +MISSING_GIT=2 +MISSING_SCRIPTS=3 + +git_attributes='.gitattributes' +git_config='.gitconfig' +layers_file='.kicad_pcb-git-diff' + +def CheckAttributes(): + if not isfile(git_attributes): + logger.debug('No '+git_attributes) + return False + attr_file=open(git_attributes,"r") + for line in attr_file: + if re.match('^\*.kicad_pcb\s+diff',line): + attr_file.close() + return True + attr_file.close() + return False + +def CheckCommand(): + if not isfile(git_config): + logger.debug('No '+git_config) + return False + cfg_file=open(git_config,"r") + for line in cfg_file: + if re.match('^\[diff\s+\"kicad_pcb_diff\"',line): + cfg_file.close() + return True + cfg_file.close() + return False + +if __name__=='__main__': + parser=argparse.ArgumentParser(description='KiCad PCB 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')==None: + logger.error('No git command, install it') + exit(MISSING_GIT) + if which('kicad_pcb-git-diff.py')==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') + attr_file=open(git_attributes,"a+") + attr_file.write("*.kicad_pcb diff=kicad_pcb_diff\n") + attr_file.close() + + # 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 files') + cfg_file=open(git_config,"a+") + cfg_file.write("[diff \"kicad_pcb_diff\"]\n") + cfg_file.write("\tcommand="+which('kicad_pcb-git-diff.py')+" -v\n") + cfg_file.close() + + # 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') + layer_file=open(layers_file,"w+") + 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") + layer_file.close() + diff --git a/kicad_pcb-diff.py b/kicad_pcb-diff.py index 1e0b6f4..1816db9 100755 --- a/kicad_pcb-diff.py +++ b/kicad_pcb-diff.py @@ -21,7 +21,7 @@ __copyright__='Copyright 2020, INTI/'+__author__ __credits__ =['Salvador E. Tropea','Jesse Vincent'] __license__ ='GPL 2.0' -__version__ ='1.0.0' +__version__ ='1.1.0' __email__ ='salvador@inti.gob.ar' __status__ ='beta' diff --git a/kicad_pcb-git-diff.py b/kicad_pcb-git-diff.py index e1cd84d..4b8d85b 100755 --- a/kicad_pcb-git-diff.py +++ b/kicad_pcb-git-diff.py @@ -8,7 +8,7 @@ __copyright__='Copyright 2020, INTI' __credits__ =['Salvador E. Tropea','Jesse Vincent'] __license__ ='GPL 2.0' -__version__ ='1.0.0' +__version__ ='1.1.0' __email__ ='salvador@inti.gob.ar' __status__ ='beta' # PCB diff tool