2020-06-06 17:46:08 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
sys.path.append("..")
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dnmapper.settings")
|
|
|
|
import django
|
|
|
|
django.setup()
|
|
|
|
|
2020-06-14 00:24:18 +02:00
|
|
|
from backend.crawler import convert_crawl, net_to_json
|
2020-06-06 17:46:08 +02:00
|
|
|
from bgpdata.models import CrawlRun
|
|
|
|
|
|
|
|
|
2020-06-14 03:36:42 +02:00
|
|
|
def _convert_crawl(crawl):
|
|
|
|
net = convert_crawl(crawl)
|
|
|
|
if net.nodes and net.edges:
|
|
|
|
crawl.graph = net_to_json(net)
|
|
|
|
crawl.save()
|
|
|
|
print("Crawl {} updated".format(crawl.id))
|
|
|
|
else:
|
|
|
|
print("Crawl {} had no nodes or edges, abort".format(crawl.id))
|
|
|
|
|
|
|
|
|
2020-06-06 17:46:08 +02:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-c", "--crawl-id", type=int)
|
2020-06-14 03:36:42 +02:00
|
|
|
parser.add_argument("-a", "--all", default=False, action="store_true")
|
|
|
|
parser.add_argument("-e", "--empty-graph-only", default=False, action="store_true")
|
2020-06-06 17:46:08 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-06-14 03:36:42 +02:00
|
|
|
if args.crawl_id and args.all:
|
|
|
|
parser.error("-c and -a don't work together")
|
2020-06-06 17:46:08 +02:00
|
|
|
|
2020-06-14 03:36:42 +02:00
|
|
|
if args.crawl_id:
|
|
|
|
try:
|
|
|
|
crawl = CrawlRun.objects.get(pk=args.crawl_id)
|
|
|
|
except CrawlRun.DoesNotExist:
|
|
|
|
parser.error("CrawlRun with id {} does not exist".format(args.crawl_id))
|
|
|
|
_convert_crawl(crawl)
|
|
|
|
elif args.all:
|
|
|
|
if args.empty_graph_only:
|
|
|
|
crawls = CrawlRun.objects.filter(graph='')
|
|
|
|
else:
|
|
|
|
crawls = CrawlRun.objects.all()
|
|
|
|
|
|
|
|
for crawl in crawls:
|
|
|
|
_convert_crawl(crawl)
|
2020-06-14 00:24:18 +02:00
|
|
|
else:
|
2020-06-14 03:36:42 +02:00
|
|
|
parser.error("Either specify a crawl with -c or use -a for all")
|
2020-06-06 17:46:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|