# Path: mothership/models.py
# class Campaign(Model, db.Model):
# 	__tablename__ = 'campaign'
# 
# 	name = db.Column(db.String(128))
# 	fuzzers = db.relationship('FuzzerInstance', backref='fuzzer', lazy='dynamic')
# 	crashes = db.relationship('Crash', backref='campaign', lazy='dynamic')
# 
# 	active = db.Column(db.Boolean(), default=False)
# 	desired_fuzzers = db.Column(db.Integer())
# 
# 	has_dictionary = db.Column(db.Boolean(), default=False)
# 	executable_name = db.Column(db.String(512))
# 	executable_args = db.Column(db.String(1024))
# 	afl_args = db.Column(db.String(1024))
# 
# 	parent_id = db.Column(db.Integer, db.ForeignKey('campaign.id'))
# 	@property
# 	def children(self):
# 		return Campaign.all(parent_id=self.id)
# 
# 	def __init__(self, name):
# 		self.name = name
# 
# 	@property
# 	def started(self):
# 		return bool(self.fuzzers.filter(FuzzerInstance.last_update).first())
# 
# 	@property
# 	def active_fuzzers(self):
# 		return sum(i.running for i in self.fuzzers if not i.master)
# 
# 	@property
# 	def master_fuzzer(self):
# 		return self.fuzzers.filter_by(master=True).first()
# 
# 	@property
# 	def num_executions(self):
# 		return sum(i.execs_done for i in self.fuzzers if i.started)
# 
# 	@property
# 	def num_crashes(self):
# 		return sum(i.unique_crashes for i in self.fuzzers if i.started)
# 
# 	@property
# 	def bitmap_cvg(self):
# 		if not self.started:
# 			return 0, 0
# 		bitmap_cvgs = []
# 		newest = self.fuzzers.order_by(desc(FuzzerInstance.last_update)).first()
# 		for fuzzer in self.fuzzers.filter(FuzzerInstance.last_update):
# 			if newest.last_update - fuzzer.last_update < 10 * 60:
# 				bitmap_cvgs.append(fuzzer.bitmap_cvg)
# 		return statistics.mean(bitmap_cvgs), statistics.stdev(bitmap_cvgs) if len(bitmap_cvgs) > 1 else 0.


import os
from flask import current_app
from flask_wtf import Form
from werkzeug.utils import secure_filename
from wtforms import StringField, SelectField, IntegerField, BooleanField
from flask_wtf.file import FileField
from wtforms import validators
from mothership.models import Campaign



class CampaignForm(Form):
	name = StringField('Name', validators=[validators.required()])
	executable_name = StringField('Executable Name', validators=[validators.required()], default='executable')
	executable_args = StringField('Executable Args', default='@@')
	afl_args = StringField('AFL Args', default='-m 100 -t 50+')
	copy_of = SelectField('Copy of', coerce=int, choices=[(-1, 'None')])
	desired_fuzzers = IntegerField('Desired Fuzzers')
	executable = FileField()
	libraries = FileField(
		render_kw={'multiple': True},
	)
	testcases = FileField(
		render_kw={'multiple': True},
	)
	ld_preload = FileField(
		'LD PRELOAD',
		render_kw={'multiple': True},
	)
	dictionary = FileField()
	use_libdislocator = BooleanField('Use libdislocator')

	def validate(self):
		check_validate = super().validate()
		if not check_validate:
			return False


# Path: mothership/models.py
# class Campaign(Model, db.Model):
# 	__tablename__ = 'campaign'
# 
# 	name = db.Column(db.String(128))
# 	fuzzers = db.relationship('FuzzerInstance', backref='fuzzer', lazy='dynamic')
# 	crashes = db.relationship('Crash', backref='campaign', lazy='dynamic')
# 
# 	active = db.Column(db.Boolean(), default=False)
# 	desired_fuzzers = db.Column(db.Integer())
# 
# 	has_dictionary = db.Column(db.Boolean(), default=False)
# 	executable_name = db.Column(db.String(512))
# 	executable_args = db.Column(db.String(1024))
# 	afl_args = db.Column(db.String(1024))
# 
# 	parent_id = db.Column(db.Integer, db.ForeignKey('campaign.id'))
# 	@property
# 	def children(self):
# 		return Campaign.all(parent_id=self.id)
# 
# 	def __init__(self, name):
# 		self.name = name
# 
# 	@property
# 	def started(self):
# 		return bool(self.fuzzers.filter(FuzzerInstance.last_update).first())
# 
# 	@property
# 	def active_fuzzers(self):
# 		return sum(i.running for i in self.fuzzers if not i.master)
# 
# 	@property
# 	def master_fuzzer(self):
# 		return self.fuzzers.filter_by(master=True).first()
# 
# 	@property
# 	def num_executions(self):
# 		return sum(i.execs_done for i in self.fuzzers if i.started)
# 
# 	@property
# 	def num_crashes(self):
# 		return sum(i.unique_crashes for i in self.fuzzers if i.started)
# 
# 	@property
# 	def bitmap_cvg(self):
# 		if not self.started:
# 			return 0, 0
# 		bitmap_cvgs = []
# 		newest = self.fuzzers.order_by(desc(FuzzerInstance.last_update)).first()
# 		for fuzzer in self.fuzzers.filter(FuzzerInstance.last_update):
# 			if newest.last_update - fuzzer.last_update < 10 * 60:
# 				bitmap_cvgs.append(fuzzer.bitmap_cvg)
# 		return statistics.mean(bitmap_cvgs), statistics.stdev(bitmap_cvgs) if len(bitmap_cvgs) > 1 else 0.


# Path: mothership/forms.py
import os
from flask import current_app
from flask_wtf import Form
from werkzeug.utils import secure_filename
from wtforms import StringField, SelectField, IntegerField, BooleanField
from flask_wtf.file import FileField
from wtforms import validators
from mothership.models import Campaign




class CampaignForm(Form):
	name = StringField('Name', validators=[validators.required()])
	executable_name = StringField('Executable Name', validators=[validators.required()], default='executable')
	executable_args = StringField('Executable Args', default='@@')
	afl_args = StringField('AFL Args', default='-m 100 -t 50+')
	copy_of = SelectField('Copy of', coerce=int, choices=[(-1, 'None')])
	desired_fuzzers = IntegerField('Desired Fuzzers')
	executable = FileField()
	libraries = FileField(
		render_kw={'multiple': True},
	)
	testcases = FileField(
		render_kw={'multiple': True},
	)
	ld_preload = FileField(
		'LD PRELOAD',
		render_kw={'multiple': True},
	)
	dictionary = FileField()
	use_libdislocator = BooleanField('Use libdislocator')

	def validate(self):
		check_validate = super().validate()
		if not check_validate:
			return False


		campaign = Campaign.get(name=self.name.data)