CMS 3D CMS Logo

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