Skip to content

Commit

Permalink
Improve script to update solutions & data
Browse files Browse the repository at this point in the history
  • Loading branch information
jdtournier committed Jan 31, 2025
1 parent 963aa11 commit 4462943
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 31 deletions.
45 changes: 45 additions & 0 deletions projects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,48 @@ This repo holds project descriptions, data and solutions for the projects to be
- [instructions](robot_arm/assignment.md)
- [data](robot_arm/data/)
- [solution](../../../tree/robot_solution/projects/robot_arm/solution) (with [full history](../../../commits/robot_solution/projects/robot_arm/solution))


# Keeping your code up to date

We have written a short script to simplify the process of keeping your
code up to date with the solutions as we progress through the course.

This will download the solutions (and data if you request it) into the *current
folder*, with this file structure:
```
.
├── DNA_shotgun_sequencing
│   ├── data
│   └── solution
├── fMRI
│   ├── data
│   └── solution
└── robot_arm
├── data
└── solution
```

## Updating your code to the latest version:

First, make sure to *change directory* using `cd` into a suitable location,
where you keep your own code. This will differ for different people, so please
use a folder that works for you. For example:
```
cd Documents/OOP
```

Once you are in the right folder, copy & paste the following into your terminal:
```
curl -s https://raw.githubusercontent.com/KCL-BMEIS/OOP/refs/heads/main/projects/update_solution | bash
```

### Updating the data to the latest version:

Make sure you are in the right folder (see above), then copy & paste the following into your terminal:
```
curl -s https://raw.githubusercontent.com/KCL-BMEIS/OOP/refs/heads/main/projects/update_solution | bash -s -- data
```



83 changes: 52 additions & 31 deletions projects/update_solution
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
#!/bin/bash

if [[ $# < 1 ]]; then
echo "usage: $0 project"
echo ""
echo "where project is one of 'shotgun', 'fmri' or 'robot'"
exit 1
if [[ $1 == "help" ]]; then
cat <<EOH
Usage:
- update solutions for all projects to latest version:
$ $0
- update data for all projects to latest version:
$ $0 data
- update solutions to specific version (get version checksum from history):
$ $0 2d6b147
- display this help page:
$ $0 help
EOH
exit 1
fi

user=kcl-bmeis
repo=oop

case $1 in
shotgun)
branch=shotgun_sequencing_solution
remote_folder=projects/DNA_shotgun_sequencing/solution
;;
fmri)
branch=fmri_solution
remote_folder=projects/fMRI/solution
;;
fmri)
branch=robot_solution
remote_folder=projects/robot_arm/solution
;;
*)
echo "no such project: $1"
exit 1;
;;
esac


curl -s https://api.github.com/repos/$user/$repo/git/trees/$branch?recursive=1 | \
while read -r line; do
[[ "$line" =~ \"path\":\ *\"${remote_folder}/(.*)\" ]] || continue
filename=${BASH_REMATCH[1]};
echo updating $filename
curl --silent --create-dirs --output $filename https://raw.githubusercontent.com/$user/$repo/$branch/$remote_folder/$filename
what=solution
[[ $1 == "data" ]] && { what="data"; shift; }

# get_folder_listing branch root_folder subfolder
get_folder_listing() {
# Avoid using GitHub API due to rate limiting restrictions:
curl --silent https://github.com/$user/$repo/tree/$1/$2/$3 | while read -r line; do
[[ "$line" =~ \"tree\":\{\"items\":\[([^\]]*)\] ]] || continue;
items=${BASH_REMATCH[1]};
while [[ "$items" =~ ^[^\}]*\"path\":\"([^\"]*)[^\}]*\"contentType\":\"([^\"]*)[^\}]*\} ]]; do
if [[ ${BASH_REMATCH[2]} == "directory" ]]; then
get_folder_listing $1 $2 ${BASH_REMATCH[1]#"$2"}
else
echo ${BASH_REMATCH[1]}
fi
items="${items#"${BASH_REMATCH[0]}"}";
done;
done
}

# update_project_files branch project_folder [SHA1]
update_project_files() {
branch=${3:-$1}
for filename in $(get_folder_listing $branch projects/$2 $what); do
local_filename=${filename#projects/}
echo updating $local_filename
curl --silent --create-dirs --output $local_filename https://raw.githubusercontent.com/$user/$repo/$branch/$filename
done
}

update_project_files shotgun_sequencing_solution DNA_shotgun_sequencing $1
update_project_files fmri_solution fMRI $1
update_project_files robot_solution robot_arm $1



0 comments on commit 4462943

Please sign in to comment.