Skip to content

Commit

Permalink
Added --help option to python support scripts and enabled --target/--…
Browse files Browse the repository at this point in the history
…host selection for configure.sh to allow exercises to be copied to the home directory of a VM
  • Loading branch information
martinbond7 committed Apr 5, 2024
1 parent 876f931 commit 71bd37a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
55 changes: 46 additions & 9 deletions scripts/configure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
#!/usr/bin/python3
"""
Usage: configure.py [--help] [--target|--host] [code]
Configure workspace for a given course by downloading the relevant
GitHub repo and initialising the workspace with a clean git repo
if no `.git` folder exists.
If command line `code` is not supplied the script prompts to choose
from a list of available course codes based on the workspace
configuration (host or embedded target).
The `--host` or `--target` saves to the current working folder
and is intended for administration of a VM image.
"""
import os
import re
import shutil
Expand Down Expand Up @@ -43,7 +56,7 @@ class Courses:
]


def read_course_code(selected: str = ''):
def read_course_code(selected: str = '') -> str:
if selected:
print(f'Using command line argument "{selected}"')
course = selected
Expand All @@ -57,7 +70,7 @@ def read_course_code(selected: str = ''):
f'A typical course code looks like C-501, AC++11-502 or TDD++-301')


def choose_course(options: list):
def choose_course(options: list) -> str:
print('Suggested courses:')
for n, option in enumerate(options, 1):
print(f'{n:2d}) {option}')
Expand Down Expand Up @@ -111,29 +124,30 @@ def save_archive(course: str, repo: Path, url: str):
f'Please check your spelling or ask your instructor for help')


def download_course(course: str):
def download_course(course: str) -> Path:
repo = course + Config.repo_suffix
url = Config.url_code_base + repo + Config.url_code_path
path = Path(repo)
check_exercise_exists(path)
save_archive(course, path, url)
return path


def course_repo(code: str):
def course_repo(code: str) -> str:
return code.replace('++', 'pp').lower()


def do_fetch_exercises(target: bool, selected: str):
def do_fetch_exercises(target: bool, selected: str) -> Path:
course = read_course_code(selected)
if not course:
course = choose_course(Courses.target if target else Courses.host)
if not course:
raise ConfigureError('No course chosen')
repo = course_repo(course)
download_course(repo)
return download_course(repo)


def cd_workspace():
def cd_workspace() -> bool:
cwd = Path().absolute()
for wd in cwd, cwd.parent:
if (wd / 'src').exists():
Expand All @@ -142,11 +156,34 @@ def cd_workspace():
raise ConfigureError('Please run this script from within the workspace root folder')


def parse_args():
code = ''
target = False
dir_check = True
for arg in sys.argv[1:]:
if arg == '--target':
target, dir_check = True, False
elif arg == '--host':
dir_check = False
elif arg.startswith('--') or code:
if code:
print(f'Course code "{code}" already specified', file=sys.stderr)
elif arg != '--help':
print(f'Unknown argument: "{arg}"', file=sys.stderr)
print(__doc__, file=sys.stderr)
exit(1)
else:
code = arg
return code, target, dir_check


def main():
status = 1
try:
target = cd_workspace()
do_fetch_exercises(target, sys.argv[1] if len(sys.argv) > 1 else '')
code, target, dir_check = parse_args()
if dir_check:
target = cd_workspace()
repo = do_fetch_exercises(target, code)
print('\nCourse exercises configured OK')
status = 0
except ConfigureError as ex:
Expand Down
30 changes: 29 additions & 1 deletion scripts/copy_solution.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/usr/bin/python3
"""
Usage: copy_solution.py [--help] [number]
Copy a specified solution replacing the current `src` and
`include` files. If `number` is not specified you can pick
from a list of available exercise solutions.
Current files are saved and committed to the git repo, or
copied to the `src.bak` folder if git is not being used.
"""
import os
import re
import shutil
Expand Down Expand Up @@ -155,11 +164,30 @@ def cd_workspace():
raise CopyError('Please run this script from within the workspace root folder')


def parse_args():
number = ''
for arg in sys.argv[1:]:
if arg == '--help':
print(__doc__, file=sys.stderr)
exit(1)
elif arg.startswith('--') or number:
if number:
print(f'Solution number "{number}" already specified', file=sys.stderr)
else:
print(f'Unknown argument: "{arg}"', file=sys.stderr)
print(__doc__, file=sys.stderr)
exit(1)
else:
number = arg
return number


def main():
status = 1
try:
number = parse_args()
cd_workspace()
do_copy_solution(sys.argv[1] if len(sys.argv) > 1 else '')
do_copy_solution(number)
status = 0
except CopyError as ex:
print(ex, file=sys.stderr)
Expand Down

0 comments on commit 71bd37a

Please sign in to comment.