-
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.
[Feature:System] Local CSV File (#27)
* Local CSV File New helper script for retrieving CSV file on an accessible file system. * Update config.php
- Loading branch information
Showing
4 changed files
with
148 additions
and
41 deletions.
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
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,91 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
require __DIR__ . "/config.php"; | ||
|
||
new csv_local(); | ||
exit(0); | ||
|
||
/** | ||
* Validate and copy CSV data file via filesystem. | ||
* | ||
* This data source option has the CSV file provided to any filesystem the | ||
* autofeed's server can access (local or mounted). LOCAL_SOURCE_CSV must | ||
* be defined in config.php, referencing the location of the CSV file upload. | ||
* | ||
* @author Peter Bailie, Rensselaer Polytechnic Institute | ||
*/ | ||
class csv_local { | ||
/** @static @property string */ | ||
private static $source_file = LOCAL_SOURCE_CSV; | ||
|
||
/** @static @property string */ | ||
private static $dest_file = CSV_FILE; | ||
|
||
/** @static @property string */ | ||
private static $err = ""; | ||
|
||
public function __construct() { | ||
// Main process | ||
switch(false) { | ||
case $this->validate_csv(): | ||
case $this->copy_csv(): | ||
// If we wind up here, something went wrong in the main process. | ||
fprintf(STDERR, "%s", self::$err); | ||
exit(1); | ||
} | ||
} | ||
|
||
/** | ||
* Validate CSV file before copy. | ||
* | ||
* Check's for the file's existence and tries to check that the file was | ||
* provided/refreshed on the same day as the autofeed was run. The day | ||
* check is to help prevent the auto feed from blindly running the same CSV | ||
* multiple days in a row and alert the sysadmin that an expected file | ||
* refresh did not happen. $this->err is set with an error message when | ||
* validation fails. | ||
* | ||
* @return boolean true when CSV is validated, false otherwise. | ||
*/ | ||
private function validate_csv() { | ||
clearstatcache(); | ||
|
||
if (!file_exists(self::$source_file)) { | ||
self::$err = sprintf("CSV upload missing: %s\n", self::$source_file); | ||
return false; | ||
} | ||
|
||
$file_modified = filemtime(self::$source_file); | ||
$today = time(); | ||
// There are 86400 seconds in a day. | ||
if (intdiv($today, 86400) !== intdiv($file_modified, 86400)) { | ||
$today = date("m-d-Y", $today); | ||
$file_modified = date("m-d-Y", $file_modified); | ||
$hash = md5(file_get_contents(self::$source_file)); | ||
self::$err = sprintf("CSV upload modified time mismatch.\nToday: %s\nUploaded File: %s\nUploaded File Hash: %s\n", $today, $file_modified, $hash); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Copy CSV file. | ||
* | ||
* $this->err is set with an error message when file copy fails. | ||
* | ||
* @return boolean true when copy is successful, false otherwise. | ||
*/ | ||
private function copy_csv() { | ||
if (file_exists(self::$dest_file)) { | ||
unlink(self::$dest_file); | ||
} | ||
|
||
if (!copy(self::$source_file, self::$dest_file)) { | ||
self::$err = sprintf("Failed to copy file.\nSource: %s\nDest: %s\n", self::$source_file, self::$dest_file); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,41 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Use this bash script to run a data sourcing script before | ||
# submitty_student_auto_feed.php. This is intended to be used with cron. | ||
# | ||
# Author: Peter Bailie, Rensselaer Polytechnic Institute | ||
|
||
display_usage() { | ||
cat << EOM | ||
usage: ssaf.sh (data_source) (term) [DB_auth] | ||
data_source: csv_local|imap_remote|json_remote | ||
Which data sourcing script to run first: csv_local.php, | ||
imap_remote.php, or json_remote.php (required) | ||
term: Term code to pass to submitty_student_auto_feed.php (required) | ||
DB_auth: DB auth string for submitty_student_auto_feed.php [optional] | ||
EOM | ||
exit 1 | ||
} | ||
|
||
if [ $# -ne 2 ] && [ $# -ne 3 ]; then | ||
display_usage | ||
fi | ||
|
||
CWD=$(dirname "$0") | ||
if [ "$1" = "csv_local" ] || [ "$1" = "imap_remote" ] || [ "$1" = "json_remote" ]; then | ||
SOURCE="${CWD}/${1}.php" | ||
else | ||
display_usage | ||
fi | ||
|
||
if $SOURCE; then | ||
if [ "$3" != "" ]; then | ||
DASH_A="-a$3" | ||
fi | ||
|
||
DASH_T="-t$2" | ||
"$CWD"/submitty_student_auto_feed.php "$DASH_T" "$DASH_A" | ||
else | ||
echo "${1}.php exited $?. Auto feed not run." | ||
fi |
This file was deleted.
Oops, something went wrong.