6 from os 
import listdir, makedirs
 
    7 from os.path 
import basename, exists, join 
as pjoin
 
    9 from .sugar 
import define_iov_type, IOVSet
 
   12 log = logging.getLogger(
"DQUtils.periods")
 
   14 DATA_PERIODS_PATH = 
"/afs/cern.ch/atlas/www/GROUPS/DATAPREPARATION/DataPeriods/" 
   22     Warning: Being deprecated 
   24     for period_file 
in listdir(DATA_PERIODS_PATH):
 
   25         if not period_file.endswith(
".runs.list"):
 
   29     runs_file = 
"%s.%s.runs.list" % (project_tag, period)
 
   30     with open(pjoin(DATA_PERIODS_PATH, runs_file)) 
as fd:
 
   31         period_data = fd.read()
 
   34         period_data = map(int, period_data.strip().
split(
"\n"))
 
   36         raise RuntimeError(
"Invalid period data")
 
   38     return IOVSet.from_runs(period_data)
 
   48     from pyAMI.client 
import Client
 
   49     amiclient = 
Client(
'atlas')
 
   50     amiclient.config.read()
 
   52     args = [
'GetRunsForDataPeriod', 
 
   53             '-projectName=%s' % ptag, 
 
   54             '-period=%s' % period]
 
   56         runs = amiclient.execute(args, format=
'dict_object').get_rows()
 
   57     except pyAMI.exceptions.AMI_Error:
 
   61     return sorted(
int(x[
'runNumber']) 
for x 
in runs.values())  
 
   65     Returns a dictionary {project_tag: {period: [runs]}} 
   67     retrieved from the coma database. 
   70     from sqlalchemy 
import (select, Table, Column, 
 
   71         String, Integer, ForeignKey)
 
   73     from re 
import compile
 
   75     LETTER_PART = compile(
r"^(\D+)")
 
   77     from DQUtils.oracle 
import make_oracle_connection
 
   81     schema = 
"ATLAS_TAGS_METADATA" 
   83     coma_runs = Table(
"coma_runs", metadata,
 
   84         Column(
"run_index", Integer),
 
   85         Column(
"run_number", Integer),
 
   89     coma_period_definitions = Table(
"coma_period_defs", metadata,
 
   90         Column(
"p_index",   Integer),
 
   91         Column(
"p_project_period", String),
 
   94     project_period = coma_period_definitions.c.p_project_period
 
   96     coma_period_p1_to_runs = Table(
"coma_period_p1_to_runs", metadata,
 
   97         Column(
"p2r_index", Integer),
 
   98         Column(
"p_index",   Integer, ForeignKey(coma_period_definitions.c.p_index)),
 
   99         Column(
"run_index", Integer, ForeignKey(coma_runs.c.run_index)),
 
  102     joined = coma_period_p1_to_runs.join(coma_runs).
join(coma_period_definitions)
 
  104     smt = (
select([project_period, coma_runs.c.run_number], from_obj=joined)
 
  105            .order_by(coma_runs.c.run_number))
 
  107     projectperiod_runs = smt.execute().fetchall()
 
  110     for projectperiod, run 
in projectperiod_runs:
 
  111         project, period = projectperiod.split(
".")
 
  112         period = period.replace(
"period", 
"")
 
  114         project_dict = mapping.setdefault(project, {})
 
  115         period_runs = project_dict.setdefault(period, [])
 
  116         if not period_runs 
or period_runs[-1] != run:
 
  117             period_runs.append(run)
 
  119         period_letter = LETTER_PART.search(period)
 
  121             (period_letter,) = period_letter.groups()
 
  122             period_runs = project_dict.setdefault(period_letter, [])
 
  123             if not period_runs 
or period_runs[-1] != run:
 
  124                 period_runs.append(run)
 
  128 def split_grl(project_tag, periods, grl_file, out_directory):
 
  129     grl = IOVSet.from_grl(grl_file)
 
  132     if project_tag 
not in period_info:
 
  133         raise RuntimeError(
"Unknown period {0}".
format(project_tag))
 
  134     ptag_periods = period_info[project_tag]
 
  138         periods = 
sorted(ptag_periods.keys())
 
  140     name_left, _, extension = 
basename(grl_file).rpartition(
".")
 
  141     assert name_left, 
"No '.' in filename?" 
  143     if not exists(out_directory):
 
  144         makedirs(out_directory)
 
  146     for period 
in periods:
 
  147         iovs = grl & IOVSet.from_runs(ptag_periods[period])
 
  149         output_name = 
"{0}_period{1}.{2}".
format(name_left, period, extension)
 
  150         log.info(
"{0:4s} : {1:3} runs, {2:5} lumiblocks".
format(
 
  151             period, len(iovs.runs), iovs.lb_counts))
 
  153         output_path = pjoin(out_directory, output_name)
 
  154         iovs.to_grl(output_path)