-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelcmd.py
More file actions
executable file
·942 lines (824 loc) · 29.7 KB
/
parallelcmd.py
File metadata and controls
executable file
·942 lines (824 loc) · 29.7 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
#!/usr/bin/env python3
import os
import time
import socket
import queue
import threading
from threading import Thread
import logging
import sys
import argparse
import subprocess
import signal
import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import itertools
from string import Formatter
import sqlite3
import socket
import random
import datetime
mq = queue.Queue()
slot = dict()
active_ps = dict()
active = mp.Value("i", 0)
dbname = "pardb" if os.getenv("PARDB") is None else os.getenv("PARDB")
dbfile = dbname + ".sqlite"
db_retries = 10
db_retry_delay = 0.2
def log(*args, sep=" "):
logging.debug(sep.join(map(str, args)))
def is_sqlite_lock_error(exc: Exception) -> bool:
return isinstance(exc, sqlite3.OperationalError) and any(
token in str(exc).lower() for token in ("locked", "busy")
)
def execute_sql_with_retry(con, sql, params=None, retries=None, retry_delay=None):
max_retries = db_retries if retries is None else retries
delay = db_retry_delay if retry_delay is None else retry_delay
last_exc = None
for attempt in range(max_retries):
try:
cur = con.cursor()
if params is None:
cur.execute(sql)
else:
cur.execute(sql, params)
return cur
except Exception as e:
last_exc = e
if is_sqlite_lock_error(e) and attempt < max_retries - 1:
log(
f"SQLite locked/busy. Retry {attempt + 1}/{max_retries} in {delay:.2f}s:",
e,
)
try:
con.rollback()
except Exception:
pass
time.sleep(delay)
continue
raise
raise last_exc
def hello(counter: mp.Value):
workerid = threading.get_native_id()
with counter.get_lock():
slot[workerid] = counter.value
counter.value += 1
affinity = None
logging.debug(f"Worker: pid={os.getpid()} ID={counter.value}, TID={workerid}")
return 0
def timedelta_parse(text):
"""
Convert input string to timedelta.
format: [[[d-]h:]m:]s
"""
tokens = text.replace("-", ":").split(":")
vals = {
key: float(val)
for val, key in zip(tokens[::-1], ("seconds", "minutes", "hours", "days"))
}
return datetime.timedelta(**vals)
def execute(
verbose=False,
dryrun=False,
randomorder=False,
prefix=None,
max_jobs=None,
check_timeleft=None,
wait_interval=None,
id_list=None,
timeout=None,
):
## check in
hostname = socket.gethostname()
workerid = threading.get_native_id()
nomorejob = False
finished = 0
while True:
time.sleep(random.randint(0, 10))
if check_timeleft is not None and check_timeleft > 0:
jobid = os.getenv("SLURM_JOB_ID", None)
assert jobid is not None, "SLURM_JOB_ID not found in environment variables."
cmd = f"squeue -h -j {jobid} -o %L"
proc = subprocess.run(cmd.split(), stdout=subprocess.PIPE)
timestr = proc.stdout.decode("utf-8").strip()
try:
left = timedelta_parse(timestr).total_seconds()
except Exception as e:
## "INVALID" when remaining time is less than a few seconds
log(f"{slot[workerid]}: Exception parsing time string '{timestr}':", e)
left = 0.0
log(
f"{slot[workerid]}: SLURM job {jobid} remaining time: {left:.2f} seconds"
)
if left < check_timeleft:
log(
f"{slot[workerid]}: Remaining time {left:.2f} seconds is less than threshold {check_timeleft}. Stop fetching new jobs."
)
break
with sqlite3.connect(dbfile) as con:
while True:
try:
con.execute("BEGIN EXCLUSIVE;")
cur = con.cursor()
id_clause = ""
if id_list:
ids = ",".join(map(str, id_list))
id_clause = f" AND Seq IN ({ids})"
if randomorder:
sql = f"SELECT Seq, Command FROM parjob WHERE Exitval is NULL{id_clause} ORDER BY RANDOM() LIMIT 1;"
else:
sql = f"SELECT Seq, Command FROM parjob WHERE Exitval is NULL{id_clause} LIMIT 1;"
cur.execute(sql)
row = cur.fetchone()
if not row:
log(f"{slot[workerid]}: No more job")
nomorejob = True
break
(
taskid,
cmd,
) = row
cur.execute(
f"UPDATE parjob SET Starttime = unixepoch('now'), Exitval = -1000 WHERE Seq = {taskid};"
)
log(f"{slot[workerid]}: taskid, cmd:", taskid, cmd)
assert cur.rowcount == 1
con.commit()
break
except Exception as e:
log(f"{slot[workerid]}: Exception:", e)
pass
if nomorejob:
if wait_interval is not None and wait_interval > 0:
print(
f"{slot[workerid]}: No job available. Waiting {wait_interval}s before retry..."
)
time.sleep(wait_interval)
nomorejob = False
continue
break
bashcmd = "bash -c '%s'" % cmd
if prefix is not None:
bashcmd = "%s bash -c '%s'" % (prefix, cmd)
if verbose:
print("%d: cmd:" % taskid, bashcmd)
if not dryrun:
starttime = time.time()
with active.get_lock():
active.value += 1
p = subprocess.Popen(
bashcmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
shell=True,
start_new_session=True,
env=os.environ.copy(),
)
with active.get_lock():
active_ps[workerid] = p
with sqlite3.connect(dbfile) as con:
execute_sql_with_retry(
con,
f"UPDATE parjob SET Hostname = '{hostname}', PID = {p.pid} WHERE Seq = {taskid};",
)
con.commit()
timed_out = False
def _kill_on_timeout():
nonlocal timed_out
timed_out = True
try:
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
except Exception:
pass
timer = None
if timeout is not None and timeout > 0:
timer = threading.Timer(timeout, _kill_on_timeout)
timer.start()
try:
for line in iter(p.stdout.readline, ""):
mq.put((workerid, taskid, line))
except Exception as e:
log(f"{slot[workerid]}: Exception:", e)
p.wait()
if timer is not None:
timer.cancel()
## check out
mq.put((workerid, taskid, None))
with active.get_lock():
active.value -= 1
del active_ps[workerid]
runtime = time.time() - starttime
exitval = -124 if timed_out else p.returncode
if timed_out:
print(f"{taskid}: Timeout after {timeout}s. Killed.", flush=True)
elif verbose:
print("%d: Done:" % taskid, p.returncode)
with sqlite3.connect(dbfile) as con:
execute_sql_with_retry(
con,
f"UPDATE parjob SET Exitval = {exitval}, JobRuntime = {runtime} WHERE Seq = {taskid};",
)
con.commit()
finished += 1
if max_jobs is not None and finished >= max_jobs:
break
return 0
def jobcount():
def dojob():
with sqlite3.connect(dbfile) as con:
cur = con.cursor()
cur.execute(
"SELECT count(1), sum(case when Exitval >= 0 then 1 else 0 end) FROM parjob;"
)
row = cur.fetchone()
(
total,
done,
) = row
return (total, done)
while True:
try:
return dojob()
except Exception as e:
log("Exception:", e)
log("Sleep and try again ...")
time.sleep(1)
def cmdlist(argv):
"""
return list of list
"""
cmds = list()
_args = list()
_type = 0 ## 0: regular, 1: file
has_separator = any(x in (":::", "::::") for x in argv)
for x in argv:
if x == ":::":
cmds.append(_args)
_args = list()
_type = 0
elif x == "::::":
cmds.append(_args)
_args = list()
_type = 1
else:
if _type == 1:
f = sys.stdin if x == "-" else open(x, "r")
try:
for line in f:
if line.startswith("#") or line.strip() == "":
continue
_args.append(line.rstrip())
finally:
if f is not sys.stdin:
f.close()
else:
_args.append(x)
cmds.append(_args)
return cmds
def progress(done, total, dashboard=False, progress=False, timeskip=0.0):
def putline():
os.system("tput ll")
print("\r", end="", flush=True)
print(
"Processing/Done/Total/Completed(%%)/Time(sec): %d/%d/%d/%.01f%%/%.02fs"
% (
active.value,
done,
total,
float(done) / total * 100,
time.time() - t0,
),
end="",
flush=True,
)
if not dashboard:
print("")
os.system("tput el")
extra = 1 if progress else 0
t0 = time.time()
t1 = time.time()
t2 = time.time()
while True:
workerid, taskid, line = mq.get()
if (workerid is None) or (done == total):
total, done = jobcount()
putline()
break
if line is not None:
if time.time() - t2 > timeskip:
t2 = time.time()
else:
continue
if dashboard:
os.system("tput ll")
print("\r", end="", flush=True)
os.system("tput sc")
for i in range(slot[workerid] + extra):
os.system("tput cuu1")
print("%d:" % taskid, line.rstrip(), end="", flush=True)
os.system("tput el")
os.system("tput rc")
else:
print("%d:" % taskid, line, end="", flush=True)
if progress:
## try not too frequent
if time.time() - t1 > 2:
total, done = jobcount()
putline()
t1 = time.time()
else:
pass
def print_table(cur, rows, row_format):
colnames = [desc[0] for desc in cur.description]
bars = ["-" * len(desc[0]) for desc in cur.description]
print(row_format.format(*colnames))
print(row_format.format(*bars))
for row in rows:
print(
row_format.format(
*map(
lambda x: str(x) if not isinstance(x, float) else "%.2f" % x,
row,
)
)
)
def selectdb(cur, filter=None):
if filter is None:
filter = "1=1"
cur.execute(
"SELECT Seq, "
"datetime(Starttime, 'unixepoch', 'localtime') as Starttime, "
"Hostname, PID, JobRuntime, Exitval, Command "
"FROM parjob "
f"WHERE {filter};"
)
rows = cur.fetchall()
row_format = " {:>4} {:<19} {:<22} {:>8} {:>11} {:>7} {:<80}"
print_table(cur, rows, row_format)
return rows, row_format
def checkdb(args):
with sqlite3.connect(dbfile) as con:
filter = "1=1"
if args.where is not None:
filter = f"{args.where}"
if args.like is not None:
filter = f"Command LIKE '{args.like}'"
if args.id:
if not isinstance(args.id, list):
args.id = [args.id]
filter = "Seq IN (%s)" % ",".join(map(str, args.id))
if args.nonzero:
nonzero_clause = "Exitval > 0"
filter = (
f"({filter}) AND {nonzero_clause}"
if filter != "1=1"
else nonzero_clause
)
# con.row_factory = sqlite3.Row
cur = con.cursor()
# cur.execute("SELECT count(1) as Total, sum(case when Exitval >= 0 then 1 else 0 end) as Finished FROM parjob")
if args.list:
selectdb(cur, filter)
else:
cur.execute(
"SELECT count(1) as Total, "
"sum(case when Exitval IS NULL then 1 else 0 end) as Pending, "
"sum(case when Exitval == -1000 then 1 else 0 end) as Running, "
"sum(case when Exitval == 0 then 1 else 0 end) as Success, "
"sum(case when Exitval > 0 then 1 else 0 end) as Failed, "
"sum(case when Exitval < 0 and Exitval != -1000 then 1 else 0 end) as Error "
"FROM parjob;"
)
row = cur.fetchone()
row_format = " {:>5} | {:>7} {:>7} | {:>7} {:>6} {:>5}"
print_table(cur, [row], row_format)
def resetdb(args):
with sqlite3.connect(dbfile) as con:
filter = "Exitval <> 0"
if args.where is not None:
filter = f"{args.where}"
if args.like is not None:
filter = f"Command LIKE '{args.like}'"
if args.all:
filter = "1=1"
if args.nonzero:
nonzero_clause = "Exitval > 0"
filter = (
f"({filter}) AND {nonzero_clause}"
if filter != "1=1"
else nonzero_clause
)
if args.id:
if not isinstance(args.id, list):
args.id = [args.id]
filter = "Seq IN (%s)" % ",".join(map(str, args.id))
cur = con.cursor()
rows, row_format = selectdb(cur, filter)
# (count,) = cur.fetchone()
count = len(rows)
ans = input("%d number of rows will be reset. Continue? (Y/N): " % count)
if ans == "Y" or ans == "y":
cur = execute_sql_with_retry(
con,
f"UPDATE parjob SET Starttime = NULL, Hostname = NULL, PID = NULL, JobRuntime = NULL, Exitval = NULL WHERE {filter};",
)
print("Reset:", cur.rowcount)
con.commit()
else:
print("Aborted.")
def deletedb(args):
with sqlite3.connect(dbfile) as con:
filter = "Exitval <> 0"
if args.like is not None:
filter = f"Command LIKE '{args.like}'"
if args.all:
filter = "1=1"
if args.id:
if not isinstance(args.id, list):
args.id = [args.id]
filter = "Seq IN (%s)" % ",".join(map(str, args.id))
cur = con.cursor()
rows, row_format = selectdb(cur, filter)
count = len(rows)
ans = input("%d number of rows will be deleted. Continue? (Y/N): " % count)
if ans == "Y" or ans == "y":
cur = execute_sql_with_retry(con, f"DELETE FROM parjob WHERE {filter};")
print("Delete:", cur.rowcount)
con.commit()
else:
print("Aborted.")
def updatedb(args):
with sqlite3.connect(dbfile) as con:
filter = "1 = 1"
if args.like is not None:
filter = f"Command LIKE '{args.like}'"
if args.id:
if not isinstance(args.id, list):
args.id = [args.id]
filter = "Seq IN (%s)" % ",".join(map(str, args.id))
cur = con.cursor()
rows, row_format = selectdb(cur, filter)
replace_a, replace_b = args.replace.split(",")
for row in rows:
cmd = row[-1]
new_cmd = cmd.replace(replace_a, replace_b)
print(
row_format.format(
*map(
lambda x: str(x) if not isinstance(x, float) else "%.2f" % x,
row,
)
),
"->",
new_cmd,
)
count = len(rows)
ans = input("%d number of rows will be updated. Continue? (Y/N): " % count)
if ans == "Y" or ans == "y":
affected_rowcount = 0
for row in rows:
seq = row[0]
cmd = row[-1]
new_cmd = cmd.replace(replace_a, replace_b)
cur = execute_sql_with_retry(
con, "UPDATE parjob SET Command = ? WHERE Seq = ?;", (new_cmd, seq)
)
affected_rowcount += cur.rowcount
print("Updated:", affected_rowcount)
con.commit()
else:
print("Aborted.")
def initdb(args):
if args.append and args.force:
raise SystemExit("--append and --force cannot be used together.")
cmds = args.cmds
args_list = cmds[1:]
cmd = " ".join(args.cmd)
## check if cmd has valid formatter
valid = any(a is not None or b is not None for _, a, b, _ in Formatter().parse(cmd))
if not valid:
cmd += " {}" * len(args_list)
task_list = list()
for i, argpair in enumerate(itertools.product(*args_list)):
fullcmd = cmd.format(*argpair)
task_list.append((i, fullcmd))
with sqlite3.connect(dbfile) as con:
cur = con.cursor()
# Enable WAL mode
cur.execute("PRAGMA journal_mode=WAL;")
cur.execute(
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'parjob';"
)
table_exists = cur.fetchone() is not None
if table_exists and args.force:
cur.execute("DROP TABLE parjob;")
table_exists = False
if table_exists and not args.append:
raise SystemExit(
"parjob table already exists in %s. Use --append to add tasks to the existing queue."
% (dbfile,)
)
if not table_exists:
sql = (
"CREATE TABLE IF NOT EXISTS parjob ("
" Seq INTEGER PRIMARY KEY AUTOINCREMENT,"
" Starttime FLOAT(44),"
" Hostname TEXT,"
" PID INT,"
" JobRuntime FLOAT(44),"
" Exitval BIGINT,"
" Command TEXT);"
)
cur.execute(sql)
print("%s created" % (dbfile))
inserted_rows = 0
for i, cmd in task_list:
sql = "SELECT 1 FROM parjob WHERE Command = '%s';" % (cmd,)
cur.execute(sql)
exists = cur.fetchone()
if args.check_dup and exists:
print("Already exists. Skip:", cmd)
else:
sql = "INSERT INTO parjob (Command) VALUES ('%s');" % (cmd,)
cur = execute_sql_with_retry(con, sql)
inserted_rows += cur.rowcount
con.commit()
print("%d tasks added." % (inserted_rows))
# res = cur.execute("select count(*) from parjob;")
# (ntotal,) = res.fetchone()
# print("%d Total added." % (ntotal))
def exec(args):
total, done = jobcount()
if args.dashboard:
## print empty lines to leave space for each worker
for i in range(args.nworkers + 1):
print("")
p = threading.Thread(
target=progress,
args=(
done,
total,
args.dashboard,
args.progress,
args.timeskip,
),
)
p.start()
env = os.environ.copy()
counter = mp.Value("i", 0)
# pool = ProcessPoolExecutor(max_workers=args.nworkers, initializer=hello, initargs=(counter,))
pool = ThreadPoolExecutor(
max_workers=args.nworkers, initializer=hello, initargs=(counter,)
)
with pool as executor:
future_list = list()
for index in range(args.nworkers):
future = executor.submit(
execute,
verbose=args.verbose,
dryrun=args.dryrun,
randomorder=args.randomorder,
prefix=args.prefix,
max_jobs=args.max_jobs,
check_timeleft=args.check_timeleft,
wait_interval=args.wait,
id_list=args.id if args.id is not None else None,
timeout=args.timeout,
)
future_list.append(future)
for future in future_list:
future.result()
mq.put((None, None, None))
p.join()
def run(args):
initdb(args)
exec(args)
def add_default_run_subcommand(argv, subcommand_names):
if any(x in ("-h", "--help") for x in argv):
return argv
if any(x in subcommand_names for x in argv):
return argv
idx = 0
while idx < len(argv):
token = argv[idx]
if token in ("--db", "--log_level"):
idx += 2
continue
if token.startswith("--db=") or token.startswith("--log_level="):
idx += 1
continue
break
out = list(argv)
out.insert(idx, "run")
return out
if __name__ == "__main__":
def usage():
# print(
# "USAGE: %s <OPTIONS> [ ::: <ARGUMENTS> ]* [ :::: ARGFILE ]*" % (sys.argv[0])
# )
parser_main.print_help()
# parser_args.print_help()
sys.exit()
parser_main = argparse.ArgumentParser(prog="OPTIONS")
parser_main.add_argument("--db", metavar="NAME", help="DB name")
parser_main.add_argument(
"--db_retries",
type=int,
default=10,
metavar="N",
help="Max retries when SQLite database is locked",
)
parser_main.add_argument(
"--log_level",
choices=["debug", "info"],
default="info",
help="Set log level (debug or info)",
)
subparsers = parser_main.add_subparsers(
title="subcommands", description="valid subcommands", dest="command"
)
## subcommand: check
parser = subparsers.add_parser("check")
parser.add_argument("-l", "--list", action="store_true", help="list")
parser.add_argument(
"--nonzero", action="store_true", help="show only nonzero return tasks"
)
parser.add_argument("--where", metavar="EXPR", help="where statement")
parser.add_argument("--like", metavar="PATTERN", help="like statement")
parser.add_argument("--id", type=int, metavar="ID", help="select by id", nargs="+")
parser.set_defaults(func=checkdb)
## subcommand: reset
parser = subparsers.add_parser("reset")
parser.add_argument("--where", metavar="EXPR", help="where statement")
parser.add_argument("--like", metavar="PATTERN", help="like statement")
parser.add_argument("-a", "--all", action="store_true", help="reset all")
parser.add_argument(
"--nonzero", action="store_true", help="reset only nonzero return tasks"
)
parser.add_argument("--id", type=int, metavar="ID", help="reset by id", nargs="+")
parser.set_defaults(func=resetdb)
## subcommand: delete
parser = subparsers.add_parser("delete")
parser.add_argument("--like", metavar="PATTERN", help="like statement")
parser.add_argument("-a", "--all", action="store_true", help="delete all")
parser.add_argument("--id", type=int, metavar="ID", help="remove by id", nargs="+")
parser.set_defaults(func=deletedb)
## subcommand: init
parser = subparsers.add_parser("init")
parser.set_defaults(func=initdb)
parser.add_argument("cmd", help="command to execute", nargs=argparse.REMAINDER)
parser.add_argument("-v", "--verbose", action="store_true", help="verbose")
parser.add_argument("-a", "--append", action="store_true", help="append")
parser.add_argument(
"-f", "--force", action="store_true", help="overwrite existing table"
)
parser.add_argument(
"--check_dup", action="store_true", help="allow duplicate commands"
)
## subcommand: exec
parser = subparsers.add_parser("exec")
parser.set_defaults(func=exec)
parser.add_argument(
"--id", type=int, metavar="ID", help="run only these job IDs", nargs="+"
)
parser.add_argument(
"-j", "--nworkers", type=int, metavar="N", help="Number of workers", default=4
)
parser.add_argument("--progress", action="store_true", help="print progress")
parser.add_argument("--dashboard", action="store_true", help="print only last line")
parser.add_argument("--dryrun", action="store_true", help="dryrun")
parser.add_argument("-v", "--verbose", action="store_true", help="verbose")
parser.add_argument(
"--timeskip", type=float, metavar="SECONDS", help="timeskip", default=0.0
)
parser.add_argument("--randomorder", action="store_true", help="randomorder")
parser.add_argument("--prefix", metavar="CMD", help="command prefix")
parser.add_argument(
"--max_jobs",
type=int,
metavar="N",
help="maximum number of jobs per process to run",
)
parser.add_argument(
"--check_timeleft",
type=float,
metavar="SECONDS",
help="check timeleft (seconds)",
)
parser.add_argument(
"--wait",
type=float,
default=None,
metavar="SECONDS",
help="wait SECONDS and retry when no job is available (default: exit immediately)",
)
parser.add_argument(
"--timeout",
type=float,
default=None,
metavar="SECONDS",
help="kill task and move to next if it runs longer than SECONDS (exit code -124)",
)
## subcommand: run (init + exec)
parser = subparsers.add_parser("run")
parser.set_defaults(func=run)
parser.add_argument("cmd", help="command to execute", nargs=argparse.REMAINDER)
parser.add_argument("-v", "--verbose", action="store_true", help="verbose")
parser.add_argument(
"--id", type=int, metavar="ID", help="run only these job IDs", nargs="+"
)
parser.add_argument("-a", "--append", action="store_true", help="append")
parser.add_argument(
"-f", "--force", action="store_true", help="overwrite existing table"
)
parser.add_argument(
"--check_dup", action="store_true", help="allow duplicate commands"
)
parser.add_argument(
"-j", "--nworkers", type=int, metavar="N", help="Number of workers", default=4
)
parser.add_argument("--progress", action="store_true", help="print progress")
parser.add_argument("--dashboard", action="store_true", help="print only last line")
parser.add_argument("--dryrun", action="store_true", help="dryrun")
parser.add_argument(
"--timeskip", type=float, metavar="SECONDS", help="timeskip", default=0.0
)
parser.add_argument("--randomorder", action="store_true", help="randomorder")
parser.add_argument("--prefix", metavar="CMD", help="command prefix")
parser.add_argument(
"--max_jobs",
type=int,
metavar="N",
help="maximum number of jobs per process to run",
)
parser.add_argument(
"--check_timeleft",
type=float,
metavar="SECONDS",
help="check timeleft (seconds)",
)
parser.add_argument(
"--wait",
type=float,
default=None,
metavar="SECONDS",
help="wait SECONDS and retry when no job is available (default: exit immediately)",
)
parser.add_argument(
"--timeout",
type=float,
default=None,
metavar="SECONDS",
help="kill task and move to next if it runs longer than SECONDS (exit code -124)",
)
## subcommand: update
parser = subparsers.add_parser("update")
parser.add_argument(
"--replace",
metavar="OLD,NEW",
help="replace statement in the form of 'old,new'",
)
parser.add_argument("--like", metavar="PATTERN", help="like statement")
parser.add_argument("--id", type=int, metavar="ID", help="reset by id", nargs="+")
parser.set_defaults(func=updatedb)
raw_argv = add_default_run_subcommand(sys.argv[1:], set(subparsers.choices.keys()))
cmds = cmdlist(raw_argv)
args, _unknown = parser_main.parse_known_args(cmds[0])
if len(_unknown) > 0:
idx_list = [i for i, x in enumerate(cmds[0]) if x in subparsers.choices.keys()]
idx = min(idx_list) if len(idx_list) > 0 else 0
## re-arrange and try again
for x in reversed(_unknown):
cmds[0].remove(x)
cmds[0].insert(idx, x)
args, _unknown = parser_main.parse_known_args(cmds[0])
if len(_unknown) > 0:
print("Unknown options:", _unknown)
usage()
level = logging.DEBUG if args.log_level == "debug" else logging.INFO
logging.basicConfig(level=level, format="%(levelname)s: %(message)s")
log("Python version:", ".".join(map(str, sys.version_info[:3])))
log("Python info:", sys.version)
if (
args.command in ("init", "run", None)
and len(cmds) == 1
and not sys.stdin.isatty()
):
stdin_args = [
line.rstrip()
for line in sys.stdin
if not line.startswith("#") and line.strip() != ""
]
if stdin_args:
cmds.append(stdin_args)
args.cmds = cmds
if args.db is not None:
dbname = args.db
dbfile = dbname + ".sqlite"
db_retries = max(1, args.db_retries)
if args.command is None:
usage()
args.func(args)
sys.exit(0)