// connect to db
db = Configuration.getMongoConnection()
cwedb = db.cwe
info = db.info
// make parser
parser = make_parser()
ch = CWEHandler()
parser.setContentHandler(ch)
// check modification date
try:
f = Configuration.getFile(cwedict)
except:
sys.exit("Cannot open url %s. Bad URL or not connected to the internet?"%(cwedict))
lastmodified = f.headers["last-modified"]
i = info.find_one({"db": "cwe"})
if i is not None:
if f.headers["last-modified"] == i["last-modified"]:
print("Not modified")
sys.exit(0)
// preparing xml by saving in a tempfile and unzipping
tmpdir = tempfile.gettempdir()
tmpfile = tempfile.NamedTemporaryFile()
cwezip = open(tmpfile.name, "wb")
cwezip.write(f.read())
cwezip.close()
with zipfile.ZipFile(tmpfile.name) as z:
z.extractall(tmpdir)
z.close()
f = open(os.path.join(tmpdir, "cwec_v2.8.xml"))
// parse xml and store in database
parser.parse(f)
bulk = cwedb.initialize_ordered_bulk_op()
for cwe in progressbar(ch.cwe):
if args.v:
print (cwe)
bulk.find({"id": cwe["id"]}).upsert().update({"$set": {"name": cwe["name"], "id": cwe["id"], "status": cwe["status"], "weaknessabs": cwe["weaknessabs"]}})
bulk.execute()
//update database info after successful program-run
info.update({"db": "cwe"}, {"$set": {"last-modified": lastmodified}}, upsert=True)
After Change
except:
sys.exit("Cannot open url %s. Bad URL or not connected to the internet?"%(cwedict))
lastmodified = f.headers["last-modified"]
i = dbLayer.getLastModified("cwe")
if i is not None:
if lastmodified == i:
print("Not modified")
sys.exit(0)