Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
 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'))")
Ejemplo n.º 5
0
    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))
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
 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'))")
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
 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()
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
0
 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")