forked from child-growth/ki-longitudinal-growth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runFileSaveLogs
73 lines (51 loc) · 2.31 KB
/
runFileSaveLogs
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
#!/usr/bin/env python3
# Type "./runFileSaveLogs -h" for help
import os
import sys
import argparse
import getpass
import datetime
import shutil
import glob
import pathlib
print(os.getcwd())
print(os.path.dirname(__file__))
# Setting working directory to this script's current directory
os.chdir(os.path.dirname(__file__))
# Setting up argument parser
parser = argparse.ArgumentParser(description='Runs the argument R script(s) - in parallel if specified - and moves the subsequent generated .Rout log files to a time-stamped, user-stamped, and optionally, identifier-stamped directory.')
def is_valid_file(parser, arg):
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
else:
return arg
def is_valid_directory(parser, arg):
if not os.path.isdir(arg):
parser.error("The specified path (%s) is not a directory!" % arg)
else:
return arg
parser.add_argument('-p', '--parallel', action='store_true', help="Runs the argument R scripts in parallel if specified")
parser.add_argument("-l", "--logDirPrefix", help="The directory in which log files will be saved to.", default="/data/KI/ki-logs/", type=lambda logDirPrefix: is_valid_directory(parser, logDirPrefix))
parser.add_argument("-i", "--identifier", help="Adds an identifier to the directory name where this is saved")
parser.add_argument('filepaths', nargs='+', type=lambda filename: is_valid_file(parser, filename), help="The filepath(s) of the R scripts to be run")
args = parser.parse_args()
# Run given R Scripts
for filename in args.filepaths:
print(filename)
system_call = "R CMD BATCH" + " " + filename
if args.parallel:
system_call = "nohup" + " " + system_call + " &"
os.system(system_call)
# Create the directory (and any parents) of the log files
#currentUser = getpass.getuser()
#currentTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#logDir = args.logDirPrefix + currentTime + "-" + currentUser
#if args.identifier is not None:
# logDir += "-" + args.identifier
#logDir += "/"
#pathlib.Path(logDir).mkdir(parents=True, exist_ok=True)
# Find and move all logs to this new directory
#currentLogPaths = glob.glob('./*.Rout')
#for currentLogPath in currentLogPaths:
# filename = currentLogPath.split("/")[-1]
# shutil.move(currentLogPath, logDir + filename)