Пример #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 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
Пример #5
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)
Пример #6
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)