#!/usr/bin/python3
import pymongo
import argparse
import os
from utils.models import JavascriptArtefact, JavascriptVectorSummary
from utils.misc import add_mongo_arguments, add_extractor_arguments, add_debug_arguments
from utils.io import save_control

a = argparse.ArgumentParser(
    description=
    "Insert control artefact features into MongoDB using artefacts from local magento release tree"
)
add_mongo_arguments(a, default_access="read-write", default_user='******')
a.add_argument(
    "--family",
    help="Software family designation eg. Magento (community edition)",
    type=str,
    required=True)
a.add_argument(
    "--urlroot",
    help=
    "Root of all URLs constructed. Should be valid artefact URL eg. github.com/owner/repo/...",
    type=str,
    required=True)
a.add_argument("--release",
               help="Name of release eg. 1.10.3a",
               type=str,
               required=True)
a.add_argument("--local", help="Root of release tree", type=str, required=True)
a.add_argument("--min-size",
               help="Minimum JS artefact size (bytes) [1500]",
#!/usr/bin/python3
import pymongo
import argparse
import json
import hashlib
from bson.objectid import ObjectId
from utils.misc import add_mongo_arguments
from utils.models import JavascriptArtefact
from utils.features import get_script
from utils.io import find_or_update_analysis_content

a = argparse.ArgumentParser(
    description="Save the specified artefact to disk as the specified filename"
)
add_mongo_arguments(a, default_access="read-only", default_user='******')
a.add_argument("--file",
               help="Save to specified file []",
               type=str,
               required=True)
g = a.add_mutually_exclusive_group(required=True)
g.add_argument('--artefact',
               help='Retrieve the specified JS script ID from the database',
               type=str)
g.add_argument('--control',
               help='Save the control JS code as specified by its URL',
               type=str)
a.add_argument('--literals',
               help="Dump string literals and usage count",
               action="store_true")
args = a.parse_args()
def load_subfamilies(db, verbose=False):
    subfamilies = []
    for t in sorted(load_controls(db, verbose=args.v, load_all=True),
                    key=lambda t: t[0].get('family')):
        u = t[0].get('origin')
        u2, subfamily = identify_control_subfamily(u)
        assert u == u2
        subfamilies.append((subfamily, u, t))
    return subfamilies


if __name__ == "__main__":
    a = argparse.ArgumentParser(description="Fix subfamily for all controls")
    add_mongo_arguments(
        a, default_user='******'
    )  # need read-write access to update the subfamily for all controls
    add_debug_arguments(a)
    a.add_argument("--dry-run",
                   help="Do not update the database",
                   action="store_true")
    a.add_argument("--list",
                   help="List current subfamilies and exit",
                   action="store_true")
    args = a.parse_args()

    mongo = pymongo.MongoClient(args.db,
                                args.port,
                                username=args.dbuser,
                                password=str(args.dbpassword))
    db = mongo[args.dbname]