Skip to content

Commit

Permalink
Add standard functions executable scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
koromodako committed Jan 25, 2019
1 parent 02fcd09 commit 9ca2da4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
__pycache__/
build/
.venv/
*venv*
*.egg-info/
2 changes: 1 addition & 1 deletion mkctf/__init__.py
Expand Up @@ -5,7 +5,7 @@
__version_info__ {tuple} -- Package version
'''
__major__ = 2
__minor__ = 0
__minor__ = 1
__patch__ = 0
__version_info__ = (__major__, __minor__, __patch__)
__version__ = f'{__major__}.{__minor__}.{__patch__}'
Expand Down
51 changes: 41 additions & 10 deletions mkctf/object/challenge.py
Expand Up @@ -11,6 +11,7 @@
from os import urandom
from stat import S_IRWXU
from asyncio import create_subprocess_exec, wait_for, TimeoutError
from textwrap import dedent
from subprocess import PIPE, CalledProcessError
from mkctf.helper.log import app_log
from mkctf.object.configurable import Configurable
Expand Down Expand Up @@ -51,19 +52,49 @@ def __create_file(self, filename, executable=False):
'''Creates a file
'''
filepath = self.working_dir().joinpath(filename)

filepath.parent.mkdir(parents=True, exist_ok=True)

if not filepath.is_file():
with filepath.open('w') as f:
if executable:
f.write('#!/usr/bin/env bash\n')

f.write('# file automatically generated by mkctf.\n')
code = []
if executable:
code.append('#!/usr/bin/env bash')
code.append('# file automatically generated by mkctf.')
code.append('')
code.append('function print {')
code.append(' (>&2 printf "\\x1b[34m${1}\\x1b[0m\\n")')
code.append('}')
code.append('')
code.append('function exit_success {')
code.append(' (>&2 printf "\\x1b[32m- Script succeeded.\\x1b[0m\\n")')
code.append(' exit 0')
code.append('}')
code.append('')
code.append('function exit_failure {')
code.append(' (>&2 printf "\\x1b[31m- Script failed.\\x1b[0m\\n")')
code.append(' exit 1')
code.append('}')
code.append('')
code.append('function exit_non_applicable {')
code.append(' (>&2 printf "\\x1b[36m- Script non applicable.\\x1b[0m\\n")')
code.append(' exit 2')
code.append('}')
code.append('')
code.append('function exit_manual {')
code.append(' (>&2 printf "\\x1b[33m- Script requires a manual operation.\\x1b[0m\\n")')
code.append(' exit 3')
code.append('}')
code.append('')
code.append('function exit_not_implemented {')
code.append(' (>&2 printf "\\x1b[35m- Script not implemented.\\x1b[0m\\n")')
code.append(' exit 4')
code.append('}')
code.append('')
if executable:
code.append('exit_not_implemented')
code.append('')

if executable:
f.write('>&2 echo "not implemented."\n')
f.write('exit 4\n')
if not filepath.is_file():
with filepath.open('w') as fp:
fp.write('\n'.join(code))

if executable:
filepath.chmod(S_IRWXU)
Expand Down

0 comments on commit 9ca2da4

Please sign in to comment.