summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Shreve <j.a.shreve@gmail.com>2021-08-21 15:54:57 -0400
committerJoshua Shreve <j.a.shreve@gmail.com>2021-08-21 15:54:57 -0400
commit4c179f900053db8acb072cf6eb6e73b40293fea7 (patch)
treea4a043b49629cb897544128e73c27163d231f36c
parent1f6206c9fe3bc7b85dd624550020452567921400 (diff)
parent3a74bee2fcd1561f5cf23a4f57fe41062203313e (diff)
Merge branch 'pr/30'
-rw-r--r--.gitignore1
-rw-r--r--configs/.gitkeep0
-rwxr-xr-xrun.sh125
-rw-r--r--src/dactyl_manuform.py15
-rw-r--r--src/generate_configuration.py30
5 files changed, 150 insertions, 21 deletions
diff --git a/.gitignore b/.gitignore
index 6eb3c45..41ac558 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,4 @@ debug_*
*/__pycache__/*
*~$*
things/
+configs/*.json \ No newline at end of file
diff --git a/configs/.gitkeep b/configs/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/configs/.gitkeep
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..ad6a922
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,125 @@
+#!/bin/bash
+
+cd "${0%/*}" || exit 1
+
+
+
+# set the default Docker image tag to dactyl-keyboard
+IMAGE_TAG="dactyl-keyboard"
+
+# by default, don't rebuild the image
+REBUILD=false;
+
+# leave config empty to use default values
+CONFIG=""
+
+
+# check for command line flags
+while test $# -gt 0; do
+ case "$1" in
+ -r|--rebuild)
+ REBUILD=true
+ shift
+ ;;
+ -t|--tag)
+ if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
+ IMAGE_TAG=$2
+ shift 2
+ else
+ echo "Error: Argument for $1 is missing" >&2
+ exit 1
+ fi
+ ;;
+ -c|--config)
+ CONFIG=$2
+ shift 2
+ ;;
+ -*|--*)
+ echo "Error: Unknown flag $1" >&2
+ exit 1
+ ;;
+ *)
+ COMMAND=$1
+ shift;
+ ;;
+ esac
+done
+
+
+
+case $COMMAND in
+ help)
+ echo "Dactyl-Manuform Keyboard Generator"
+ echo ""
+ echo "Use this tool to configure and generate files for building a keyboard. All"
+ echo "commands will be run in a Docker contianer, which will be built if it does"
+ echo "not already exist."
+ echo ""
+ echo ""
+ echo "Usage:"
+ echo " run [-r] [-i <docker-image-tag>] [-c <configuration-name>] <command>"
+ echo ""
+ echo "Available Commands:"
+ echo " help Show this help"
+ echo " build Rebuild the docker image"
+ echo " release Run model_builder.py"
+ echo " generate Output the keyboard files to the './things' directory"
+ echo " configure Generate a configuration file with default values. The config"
+ echo " file will be saved to configs/<configuration-name>.json. If the"
+ echo " -c flag is not set, the defailt config_name will be used."
+ echo ""
+ echo "Flags:"
+ echo " -c Set the configuration file to use. This should be the name of the file"
+ echo " only, without a file extension, and it is relative to the './configs'"
+ echo " directory. For example, '-c my-custom-dm' will refer to a file located"
+ echo " at './configs/my-custom-dm.json'"
+ echo " -r Rebuild the docker image"
+ echo " -t The tag that should be applied to the docker image"
+ exit 0
+ ;;
+ build)
+ docker build -t ${IMAGE_TAG} -f docker/Dockerfile .
+ exit 0
+ ;;
+ generate)
+ SCRIPT=dactyl_manuform.py
+ ;;
+ configure)
+ SCRIPT=generate_configuration.py
+ ;;
+ release)
+ SCRIPT=model_builder.py
+ ;;
+ *)
+ echo "Invalid command. Try 'run help'"
+ exit 1
+esac
+
+
+# get the image ID, and save the return code so we'll know if the image exists
+IMAGE_ID=$(docker inspect --type=image --format={{.Id}} ${IMAGE_TAG})
+INSPECT_RETURN_CODE=$?
+
+# if we were specifically told to rebuild, or if the image doesn't exists, then build the docker image
+if $REBUILD || [ $INSPECT_RETURN_CODE -ne 0 ]; then
+ docker build -t ${IMAGE_TAG} -f docker/Dockerfile .
+fi
+
+
+# if a config file was specified, set the command line argument for the python script
+if [[ ! -z $CONFIG ]]; then
+ CONFIG_OPTION="--config=${CONFIG}"
+fi
+
+# run the command in a temporary container
+docker run --name dm-run -d --rm -v "`pwd`/src:/app/src" -v "`pwd`/things:/app/things" -v "`pwd`/configs:/app/configs" ${IMAGE_TAG} python3 $SCRIPT $CONFIG_OPTION > /dev/null 2>&1
+
+# show progress indicator while until dm-run container completes
+while $(docker inspect --format={{.Id}} dm-run > /dev/null 2>&1); do
+ echo -n "."
+ sleep 1.5
+done
+
+echo ""
+echo "Dactyl-Manuform '${COMMAND}' is complete!"
+echo "" \ No newline at end of file
diff --git a/src/dactyl_manuform.py b/src/dactyl_manuform.py
index 0fd4be6..1dc1507 100644
--- a/src/dactyl_manuform.py
+++ b/src/dactyl_manuform.py
@@ -1,6 +1,7 @@
import numpy as np
from numpy import pi
import os.path as path
+import getopt, sys
import json
import os
@@ -23,11 +24,15 @@ import generate_configuration as cfg
for item in cfg.shape_config:
locals()[item] = cfg.shape_config[item]
-## LOAD RUN CONFIGURATION FILE AND WRITE TO ANY VARIABLES IN FILE.
-with open('run_config.json', mode='r') as fid:
- data = json.load(fid)
-for item in data:
- locals()[item] = data[item]
+## CHECK FOR CONFIG FILE AND WRITE TO ANY VARIABLES IN FILE.
+opts, args = getopt.getopt(sys.argv[1:], "", ["config="]);
+for opt, arg in opts:
+ if opt in ('--config'):
+ with open(os.path.join(r"..", "configs", arg + '.json'), mode='r') as fid:
+ data = json.load(fid)
+ for item in data:
+ locals()[item] = data[item]
+
# Really rough setup. Check for ENGINE, set it not present from configuration.
try:
diff --git a/src/generate_configuration.py b/src/generate_configuration.py
index ee70e3b..79ef5b3 100644
--- a/src/generate_configuration.py
+++ b/src/generate_configuration.py
@@ -1,3 +1,6 @@
+import sys
+import getopt
+import os
import json
@@ -383,23 +386,18 @@ shape_config = {
####################################
def save_config():
- print("Saving Configuration")
- with open('run_config.json', mode='w') as fid:
- json.dump(shape_config, fid, indent=4)
-
-def update_config(fname, fname_out=None):
- if fname_out is None:
- fname_out == "updated_config.json"
- # Open existing config, update with any new parameters, and save to updated_config.json
- with open(fname, mode='r') as fid:
- last_shape_config = json.load(fid)
- shape_config.update(last_shape_config)
-
- with open(fname_out, mode='w') as fid:
+ # Check to see if the user has specified an alternate config
+ opts, args = getopt.getopt(sys.argv[1:], "", ["config="]);
+ for opt, arg in opts:
+ if opt in ('--config'):
+ # If a config file was specified, set the config_name and save_dir
+ shape_config['save_dir'] = arg
+ shape_config['config_name'] = arg
+
+ # Write the config to ./configs/<config_name>.json
+ with open(os.path.join(r"..", "configs", shape_config['config_name'] + '.json'), mode='w') as fid:
json.dump(shape_config, fid, indent=4)
if __name__ == '__main__':
- save_config()
- from dactyl_manuform import *
- run() \ No newline at end of file
+ save_config() \ No newline at end of file