Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:48

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 key.py
0023 ===============
0024 
0025 See also::
0026 
0027    ana/geocache.bash 
0028    boostrap/BOpticksResource.cc
0029 
0030 NB path layout changes need to be done in triplicate in bash/py/C++ 
0031 
0032 Analogous to bpath.py in old route 
0033 
0034 
0035 Notes
0036 -------
0037 
0038 * some old IDPATH comparison code squatting in this namespace moved to cfgeocache
0039 
0040 """
0041 import os, json, logging, numpy as np, argparse
0042 log = logging.getLogger(__name__)
0043 
0044 def keydir(keyspec=None):
0045     if keyspec is None:
0046         keyspec = os.environ.get("OPTICKS_KEY", None) 
0047     pass
0048     return Key.Keydir(keyspec) 
0049 
0050 def key_(keyspec=None):
0051     if keyspec is None:
0052         keyspec = os.environ.get("OPTICKS_KEY", None) 
0053     pass
0054     return Key(keyspec)
0055 
0056 
0057 
0058 class Key(object):
0059     @classmethod
0060     def GeoCachePrefix(cls):
0061         geocache_prefix_default = os.path.expanduser("~/.opticks") 
0062         geocache_prefix = os.environ.get("OPTICKS_GEOCACHE_PREFIX", geocache_prefix_default ) 
0063         return geocache_prefix 
0064 
0065     @classmethod
0066     def GeoCacheDir(cls):
0067         return "%s/geocache" % cls.GeoCachePrefix()
0068 
0069     @classmethod
0070     def Keydir(cls, key):
0071         """
0072         Should match bash geocache-keydir
0073         """
0074         if key is None: return None
0075         elem = key.split(".")
0076         assert len(elem) == 4, elem 
0077         exe,kls,top,dig = elem 
0078         assert len(dig) == 32, "OPTICKS_KEY digest %s is expected to be length 32, not %d " % (dig, len(dig))
0079         geocache_dir = cls.GeoCacheDir()
0080         tmpl = "{geocache_dir}/{exe}_{top}_g4live/g4ok_gltf/{dig}/1".format(**locals())
0081         keydir = os.path.expandvars(tmpl)
0082         os.environ["KEYDIR"] = keydir 
0083         if not "TMP" in os.environ:
0084             os.environ["TMP"] = os.path.expandvars("/tmp/$USER/opticks")
0085         pass
0086         return keydir
0087 
0088     def __init__(self, keyspec=None):
0089         if keyspec is None:
0090             keyspec = os.environ.get("OPTICKS_KEY",None)
0091         pass
0092         keydir = Key.Keydir(keyspec) 
0093         exists = os.path.isdir(keydir)
0094         meta = json.load(open(os.path.join(keydir, "cachemeta.json")))
0095 
0096         self.keyspec = keyspec
0097         self.keydir = keydir
0098         self.exists = exists
0099         self.digest = keyspec.split(".")[-1]
0100         self.meta = meta 
0101         self.version = int(meta["GEOCACHE_CODE_VERSION"])
0102         self.gdmlpath = self.extract_argument_after(meta, "--gdmlpath")
0103   
0104     @classmethod
0105     def extract_argument_after(cls, meta, k):
0106         """
0107         :param meta: metadata dict including argline key   
0108         :param k: argument to look for the value of
0109         :return path: commandline value following k or None if k is not found
0110         """
0111         argline = meta.get("argline","-")
0112         args = argline.split(" ")
0113         try:
0114             ppos = args.index(k)
0115         except ValueError:
0116             ppos = -1 
0117         pass
0118         log.debug("ppos %d" % ppos)
0119         path = None
0120         if ppos == -1:
0121             pass
0122         elif ppos + 1 >= len(args):
0123             log.fatal("truncated argline ?")
0124         else:
0125             path = args[ppos+1] 
0126         pass
0127         return path
0128 
0129  
0130     def __repr__(self):
0131         version = self.version
0132         keyspec = self.keyspec 
0133         keydir = self.keydir
0134         return "\n".join(["Key.v{version}:{keyspec}","{keydir}"]).format(**locals())
0135 
0136 
0137     def __str__(self):
0138         return "\n".join([self.keyspec, self.keydir, self.gdmlpath])
0139 
0140 
0141 if __name__ == '__main__':
0142     pass
0143     logging.basicConfig(level=logging.INFO)
0144    
0145     parser = argparse.ArgumentParser(__doc__)
0146     parser.add_argument( "-v", "--verbose", action="store_true", help="Report key and keydir" )
0147     args = parser.parse_args()
0148 
0149     key = Key()
0150     if args.verbose:
0151         print(key)
0152     else:
0153         print(key.keydir)
0154     pass