def analyze_file(self, pyfile, use_cache=False): # don't analyze the file again if we've already done it and it hasn't # changed. If `use_cache` is True then lookup/record in global cache. mtime = os.path.getmtime(pyfile) if pyfile in self.fileinfo: if mtime <= self.fileinfo[pyfile][1]: return self.fileinfo[pyfile][0] if use_cache: info = _FileInfoCache.lookup(pyfile) if info is not None and mtime <= info[1]: self.fileinfo[pyfile] = info return info[0] logger.info("analyzing %s", pyfile) myvisitor = PythonSourceFileAnalyser(pyfile, self) self.modinfo[get_module_path(pyfile)] = myvisitor self.fileinfo[myvisitor.fname] = (myvisitor, os.path.getmtime(myvisitor.fname)) self.files_count += 1 if use_cache: _FileInfoCache.record( pyfile, (myvisitor, os.path.getmtime(myvisitor.fname))) return myvisitor
def __init__(self, watchdir, use_observer=True, observer=None): super(ProjDirFactory, self).__init__() self._lock = threading.RLock() self.observer = None self.watchdir = watchdir self._files = {} # mapping of file pathnames to _FileInfo objects self._classes = {} # mapping of class names to _FileInfo objects try: added_set = set() changed_set = set() deleted_set = set() modeldir = watchdir + PROJ_DIR_EXT if modeldir not in sys.path: sys.path = [modeldir] + sys.path logger.info("added %s to sys.path" % modeldir) for pyfile in find_files(self.watchdir, "*.py"): self.on_modified(pyfile, added_set, changed_set, deleted_set) if use_observer: self._start_observer(observer) self.publish_updates(added_set, changed_set, deleted_set) else: # sometimes for debugging/testing it's easier to turn observer off self.observer = None except Exception as err: self._error(str(err)) logger.error(str(err))
def analyze_file(self, pyfile, use_cache=False): # don't analyze the file again if we've already done it and it hasn't # changed. If `use_cache` is True then lookup/record in global cache. mtime = os.path.getmtime(pyfile) if pyfile in self.fileinfo: if mtime <= self.fileinfo[pyfile][1]: return self.fileinfo[pyfile][0] if use_cache: info = _FileInfoCache.lookup(pyfile) if info is not None and mtime <= info[1]: self.fileinfo[pyfile] = info return info[0] logger.info("analyzing %s", pyfile) myvisitor = PythonSourceFileAnalyser(pyfile, self) self.modinfo[get_module_path(pyfile)] = myvisitor self.fileinfo[myvisitor.fname] = (myvisitor, os.path.getmtime(myvisitor.fname)) self.files_count += 1 if use_cache: _FileInfoCache.record(pyfile, (myvisitor, os.path.getmtime(myvisitor.fname))) return myvisitor
def _initialize(self): if os.path.isfile(os.path.join(self.macrodir, self.macro)): logger.info('Reconstructing project using %s macro' % self.macro) self.load_macro(self.macro) else: self.command("# Auto-generated file - MODIFY AT YOUR OWN RISK") self.command("top = set_as_top(create('openmdao.main.assembly.Assembly'))")
def register_class_factory(factory): """Add a Factory to the factory list.""" global _factories with _factory_lock: if factory not in _factories: logger.info("adding new factory: %s" % factory) _factories.append(factory)
def _initialize(self): if os.path.isfile(os.path.join(self.macrodir, self.macro)): logger.info('Reconstructing project using %s macro' % self.macro) self.load_macro(self.macro) else: self.command("# Auto-generated file - MODIFY AT YOUR OWN RISK") self.command( "top = set_as_top(create('openmdao.main.assembly.Assembly'))")
def remove_class_factory(factory): """Remove a Factory from the factory list.""" with _factory_lock: for fct in _factories: if fct is factory: if hasattr(factory, 'cleanup'): factory.cleanup() logger.info("removing factory: %s" % factory) _factories.remove(factory) return
def _reload(self): mtime = os.path.getmtime(self.fpath) if self.modtime < mtime: cmpfname = os.path.splitext(self.fpath)[0] + '.pyc' # unless we remove the .pyc file, reload will just use it and won't # see any source updates. :( if os.path.isfile(cmpfname): os.remove(cmpfname) logger.info("reloading module %s" % self.modpath) reload(sys.modules[self.modpath]) self.modtime = mtime self._update_class_info()
def _initialize(self): if os.path.isfile(os.path.join(self.macrodir, self.macro)): logger.info('Reconstructing project using %s macro' % self.macro) self.load_macro(self.macro) else: self.command("# Auto-generated file - MODIFY AT YOUR OWN RISK")