Skip to content

Commit

Permalink
--init
Browse files Browse the repository at this point in the history
  • Loading branch information
andgineer committed Apr 14, 2019
1 parent 5c0ea81 commit 44aaefa
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 47 deletions.
55 changes: 11 additions & 44 deletions bombard/args.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Parse bombard command line args
Parse bombard command line args.
"""
import sys
import os.path
Expand All @@ -14,22 +14,12 @@
DIR_DESC_FILE_NAME = 'README.md' # if directory as campaign file then show content of this file from the directory
THREADS_NUM = 10
CAMPAIGN_FILE_NAME = 'bombard.yaml'
INIT_EXAMPLE = 'easy.yaml'
REPEAT = 10
THRESHOLD = 1000
TIMEOUT = 10


def expand_relative_file_name(file_name):
"""
Replace RELATIVE_PREFIX with package folder so bombard script can use internal examples without full path spec
"""
if file_name.strip().startswith(EXAMPLES_PREFIX):
# resource_string(__name__, args.file_name[1:]) # recommended use resource to be zipfile compatible. but this is a pain for !include
return os.path.join(os.path.dirname(bombard.__file__), 'examples', file_name[len(EXAMPLES_PREFIX):])
else:
return file_name


def get_args():
parser = argparse.ArgumentParser(
description=markdown_for_terminal(f'''bombard: utility to bombard with HTTP-requests.
Expand Down Expand Up @@ -91,41 +81,18 @@ def get_args():
)
parser.add_argument(
'--dry', '-d', dest='dry', default=False, action='store_true',
help=f'without actual HTTP requests. if there is "dry" parameter in an ammo use it as fake request result.'
help=f'run without actual HTTP requests. if there is "dry" parameter in an ammo use it as fake request result.'
)
parser.add_argument(
'--init', '-i', dest='init', default=False, action='store_true',
help=f'''copy `{INIT_EXAMPLE}` example to current folder with the name {CAMPAIGN_FILE_NAME}
so it will be used as default with `bombard` command. If you want to copy different example add option
`--example <the name you want>`. For full list of examples use `--examples` option.
'''
)

args = parser.parse_args()

if args.example is not None:
if args.file_name != CAMPAIGN_FILE_NAME:
print(red(f'--example option found - ignoring campaign file name "{args.file_name}".'))
args.file_name = EXAMPLES_PREFIX + args.example
if not args.file_name.endswith('.yaml'):
args.file_name += '.yaml'
if args.examples:
if args.file_name != CAMPAIGN_FILE_NAME:
print(red(f'--examples option found - ignoring campaign file name "{args.file_name}".'))
if args.example is not None:
print(red('Please do not use --example and --examples options simultaneously.'))
args.file_name = EXAMPLES_PREFIX

args.file_name = expand_relative_file_name(args.file_name)

if os.path.isdir(args.file_name):
file_name = os.path.join(args.file_name, DIR_DESC_FILE_NAME)
if not os.path.isfile(file_name):
print(f'\nNo {DIR_DESC_FILE_NAME} in folder {args.file_name}. \nFolder content:\n')
for name in os.listdir(args.file_name):
print(name)
else:
print(f'\n{args.file_name}:\n')
print(markdown_for_terminal(open(file_name, 'r').read()))
print(args.file_name)

if not os.path.isfile(args.file_name):
print(red(f'\nCannot find campaign file "{args.file_name}"\n'))
parser.print_help(sys.stderr)
exit(1)
args.parser = parser

return args

Expand Down
46 changes: 46 additions & 0 deletions bombard/expand_file_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os.path
from bombard.args import EXAMPLES_PREFIX, CAMPAIGN_FILE_NAME, DIR_DESC_FILE_NAME
from bombard.terminal_colours import red
from bombard.show_descr import markdown_for_terminal
import bombard


def expand_relative_file_name(file_name):
"""
Replace RELATIVE_PREFIX with package folder so bombard script can use internal examples without full path spec
"""
if file_name.strip().startswith(EXAMPLES_PREFIX):
# resource_string(__name__, args.file_name[1:]) # recommended use resource to be zipfile compatible. but this is a pain for !include
return os.path.join(os.path.dirname(bombard.__file__), 'examples', file_name[len(EXAMPLES_PREFIX):])
else:
return file_name


def get_campaign_file_name(args):
if args.example is not None:
if args.file_name != CAMPAIGN_FILE_NAME:
print(red(f'--example option found - ignoring campaign file name "{args.file_name}".'))
args.file_name = EXAMPLES_PREFIX + args.example
if not args.file_name.endswith('.yaml'):
args.file_name += '.yaml'
if args.examples:
if args.file_name != CAMPAIGN_FILE_NAME:
print(red(f'--examples option found - ignoring campaign file name "{args.file_name}".'))
if args.example is not None:
print(red('Please do not use --example and --examples options simultaneously.'))
args.file_name = EXAMPLES_PREFIX

return expand_relative_file_name(args.file_name)


def show_folder(folder_path):
file_name = os.path.join(folder_path, DIR_DESC_FILE_NAME)
if not os.path.isfile(file_name):
print(f'\nNo {DIR_DESC_FILE_NAME} in folder {folder_path}. \nFolder content:\n')
for name in os.listdir(folder_path):
print(name)
else:
print(f'\n{folder_path}:\n')
print(markdown_for_terminal(open(file_name, 'r').read()))


38 changes: 36 additions & 2 deletions bombard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
from bombard.campaign_yaml import yaml
from bombard.bombardier import Bombardier
import logging
from bombard.args import get_args
from bombard.args import get_args, CAMPAIGN_FILE_NAME, INIT_EXAMPLE
from typing import Optional
from bombard.request_logging import setup_logging, log
import os.path
from shutil import copyfile
from bombard.expand_file_name import get_campaign_file_name, show_folder, expand_relative_file_name
import sys
from bombard.terminal_colours import red


def guess_type(value: str):
Expand Down Expand Up @@ -54,6 +59,21 @@ def add_names_to_requests(campaign_book):
request['name'] = name


def init(args):
"""
Copies the example to current folder as bombard.yaml
"""
if args.example is None:
src = expand_relative_file_name(f'bombard://{INIT_EXAMPLE}')
else:
src = get_campaign_file_name(args) # actually it will be from ==example
if os.path.isfile(CAMPAIGN_FILE_NAME):
log.error(f'File {CAMPAIGN_FILE_NAME} already exists.')
exit(1)
copyfile(src, CAMPAIGN_FILE_NAME)
#todo copy external python scripts if it is included into the example (create yaml CopyLoader)


def campaign(args):
if args.quiet:
level = logging.WARNING
Expand All @@ -64,11 +84,25 @@ def campaign(args):

setup_logging(level=level, log_file_name=args.log)

if args.init:
init(args)
exit(0)

log.debug(f'Starting bombard campaign with args\n' + ' '*4 + f'{args.__dict__}')
campaign_file_name = get_campaign_file_name(args)

if os.path.isdir(campaign_file_name):
show_folder(campaign_file_name)
exit(0)

supply = get_supply_from_cli(args.supply)

campaign_book = yaml.load(open(args.file_name, 'r'))
if not os.path.isfile(campaign_file_name) and not args.init:
print(red(f'\nCannot find campaign file "{args.file_name}"\n'))
args.parser.print_help(sys.stderr)
exit(1)

campaign_book = yaml.load(open(campaign_file_name, 'r'))
log.debug(f'Loaded bombard campaign from "{args.file_name}": {len(campaign_book["ammo"])} ammo.')

load_book_supply(supply, campaign_book.get('supply', {}))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name='bombard',
version='1.5',
version='1.6',
# scripts=['bin/bombard'],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit 44aaefa

Please sign in to comment.