HWRF  trunk@4391
hwrf_datastore.py
1 #! /usr/bin/env python
2 
3 ##@namespace ush.hwrf_datastore
4 # A utility script for dumping or modifying the contents of an HWRF
5 # sqlite3 database file.
6 #
7 # This script can perform one of several operations on an sqlite3 file
8 # generated by the HWRF system. It is called as follows:
9 # @code{.sh}
10 # hwrf_datastore.py OP [ARG] file1.sqlite3 [file2.sqlite3 [...]]
11 # @endcode
12 #
13 # Valid operations (OP) are:
14 # * DUMP --- dump entire database to stdout
15 # * UNFAIL --- mark all failed or running tasks as unstarted
16 # * UNRUN --- mark all tasks as unstarted
17 # * UNRUN_ONE taskname --- unrun the specified task.
18 #Only UNRUN_ONE takes an argument, and that argument is mandatory: the
19 #task to "unrun". The argument is the task id, which is everything in
20 #the id after the %s:: in the output of a call to hwrf_datastore.py DUMP.
21 
22 import logging,sys
23 import produtil.datastore
24 
25 from produtil.datastore import Datastore,TASK_CATEGORY,UNSTARTED,COMPLETED
26 
27 def unfail(ds):
28  """!Marks all tasks as not having failed.
29  @param ds the produtil.datastore.Datastore to modify."""
30  with ds.transaction() as t:
31  t.mutate("""UPDATE products SET available=?
32  WHERE id LIKE '%s%%' AND
33  NOT available=?"""%(TASK_CATEGORY),
34  (UNSTARTED,COMPLETED))
35 
36 def unrun(ds):
37  """!Marks all products as unavailable and tasks as unstarted.
38  @param ds the produtil.datastore.Datastore to modify."""
39  with ds.transaction() as t:
40  t.mutate("UPDATE products SET available=?",(UNSTARTED,))
41 
42 def unrun_one(ds,did):
43  """!Marks a specific task as unstarted.
44  @param ds the produtil.datastore.Datastore to modify.
45  @param did the taskname"""
46  with ds.transaction() as t:
47  taskid="%s::%s"%(TASK_CATEGORY,str(did))
48  print>>sys.stderr,'Marking %s as unstarted.'%(taskid,)
49  t.mutate("UPDATE products SET available=? WHERE id=?",
50  (UNSTARTED,taskid))
51 
52 def dump(ds):
53  """!Dumps the contents of the given Datastore to stdout
54  @param ds the produtil.datastore.Datastore to dump"""
55  ds.dump()
56 
57 def usage(args):
58  """!Sends a program usage message to stderr and exits with status 2
59  @param args the command-line arguments that were provided."""
60  print>>sys.stderr,'''FORMAT: hwrf_datastore.py OP [ARG] file1.sqlite3 [file2.sqlite3 [...]]
61 where OP is one of:
62  DUMP - dump entire database to stdout
63  UNFAIL - mark all failed or running tasks as unstarted
64  UNRUN - mark all tasks as unstarted
65  UNRUN_ONE - see below
66 Only UNRUN_ONE takes an argument, and that argument is mandatory: the
67 task to "unrun". The argument is the task id, which is everything in
68 the id after the %s:: in the output of ab hwrf_datastore.py DUMP.
69 '''%(TASK_CATEGORY,)
70  for arg in args:
71  print>>sys.stderr,arg
72  sys.exit(2)
73 
74 def main():
75  """!Main program. Parses arguments and calls other functions in
76  this program to do the real work."""
77  if len(sys.argv)<3: usage()
78 
79  first_arg=2
80  opargs=[]
81  readonly=False
82  if sys.argv[1].upper()=='UNFAIL': op=unfail
83  elif sys.argv[1].upper()=='DUMP': op=dump
84  elif sys.argv[1].upper()=='UNRUN': op=unrun
85  elif sys.argv[1].upper()=='UNRUN_ONE':
86  op=unrun_one
87  opargs=[sys.argv[2]]
88  first_arg=3
89  else:
90  usage('Unrecognized datastore operation %s'%(sys.argv[1],))
91 
92  if len(sys.argv)<first_arg+1: usage()
93 
94  if op is dump: readonly=True
95 
96  logging.basicConfig(stream=sys.stderr,level=logging.INFO)
97  for filename in sys.argv[first_arg:]:
98  ds=Datastore(filename,logger=logging.getLogger(),locking=not readonly)
99  with ds.transaction() as t:
100  op(ds,*opargs)
101 
102 if __name__=='__main__': main()
Stores products and tasks in an sqlite3 database file.
Definition: datastore.py:1