ardmk-init linted using pylint
This commit is contained in:
parent
a165a3bf26
commit
c2d17c825a
1 changed files with 138 additions and 90 deletions
228
bin/ardmk-init
228
bin/ardmk-init
|
@ -1,15 +1,30 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
Arduino-mk Makefile and project initialiser
|
||||||
|
|
||||||
|
This script can be used in it's basic form create a project specific Makefile
|
||||||
|
for use with Arduino-mk. Addionally, it can be used to create a template
|
||||||
|
Arduino source file and a traditional boilerplate project file structure.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
* Run prompted within current working directory: `ardmk-init`
|
||||||
|
* Create Arduino Uno Makefile (useful within a library example): `ardmk-init -qb uno`
|
||||||
|
* Create boilerplate Arduino Uno project in current working directory of same
|
||||||
|
name: `ardmk-init -b uno --quiet --project`
|
||||||
|
* Create Arduino-mk nano Makefile in current working directory with template .ino:
|
||||||
|
`ardmk-init -b nano -u atmega328 -qtn my-project`
|
||||||
|
|
||||||
|
See `armk-init --help` for CLI arguments
|
||||||
|
"""
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import serial
|
|
||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
from clint.textui import prompt, validators, colored, puts
|
from clint.textui import prompt, validators, colored, puts
|
||||||
|
|
||||||
## Global Vars
|
## Global Vars
|
||||||
VERSION = "1.0"
|
VERSION = "1.0"
|
||||||
directory = os.getcwd()
|
ARD_TEMPLATE = "\n\
|
||||||
ard_template = "\n\
|
|
||||||
#include <Arduino.h>\n\
|
#include <Arduino.h>\n\
|
||||||
#include <Wire.h>\n\
|
#include <Wire.h>\n\
|
||||||
\n\
|
\n\
|
||||||
|
@ -22,30 +37,38 @@ void loop() {\n\
|
||||||
"
|
"
|
||||||
|
|
||||||
## Command Parser
|
## Command Parser
|
||||||
parser = argparse.ArgumentParser(description='Arduino Makefile and boilerplate project generator. For use with Ard-Makefile: https://github.com/sudar/Arduino-Makefile. Script created by John Whittington https://github.com/tuna-f1sh 2017\n\nVersion: ' + VERSION, usage='"ardmk-init -b uno -quiet -project" Automonously create boiler plate Arduino Uno project in current working directory with same name. See --help for more.')
|
PARSER = argparse.ArgumentParser(description='Arduino Makefile and boilerplate project generator.\
|
||||||
parser.add_argument('-v', '--verbose', action='store_true', help="print file contents during creation")
|
For use with Ard-Makefile: https://github.com/sudar/Arduino-Makefile.\
|
||||||
parser.add_argument('-d', '--directory', default=directory, help='directory to run generator')
|
Script created by John Whittington https://github.com/tuna-f1sh 2017\
|
||||||
parser.add_argument('-b', '--board', default='uno', help='board tag')
|
\n\nVersion: ' + VERSION, usage='ardmk-init # prompted CLI, see --help for more.')
|
||||||
parser.add_argument('-u', '--micro', default='AUTO', help='microcontroller on board')
|
PARSER.add_argument('-v', '--verbose', action='store_true',
|
||||||
parser.add_argument('-f', '--freq', default='AUTO', help='mlock frequency')
|
help="print file contents during creation")
|
||||||
parser.add_argument('-p', '--port', default='AUTO', help='monitor port')
|
PARSER.add_argument('-d', '--directory', default=os.getcwd(), help='directory to run generator')
|
||||||
parser.add_argument('-n', '--name', default=os.path.basename(directory), help='project name')
|
PARSER.add_argument('-b', '--board', default='uno', help='board tag')
|
||||||
parser.add_argument('-q', '--quiet', action='store_true', help='run quiet without user prompts')
|
PARSER.add_argument('-u', '--micro', default='AUTO', help='microcontroller on board')
|
||||||
parser.add_argument('-P', '--project', action='store_true', help='create boilerplate project with src, lib and bin folder structure')
|
PARSER.add_argument('-f', '--freq', default='AUTO', help='mlock frequency')
|
||||||
parser.add_argument('-t', '--template', action='store_true', help='create bare minimum Arduino source file')
|
PARSER.add_argument('-p', '--port', default='AUTO', help='monitor port')
|
||||||
args = parser.parse_args()
|
PARSER.add_argument('-n', '--name', default=os.path.basename(os.getcwd()), help='project name')
|
||||||
|
PARSER.add_argument('-q', '--quiet', action='store_true', help='run quiet without user prompts')
|
||||||
|
PARSER.add_argument('-P', '--project', action='store_true',
|
||||||
|
help='create boilerplate project with src, lib and bin folder structure')
|
||||||
|
PARSER.add_argument('-t', '--template', action='store_true',
|
||||||
|
help='create bare minimum Arduino source file')
|
||||||
|
ARGS = PARSER.parse_args()
|
||||||
|
|
||||||
directory = args.directory
|
def generate_makefile():
|
||||||
|
"""
|
||||||
def generateMakefile():
|
Generate the Makefile content using prompts or parsed arguments
|
||||||
|
"""
|
||||||
# Header
|
# Header
|
||||||
fileContents = "# Generated by ard-make version " + VERSION + "\n\n"
|
file_content = "# Generated by ard-make version " + VERSION + "\n\n"
|
||||||
|
|
||||||
# Basic
|
# Basic
|
||||||
if not args.quiet:
|
if not ARGS.quiet:
|
||||||
puts(colored.cyan("Generating Arduino Ard-Makefile project in " + os.path.abspath(directory)))
|
puts(colored.cyan("Generating Arduino Ard-Makefile project in "
|
||||||
|
+ os.path.abspath(ARGS.directory)))
|
||||||
btag = prompt.query('Board tag?', default='uno')
|
btag = prompt.query('Board tag?', default='uno')
|
||||||
if not btag == 'uno':
|
if btag != 'uno':
|
||||||
bsub = prompt.query('Board sub micro?', default='atmega328')
|
bsub = prompt.query('Board sub micro?', default='atmega328')
|
||||||
f_cpu = prompt.query('Board frequency', default='16000000L')
|
f_cpu = prompt.query('Board frequency', default='16000000L')
|
||||||
else:
|
else:
|
||||||
|
@ -53,22 +76,24 @@ def generateMakefile():
|
||||||
f_cpu = 'AUTO'
|
f_cpu = 'AUTO'
|
||||||
monitor_port = prompt.query('Arduino port?', default='AUTO')
|
monitor_port = prompt.query('Arduino port?', default='AUTO')
|
||||||
else:
|
else:
|
||||||
btag = args.board
|
btag = ARGS.board
|
||||||
bsub = args.micro
|
bsub = ARGS.micro
|
||||||
f_cpu = args.freq
|
f_cpu = ARGS.freq
|
||||||
monitor_port = args.port
|
monitor_port = ARGS.port
|
||||||
|
|
||||||
fileContents += checkDefine('BOARD_TAG', btag)
|
file_content += check_define('BOARD_TAG', btag)
|
||||||
fileContents += checkDefine('BOARD_SUB', bsub)
|
file_content += check_define('BOARD_SUB', bsub)
|
||||||
fileContents += checkDefine('F_CPU', f_cpu)
|
file_content += check_define('F_CPU', f_cpu)
|
||||||
fileContents += checkDefine('MONITOR_PORT', monitor_port)
|
file_content += check_define('MONITOR_PORT', monitor_port)
|
||||||
|
|
||||||
# Extended
|
# Extended
|
||||||
if not args.quiet:
|
if not ARGS.quiet:
|
||||||
if not prompt.yn('Extended options?', default='n'):
|
if not prompt.yn('Extended options?', default='n'):
|
||||||
if not prompt.yn('Define local folders?', default='n'):
|
if not prompt.yn('Define local folders?', default='n'):
|
||||||
src_dir = prompt.query('Sources folder (Makefile will be created here)?', default='', validators=[])
|
src_dir = prompt.query('Sources folder (Makefile will be created here)?',\
|
||||||
userlibs = prompt.query('Library folder (will create if does not exist) - AUTO is Sketchbook directory?', default='AUTO', validators=[])
|
default='', validators=[])
|
||||||
|
userlibs = prompt.query('Library folder (will create if does not exist) - AUTO is Sketchbook directory?',
|
||||||
|
default='AUTO', validators=[])
|
||||||
obj_dir = prompt.query('Output directory?', default='AUTO', validators=[])
|
obj_dir = prompt.query('Output directory?', default='AUTO', validators=[])
|
||||||
else:
|
else:
|
||||||
src_dir = ''
|
src_dir = ''
|
||||||
|
@ -78,37 +103,38 @@ def generateMakefile():
|
||||||
isp_prog = prompt.query('ISP programmer?', default='atmelice_isp')
|
isp_prog = prompt.query('ISP programmer?', default='atmelice_isp')
|
||||||
isp_port = prompt.query('ISP port?', default='AUTO')
|
isp_port = prompt.query('ISP port?', default='AUTO')
|
||||||
if not prompt.yn('Quiet make?', default='n'):
|
if not prompt.yn('Quiet make?', default='n'):
|
||||||
fileContents += "ARDUINO_QUIET = 1\n"
|
file_content += "ARDUINO_QUIET = 1\n"
|
||||||
|
|
||||||
fileContents += checkDefine('ISP_PROG', isp_prog)
|
file_content += check_define('ISP_PROG', isp_prog)
|
||||||
fileContents += checkDefine('ISP_PORT', isp_port)
|
file_content += check_define('ISP_PORT', isp_port)
|
||||||
fileContents += checkDefine('BOARDS_TXT', boards_txt)
|
file_content += check_define('BOARDS_TXT', boards_txt)
|
||||||
|
|
||||||
# Check andd create folders
|
# Check andd create folders
|
||||||
checkCreateFolder(src_dir)
|
check_create_folder(src_dir)
|
||||||
checkCreateFolder(userlibs)
|
check_create_folder(userlibs)
|
||||||
checkCreateFolder(obj_dir)
|
check_create_folder(obj_dir)
|
||||||
|
|
||||||
# Makefile will be in src_dir so lib and bin must be relative
|
# Makefile will be in src_dir so lib and bin must be relative
|
||||||
if src_dir:
|
if src_dir:
|
||||||
userlibs = "../" + userlibs
|
userlibs = "../" + userlibs
|
||||||
obj_dir = "../" + obj_dir
|
obj_dir = "../" + obj_dir
|
||||||
|
|
||||||
fileContents += checkDefine('USER_LIB_PATH', userlibs)
|
file_content += check_define('USER_LIB_PATH', userlibs)
|
||||||
fileContents += checkDefine('OBJDIR', obj_dir)
|
file_content += check_define('OBJDIR', obj_dir)
|
||||||
else:
|
else:
|
||||||
src_dir = ''
|
src_dir = ''
|
||||||
|
|
||||||
if args.template or not prompt.yn('Create template Arduino source?', default='n'):
|
if ARGS.template or not prompt.yn('Create template Arduino source?', default='n'):
|
||||||
sourceFilename = prompt.query('Name of project?', default=os.path.basename(os.getcwd()))
|
source_filename = prompt.query('Name of project?',
|
||||||
|
default=os.path.basename(os.getcwd()))
|
||||||
if src_dir:
|
if src_dir:
|
||||||
writeTemplate(src_dir + "/" + sourceFilename)
|
write_template(src_dir + "/" + source_filename)
|
||||||
else:
|
else:
|
||||||
writeTemplate(sourceFilename)
|
write_template(source_filename)
|
||||||
fileContents += checkDefine('TARGET', sourceFilename)
|
file_content += check_define('TARGET', source_filename)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if args.project:
|
if ARGS.project:
|
||||||
src_dir = 'src'
|
src_dir = 'src'
|
||||||
userlibs = 'lib'
|
userlibs = 'lib'
|
||||||
obj_dir = 'bin'
|
obj_dir = 'bin'
|
||||||
|
@ -118,89 +144,111 @@ def generateMakefile():
|
||||||
obj_dir = 'AUTO'
|
obj_dir = 'AUTO'
|
||||||
|
|
||||||
# Check andd create folders
|
# Check andd create folders
|
||||||
checkCreateFolder(src_dir)
|
check_create_folder(src_dir)
|
||||||
checkCreateFolder(userlibs)
|
check_create_folder(userlibs)
|
||||||
checkCreateFolder(obj_dir)
|
check_create_folder(obj_dir)
|
||||||
|
|
||||||
# Makefile will be in src_dir so lib and bin must be relative
|
# Makefile will be in src_dir so lib and bin must be relative
|
||||||
if src_dir:
|
if src_dir:
|
||||||
userlibs = "../" + userlibs
|
userlibs = "../" + userlibs
|
||||||
obj_dir = "../" + obj_dir
|
obj_dir = "../" + obj_dir
|
||||||
|
|
||||||
fileContents += checkDefine('USER_LIB_PATH', userlibs)
|
file_content += check_define('USER_LIB_PATH', userlibs)
|
||||||
fileContents += checkDefine('OBJDIR', obj_dir)
|
file_content += check_define('OBJDIR', obj_dir)
|
||||||
|
|
||||||
if args.project or args.template:
|
if ARGS.project or ARGS.template:
|
||||||
if src_dir:
|
if src_dir:
|
||||||
writeTemplate(src_dir + "/" + args.name)
|
write_template(src_dir + "/" + ARGS.name)
|
||||||
else:
|
else:
|
||||||
writeTemplate(args.name)
|
write_template(ARGS.name)
|
||||||
fileContents += checkDefine('TARGET', args.name)
|
file_content += check_define('TARGET', ARGS.name)
|
||||||
|
|
||||||
if not "ARDMK_DIR" in os.environ:
|
if not "ARDMK_DIR" in os.environ:
|
||||||
if args.quiet:
|
if ARGS.quiet:
|
||||||
puts(colored.magenta('Warning: ARDMK_DIR environment variable not defined. Must be defined for Makefile to work'))
|
puts(colored.magenta('Warning: ARDMK_DIR environment variable not defined. \
|
||||||
|
Must be defined for Makefile to work'))
|
||||||
else:
|
else:
|
||||||
ardmk = prompt.query('Arduino Makefile path?', default='/usr/share/arduino', validators=[validators.PathValidator()])
|
ardmk = prompt.query('Arduino Makefile path?',
|
||||||
|
default='/usr/share/arduino',
|
||||||
|
validators=[validators.PathValidator()])
|
||||||
ardmk = "ARDMK_DIR := " + ardmk + "\n"
|
ardmk = "ARDMK_DIR := " + ardmk + "\n"
|
||||||
|
|
||||||
fileContents += "\ninclude $(ARDMK_DIR)/Arduino.mk"
|
file_content += "\ninclude $(ARDMK_DIR)/Arduino.mk"
|
||||||
|
|
||||||
# Add forward slash if source directory exists
|
# Add forward slash if source directory exists
|
||||||
if src_dir:
|
if src_dir:
|
||||||
writeToMakefile(fileContents, (src_dir + "/"))
|
write_to_makefile(file_content, (src_dir + "/"))
|
||||||
else:
|
else:
|
||||||
writeToMakefile(fileContents, "")
|
write_to_makefile(file_content, "")
|
||||||
|
|
||||||
return fileContents
|
return file_content
|
||||||
|
|
||||||
def writeToMakefile(fileContents, path):
|
def write_to_makefile(file_content, path):
|
||||||
|
"""
|
||||||
|
Write the Makefile file
|
||||||
|
"""
|
||||||
makefile = open(path + "Makefile", 'w')
|
makefile = open(path + "Makefile", 'w')
|
||||||
puts(colored.cyan("Writing Makefile..."))
|
puts(colored.cyan("Writing Makefile..."))
|
||||||
if args.verbose:
|
if ARGS.verbose:
|
||||||
puts(colored.yellow(fileContents))
|
puts(colored.yellow(file_content))
|
||||||
makefile.write(fileContents)
|
makefile.write(file_content)
|
||||||
makefile.close()
|
makefile.close()
|
||||||
|
|
||||||
def writeTemplate(filename):
|
def write_template(filename):
|
||||||
|
"""
|
||||||
|
Write template Arduino .ino source
|
||||||
|
"""
|
||||||
puts(colored.cyan("Writing " + os.path.abspath(filename) + ".ino..."))
|
puts(colored.cyan("Writing " + os.path.abspath(filename) + ".ino..."))
|
||||||
if os.path.isfile(filename + '.ino'):
|
if os.path.isfile(filename + '.ino'):
|
||||||
if args.quiet:
|
if ARGS.quiet:
|
||||||
puts(colored.red(filename + '.ino' + ' already exists! Stopping.'))
|
puts(colored.red(filename + '.ino' + ' already exists! Stopping.'))
|
||||||
return
|
return
|
||||||
puts(colored.red(filename + '.ino' + ' already exists! Overwrite?'))
|
puts(colored.red(filename + '.ino' + ' already exists! Overwrite?'))
|
||||||
if prompt.yn('Continue?', default='n'):
|
if prompt.yn('Continue?', default='n'):
|
||||||
return
|
return
|
||||||
src = open((filename + ".ino"),'w')
|
src = open((filename + ".ino"), 'w')
|
||||||
if args.verbose:
|
if ARGS.verbose:
|
||||||
puts(colored.yellow(ard_template))
|
puts(colored.yellow(ARD_TEMPLATE))
|
||||||
src.write("/* Project: " + filename + " */\n" + ard_template)
|
src.write("/* Project: " + filename + " */\n" + ARD_TEMPLATE)
|
||||||
src.close()
|
src.close()
|
||||||
|
|
||||||
def checkCreateFolder(folder):
|
def check_create_folder(folder):
|
||||||
|
"""
|
||||||
|
Check if folder exists and make it if it doesn't and hasn't been set to AUTO
|
||||||
|
"""
|
||||||
if folder and not folder == 'AUTO':
|
if folder and not folder == 'AUTO':
|
||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
puts(colored.cyan(("Creating " + os.path.abspath(folder) + " folder")))
|
puts(colored.cyan(("Creating " + os.path.abspath(folder) + " folder")))
|
||||||
os.makedirs(folder)
|
os.makedirs(folder)
|
||||||
|
|
||||||
def checkDefine(define, user):
|
def check_define(define, user):
|
||||||
|
"""
|
||||||
|
Check whether user has set define and return Makefile formatted string if they have
|
||||||
|
"""
|
||||||
|
# Return is empty unless user has passed value
|
||||||
|
string = ""
|
||||||
|
|
||||||
|
# Set define only if not empty or set to AUTO
|
||||||
if user and not user == 'AUTO':
|
if user and not user == 'AUTO':
|
||||||
return define + " = " + user + "\n"
|
string = define + " = " + user + "\n"
|
||||||
else:
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def main():
|
return string
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
# Create directory if not exist
|
# Create directory if not exist
|
||||||
checkCreateFolder(directory)
|
check_create_folder(ARGS.directory)
|
||||||
# Change to dir so all commands are run relative
|
# Change to dir so all commands are run relative
|
||||||
os.chdir(directory)
|
os.chdir(ARGS.directory)
|
||||||
if os.path.isfile('Makefile'):
|
if os.path.isfile('Makefile'):
|
||||||
if args.quiet:
|
if ARGS.quiet:
|
||||||
puts(colored.red('Makefile in ' + os.path.abspath(directory) + ' already exists! Stopping.'))
|
puts(colored.red('Makefile in ' + os.path.abspath(ARGS.directory)
|
||||||
return
|
+ ' already exists! Stopping.'))
|
||||||
puts(colored.red('Makefile in ' + os.path.abspath(directory) + ' already exists! Overwrite?'))
|
quit()
|
||||||
if prompt.yn('Continue?', default='n'):
|
|
||||||
return
|
|
||||||
generateMakefile();
|
|
||||||
|
|
||||||
main()
|
# Confirm with user if not quiet mode
|
||||||
|
puts(colored.red('Makefile in ' + os.path.abspath(ARGS.directory)
|
||||||
|
+ ' already exists! Overwrite?'))
|
||||||
|
if prompt.yn('Continue?', default='n'):
|
||||||
|
quit()
|
||||||
|
# Run it
|
||||||
|
generate_makefile()
|
||||||
|
|
Loading…
Reference in a new issue