-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,51 @@ | ||
# stubber | ||
# CIS 1912 Stubber | ||
|
||
This is a stubber for homework code. To use it, wrap the code you want to stub with: | ||
|
||
```python | ||
# ~~ SOLN | ||
``` | ||
|
||
and | ||
|
||
```python | ||
# ~~ STUBWITH stubbed_lines | ||
``` | ||
|
||
For example, if you have | ||
|
||
```python | ||
# ~~ SOLN | ||
import os | ||
import json | ||
# ~~ STUBWITH import numpy | ||
``` | ||
|
||
then running the stubber will return | ||
|
||
```python | ||
import numpy | ||
``` | ||
|
||
You can also have an empty STUBWITH to just remove the lines. | ||
|
||
## Installing the Stubber | ||
|
||
To install the stubber, download it and move it to your `bin` (or add the path to the file to your `PATH`). Then, give execute permissions to the file. To summarize, you can just run: | ||
|
||
```bash | ||
mv stub /usr/local/bin/stub | ||
chmod +x /usr/local/bin/stub | ||
``` | ||
|
||
## Running the Stubber | ||
|
||
To run the stubber, use the following command: | ||
|
||
```bash | ||
stub [--copy] [--rm] <directory> [<directory> ...]" | ||
``` | ||
If the `--copy` argument is included, the stubbed files will be written to a copy, where all the new files will have `_stubbed` added to the end. If this argument is omitted, the files will be edited in place. | ||
If the `--rm` argument is included, the `SOLN` and `STUBWITH` lines will be removed, but the code in between won't actually. It doesn't actually stub, but rather just remove the stub comments. If it's not included, the stubber works as expected. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/bin/bash | ||
|
||
# find . -type f -print0 | xargs -0 perl -i -0777pe 's/# ~~ SOLN[\s\S]*?# ~~ STUBWITH[ ]?(.*?)/$1/sg' | ||
|
||
ignore_patterns=( | ||
"node_modules" | ||
"*.log" | ||
".git" | ||
".vite" | ||
".vscode" | ||
".idea" | ||
"__pycache__" | ||
".DS_Store" | ||
) | ||
|
||
WHITE='\033[0m' | ||
LIGHT_GREY='\033[0;90m' | ||
|
||
directories=() | ||
copy_mode=false | ||
rm_mode=false | ||
changed_files_count=0 | ||
|
||
while [[ "$#" -gt 0 ]]; do | ||
case "$1" in | ||
--copy) | ||
copy_mode=true | ||
;; | ||
--rm) | ||
rm_mode=true | ||
;; | ||
*) | ||
directories+=("$1") | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
if [[ ${#directories[@]} -eq 0 ]]; then | ||
echo "Usage: $(basename "$0") [--copy] [--rm] <directory> [<directory> ...]" | ||
exit 1 | ||
fi | ||
|
||
shopt -s extglob | ||
shopt -s nullglob | ||
|
||
should_ignore() { | ||
local file=$1 | ||
for pattern in "${ignore_patterns[@]}"; do | ||
case "$file" in | ||
$pattern|*/$pattern|$pattern/*|*/$pattern/*) return 0 ;; | ||
esac | ||
done | ||
return 1 | ||
} | ||
|
||
for directory in "${directories[@]}"; do | ||
if [[ ! -d "$directory" ]]; then | ||
echo "Error: $directory is not a valid directory." | ||
exit 1 | ||
fi | ||
|
||
for file in $(find "$directory" -type f); do | ||
if should_ignore "$file"; then | ||
continue | ||
fi | ||
|
||
tmpfile=$(mktemp) | ||
|
||
if $rm_mode; then | ||
perl -0777pe 's/^[ \t]*# ~~ SOLN.*?\n|^[ \t]*# ~~ STUBWITH.*?\n//mg' "$file" > "$tmpfile" | ||
else | ||
perl -0777pe 's/# ~~ SOLN[\s\S]*?# ~~ STUBWITH[ ]?(.*?)/$1/sg' "$file" > "$tmpfile" | ||
fi | ||
|
||
if ! diff -q "$file" "$tmpfile" > /dev/null; then | ||
if $copy_mode; then | ||
filename="${file%.*}" | ||
extension="${file##*.}" | ||
|
||
if [[ "$filename" == "$extension" ]]; then | ||
newfile="${filename}_stubbed" | ||
else | ||
newfile="${filename}_stubbed.${extension}" | ||
fi | ||
|
||
mv "$tmpfile" "$newfile" | ||
echo "Modified: ${file} -> ${newfile}" | ||
else | ||
mv "$tmpfile" "$file" | ||
echo "Modified: ${file}" | ||
fi | ||
((changed_files_count++)) | ||
else | ||
rm "$tmpfile" | ||
echo -e "${LIGHT_GREY}No change: $file${WHITE}" | ||
fi | ||
done | ||
done | ||
|
||
echo "Number of changed files: $changed_files_count" |