-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
436 lines (351 loc) · 11.9 KB
/
setup.sh
File metadata and controls
436 lines (351 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/bin/bash
# Student Attendance Tracker setup script
#global variables
myprog_name=""
prog_dir=""
setup_done=false
# Ctrl+C handler
cancel_when_interrupted() {
echo "Interrupted, canceling..."
if [ -d "$prog_dir" ]; then
arch_name="attendance_tracker_${myprog_name}_archive.tar.gz"
echo "Archiving the incomplete project..."
tar -czf "$arch_name" "$prog_dir" 2>/dev/null
if [ $? -eq 0 ]; then
echo "Archive created: $arch_name"
else
echo "Failed to create archive"
fi
echo "removing incomplete directory..."
rm -rf "$prog_dir"
if [ ! -d "$prog_dir" ]; then
echo "Directory $prog_dir removed successfuly"
else
echo "Failed to remove $prog_dir directory"
fi
echo "Setting the directory is cancelled"
fi
exit 1
}
# validations
place_for_validation() {
echo "checking environment.."
echo -n "Verifying for Python3.."
if command -v python3 &> /dev/null; then
python_version3=$(python3 --version 2>&1)
echo "Found"
echo "$python_version3"
else
echo "Python not found, try another option"
echo "Python3 required to run this script"
fi
}
# project name
fetch_prog_name() {
echo "Student Attendance Tracker"
while true; do
echo -n "Enter project name: "
read myprog_name
if [ -z "$myprog_name" ]; then
echo "Input can not be empty"
echo ""
continue
fi
myprog_name=$(echo "$myprog_name" | tr -d ' ' | tr '[:upper:]' '[:lower:]')
prog_dir="attendance_tracker_${myprog_name}"
if [ -d "$prog_dir" ]; then
echo ""
echo "directory '$prog_dir' already existing"
echo -n "$myprog_name will you make changes? (yes/no): "
read changes
if [ "$changes" = "yes" ] || [ "$changes" = "Yes" ]; then
echo "Deleting the existing directory $myprog_name"
rm -rf "$prog_dir"
if [ $? -eq 0 ]; then
echo "The existing directory deleted"
break
else
echo "Failed to delete the existing directory"
exit 1
fi
break
else
echo "Choose a different project name"
echo ""
fi
else
break
fi
done
echo "Project name: $myprog_name"
echo "Target directory: $prog_dir"
}
# create structure
make_dir_structure() {
echo "Making directory structure"
mkdir -p "$prog_dir"
if [ -d "$prog_dir" ]; then
echo "Main directoty Created: $prog_dir/"
else
echo "main directory failed"
exit 1
fi
mkdir -p "$prog_dir/Helpers"
if [ -d "$prog_dir/Helpers" ]; then
echo "Helper directory Created: $prog_dir/Helpers/"
else
echo "Helpers directory failed"
exit 1
fi
mkdir -p "$prog_dir/reports"
if [ -d "$prog_dir/reports" ]; then
echo "Report directory Created: $prog_dir/reports/"
else
echo "reports directory failed"
exit 1
fi
echo "directory structure created"
}
# generate files
construct_application_files() {
echo "Constructing project files..."
# attendance_checker.py
cat > "$prog_dir/attendance_checker.py" << 'pyattendance_checker'
#!/usr/bin/env python3
import csv
import json
import os
from datetime import datetime
def run_attendance_check():
# 1. Load Config
with open('Helpers/config.json', 'r') as f:
config = json.load(f)
# 2. Archive old reports.log if it exists
if os.path.exists('reports/reports.log'):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
os.rename('reports/reports.log', f'reports/reports_{timestamp}.log.archive')
# 3. Process Data
with open('Helpers/assets.csv', mode='r') as f, open('reports/reports.log', 'w') as log:
reader = csv.DictReader(f)
total_sessions = config['total_sessions']
log.write(f"--- Attendance Report Run: {datetime.now()} ---\n")
for row in reader:
name = row['Names']
email = row['Email']
attended = int(row['Attendance Count'])
# Simple Math: (Attended / Total) * 100
attendance_pct = (attended / total_sessions) * 100
message = ""
if attendance_pct < config['thresholds']['failure']:
message = f"URGENT: {name}, your attendance is {attendance_pct:.1f}%. You will fail this class."
elif attendance_pct < config['thresholds']['warning']:
message = f"WARNING: {name}, your attendance is {attendance_pct:.1f}%. Please be careful."
if message:
if config['run_mode'] == "live":
log.write(f"[{datetime.now()}] ALERT SENT TO {email}: {message}\n")
print(f"Logged alert for {name}")
else:
print(f"[DRY RUN] Email to {email}: {message}")
if __name__ == "__main__":
run_attendance_check()
pyattendance_checker
# execution
chmod +x "$prog_dir/attendance_checker.py"
if [ -f "$prog_dir/attendance_checker.py" ]; then
echo "Execution created: attendance_checker.py"
else
echo "Execution failed: attendance_checker.py"
exit 1
fi
# assets.csv
cat > "$prog_dir/Helpers/assets.csv" << 'csv_file'
Email,Names,Attendance Count,Absence Count
alice@example.com,Alice Johnson,14,1
bob@example.com,Bob Smith,7,8
charlie@example.com,Charlie Davis,4,11
diana@example.com,Diana Prince,15,0
moses@example.com,Mugisha Moses,2,1
csv_file
if [ -f "$prog_dir/Helpers/assets.csv" ]; then
echo "Assets file created: Helpers/assets.csv"
else
echo "Assets file Failed"
exit 1
fi
# config.json
cat > "$prog_dir/Helpers/config.json" << 'json_file'
{
"thresholds": {
"warning": 75,
"failure": 50
},
"run_mode": "live",
"total_sessions": 15
}
json_file
if [ -f "$prog_dir/Helpers/config.json" ]; then
echo "config file Created: Helpers/config.json (defaults: 75%, 50%)"
else
echo "Config file Failed"
exit 1
fi
# reports.log
cat > "$prog_dir/reports/reports.log" << log_file
Student Attendance Tracker, Log report
Initiated by: $(date)
log_file
if [ -f "$prog_dir/reports/reports.log" ]; then
echo "Report file Created"
else
echo "Report file Failed"
exit 1
fi
echo ""
echo "files generated successfully"
echo ""
}
# Update thresholds
adjust_thresholds() {
echo "Settings for configurations"
echo -n "Do you want to update these thresholds? (yes/no): "
read adjust_thresholds
if [ "$adjust_thresholds" = "yes" ] || [ "$adjust_thresholds" = "y" ]; then
echo "Adjusting thresholds..."
while true; do
echo -n " Please enter your new warning threshold (75-100): "
read warning_thresholds
if ! [[ "$warning_thresholds" =~ ^[0-9]+$ ]]; then
echo "Please enter a number"
continue
fi
if [ "$warning_thresholds" -lt 75 ] || [ "$warning_thresholds" -gt 100 ]; then
echo "The number must be between 75 and 100"
continue
fi
# Passed validations
echo "You have Completed Warning threshold: $warning_thresholds%"
break
done
# Fetch failure threshold and validation
while true; do
echo -n "Please enter new failure threshold (50-100): "
read failure_thresholds
# Checking input and and validations
if ! [[ "$failure_thresholds" =~ ^[0-9]+$ ]]; then
echo "Input must be a numeric characher"
continue
fi
if [ "$failure_thresholds" -lt 50 ] || [ "$failure_thresholds" -gt 100 ]; then
echo "The number must be in between 50 and 100"
continue
fi
# Failure% and warning%
if [ "$failure_thresholds" -ge "$warning_thresholds" ]; then
echo "Failure (%) threshold must be less than warning (%) threshold"
echo "Currently warning percentage: $warning_thresholds%"
continue
fi
echo "The Failure threshold is: $failure_thresholds%"
break
done
echo "Applying configuration"
# sed and config
conf_file="$prog_dir/Helpers/config.json"
# Update warning_threshold using sed
# Layout: "warning_threshold": <any_number>
# Convert to: "warning_threshold": <new_value>
sed -i "s/\"warning\":[[:space:]]*[0-9]*/\"warning\": $warning_thresholds/" "$conf_file"
sed -i "s/\"failure\":[[:space:]]*[0-9]*/\"failure\": $failure_thresholds/" "$conf_file"
if [ $? -eq 0 ]; then
echo "The warning_threshold is being updated to $warning_thresholds"
else
echo "Updating failed in warning_threshold"
fi
# sed update failure_threshold
sed -i "s/\"failure_threshold\": [0-9]*/\"failure_threshold\": $failure_thresholds/" "$conf_file"
if [ $? -eq 0 ]; then
echo "The failure_threshold has been updated to $failure_thresholds"
else
echo "The updating proccess failed in failure_threshold"
fi
echo "Updated the configuration successfully"
echo "Warning Threshold: ${warning_thresholds}%"
echo "Failure Threshold: ${failure_thresholds}%"
else
echo "Using default thresholds (Warning: 75%, Failure: 50%)"
fi
}
# verify structure
check_structure() {
echo "Verifying structure of project"
verif_passed=true
# Checking directories
if [ -d "$prog_dir" ]; then
echo "Our project's main directory exists: $prog_dir/"
else
echo "Our project's main directory does not exist: $prog_dir/"
verif_passed=false
fi
if [ -d "$prog_dir/Helpers" ]; then
echo "The helpers Subdirectory already exists: Helpers/"
else
echo "The helpers Subdirectory does not exist: Helpers/"
verif_passed=false
fi
# Checking for the reports subdirectory
if [ -d "$prog_dir/reports" ]; then
echo "The reports Subdirectory arleady exists: reports/"
else
echo "Subdirectory not existing: reports/"
verif_passed=false
fi
# Checking for all required files
check_files=(
"attendance_checker.py"
"Helpers/assets.csv"
"Helpers/config.json"
"reports/reports.log"
)
for file in "${check_files[@]}"; do
if [ -f "$prog_dir/$file" ]; then
echo "Found: $file"
else
echo "Files missing: $file"
verif_passed=false
fi
done
echo ""
# Final verifications
if [ "$verif_passed" = true ]; then
echo "structure's verification passed"
return 0
else
echo "Structure's verification failed to pass"
echo "Check the error and try again later."
return 1
fi
}
# view summary
view_summary() {
echo ""
echo "Setup completed"
echo "Project specifications:"
echo "Directory: $prog_dir"
echo "number of Files: 4 files created (Python file, CSV file, JSON file, LOG log)"
}
# Main execution
trap cancel_when_interrupted SIGINT
fetch_prog_name
make_dir_structure
construct_application_files
adjust_thresholds
place_for_validation
echo "Student Attendance Tracker setup"
if check_structure; then
setup_done=true
view_summary
exit 0
else
echo "setup failed during verification"
exit 1
fi