def _guess_module_logger_name(modulename,frame=None): #print " _guess_module_logger_name",modulename # find the filename of the calling module import sys,os.path if frame is None: frame = sys._getframe(3) # assuming 2 nested calls to the module boundary! else: print 'using frame from the caller' # accessing __file__ from globals() is much more reliable than f_code.co_filename (name = os.path.normcase(frame.f_code.co_filename)) try: name = os.path.realpath(os.path.abspath(frame.f_globals['__file__'])) except KeyError: # no file associated with the frame (e.g. interactive prompt, exec statement) name = '_program_' #print " _guess_module_logger_name",name del frame #if private_logger: # private_logger.debug('searching for package matching calling module co_filename= %s',str(name)) # sometimes the filename is an absolute path, try to find a relative module path from the PYTHONPATH # and remove the trailing path -> the result will be used as the logger name from Ganga.Utility.files import remove_prefix name = remove_prefix(name,sys.path) def remove_tail(s,tail): idx = s.rfind(tail) if idx != -1: return s[:idx] return s # get rid of trailing .py .pyc .pyo name = remove_tail(name,'.py') # replace slashes with dots name = name.replace(os.sep,'.') # return full module name if modulename == 1: return name # remove module name name = remove_tail(name,'.') if name == 'ganga': #interactive IPython session name = "Ganga.GPI" # return package name if not modulename: return name # return custom module name return name+'.'+modulename
def getGangaModulesAsSandboxFiles(modules): """ This returns a list of sandbox files corresponding to specified Ganga modules. Ganga modules are placed in a well-known location in the sandbox. """ import inspect, sys from Ganga.Utility.files import remove_prefix from Ganga.GPIDev.Lib.File import File files = [] for m in modules: fullpath = os.path.realpath(inspect.getsourcefile(m)) dir, fn = os.path.split(remove_prefix(fullpath, sys.path)) if os.path.join(dir, fn) == fullpath: raise Exception("Cannot find the prefix for %s" % fullpath) files.append(File(fullpath, subdir=os.path.join(PYTHON_DIR, dir))) return files
def getGangaModulesAsSandboxFiles(modules): """ This returns a list of sandbox files corresponding to specified Ganga modules. Ganga modules are placed in a well-known location in the sandbox. """ import inspect import sys from Ganga.Utility.files import remove_prefix from Ganga.GPIDev.Lib.File import File files = [] for m in modules: fullpath = os.path.realpath(inspect.getsourcefile(m)) dir, fn = os.path.split(remove_prefix(fullpath, sys.path)) if os.path.join(dir, fn) == fullpath: raise GangaIOError('Cannot find the prefix for %s' % fullpath) files.append(File(fullpath, subdir=os.path.join(PYTHON_DIR, dir))) return files
def _guess_module_logger_name(modulename, frame=None): """Gues the Module name from the current frame or from a given frame if specified. If module == 1 return full name, else trim module name""" # find the filename of the calling module if frame is None: # assuming 2 nested calls to the module boundary! frame = sys._getframe(3) else: print("using frame from the caller") global lookup_frame_names this__file__ = None if "__file__" in frame.f_globals: this__file__ = frame.f_globals["__file__"] if this__file__ in lookup_frame_names: del frame return lookup_frame_names[this_file] else: should_store = True else: should_store = False # accessing __file__ from globals() is much more reliable than # f_code.co_filename (name = os.path.normcase(frame.f_code.co_filename)) if this__file__ is not None: name = os.path.realpath(os.path.abspath(this__file__)) else: # no file associated with the frame (e.g. interactive prompt, exec # statement) name = "_program_" # print " _guess_module_logger_name",name del frame # if private_logger: # private_logger.debug('searching for package matching calling module co_filename= %s',str(name)) # sometimes the filename is an absolute path, try to find a relative module path from the PYTHONPATH # and remove the trailing path -> the result will be used as the logger # name from Ganga.Utility.files import remove_prefix name = remove_prefix(name, sys.path) def remove_tail(s, tail): idx = s.rfind(tail) if idx != -1: return s[:idx] return s # get rid of trailing .py .pyc .pyo name = remove_tail(name, ".py") # replace slashes with dots name = name.replace(os.sep, ".") # return full module name if modulename == 1: return name # remove module name name = remove_tail(name, ".") if name == "ganga": # interactive IPython session name = "Ganga.GPI" # return package name if not modulename: return name return_name = name + "." + modulename if should_store is True: lookup_frame_names[this__file__] = return_name # return custom module name return return_name
def _guess_module_logger_name(modulename, frame=None): """Gues the Module name from the current frame or from a given frame if specified. If module == 1 return full name, else trim module name""" # find the filename of the calling module if frame is None: # assuming 2 nested calls to the module boundary! frame = sys._getframe(3) else: print('using frame from the caller') global lookup_frame_names this__file__ = None if '__file__' in frame.f_globals: this__file__ = frame.f_globals['__file__'] if this__file__ in lookup_frame_names: del frame return lookup_frame_names[this_file] else: should_store = True else: should_store = False # accessing __file__ from globals() is much more reliable than # f_code.co_filename (name = os.path.normcase(frame.f_code.co_filename)) if this__file__ is not None: name = os.path.realpath(os.path.abspath(this__file__)) else: # no file associated with the frame (e.g. interactive prompt, exec # statement) name = '_program_' # print " _guess_module_logger_name",name del frame # if private_logger: # private_logger.debug('searching for package matching calling module co_filename= %s',str(name)) # sometimes the filename is an absolute path, try to find a relative module path from the PYTHONPATH # and remove the trailing path -> the result will be used as the logger # name from Ganga.Utility.files import remove_prefix name = remove_prefix(name, sys.path) def remove_tail(s, tail): idx = s.rfind(tail) if idx != -1: return s[:idx] return s # get rid of trailing .py .pyc .pyo name = remove_tail(name, '.py') # replace slashes with dots name = name.replace(os.sep, '.') # return full module name if modulename == 1: return name # remove module name name = remove_tail(name, '.') if name == 'ganga': # interactive IPython session name = "Ganga.GPI" # return package name if not modulename: return name return_name = name + '.' + modulename if should_store is True: lookup_frame_names[this__file__] = return_name # return custom module name return return_name