|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import time |
| 4 | + |
| 5 | +from optparse import make_option |
| 6 | +from pathlib import Path |
| 7 | +from StringIO import StringIO |
| 8 | +from textwrap import dedent |
| 9 | +from xym import xym |
| 10 | + |
| 11 | +from django.conf import settings |
| 12 | +from django.core.management.base import BaseCommand |
| 13 | + |
| 14 | +class Command(BaseCommand): |
| 15 | + """ |
| 16 | + Populate the yang models repositories from drafts and RFCs. |
| 17 | +
|
| 18 | + Extracts yang models from RFCs (found in settings.RFC_PATH and places |
| 19 | + them in settings.YANG_RFC_MODEL_DIR, and from active drafts, placed in |
| 20 | + settings.YANG_DRAFT_MODEL_DIR if valid and settings.YANG_INVAL_MODEL_DIR |
| 21 | + if not. |
| 22 | +
|
| 23 | + """ |
| 24 | + |
| 25 | + help = dedent(__doc__).strip() |
| 26 | + |
| 27 | + option_list = BaseCommand.option_list + ( |
| 28 | + make_option('--clean', |
| 29 | + action='store_true', dest='clean', default=False, |
| 30 | + help='Remove the current directory content before writing new models.'), |
| 31 | + ) |
| 32 | + |
| 33 | + def handle(self, *filenames, **options): |
| 34 | + """ |
| 35 | +
|
| 36 | + * All yang modules from published RFCs should be extracted and be |
| 37 | + available in an rfc-yang repository. |
| 38 | +
|
| 39 | + * All valid yang modules from active, not replaced, internet drafts |
| 40 | + should be extracted and be available in a draft-valid-yang repository. |
| 41 | +
|
| 42 | + * All, valid and invalid, yang modules from active, not replaced, |
| 43 | + internet drafts should be available in a draft-all-yang repository. |
| 44 | + (Actually, given precedence ordering, it would be enough to place |
| 45 | + non-validating modules in a draft-invalid-yang repository instead). |
| 46 | +
|
| 47 | + * In all cases, example modules should be excluded. |
| 48 | +
|
| 49 | + * Precedence is established by the search order of the repository as |
| 50 | + provided to pyang. |
| 51 | +
|
| 52 | + * As drafts expire, models should be removed in order to catch cases |
| 53 | + where a module being worked on depends on one which has slipped out |
| 54 | + of the work queue. |
| 55 | +
|
| 56 | + """ |
| 57 | + |
| 58 | + verbosity = int(options.get('verbosity')) |
| 59 | + |
| 60 | + def extract_from(file, dir, strict=True): |
| 61 | + saved_stderr = sys.stderr |
| 62 | + sys.stderr = StringIO() |
| 63 | + model_list = [] |
| 64 | + try: |
| 65 | + model_list = xym.xym(str(item), str(item.parent), str(dir), strict=strict, debug_level=(verbosity>1)) |
| 66 | + for name in model_list: |
| 67 | + modfile = moddir / name |
| 68 | + mtime = item.stat().st_mtime |
| 69 | + os.utime(str(modfile), (mtime, mtime)) |
| 70 | + if '"' in name: |
| 71 | + name = name.replace('"', '') |
| 72 | + modfile.rename(str(moddir/name)) |
| 73 | + model_list = [ n.replace('"','') for n in model_list ] |
| 74 | + except Exception as e: |
| 75 | + print("** Error when extracting from %s: %s" % (item, str(e))) |
| 76 | + sys.stderr = saved_stderr |
| 77 | + return model_list |
| 78 | + |
| 79 | + # Extract from new RFCs |
| 80 | + |
| 81 | + rfcdir = Path(settings.RFC_PATH) |
| 82 | + |
| 83 | + moddir = Path(settings.YANG_RFC_MODEL_DIR) |
| 84 | + if not moddir.exists(): |
| 85 | + moddir.mkdir(parents=True) |
| 86 | + |
| 87 | + latest = 0 |
| 88 | + for item in moddir.iterdir(): |
| 89 | + if item.stat().st_mtime > latest: |
| 90 | + latest = item.stat().st_mtime |
| 91 | + |
| 92 | + print("Extracting to %s ..." % moddir) |
| 93 | + for item in rfcdir.iterdir(): |
| 94 | + if item.is_file() and item.name.startswith('rfc') and item.name.endswith('.txt'): |
| 95 | + if item.stat().st_mtime > latest: |
| 96 | + model_list = extract_from(item, moddir) |
| 97 | + for name in model_list: |
| 98 | + if name.startswith('ietf') or name.startswith('iana'): |
| 99 | + if verbosity > 1: |
| 100 | + print(" Extracted from %s: %s" % (item, name)) |
| 101 | + else: |
| 102 | + sys.stdout.write('.') |
| 103 | + sys.stdout.flush() |
| 104 | + else: |
| 105 | + modfile = moddir / name |
| 106 | + modfile.unlink() |
| 107 | + if verbosity > 1: |
| 108 | + print(" Skipped module from %s: %s" % (item, name)) |
| 109 | + print("") |
| 110 | + |
| 111 | + # Extract valid modules from drafts |
| 112 | + |
| 113 | + six_months_ago = time.time() - 6*31*24*60*60 |
| 114 | + def active(item): |
| 115 | + return item.stat().st_mtime > six_months_ago |
| 116 | + |
| 117 | + draftdir = Path(settings.INTERNET_DRAFT_PATH) |
| 118 | + |
| 119 | + moddir = Path(settings.YANG_DRAFT_MODEL_DIR) |
| 120 | + if not moddir.exists(): |
| 121 | + moddir.mkdir(parents=True) |
| 122 | + print("Emptying %s ..." % moddir) |
| 123 | + for item in moddir.iterdir(): |
| 124 | + item.unlink() |
| 125 | + |
| 126 | + print("Extracting to %s ..." % moddir) |
| 127 | + for item in draftdir.iterdir(): |
| 128 | + if item.is_file() and item.name.startswith('draft') and item.name.endswith('.txt') and active(item): |
| 129 | + model_list = extract_from(item, moddir) |
| 130 | + for name in model_list: |
| 131 | + if not name.startswith('example'): |
| 132 | + if verbosity > 1: |
| 133 | + print(" Extracted valid module from %s: %s" % (item, name)) |
| 134 | + else: |
| 135 | + sys.stdout.write('.') |
| 136 | + sys.stdout.flush() |
| 137 | + else: |
| 138 | + modfile = moddir / name |
| 139 | + modfile.unlink() |
| 140 | + if verbosity > 1: |
| 141 | + print(" Skipped module from %s: %s" % (item, name)) |
| 142 | + print("") |
| 143 | + |
| 144 | + # Extract invalid modules from drafts |
| 145 | + valdir = moddir |
| 146 | + moddir = Path(settings.YANG_INVAL_MODEL_DIR) |
| 147 | + if not moddir.exists(): |
| 148 | + moddir.mkdir(parents=True) |
| 149 | + print("Emptying %s ..." % moddir) |
| 150 | + for item in moddir.iterdir(): |
| 151 | + item.unlink() |
| 152 | + |
| 153 | + print("Extracting to %s ..." % moddir) |
| 154 | + for item in draftdir.iterdir(): |
| 155 | + if item.is_file() and item.name.startswith('draft') and item.name.endswith('.txt') and active(item): |
| 156 | + model_list = extract_from(item, moddir, strict=False) |
| 157 | + for name in model_list: |
| 158 | + modfile = moddir / name |
| 159 | + if (valdir/name).exists(): |
| 160 | + modfile.unlink() |
| 161 | + if verbosity > 1: |
| 162 | + print(" Skipped valid module from %s: %s" % (item, name)) |
| 163 | + elif not name.startswith('example'): |
| 164 | + if verbosity > 1: |
| 165 | + print(" Extracted invalid module from %s: %s" % (item, name)) |
| 166 | + else: |
| 167 | + sys.stdout.write('.') |
| 168 | + sys.stdout.flush() |
| 169 | + else: |
| 170 | + modfile.unlink() |
| 171 | + if verbosity > 1: |
| 172 | + print(" Skipped module from %s: %s" % (item, name)) |
| 173 | + print("") |
0 commit comments