Пример #1
0
def commit_files(user, session):
    """ Moves compiled yang moudles to user's yang directory """

    directory = ServerSettings.session_path(session)
    if not os.path.exists(directory):
        logging.error('Session storage %s does not exist' % directory)
        return False, None

    yangdst = ServerSettings.yang_path(user)
    cxmldst = ServerSettings.cxml_path(user)
    count = 0

    if not os.path.exists(yangdst):
        logging.debug('Created ' + yangdst)
        os.makedirs(yangdst)

    if not os.path.exists(cxmldst):
        logging.debug('Created ' + cxmldst)
        os.makedirs(cxmldst)

    modules = ET.Element('modules')
    for cxmlpath in glob.glob(os.path.join(directory, '*.xml')):
        basename = os.path.basename(cxmlpath)
        if basename == 'dependencies.xml':
            continue
        base = os.path.splitext(basename)[0]
        yang_src_path = os.path.join(directory, base + '.yang')
        yang_dst_path = os.path.join(yangdst, base + '.yang')
        cxml_dst_path = os.path.join(cxmldst, base + '.xml')
        logging.debug('Committing ' + yang_src_path)
        if os.path.exists(yang_src_path):
            logging.debug('Commit ' + yang_dst_path)
            _clean_oldfiles(yangdst, base)
            _clean_oldfiles(cxmldst, base)
            os.rename(yang_src_path, yang_dst_path)
            os.rename(cxmlpath, cxml_dst_path)
            module = ET.Element('module')
            module.text = base + '.yang'
            modules.append(module)
            count += 1

    # There is a update in yang modules, delete existing dependency file
    # so that it will be recompiled next time
    if count > 0:
        session_d = os.path.join(directory, 'dependencies.xml')
        if os.path.exists(session_d):
            logging.debug('Moving dependency file ...')
            os.rename(session_d, os.path.join(yangdst, 'dependencies.xml'))
        else:
            logging.debug('Compiling user dependency ...')
            Compiler.compile_pyimport(user)

        # added module might affect existing module, recompile them
        _compile_dependecies(user, [m.text for m in modules], None)

    logging.debug('Committed ' + str(count) + ' file(s)')
    return True, modules
Пример #2
0
def commit_files(user, session):
    """ Moves compiled yang moudles to user's yang directory """

    directory = ServerSettings.session_path(session)
    if not os.path.exists(directory):
        logging.error('Session storage %s does not exist' % directory)
        return False, None

    yangdst = ServerSettings.yang_path(user)
    cxmldst = ServerSettings.cxml_path(user)
    count = 0

    if not os.path.exists(yangdst):
        logging.debug('Created ' + yangdst)
        os.makedirs(yangdst)

    if not os.path.exists(cxmldst):
        logging.debug('Created ' + cxmldst)
        os.makedirs(cxmldst)

    modules = ET.Element('modules')
    for cxmlpath in glob.glob(os.path.join(directory, '*.xml')):
        basename = os.path.basename(cxmlpath)
        if basename == 'dependencies.xml':
            continue
        base = os.path.splitext(basename)[0]
        yang_src_path = os.path.join(directory, base + '.yang')
        yang_dst_path = os.path.join(yangdst, base + '.yang')
        cxml_dst_path = os.path.join(cxmldst, base + '.xml')
        logging.debug('Committing ' + yang_src_path)
        if os.path.exists(yang_src_path):
            logging.debug('Commit ' + yang_dst_path)
            _clean_oldfiles(yangdst, base)
            _clean_oldfiles(cxmldst, base)
            os.rename(yang_src_path, yang_dst_path)
            os.rename(cxmlpath, cxml_dst_path)
            module = ET.Element('module')
            module.text = base + '.yang'
            modules.append(module)
            count += 1

    # There is a update in yang modules, delete existing dependency file
    # so that it will be recompiled next time
    if count > 0:
        session_d = os.path.join(directory, 'dependencies.xml')
        if os.path.exists(session_d):
            logging.debug('Moving dependency file ...')
            os.rename(session_d, os.path.join(yangdst, 'dependencies.xml'))
        else:
            logging.debug('Compiling user dependency ...')
            Compiler.compile_pyimport(user)

        # added module might affect existing module, recompile them
        _compile_dependecies(user, [m.text for m in modules], None)

    logging.debug('Committed ' + str(count) + ' file(s)')
    return True, modules
Пример #3
0
def sync_file(user, session, filename, index):
    """ Compile yang module """
    if index == '0':
        logging.debug('Compiling session dependency ...')
        Compiler.compile_pyimport(user, session)

    _file = os.path.join(_get_session_path(session), filename)
    if os.path.exists(_file):
        (rc, msgs) = Compiler.compile_cxml(user, session, _file)
    else:
        logging.error('sync_file: File %s not found ' % filename)
        (rc, msgs) = (False, None)
    return (rc, msgs)
Пример #4
0
def sync_file(user, session, filename, index):
    """ Compile yang module """
    if index == '0':
        logging.debug('Compiling session dependency ...')
        (rc, msg) = Compiler.compile_pyimport(user, session)
        if not rc:
            return (rc, msg)

    _file = os.path.join(ServerSettings.session_path(session), filename)
    if os.path.exists(_file):
        (rc, msgs) = Compiler.compile_cxml(user, session, _file)
    else:
        logging.error('sync_file: File %s not found ' % filename)
        (rc, msgs) = (False, None)
    return (rc, msgs)
Пример #5
0
def dependencies_graph(username, modules=[]):
    depfile = os.path.join(ServerSettings.yang_path(username),
                           'dependencies.xml')
    if not os.path.exists(depfile):
        (rc, msg) = Compiler.compile_pyimport(username, None)
        if not rc:
            return rc, msg

    dgraph = DYGraph(depfile)
    g = dgraph.digraph([m.text.split('.yang')[0] for m in modules])
    if g is None:
        return (
            False,
            """Failed to generate dependency graph, please make sure that grapviz
python package is installed !!""")

    try:
        g.render(filename=os.path.join(settings.BASE_DIR, 'static', 'graph'))
    except:
        return (
            False,
            """Failed to render dependency graph, please make sure that grapviz
binaries (http://www.graphviz.org/Download.php) are installed on
the server !!""")

    return True, g.comment
Пример #6
0
def _compile_dependecies(user, modules):
    """ Compile affected modules """
    logging.debug('Compiling dependency ..')
    
    yangdst = _get_yang_path(user)
    dfile = os.path.join(yangdst, 'dependencies.xml')
    if not os.path.exists(dfile):
        logging.debug('Dependency file not found!!')
        return

    dmodules = Set([])
    dgraph = DYGraph(dfile)
    for m in modules:
        module = dgraph.dependency_module(m)
        if module is None:
            continue
        for name in module.imports:
            dmodules.add(name) 
        for name in module.depends:
            dmodules.add(name)

    dmodules_list = list(dmodules)
    for yangfile in glob.glob(os.path.join(yangdst, '*.yang')):
        basename = os.path.basename(yangfile)
        #skip dependency module itself
        if basename in modules:
            continue

        base = os.path.splitext(basename)[0]
        if '@' in base:
            base = base.split('@')[0]
        if base in dmodules_list:
            (rc, msgs) = Compiler.compile_cxml(user, None, yangfile)
Пример #7
0
def _compile_dependecies(user, modules, session=None):
    """ Compile affected modules """
    logging.debug('_compile_dependecies: enter')

    dmodules_list = Compiler.get_dependencies(user, modules, session)
    if not dmodules_list:
        logging.debug('_compile_dependecies: no dependency found !!')
        return

    # strip file path
    dmodules = []
    for m in dmodules_list:
        base_m = os.path.basename(m)
        base_m = os.path.splitext(base_m)[0]
        if '@' in base_m:
            base_m = base_m.split('@')[0]
        dmodules.append(base_m)

    yangdst = ServerSettings.yang_path(user)
    for yangfile in glob.glob(os.path.join(yangdst, '*.yang')):
        basename = os.path.basename(yangfile)

        # skip dependency module itself
        if basename in modules: continue

        base = os.path.splitext(basename)[0]
        if '@' in base: base = base.split('@')[0]

        if base in dmodules:
            # ignore some common files
            if base in ignore_list:
                logging.debug('Compile dependency: ignoring ' + base)
                continue
            Compiler.compile_cxml(user, None, yangfile)

    logging.debug('_compile_dependecies: done')
Пример #8
0
def _compile_dependecies(user, modules, session=None):
    """ Compile affected modules """
    logging.debug('_compile_dependecies: enter')

    dmodules_list = Compiler.get_dependencies(user, modules, session)
    if not dmodules_list:
        logging.debug('_compile_dependecies: no dependency found !!')
        return

    #strip file path
    dmodules = []
    for m in dmodules_list:
        base_m = os.path.basename(m)
        base_m = os.path.splitext(base_m)[0]
        if '@' in base_m:
            base_m = base_m.split('@')[0]
        dmodules.append(base_m)

    yangdst = ServerSettings.yang_path(user)
    for yangfile in glob.glob(os.path.join(yangdst, '*.yang')):
        basename = os.path.basename(yangfile)

        #skip dependency module itself
        if basename in modules: continue

        base = os.path.splitext(basename)[0]
        if '@' in base: base = base.split('@')[0]

        if base in dmodules:
            # ignore some common files
            if base in ignore_list:
                logging.debug('Compile dependency: ignoring ' + base)
                continue
            Compiler.compile_cxml(user, None, yangfile)

    logging.debug('_compile_dependecies: done')
Пример #9
0
def dependencies_graph(username, modules=[]):
    depfile = os.path.join(ServerSettings.yang_path(username), 'dependencies.xml')
    if not os.path.exists(depfile):
        (rc, msg) = Compiler.compile_pyimport(username, None)
        if not rc: return (rc, msg)

    dgraph = DYGraph(depfile)
    g = dgraph.digraph([m.text.split('.yang')[0] for m in modules])
    if g is None:
        return (False, """Failed to generate dependency graph, please make sure that grapviz
python package is installed !!""")

    try:
        g.render(filename=os.path.join(settings.BASE_DIR, 'static', 'graph'))
    except:
        return (False, """Failed to render dependency graph, please make sure that grapviz
binaries (http://www.graphviz.org/Download.php) are installed on
the server !!""")

    return (True, g.comment)