Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python run script #38

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
debug_*
*/__pycache__/*
*~$*
things/*.scad
things/*.step
things/*.stl
things/**/*.scad
things/**/*.step
things/**/*.stl
4 changes: 1 addition & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ RUN apt-get update && \
apt-get install -y libgl1-mesa-glx gcc bash && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app
RUN micromamba install -y -n base -c conda-forge -c cadquery \
python=3 \
cadquery=master \
Expand All @@ -14,5 +13,4 @@ RUN micromamba install -y -n base -c conda-forge -c cadquery \

RUN pip3 install solidpython


WORKDIR /app/src
WORKDIR /app
53 changes: 53 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os.path as path
import getopt, sys
import src.argument_parser as parser

args = parser.parse()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to ask, but have you considered to use internal python lib argparse instead of re implementing this?


if len(args['args']) < 1:
print("A command must be specified")
sys.exit(1)

command = args['args'][0]
if command not in ('help', 'generate', 'configure', 'realease'):
print("Invalid command. Try 'help'")
sys.exit(1)

def show_usage():
print("Dactyl-Manuform Keyboard Generator")
print("")
print("Use this tool to configure and generate files for building a keyboard.")
print("")
print("")
print("Usage:")
print(" run.py [-d|--debug] [-u|--update] [--config <configuration-name>] <command>")
print("")
print("Available Commands:")
print(" help Show this help")
print(" release Run model_builder.py")
print(" generate Output the keyboard files to the './things' directory")
print(" configure Generate a configuration file with default values. The config")
print(" file will be saved to configs/<configuration-name>. If the")
print(" --config flag is not set, the default config_name will be used.")
print("")
print("Flags:")
print(" --config Set the configuration file to use, relative to the './configs'")
print(" directory.")
print(" -u|--update Update a config file. This flag must be set if the config file")
print(" already exists.")
print(" -d|--debug Show debug output")
print("")

if command == 'help':
show_usage()
elif command == 'generate':
import src.dactyl_manuform as command
command.run(args)
elif command == 'configure':
import src.generate_configuration as command
command.save_config(args)
elif command == 'release':
import src.model_builder as command
command.run()


87 changes: 12 additions & 75 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
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
Expand All @@ -30,96 +25,38 @@ while test $# -gt 0; do
exit 1
fi
;;
-c|--config)
CONFIG=$2
shift 2
;;
-*|--*)
echo "Error: Unknown flag $1" >&2
exit 1
;;
*)
COMMAND=$1
# build a command string to pass to run.py
COMMAND="$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
# run the command in a container
docker run --name dm-run -d -v "`pwd`:/app" ${IMAGE_TAG} python3 run.py $COMMAND > /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
while [ "$(docker inspect --format={{.State.Status}} dm-run)" != 'exited' ]; do
echo -n "."
sleep 1.5
done

echo ""
echo "Dactyl-Manuform '${COMMAND}' is complete!"
echo ""

# display the output of run.py
docker logs dm-run

# remove the container
docker rm dm-run > /dev/null 2>&1
60 changes: 60 additions & 0 deletions src/argument_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os.path as path
import getopt, sys

def parse():
# set defaults
debug = False
update = False
config_name = 'DM'
relative_path = r".";
file_name = "run_config"
format = 'json'
absolute_path = path.abspath(path.join(__file__, r"..", relative_path, file_name + r"." + format))

opts, args = getopt.getopt(sys.argv[1:], "ud", ["config=", "update", "debug"])
for opt, arg in opts:
if opt in ('--config'):
config_path_parts = arg.split(r'/')
file_parts = config_path_parts.pop().split(r'.')

file_name = file_parts[0]
if len(file_parts) == 2:
format = file_parts[1]

config_name = file_name

if len(config_path_parts) > 0:
relative_path = path.join(*config_path_parts)

absolute_path = path.abspath(path.join(__file__, r"..", r"..", "configs", relative_path, file_name + r"." + format))
elif opt in ('-u', "--update"):
update = True
elif opt in ('-d', "--debug"):
debug = True


if debug:
print("CONFIG OPTIONS")
print("config.name: " + config_name)
print("config.relative_path: " + relative_path)
print("config.file_name: " + file_name)
print("config.format: " + format)
print("config.absolute_path: " + absolute_path)
print("update: " + str(update))
print("opts: " + str(opts))
print("args: " + str(args))
print("")

return {
'config': {
'name': config_name,
'relative_path': relative_path,
'file_name': file_name,
'format': format,
'absolute_path': absolute_path
},
'debug': debug,
'update': update,
'opts': opts,
'args': args
}
Loading