示例#1
0
def test_compressor(fmt):
    comp_cls = loader.get_class(fmt)
    with temporary_file() as path:
        # Write samples
        with comp_cls(path, mode="w") as f:
            for sample in SAMPLE:
                f.write(sample)
        # Read result at once
        with comp_cls(path, mode="r") as f:
            data = f.read()
        assert isinstance(data, str)
        assert data == RESULT
        # Read result by lines
        with comp_cls(path, mode="r") as f:
            lines = list(f)
        assert len(lines) == 2
        assert "".join(lines) == RESULT
示例#2
0
def test_temporary_file(text):
    with temporary_file(text) as fn:
        assert os.path.exists(fn)
        data = read_file(fn)
    assert not os.path.exists(fn)
    assert data == text
示例#3
0
 def compile(self, data):
     """
     Compile MIB, upload to database and store MIB file
     :param data: MIB text
     :return:
     """
     if not config.path.smilint or not os.path.exists(config.path.smilint):
         return {
             "status": False,
             "msg": "smilint is missed",
             "error": ERR_MIB_TOOL_MISSED
         }
     if not config.path.smilint or not os.path.exists(config.path.smidump):
         return {
             "status": False,
             "msg": "smidump is missed",
             "error": ERR_MIB_TOOL_MISSED
         }
     # Put data to temporary file
     with temporary_file(data) as tmp_path:
         # Pass MIB through smilint to detect missed modules
         self.logger.debug(
             "Pass MIB through smilint to detect missed modules")
         f = subprocess.Popen([config.path.smilint, "-m", tmp_path],
                              stderr=subprocess.PIPE,
                              env=self.SMI_ENV).stderr
         for l in f:
             match = self.rx_module_not_found.search(l.strip())
             if match:
                 return {
                     "status": False,
                     "msg": "Required MIB missed: %s" % match.group(1),
                     "code": ERR_MIB_MISSED,
                 }
         self.logger.debug("Convert MIB to python module and load")
         # Convert MIB to python module and load
         with temporary_file() as py_path:
             subprocess.check_call(
                 [
                     config.path.smidump, "-k", "-q", "-f", "python", "-o",
                     py_path, tmp_path
                 ],
                 env=self.SMI_ENV,
             )
             with open(py_path) as f:
                 p_data = unicode(f.read(), "ascii",
                                  "ignore").encode("ascii")
             with open(py_path, "w") as f:
                 f.write(p_data)
             m = imp.load_source("mib", py_path)
         # NOW we can deduce module name
         mib_name = m.MIB["moduleName"]
         # Check module dependencies
         depends_on = {}  # MIB Name -> Object ID
         self.logger.debug("Check module dependencies: %s",
                           m.MIB.get("imports", ""))
         if "imports" in m.MIB:
             for i in m.MIB["imports"]:
                 if "module" not in i:
                     continue
                 rm = i["module"]
                 if rm in depends_on:
                     continue
                 md = MIB.get_by_name(rm)
                 if md is None:
                     return {
                         "status": False,
                         "msg": "Required MIB missed: %s" % rm,
                         "code": ERR_MIB_MISSED,
                     }
                 depends_on[rm] = md
         # Get MIB latest revision date
         try:
             last_updated = datetime.datetime.strptime(
                 sorted([x["date"]
                         for x in m.MIB[mib_name]["revisions"]])[-1],
                 "%Y-%m-%d %H:%M")
         except ValueError:
             last_updated = datetime.datetime(year=1970, month=1, day=1)
         self.logger.debug("Extract MIB typedefs")
         # Extract MIB typedefs
         typedefs = {}
         if "typedefs" in m.MIB:
             for t in m.MIB["typedefs"]:
                 typedefs[t] = MIB.parse_syntax(m.MIB["typedefs"][t])
         # Check mib already uploaded
         mib_description = m.MIB[mib_name].get("description", None)
         mib = MIB.objects.filter(name=mib_name).first()
         if mib is not None:
             mib.description = mib_description
             mib.last_updated = last_updated
             mib.depends_on = sorted(depends_on)
             mib.typedefs = typedefs
             mib.save()
             # Delete all MIB Data
             mib.clean()
         else:
             # Create MIB
             mib = MIB(
                 name=mib_name,
                 description=mib_description,
                 last_updated=last_updated,
                 depends_on=sorted(depends_on),
                 typedefs=typedefs,
             )
             mib.save()
         # Upload MIB data
         cdata = []
         for i in ["nodes", "notifications"]:
             if i in m.MIB:
                 cdata += [{
                     "name":
                     "%s::%s" % (mib_name, node),
                     "oid":
                     v["oid"],
                     "description":
                     v.get("description"),
                     "syntax":
                     v["syntax"]["type"] if "syntax" in v else None,
                 } for node, v in six.iteritems(m.MIB[i])]
         mib.load_data(cdata)
         # Move file to permanent place
         safe_rewrite(self.get_path(mib_name), data)
     return {"status": True, "mib": mib_name}
示例#4
0
def test_file_instance(fmt):
    comp_cls = loader.get_class(fmt)
    with temporary_file() as path:
        # Write samples
        with comp_cls(path, mode="w") as f:
            assert isinstance(f, io.TextIOWrapper)
示例#5
0
def test_base_close():
    with temporary_file() as path:
        with pytest.raises(NotImplementedError):
            BaseCompressor(path).close()