Always better in Python.

This commit is contained in:
Francois Best 2014-04-02 08:55:45 +02:00
parent f8511a9591
commit e39f6a7b99
2 changed files with 116 additions and 59 deletions

116
res/validate.py Normal file
View File

@ -0,0 +1,116 @@
# -*- coding: utf-8 -*-
import sys
import os
import shutil
import subprocess
from pprint import pprint
# ------------------------------------------------------------------------------
rootDir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
logsDir = os.path.join(rootDir, 'logs')
resDir = os.path.join(rootDir, 'res')
srcDir = os.path.join(rootDir, 'src')
# ------------------------------------------------------------------------------
class Arduino:
if sys.platform == 'darwin':
binary = '/Applications/Arduino.app/Contents/MacOS/JavaApplicationStub'
home = os.path.expanduser('~/Documents/Arduino')
elif sys.platform == 'win32':
binary = 'arduino.exe'
home = os.path.expanduser('~/My Documents/Arduino')
elif sys.platform == 'linux':
binary = 'arduino'
home = os.path.expanduser('~/Arduino')
else:
print('Unsupported platform %s' % str(sys.platform))
sys.exit(1)
libraryDir = os.path.join(home, 'libraries')
boards = {
'Uno': 'arduino:avr:uno',
'Leonardo': 'arduino:avr:leonardo',
'Mega': 'arduino:avr:mega',
}
def checkReturnCode(code):
if code == 0:
return True
if code == 1:
print("Operation failed.")
if code == 2:
print("File not found")
if code == 3:
print("Invalid argument")
return False
def verify(sketch, board):
return Arduino.checkReturnCode(subprocess.call([
Arduino.binary,
'--verify', sketch,
'--board', board,
#'--verbose-build',
]))
# ------------------------------------------------------------------------------
class ArduinoMidiLibrary:
def __init__(self):
self.path = os.path.join(Arduino.libraryDir, 'MIDI')
self.sources = self.getSources()
self.resources = self.getResources()
def getSources(self):
sources = dict()
for root, dirs, files in os.walk(srcDir):
for name, ext in [os.path.splitext(f) for f in files]:
if ext in ('.cpp', '.hpp', '.h'):
source = os.path.join(root, name + ext)
dest = os.path.join(self.path, os.path.relpath(source, srcDir))
sources[source] = dest
return sources
def getResources(self):
return {
os.path.join(resDir, 'keywords.txt'): os.path.join(self.path, 'keywords.txt'),
os.path.join(resDir, 'examples/'): os.path.join(self.path, 'examples/'),
}
def install(self):
payloads = dict(list(self.sources.items()) + list(self.resources.items()))
for s,d in payloads.items():
if not os.path.exists(os.path.dirname(d)):
os.makedirs(os.path.dirname(d))
if os.path.isfile(s):
shutil.copy2(s, d)
elif os.path.isdir(s):
if os.path.exists(d):
shutil.rmtree(d)
shutil.copytree(s, d)
def getInstalledExamples(self):
exDir = os.path.join(self.path, 'examples')
return [os.path.join(exDir, x, x + '.ino') for x in os.listdir(exDir)]
def validate(self):
for boardName, boardId in Arduino.boards.items():
# Validate examples
print('Validation for Arduino %s' % boardName)
for example in self.getInstalledExamples():
Arduino.verify(example, boardId)
# ------------------------------------------------------------------------------
def main():
lib = ArduinoMidiLibrary()
lib.install()
lib.validate()
# ------------------------------------------------------------------------------
if __name__ == '__main__':
main()

View File

@ -1,59 +0,0 @@
#!/bin/sh
cd "`dirname "${0}"`"
root="`pwd`/.."
logsDir="$root/logs"
libDir="$HOME/Documents/Arduino/libraries/MIDI"
arduino="/Applications/Arduino.app/Contents/MacOS/JavaApplicationStub"
# ------------------------------------------------------------------------------
function cleanup
{
# Parameter:
# $1: working directory
mkdir -p "$1"
rm -rf "$/*"
}
function installTo
{
# Parameter:
# $1: install directory
cp -rf "$root/src/*" "$1/"
cp -rf "$root/res/examples" "$1/"
cp -rf "$root/res/keywords.txt" "$1/"
}
function verify
{
# Parameter:
# $1: path to .ino file
# $2: board option
"$arduino" \
--verify "$1" \
--verbose-build \
#--board arduino:avr:leonardo
}
function checkExamples
{
cwd=`pwd`
cd "$libDir/examples"
for d in *;
do
verify "$libDir/examples/$d/$d.ino" \
> "$logsDir/$d.build.log" \
2> "$logsDir/$d.build.err.log"
done
cd "$cwd"
}
# ------------------------------------------------------------------------------
# Main
#cleanup "$libDir"
#installTo "$libDir"
checkExamples