#!/usr/bin/python3 # # Startup kicad schema or PCB editor. # # - Changes to the directory containing the specified file. # - Fixes the filename if possible. import os import os.path import sys def usage( scriptname, extension ): print( "usage:" ) print( " %s [options] file%s" % ( scriptname, extension ) ) print( "options:" ) print( " -n new file, do not check for presence of the file." ) def doit(): scriptname = os.path.basename( sys.argv[0] ) if scriptname == "e": extension = ".kicad_sch" application = "eeschema" elif scriptname == "p": extension = ".kicad_pcb" application = "pcbnew" else: print( "unrecognized script name '%s'" % scriptname ) return if len(sys.argv) < 2: usage( scriptname, extension ) return new_file = False i = 1 while i < len(sys.argv) and sys.argv[i].startswith( "-" ): arg = sys.argv[i] i += 1 if arg == "-h" or arg == "--help": usage( scriptname, extension ) return elif arg == "-n": new_file = True else: print( "unknown opetion '%s'" % arg ) return if (i+1) != len(sys.argv): usage( scriptname, extension ) return filename = os.path.abspath( sys.argv[i] ) basename = os.path.basename( filename ) dirname = filename if not os.path.isdir( filename ): dirname = os.path.dirname( filename ) if dirname != "": os.chdir( dirname ) name = basename if not name.endswith( extension ): if name.endswith( ".kicad_" ): name = name[:-7] elif name.endswith( "." ): name = name[:-1] name = name + extension name = os.path.join( dirname, name ) if new_file or os.path.isfile( name ): print( "Directory:", os.getcwd() ) print( "Running: ", application, name ) # os.execlp( application, application, name ) os.spawnlp( os.P_NOWAIT, application, application, name ) elif not os.path.exists( name ): print( "File '%s' does not exist." % name ) else: print( "'%s' is not a regular file." % name ) if __name__ == "__main__": doit()