Belle II Software development
CommandIoVsHelper Class Reference

Public Member Functions

def __init__ (self, whichcommand, args, db)
 
def add_arguments (self)
 
def get_iovs (self)
 
def modify_db (self, func, func_args, whichcommand=None)
 

Public Attributes

 whichcommand
 from whichcommand it is called (copy, delete or modify)
 
 db
 conditions_db.ConditionsDB instance
 
 iovfilter
 ItemFilter.
 
 num_all_iovs
 number of iovs before payload and revision selection
 
 past_dict
 Dictionary with past participles.
 

Protected Attributes

 _args
 argparse.ArgumentParser instance
 

Detailed Description

Class to unify common parts of b2conditionsdb iovs commands

This class defines common argparse arguments,
common filter of iovs and common multithreading

Definition at line 488 of file cli_management.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  whichcommand,
  args,
  db 
)
initialization, just remember the arguments or parser and the database instance

Args:
    whichcommand (str): from whichcommand it is called (copy, delete or modify)
    args (argparse.ArgumentParser): where to append new arguments
    db (conditions_db.ConditionsDB): database instance to be used

Definition at line 496 of file cli_management.py.

496 def __init__(self, whichcommand, args, db):
497 """initialization, just remember the arguments or parser and the database instance
498
499 Args:
500 whichcommand (str): from whichcommand it is called (copy, delete or modify)
501 args (argparse.ArgumentParser): where to append new arguments
502 db (conditions_db.ConditionsDB): database instance to be used
503 """
504
505
506 self.whichcommand = whichcommand
507
508 self._args = args
509
510 self.db = db
511
512 self.iovfilter = ItemFilter(args)
513
514 self.num_all_iovs = None
515
516 self.past_dict = {"delete": "deleted", "modify": "modified", "copy": "copied", "create": "created"}
517

Member Function Documentation

◆ add_arguments()

def add_arguments (   self)
Add arguments to the parser

Definition at line 518 of file cli_management.py.

518 def add_arguments(self):
519 """Add arguments to the parser"""
520
521 self._args.add_argument("tag", metavar="INPUT_TAGNAME",
522 help=f"globaltag for which the the IoVs should be {self.past_dict[self.whichcommand]}")
523 if self.whichcommand == "copy":
524 self._args.add_argument("output", metavar="OUTPUT_TAGNAME", help="globaltag to which the the IoVs should be copied")
525 if self.whichcommand == "modify":
526 self._args.add_argument("new_iov", metavar="NEW_IOV", help="New iov to be set to all considered iovs."
527 " It should be a string with 4 numbers separated by spaces."
528 " Use * to mark the fields that should not be modified. For example"
529 " if 7 0 * * is given the iov (7, 1, 9, 42) will become (7 0 9 42).")
530 self._args.add_argument("--iov-id", default=None, type=int,
531 help="IoVid of the iov to be considered")
532 self._args.add_argument(
533 "--iov-pattern",
534 default=None,
535 help="whitespace-separated string with pattern of the iov to be considered. "
536 " Use * to mark the fields that shold be ignored. Valid patterns are 0 0 -1 -1"
537 " (a very specific IoV), 0 * -1 -1 (any iov that starts in any run of exp 0 and ends exactly in exp -1, run -1)"
538 ", * * 3 45 (any Iov ending in exp 3, run 45, regardless from where it starts).")
539 self._args.add_argument("--run-range", nargs=4, default=None, type=int,
540 metavar=("FIRST_EXP", "FIRST_RUN", "FINAL_EXP", "FINAL_RUN"),
541 help="Can be four numbers to limit the run range to be considered"
542 " Only iovs overlapping, even partially, with this range will be considered.")
543 self._args.add_argument("--fully-contained", action="store_true",
544 help="If given together with ``--run_range`` limit the list of payloads "
545 "to the ones fully contained in the given run range")
546 if self.whichcommand == "copy":
547 self._args.add_argument("--set-run-range", action="store_true",
548 help="If given together with ``--run_range`` modify the interval of validity"
549 " of partially overlapping iovs to be fully contained in the given run range")
550 self.iovfilter.add_arguments("payloads")
551 self._args.add_argument("--revision", metavar='revision', type=int,
552 help="Specify the revision of the payload to be removed")
553 self._args.add_argument("--dry-run", help="Don't do anything, just print what would be done",
554 action="store_true", default=False)
555 if self.whichcommand == "copy":
556 self._args.add_argument("--replace", help="Modify the iovs in the output tag to avoid overlaps",
557 action="store_true", default=False)
558 self._args.add_argument("-j", type=int, default=10, dest="nprocess",
559 help="Number of concurrent threads to use.")
560

◆ get_iovs()

def get_iovs (   self)
Get the iovs already filtered

Definition at line 561 of file cli_management.py.

561 def get_iovs(self):
562 """Get the iovs already filtered"""
563 if not self.iovfilter.check_arguments():
564 B2ERROR("Issue with arguments")
565
566 all_iovs = self.db.get_all_iovs(
567 self._args.tag,
568 run_range=self._args.run_range,
569 fully_contained=self._args.fully_contained,
570 message=str(self.iovfilter))
571 self.num_all_iovs = len(all_iovs)
572 iovs_to_return = []
573 for iov in all_iovs:
574 if self._args.iov_id and iov.iov_id != self._args.iov_id:
575 continue
576 if not self.iovfilter.check(iov.name):
577 continue
578 if self._args.iov_pattern and not fnmatch("{} {} {} {}".format(*iov.iov),
579 "{} {} {} {}".format(*self._args.iov_pattern.split())):
580 continue
581 if self._args.revision and iov.revision != self._args.revision:
582 continue
583 iovs_to_return.append(iov)
584 return iovs_to_return
585

◆ modify_db()

def modify_db (   self,
  func,
  func_args,
  whichcommand = None 
)
Modify the database using multithreading

Definition at line 586 of file cli_management.py.

586 def modify_db(self, func, func_args, whichcommand=None):
587 """Modify the database using multithreading"""
588 if whichcommand is None:
589 whichcommand = self.whichcommand
590 try:
591 with ThreadPoolExecutor(max_workers=self._args.nprocess) as pool:
592 start = time.monotonic()
593 for iov_num, _ in enumerate(pool.map(func, func_args), 1):
594 eta = (time.monotonic() - start) / iov_num * (len(func_args) - iov_num)
595 B2INFO(f"{iov_num}/{len(func_args)} iovs {self.past_dict[whichcommand]}, ETA: {eta:.1f} seconds")
596 except RuntimeError:
597 B2ERROR(f"Not all iovs could be {self.past_dict[whichcommand]}. This could be a server/network problem "
598 "or the destination globaltag was not writeable.")
599 raise
600
601

Member Data Documentation

◆ _args

_args
protected

argparse.ArgumentParser instance

Definition at line 508 of file cli_management.py.

◆ db

db

conditions_db.ConditionsDB instance

Definition at line 510 of file cli_management.py.

◆ iovfilter

iovfilter

ItemFilter.

Definition at line 512 of file cli_management.py.

◆ num_all_iovs

num_all_iovs

number of iovs before payload and revision selection

Definition at line 514 of file cli_management.py.

◆ past_dict

past_dict

Dictionary with past participles.

Definition at line 516 of file cli_management.py.

◆ whichcommand

whichcommand

from whichcommand it is called (copy, delete or modify)

Definition at line 506 of file cli_management.py.


The documentation for this class was generated from the following file: