new_cache_state.py
Go to the documentation of this file.
1 #!/bin/env python3
2 import cache_state
3 import argparse
4 import os, os.path
5 import sys
6 import samweb_client as swc
7 import pycurl
8 import subprocess
9 import shlex
10 
11 
12 examples = ''
13 parser= argparse.ArgumentParser(epilog=examples, formatter_class=argparse.RawDescriptionHelpFormatter)
14 
15 gp = parser.add_mutually_exclusive_group()
16 gp.add_argument("files",
17  nargs="*",
18  default=[],
19  metavar="FILE",
20  help="Files to consider. Can be specified as a full /pnfs path, or just the SAM filename",
21 )
22 gp.add_argument("-d", "--dataset",
23  metavar="DATASET",
24  dest="dataset_name",
25  help="Name of the SAM dataset to check cache status of",
26 )
27 gp.add_argument("-q", "--dim",
28  metavar="\"DIMENSION\"",
29  dest="dimensions",
30  help="sam dimensions to check cache status of",
31  )
32 
33 parser.add_argument("-s","--sparse", type=int, dest='sparse',help="Sparsification factor. This is used to check only a portion of a list of files",default=1)
34 parser.add_argument("-ss", "--snapshot", dest="snapshot", help="[Also requires -d] Use this snapshot ID for the dataset. Specify 'latest' for the most recent one.")
35 parser.add_argument("-v","--verbose", action="store_true", dest="verbose", default=False, help="Print information about individual files")
36 parser.add_argument("-p","--prestage", action="store_true", dest="prestage", default=False, help="Prestage the files specified")
37 parser.add_argument("-m", "--method", choices=["rest", "pnfs"], default="rest", help="Use this method to look up file status.")
38 
39 args=parser.parse_args()
40 
41 # gotta make sure you have a valid certificate.
42 # otherwise the results may lie...
43 if args.method in ("rest"):
44  try:
45  subprocess.check_call(shlex.split("setup_fnal_security --check"), stdout=open(os.devnull), stderr=subprocess.STDOUT)
46  except subprocess.CalledProcessError:
47  print( "Your proxy is expired or missing. Please run `setup_fnal_security` and then try again." )
48  sys.exit(2)
49 
50 filelist = None if args.dataset_name else args.files
51 
52 sam = swc.SAMWebClient("dune")
53 
54 cache_count = 0
55 
56 # Figure out where we want to get our list of files from
57 
58 # See if a SAM dataset was specified
59 filelist = []
60 if args.dataset_name:
61  print( "Retrieving file list for SAM dataset definition name: '%s'..." % args.dataset_name, end="" )
62  sys.stdout.flush()
63  try:
64  thislist = sam.listFiles(defname=args.dataset_name)
65  print(len(thislist))
66  samlist = []
67  a = 0
68  cached = 0
69  pending = 0
70  c = cache_state.make_curl() if args.method == "rest" else None
71  for f in thislist:
72  if not (a%100): print("Locating files: %i/%i"%(a, len(thislist)), end='\r')
73  locs = sam.locateFile(f)
74  for l in locs:
75  split_path = l['full_path'].split(':')
76  if split_path[0] == 'enstore':
77  if args.method == "pnfs":
78  this_cached = cache_state.is_file_online_pnfs(os.path.join(split_path[1], f))
79  if args.verbose:
80  print( f, "ONLINE" if this_cached else "NEARLINE")
81  if this_cached: cached += 1
82  break
83  else:
84  qos,targetQos = cache_state.get_file_qos(c, os.path.join(split_path[1], f))
85  if "ONLINE" in qos: cached += 1
86  if "disk" in targetQos: pending += 1
87  a += 1
88  print()
89  print(len(samlist))
90 
91  print("%i/%i files are cached"%(cached, a))
92 
93  #filelist = enstore_locations_to_paths(list(samlist), args.sparse)
94  print( " done." )
95  except Exception as e:
96  print( e )
97  print()
98  print( 'Unable to retrieve SAM information for dataset: %s' %(args.dataset_name) )
99  exit(-1)
100  # Take the rest of the commandline as the filenames
101  filelist = args
102 
def make_curl()
Definition: cache_state.py:61
int open(const char *, int)
Opens a file descriptor.
def is_file_online_pnfs(f)
Definition: cache_state.py:177
def get_file_qos(c, filename)
Definition: cache_state.py:92
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35