예제 #1
0
    def add_parsed_module(self, module):
        if module is None:
            return None
        if module.arg is None:
            error.err_add(self.errors, module.pos,
                          'EXPECTED_ARGUMENT', module.keyword)
            return None
        top_keywords = ['module', 'submodule']
        if module.keyword not in top_keywords:
            error.err_add(self.errors, module.pos,
                          'UNEXPECTED_KEYWORD_N', (module.keyword, top_keywords))
            return None

        rev = util.get_latest_revision(module)
        if (module.arg, rev) in self.modules:
            other = self.modules[(module.arg, rev)]
            if (hasattr(other, 'i_adler32') and
                hasattr(module, 'i_adler32') and
                other.i_adler32 != module.i_adler32):
                error.err_add(self.errors, module.pos,
                              'DUPLICATE_MODULE', (module.arg, other.pos))
                return None
            # exactly same module
            return other

        self.modules[(module.arg, rev)] = module
        statements.validate_module(self, module)

        return module
예제 #2
0
    def add_parsed_module(self, module):
        if module is None:
            return None
        if module.arg is None:
            error.err_add(self.errors, module.pos, 'EXPECTED_ARGUMENT',
                          module.keyword)
            return None
        top_keywords = ['module', 'submodule']
        if module.keyword not in top_keywords:
            error.err_add(self.errors, module.pos, 'UNEXPECTED_KEYWORD_N',
                          (module.keyword, top_keywords))
            return None

        rev = util.get_latest_revision(module)
        if (module.arg, rev) in self.modules:
            other = self.modules[(module.arg, rev)]
            if (hasattr(other, 'i_adler32') and hasattr(module, 'i_adler32')
                    and other.i_adler32 != module.i_adler32):
                error.err_add(self.errors, module.pos, 'DUPLICATE_MODULE',
                              (module.arg, other.pos))
                return None
            # exactly same module
            return other

        self.modules[(module.arg, rev)] = module
        statements.validate_module(self, module)

        return module
예제 #3
0
    def _ensure_revs(self, revs):
        i = 0
        length = len(revs)
        while i < length:
            (rev, handle) = revs[i]
            if rev is None:
                # now we must read the revision from the module
                try:
                    r = self.repository.get_module_from_handle(handle)
                except self.repository.ReadError, ex:
                    continue
                (ref, format, text) = r

                if format == None:
                    format = util.guess_format(text)
                    
                if format == 'yin':
                    p = yin_parser.YinParser({'no_include':True})
                else:
                    p = yang_parser.YangParser()

                module = p.parse(self, ref, text)
                if module is not None:
                    rev = util.get_latest_revision(module)
                    revs[i] = (rev, ('parsed', module, ref))
            i += 1
예제 #4
0
    def _ensure_revs(self, revs):
        i = 0
        length = len(revs)
        while i < length:
            (rev, handle) = revs[i]
            if rev is None:
                # now we must read the revision from the module
                try:
                    r = self.repository.get_module_from_handle(handle)
                except self.repository.ReadError, ex:
                    i += 1
                    continue
                (ref, format, text) = r

                if format == None:
                    format = util.guess_format(text)

                if format == 'yin':
                    p = yin_parser.YinParser({
                        'no_include': True,
                        'no_extensions': True
                    })
                else:
                    p = yang_parser.YangParser()

                module = p.parse(self, ref, text)
                if module is not None:
                    rev = util.get_latest_revision(module)
                    revs[i] = (rev, ('parsed', module, ref))
            i += 1
예제 #5
0
    def add_module(self,
                   ref,
                   text,
                   format=None,
                   expect_modulename=None,
                   expect_revision=None,
                   expect_failure_error=True):
        """Parse a module text and add the module data to the context

        `ref` is a string which is used to identify the source of
              the text for the user.  used in error messages
        `text` is the raw text data
        `format` is one of 'yang' or 'yin'.

        Returns the parsed and validated module on success, and None on error.
        """
        if format == None:
            format = util.guess_format(text)

        if format == 'yin':
            p = yin_parser.YinParser()
        else:
            p = yang_parser.YangParser()

        module = p.parse(self, ref, text)
        if module is None:
            return None

        if expect_modulename is not None and expect_modulename != module.arg:
            if expect_failure_error:
                error.err_add(self.errors, module.pos, 'BAD_MODULE_NAME',
                              (module.arg, ref, expect_modulename))
                return None
            else:
                error.err_add(self.errors, module.pos, 'WBAD_MODULE_NAME',
                              (module.arg, ref, expect_modulename))
        if expect_revision is not None:
            latest_rev = util.get_latest_revision(module)
            if expect_revision != latest_rev:
                if expect_failure_error:
                    error.err_add(self.errors, module.pos, 'BAD_REVISION',
                                  (latest_rev, ref, expect_revision))
                    return None
                else:
                    error.err_add(self.errors, module.pos, 'WBAD_REVISION',
                                  (latest_rev, ref, expect_revision))

        module.i_adler32 = zlib.adler32(text)
        return self.add_parsed_module(module)
    def add_module(self, ref, text, format=None,
                   expect_modulename=None, expect_revision=None,
                   expect_failure_error=True):
        """Parse a module text and add the module data to the context

        `ref` is a string which is used to identify the source of
              the text for the user.  used in error messages
        `text` is the raw text data
        `format` is one of 'yang' or 'yin'.

        Returns the parsed and validated module on success, and None on error.
        """
        if format == None:
            format = util.guess_format(text)

        if format == 'yin':
            p = yin_parser.YinParser()
        else:
            p = yang_parser.YangParser()

        module = p.parse(self, ref, text)
        if module is None:
            return None

        if expect_modulename is not None and expect_modulename != module.arg:
            if expect_failure_error:
                error.err_add(self.errors, module.pos, 'BAD_MODULE_NAME',
                              (module.arg, ref, expect_modulename))
                return None
            else:
                error.err_add(self.errors, module.pos, 'WBAD_MODULE_NAME',
                              (module.arg, ref, expect_modulename))
        if expect_revision is not None:
            latest_rev = util.get_latest_revision(module)
            if expect_revision != latest_rev:
                if expect_failure_error:
                    error.err_add(self.errors, module.pos, 'BAD_REVISION',
                                  (latest_rev, ref, expect_revision))
                    return None
                else:
                    error.err_add(self.errors, module.pos, 'WBAD_REVISION',
                                  (latest_rev, ref, expect_revision))

        module.i_adler32 = zlib.adler32(text)
        return self.add_parsed_module(module)
예제 #7
0
 def del_module(self, module):
     """Remove a module from the context"""
     rev = util.get_latest_revision(module)
     del self.modules[(module.arg, rev)]
예제 #8
0
            ref = handle[2]
        else:
            # get it from the repos
            try:
                r = self.repository.get_module_from_handle(handle)
                (ref, format, text) = r
                module = self.add_module(ref, text, format)
            except self.repository.ReadError, ex:
                error.err_add(self.errors, pos, 'READ_ERROR', str(ex))
                return None
        if module == None:
            return None
        if modulename != module.arg:
            error.err_add(self.errors, module.pos, 'BAD_MODULE_FILENAME',
                          (module.arg, ref, modulename))
            rev = util.get_latest_revision(module)
            self.del_module(module)
            self.modules[(modulename, rev)] = None
            return None
        return module

    def read_module(self, modulename, revision=None, extra={}):
        """Searches for a module named `modulename` in the repository

        The module is just read, and not compiled at all.
        Returns the module if found, and None otherwise"""

        if modulename not in self.revs:
            # this module doesn't exist in the repos at all
            return None
        elif self.revs[modulename] == []:
예제 #9
0
class FileRepository(Repository):
    def __init__(self, path=""):
        """Create a Repository which searches the filesystem for modules

        `path` is a `os.pathsep`-separated string of directories
        """

        Repository.__init__(self)
        self.dirs = string.split(path, os.pathsep)

        # add standard search path
        self.dirs.append('.')

        modpath = os.getenv('YANG_MODPATH')
        if modpath is not None:
            self.dirs.extend(string.split(modpath, os.pathsep))

        home = os.getenv('HOME')
        if home is not None:
            self.dirs.append(os.path.join(home, 'yang', 'modules'))

        inst = os.getenv('YANG_INSTALL')
        if inst is not None:
            self.dirs.append(os.path.join(inst, 'yang', 'modules'))
        else:
            self.dirs.append(
                os.path.join(sys.prefix, 'share', 'yang', 'modules'))

        self.modules = None

    def _setup(self, ctx):
        # check all dirs for yang and yin files
        self.modules = []
        r = re.compile(r"^(.*?)(\@(\d{4}-\d{2}-\d{2}))?\.(yang|yin)$")
        for d in self.dirs:
            try:
                files = os.listdir(d)
            except OSError:
                files = []
            for fname in files:
                m = r.search(fname)
                if m is not None:
                    (name, _dummy, rev, format) = m.groups()
                    absfilename = os.path.join(d, fname)
                    if absfilename.startswith("./"):
                        absfilename = absfilename[2:]
                    handle = (format, absfilename)
                    self.modules.append((name, rev, handle))

    # FIXME: bad strategy; when revisions are not used in the filename
    # this code parses all modules :(  need to do this lazily
    def _peek_revision(self, absfilename, format, ctx):
        try:
            fd = file(absfilename)
            text = fd.read()
        except IOError, ex:
            return None
        if format == 'yin':
            p = yin_parser.YinParser()
        else:
            p = yang_parser.YangParser()

        # FIXME: optimization - do not parse the entire text
        # just to read the revisions...
        module = p.parse(ctx, absfilename, text)
        if module is None:
            return None
        return (util.get_latest_revision(module), module)
예제 #10
0
 def del_module(self, module):
     """Remove a module from the context"""
     rev = util.get_latest_revision(module)
     del self.modules[(module.arg, rev)]
예제 #11
0
            ref = handle[2]
        else:
            # get it from the repos
            try:
                r = self.repository.get_module_from_handle(handle)
                (ref, format, text) = r
                module = self.add_module(ref, text, format)
            except self.repository.ReadError, ex:
                error.err_add(self.errors, pos, 'READ_ERROR', str(ex))
                return None
        if module == None:
            return None
        if modulename != module.arg:
            error.err_add(self.errors, module.pos, 'BAD_MODULE_FILENAME',
                          (module.arg, ref, modulename))
            rev = util.get_latest_revision(module)
            self.del_module(module)
            self.modules[(modulename, rev)] = None
            return None
        return module

    def read_module(self, modulename, revision=None, extra={}):
        """Searches for a module named `modulename` in the repository

        The module is just read, and not compiled at all.
        Returns the module if found, and None otherwise"""

        if modulename not in self.revs:
            # this module doesn't exist in the repos at all
            return None
        elif self.revs[modulename] == []: