コード例 #1
0
ファイル: uploader.py プロジェクト: center3d/yang-explorer
def upload_file(_file, directory):
    """ Upload yang model into session storage """
    f = None
    filename = None
    try:
        if not os.path.exists(directory):
            logging.debug('Creating session storage ..')
            os.makedirs(directory)

        if not os.path.exists(directory):
            logging.error('Failed to create session storage ..')
            return None

        logging.debug('Copying file content ..')
        f = tempfile.NamedTemporaryFile('w+', suffix='.py', dir=directory, delete=False)
        fname = f.name
        for chunk in _file.chunks():
            f.write(chunk)
        f.close()

        parser = Parser(fname)
        target_file = os.path.join(directory, parser.get_filename())
        os.rename(fname, target_file)
        filename = parser.get_filename()
    except:
        logging.exception('Failed to upload file: ')
    finally:
        logging.debug('Cleaning up ..')
        if f is not None and os.path.exists(f.name):
            logging.debug('Deleting ' + f.name)
            os.remove(f.name)

    return filename
コード例 #2
0
ファイル: schema.py プロジェクト: CiscoDevNet/yang-explorer
def download_helper(username, device, dest, rpc, models):
    """Download list of models in destination directory from device"""
    if not models:
        return

    logger.info("Downloading " + str(models))

    identifier = rpc[0][0]
    dep_models = set()

    # dowload all models in the list
    for modelname in models:
        identifier.text = modelname.split("@")[0]
        fname = os.path.join(dest, identifier.text + ".yang")

        if not os.path.exists(fname):
            schema = Adapter.run_netconf(username, device, rpc)

            # write to file
            with open(fname, "w") as f:
                f.write(schema[0][0].text)

            # calculate dependency
            parser = Parser(fname)
            dep_models |= set(parser.get_dependency())

    # recursively download dependency
    download_helper(username, device, dest, rpc, dep_models)
コード例 #3
0
ファイル: uploader.py プロジェクト: montgok/yang-explorer
def upload_file(_file, directory):
    """ Upload yang model into session storage """
    f = None
    filename = None
    try:
        if not os.path.exists(directory):
            logging.debug('Creating session storage ..')
            os.makedirs(directory)

        if not os.path.exists(directory):
            logging.error('Failed to create session storage ..')
            return None

        logging.debug('Copying file content ..')
        f = tempfile.NamedTemporaryFile('w+', suffix='.py', dir=directory, delete=False)
        fname = f.name
        for chunk in _file.chunks():
            f.write(chunk)
        f.close()

        parser = Parser(fname)
        target_file = os.path.join(directory, parser.get_filename())
        os.rename(fname, target_file)
        filename = parser.get_filename()
    except:
        logging.exception('Failed to upload file: ')
    finally:
        logging.debug('Cleaning up ..')
        if f is not None and os.path.exists(f.name):
            logging.debug('Deleting ' + f.name)
            os.remove(f.name)

    return filename
コード例 #4
0
def download_helper(username, device, dest, rpc, models):
    """Download list of models in destination directory from device"""
    if not models:
        return

    logger.info('Downloading ' + str(models))

    identifier = rpc[0][0]
    dep_models = set()

    # dowload all models in the list
    for modelname in models:
        identifier.text = modelname.split('@')[0]
        fname = os.path.join(dest, identifier.text + '.yang')

        if not os.path.exists(fname):
            schema = Adapter.run_netconf(username, device, rpc)

            # write to file
            with open(fname, 'w') as f:
                f.write(schema[0][0].text)

            # calculate dependency
            parser = Parser(fname)
            dep_models |= set(parser.get_dependency())

    # recursively download dependency
    download_helper(username, device, dest, rpc, dep_models)
コード例 #5
0
def download_yang(request, req):
    """
    This API download yang schema from device
    """
    logger.debug('Download Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(
            Response.error('download', 'Netconf agent address missing!!'))

    # clear session directory if it exists
    Uploader.clear_upload_files(None, request.session.session_key)

    # create session directory if it does not exist
    session_dir = Uploader.create_session_storage(request.session.session_key)
    if session_dir is None:
        logger.error('download_yang: Invalid session')
        return HttpResponse(Response.error('download', 'Invalid session_id'))

    # extact list of models from request
    req_xml = ET.fromstring(req)
    models = [sc.text.strip() for sc in req_xml.find('schemas')]

    # download all models recursively
    rpc = ET.fromstring(get_schema_rpc)
    download_helper(request.user.username, device, session_dir, rpc, models)

    # prepare list of downloaded models
    modules = ET.Element('modules')
    for _file in glob.glob(os.path.join(session_dir, '*.yang')):

        # see if we need to rename file with revision date
        parser = Parser(_file)
        new_fname = os.path.join(session_dir, parser.get_filename())
        if _file != new_fname:
            os.rename(_file, new_fname)

        module = ET.Element('module')
        module.text = os.path.basename(new_fname)
        modules.append(module)
    return modules
コード例 #6
0
ファイル: schema.py プロジェクト: CiscoDevNet/yang-explorer
def download_yang(request, req):
    """
    This API download yang schema from device
    """
    logger.debug("Download Yang Schema")

    req = req.replace("<metadata>", "")
    req = req.replace("</metadata>", "")

    protocol, device, fmt, _, payload = Adapter.parse_request(req)
    if device.get("host", None) is None:
        return HttpResponse(Response.error("download", "Netconf agent address missing!!"))

    # clear session directory if it exists
    Uploader.clear_upload_files(None, request.session.session_key)

    # create session directory if it does not exist
    session_dir = Uploader.create_session_storage(request.session.session_key)
    if session_dir is None:
        logger.error("download_yang: Invalid session")
        return HttpResponse(Response.error("download", "Invalid session_id"))

    # extact list of models from request
    req_xml = ET.fromstring(req)
    models = [sc.text.strip() for sc in req_xml.find("schemas")]

    # download all models recursively
    rpc = ET.fromstring(get_schema_rpc)
    download_helper(request.user.username, device, session_dir, rpc, models)

    # prepare list of downloaded models
    modules = ET.Element("modules")
    for _file in glob.glob(os.path.join(session_dir, "*.yang")):

        # see if we need to rename file with revision date
        parser = Parser(_file)
        new_fname = os.path.join(session_dir, parser.get_filename())
        if _file != new_fname:
            os.rename(_file, new_fname)

        module = ET.Element("module")
        module.text = os.path.basename(new_fname)
        modules.append(module)
    return modules