<s> import os , glob <EOL> from datetime import datetime <EOL> import logging <EOL> import shutil <EOL> import lxml . etree as ET <EOL> from zipfile import ZipFile <EOL> from django . http import HttpResponse <EOL> from explorer . utils . misc import ServerSettings <EOL> from django . conf import settings <EOL> from explorer . utils . misc import Response <EOL> from explorer . utils . adapter import Adapter <EOL> get_schema_rpc = '''<STR_LIT>''' <EOL> rpc = ET . Element ( "<STR_LIT>" ) <EOL> rpc . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> rpc . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def get_schema ( request , req , all = False ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( '<STR_LIT>' ) <EOL> req = req . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> req = req . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> protocol , device , fmt , payload = Adapter . parse_request ( req ) <EOL> if device . get ( '<STR_LIT:host>' , None ) is None : <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> rpc_xml = ET . fromstring ( get_schema_rpc ) <EOL> xml = Adapter . run_netconf ( request . user . username , device , rpc_xml ) <EOL> if xml is None : <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if xml . text is not None : <EOL> if xml . text . find ( '<STR_LIT:error>' ) : <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , xml . text ) ) <EOL> sclist = xml [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> schemas = ET . Element ( '<STR_LIT>' ) <EOL> for sc in sclist : <EOL> schema = ET . Element ( '<STR_LIT>' ) <EOL> id = sc [ <NUM_LIT:0> ] . text <EOL> ver = sc [ <NUM_LIT:1> ] . text <EOL> if all == False : <EOL> if id . isupper ( ) or '<STR_LIT>' in id or '<STR_LIT>' in id : <EOL> continue <EOL> schema . set ( '<STR_LIT:name>' , id ) <EOL> if ver is None : <EOL> schema . set ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> else : <EOL> schema . set ( '<STR_LIT:version>' , ver ) <EOL> unmatched = validate_schema ( request . user . username , id , ver ) <EOL> if unmatched is not None : <EOL> schema . set ( '<STR_LIT>' , unmatched ) <EOL> schemas . append ( schema ) <EOL> return HttpResponse ( Response . success ( '<STR_LIT>' , '<STR_LIT>' , xml = schemas ) ) <EOL> def download_yang ( request , req ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( '<STR_LIT>' ) <EOL> req = req . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> req = req . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> protocol , device , fmt , payload = Adapter . parse_request ( req ) <EOL> if device . get ( '<STR_LIT:host>' , None ) is None : <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> session_dir = ServerSettings . schema_path ( request . session . session_key ) <EOL> if not os . path . exists ( session_dir ) : <EOL> os . makedirs ( session_dir ) <EOL> if not os . path . exists ( session_dir ) : <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for fname in os . listdir ( session_dir ) : <EOL> if fname . endswith ( '<STR_LIT>' ) : <EOL> fn = os . path . join ( session_dir , fname ) <EOL> os . remove ( fn ) <EOL> modules = ET . Element ( '<STR_LIT>' ) <EOL> reqxml = ET . fromstring ( req ) <EOL> schemas = reqxml . find ( '<STR_LIT>' ) <EOL> for sc in schemas : <EOL> id = sc . text <EOL> module = ET . Element ( '<STR_LIT>' ) <EOL> get_sc = ET . Element ( '<STR_LIT>' ) <EOL> get_sc . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> identifier = ET . Element ( "<STR_LIT>" ) <EOL> sfile = id . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> identifier . text = sfile <EOL> module . text = id + '<STR_LIT>' <EOL> get_sc . append ( identifier ) <EOL> rpc . append ( get_sc ) <EOL> modules . append ( module ) <EOL> schema = Adapter . run_netconf ( request . user . username , device , rpc ) <EOL> fname = os . path . join ( session_dir , id + '<STR_LIT>' ) <EOL> with open ( fname , '<STR_LIT:w>' ) as f : <EOL> f . write ( schema [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . text ) <EOL> rpc . remove ( get_sc ) <EOL> return modules <EOL> def download_schema ( request , req ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( '<STR_LIT>' ) <EOL> modules = download_yang ( request , req ) <EOL> session_dir = ServerSettings . schema_path ( request . session . session_key ) <EOL> http_host = request . META [ '<STR_LIT>' ] <EOL> current = str ( datetime . now ( ) ) <EOL> current = current . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:->' ) <EOL> current = current [ : current . rindex ( '<STR_LIT:.>' ) ] <EOL> zfname = '<STR_LIT>' + current + '<STR_LIT>' <EOL> zfile = session_dir + '<STR_LIT:/>' + zfname <EOL> homedir = os . getcwd ( ) <EOL> os . chdir ( session_dir ) <EOL> with ZipFile ( zfile , "<STR_LIT:w>" ) as lz : <EOL> for f in glob . glob ( "<STR_LIT>" ) : <EOL> lz . write ( f ) <EOL> os . remove ( f ) <EOL> if not lz . namelist ( ) : <EOL> os . remove ( zfile ) <EOL> os . chdir ( homedir ) <EOL> url = '<STR_LIT>' + http_host + '<STR_LIT:/>' + '<STR_LIT>' + request . session . session_key + '<STR_LIT:/>' + zfname <EOL> return HttpResponse ( Response . success ( '<STR_LIT>' , msg = url ) ) <EOL> def add_schema ( request , req ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( '<STR_LIT>' ) <EOL> modules = download_yang ( request , req ) <EOL> return HttpResponse ( Response . success ( '<STR_LIT>' , '<STR_LIT>' , xml = modules ) ) <EOL> def validate_schema ( user , name , version ) : <EOL> if version is None : <EOL> fn = os . path . join ( ServerSettings . yang_path ( user ) , name + '<STR_LIT>' ) <EOL> else : <EOL> fn = os . path . join ( ServerSettings . yang_path ( user ) , name + '<STR_LIT:@>' + version + '<STR_LIT>' ) <EOL> if os . path . exists ( fn ) : <EOL> return None <EOL> dirpath = os . path . join ( settings . BASE_DIR , ServerSettings . yang_path ( user ) ) <EOL> sfile = os . path . basename ( fn . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] ) <EOL> if not sfile . endswith ( '<STR_LIT>' ) : <EOL> sfile = sfile + '<STR_LIT>' <EOL> for file in os . listdir ( dirpath ) : <EOL> yfile = os . path . basename ( file . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] ) <EOL> if not yfile . endswith ( '<STR_LIT>' ) : <EOL> yfile = yfile + '<STR_LIT>' <EOL> if sfile == yfile : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' </s>
