CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
command_line.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 """
3 Command line module that the "command line" script.
4 
5 Works by taking the main keyword (first command given to the script),
6 passing that to the function that will deal with that action, along with the following arguments as parameters for that function.
7 """
8 
9 from . import querying
10 import argparse
11 import datetime
12 
13 def list_object(arguments):
14 
15  # set up connection
16  connection = querying.connect(arguments.db, secrets=arguments.secrets, mode=arguments.mode)
17 
18  options = ["tag", "gt", "gts_for_tag"]
19  number_of_options_given = 0
20  for option in options:
21  if getattr(arguments, option):
22  number_of_options_given += 1
23  if number_of_options_given != 1:
24  print("You must specify a single object to list.")
25  exit()
26 
27  if arguments.tag:
28  tag_name = arguments.tag
29  tag = connection.tag(name=tag_name)
30  if tag:
31  iovs = tag.iovs(amount=arguments.limit)
32  iovs.as_table()
33  else:
34  print("The Tag '%s' was not found in the database '%s'." % (tag_name, arguments.db))
35  exit()
36 
37  elif arguments.gt:
38  gt_name = arguments.gt
39  gt = connection.global_tag(name=gt_name)
40  if gt:
41  gt_maps = gt.tags(amount=arguments.limit)
42  gt_maps.as_table(hide=["global_tag_name"])
43  else:
44  print("The Global Tag '%s' was not found in the database '%s'." % (gt_name, arguments.db))
45  exit()
46 
47  elif arguments.gts_for_tag:
48  tag_name = arguments.gts_for_tag
49  tag = connection.tag(name=tag_name)
50  gts = tag.parent_global_tags(amount=arguments.limit)
51  gts.as_table(columns=["name", "insertion_time", "snapshot_time"])
52 
53 def diff_of_tags(arguments):
54  # get a CondDBFW Tag object for the first tag
55  # then use the diff() method to draw the table of differences
56 
57  # set up connection
58  connection = querying.connect(arguments.db, secrets=arguments.secrets, mode=arguments.mode)
59 
60  tag1 = connection.tag(name=arguments.tag1)
61  tag2 = connection.tag(name=arguments.tag2)
62 
63  tag1.diff(tag2).as_table(columns=["since", arguments.tag1, arguments.tag2])
64 
65 def diff_of_gts(arguments):
66  # get a CondDBFW Global Tag object for the first GT
67  # then use the diff() method to draw the table of differences
68 
69  # set up connection
70  connection = querying.connect(arguments.db, secrets=arguments.secrets, mode=arguments.mode)
71 
72  gt1 = connection.global_tag(name=arguments.gt1)
73  gt2 = connection.global_tag(name=arguments.gt2)
74 
75  gt1.diff(gt2).as_table(columns=["Record", "Label", "%s Tag" % arguments.gt1, "%s Tag" % arguments.gt2])
76 
77 def search(arguments):
78 
79  raise NotImplementedError("Todo")
80 
81  connection = querying.connect(arguments.db, secrets=arguments.secrets, mode=arguments.mode)
82 
83  search_string = connection.regexp(".*%s.*" % arguments.string)
84 
85 def copy_tag(arguments):
86 
87  # set up connection
88  source_connection = querying.connect(arguments.db, secrets=arguments.secrets, mode=arguments.mode, map_blobs=True)
89  dest_connection = querying.connect(arguments.dest_db, secrets=arguments.secrets, mode=arguments.mode, map_blobs=True)
90 
91  # get tag from the source database, adjust it, and copy it (with the defined IOV range) to the destination database
92 
93  print("Reading source Tag.")
94  source_tag = source_connection.tag(name=arguments.input_tag)
95  if source_tag == None:
96  raise Exception("Source Tag doesn't exist.")
97 
98  # get all IOVs within the range [start, end]
99  print("Reading source IOVs.")
100  since_range = source_connection.range(arguments.start, arguments.end)
101  source_iovs = source_tag.iovs(since=since_range).data()
102 
103  # get hashes of all IOVs contained in the Tag in the source database
104  print("Reading source Payloads.")
105  hashes = source_tag.iovs().get_members("payload_hash").data()
106  payloads = source_connection.payload(hash=hashes)
107 
108  print("Writing to destination database...")
109 
110  # set end_of_validity to -1 because sqlite doesn't support long ints
111  source_tag.end_of_validity = -1
112  source_tag.name = arguments.dest_tag
113  source_tag.modification_time = datetime.datetime.utcnow()
114 
115  # create new iovs
116  new_iovs = []
117  for iov in source_iovs:
118  new_iovs.append(dest_connection.models["iov"](iov.as_dicts(convert_timestamps=False), convert_timestamps=False))
119 
120  # write new tag to destination database
121  print("Writing destination Tag.")
122  if dest_connection.tag(name=arguments.dest_tag) != None:
123  dest_connection.write_and_commit(source_tag)
124 
125  # write new iovs
126  print("Writing IOVs to destination Tag.")
127  for iov in new_iovs:
128  if dest_connection.iov(tag_name=iov.tag_name, since=iov.since, insertion_time=iov.insertion_time) == None:
129  dest_connection.write_and_commit(iov)
130 
131  # get payloads used by IOVs and copy those over
132  print("Copying Payloads over.")
133  for payload in payloads:
134  if dest_connection.payload(hash=payload.hash) == None:
135  dest_connection.write_and_commit(payload)
136 
137  print("Copy complete.")
138 
139 def copy_global_tag(arguments):
140  raise NotImplementedError("Copying Global Tags is currently not supported for this transition command-line interface for CondDBFW.")
141 
142  # set up connection
143  source_connection = querying.connect(arguments.db, secrets=arguments.secrets, mode=arguments.mode, map_blobs=True)
144  dest_connection = querying.connect(arguments.dest_db, secrets=arguments.secrets, mode=arguments.mode, map_blobs=True)
145 
146  # get CondDBFW Global Tag object
147  global_tag = source_connection.global_tag(name=arguments.input_gt)
148  if global_tag == None:
149  raise Exception("Source Global Tag doesn't exist.")
150 
151  tag_names = global_tag.tags().get_members("tag_name").data()
152  tags = source_connection.tag(name=tags)
153 
154  # copy global tag first
155  global_tag.insertion_time = datetime.datetime.utcnow()
156  global_tag.validity = -1
157  dest_connection.write_and_commit(global_tag)
158 
159  for tag in tags:
160  # create temporary argument class
161  class args(object):
162  def __init__(self):
163  self.input_tag = tag.name
164  self.dest_tag = tag.name
165  self.start = 1
166  self.end = tag.latest_iov()+1
167  for attribute in dir(arguments):
168  self.__dict__[attribute] = getattr(arguments, attribute)
169 
170  copy_tag(args())
171 
172 def parse_command_line(arguments):
173  """
174  Assumes script name has been removed from the list of arguments.
175  Hence, arguments[0] is the subcommand.
176  """
177  top_level_parser = argparse.ArgumentParser(description="CondDBFW Command line tool")
178  top_level_parser.add_argument("--db", type=str, required=False, default="frontier://FrontierProd/CMS_CONDITIONS")
179  top_level_parser.add_argument("--mode", type=str, required=False, default="w")
180  top_level_parser.add_argument("--secrets", type=str, required=False)
181  top_level_parser.add_argument("--limit", type=int, required=False, default=10)
182 
183  subparser = top_level_parser.add_subparsers(title="Subcommands")
184 
185  list_parser = subparser.add_parser("list", description="Lists the Metadata objects contained within the given object.")
186  list_parser.add_argument("--tag", required=False, help="List all IOVs in a Tag.")
187  list_parser.add_argument("--gt", required=False, help="List all Global Tag Maps linked to a Global Tag.")
188  list_parser.add_argument("--gts-for-tag", required=False, help="List all Global Tags that contain a Tag.")
189 
190  list_parser.set_defaults(func=list_object)
191 
192  diff_parser = subparser.add_parser("diff-tags", description="Gives the differences in payload hashes used by IOVs between Tags.")
193  diff_parser.add_argument("--tag1", required=True, help="First Tag to use in the comparison.")
194  diff_parser.add_argument("--tag2", required=True, help="Second Tag to use in the comparison.")
195 
196  diff_parser.set_defaults(func=diff_of_tags)
197 
198  gt_diff_parser = subparser.add_parser("diff-gts", description="Gives the differences in Global Tag Maps contained within Global Tag.")
199  gt_diff_parser.add_argument("--gt1", required=True, help="First Global Tag to use in the comparison.")
200  gt_diff_parser.add_argument("--gt2", required=True, help="Second Global Tag to use in the comparison.")
201 
202  gt_diff_parser.set_defaults(func=diff_of_gts)
203 
204  copy_tag_parser = subparser.add_parser("copy-tag", description="Copies a Tag with its IOVs and Payloads to a destination database."
205  + "\nFor copying to official databases, use cmsDbCondUpload (https://cms-conddb-dev.cern.ch/cmsDbCondUpload).")
206  copy_tag_parser.add_argument("--dest-db", required=True, help="Database to copy the Tag and its IOVs to.")
207  copy_tag_parser.add_argument("--input-tag", required=True, help="Tag to take data from in source database.")
208  copy_tag_parser.add_argument("--dest-tag", required=True, help="Tag to copy input Tag to in the destination database.")
209  copy_tag_parser.add_argument("--start", required=True, help="Since to start from. If this is between two, the highest one is taken (no adjustments are made).")
210  copy_tag_parser.add_argument("--end", required=True, help="Since to finidh at. If this is between two, the lowest one is taken (no adjustments are made).")
211 
212  copy_tag_parser.set_defaults(func=copy_tag)
213 
214  parsed_arguments = top_level_parser.parse_args()
215 
216  print("Using database '%s'." % parsed_arguments.db)
217 
218  parsed_arguments.func(parsed_arguments)
219 
220 if __name__ == "__main__":
221  import sys
222  parse_command_line(sys.argv[1:])
def parse_command_line
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def connect
Definition: querying.py:453
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79