From d9ae7d82b22c8bb269d8904390cff3ecb6b315f8 Mon Sep 17 00:00:00 2001 From: Matt Traudt <sirmatt@ksu.edu> Date: Sun, 25 Mar 2018 21:39:57 -0400 Subject: [PATCH] Add init command --- README.md | 1 + sbws/__main__.py | 4 ++ sbws/commands/client.py | 4 ++ sbws/commands/init.py | 37 +++++++++++++++++++ sbws/commands/server.py | 5 +++ sbws/globals.py | 19 ++++++++++ .../passwords.txt.example | 0 setup.py | 11 ++++-- 8 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 sbws/commands/init.py create mode 100644 sbws/globals.py rename passwords.txt.example => sbws/passwords.txt.example (100%) diff --git a/README.md b/README.md index 781d5fb8..fb8b15e6 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Clone the repo virtualenv -p python3 venv source venv/bin/activate pip install . + sbws init sbws client -h sbws server -h diff --git a/sbws/__main__.py b/sbws/__main__.py index 6a081fb8..61b5c8b6 100644 --- a/sbws/__main__.py +++ b/sbws/__main__.py @@ -1,4 +1,5 @@ import sbws.commands.client +import sbws.commands.init import sbws.commands.server from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter @@ -10,6 +11,7 @@ def create_parser(): p = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) sub = p.add_subparsers(dest='command') sbws.commands.client.gen_parser(sub) + sbws.commands.init.gen_parser(sub) sbws.commands.server.gen_parser(sub) return p @@ -22,6 +24,8 @@ def main(): known_commands = { 'client': {'f': sbws.commands.client.main, 'a': def_args, 'kw': def_kwargs}, + 'init': {'f': sbws.commands.init.main, + 'a': def_args, 'kw': def_kwargs}, 'server': {'f': sbws.commands.server.main, 'a': def_args, 'kw': def_kwargs}, } diff --git a/sbws/commands/client.py b/sbws/commands/client.py index 0480b2dd..cedce067 100644 --- a/sbws/commands/client.py +++ b/sbws/commands/client.py @@ -5,6 +5,7 @@ from ..lib.resultdump import Result from ..lib.relaylist import RelayList from ..util.simpleauth import is_good_clientside_password_file from ..util.simpleauth import authenticate_to_server +from sbws.globals import is_initted import sbws.util.stem as stem_utils from stem.control import EventType from argparse import ArgumentDefaultsHelpFormatter @@ -15,6 +16,7 @@ import random import socks import socket import time +import os log = None @@ -273,6 +275,8 @@ def main(args): global log log = PastlyLogger(debug='/dev/stdout', overwrite=['debug'], log_threads=True) + if not is_initted(os.getcwd()): + fail_hard('Directory isn\'t initted') if args.threads < 1: fail_hard('--threads must be larger than 1') diff --git a/sbws/commands/init.py b/sbws/commands/init.py new file mode 100644 index 00000000..2c61bd10 --- /dev/null +++ b/sbws/commands/init.py @@ -0,0 +1,37 @@ +from sbws.globals import G_INIT_FILE_MAP +from ..lib.pastlylogger import PastlyLogger +from argparse import ArgumentDefaultsHelpFormatter +import os +import shutil + +log = None + + +def fail_hard(*s): + ''' Optionally log something to stdout ... and then exit as fast as + possible ''' + if s: + log.error(*s) + exit(1) + + +def gen_parser(sub): + p = sub.add_parser('init', formatter_class=ArgumentDefaultsHelpFormatter) + + +def main(args): + global log + log = PastlyLogger(debug='/dev/stdout', overwrite=['debug'], + log_threads=True) + + dotdir = os.path.join(os.getcwd(), '.sbws') + if os.path.exists(dotdir): + fail_hard('Directory already seems to be initted') + os.makedirs(dotdir) + + for src, dst, ftype in G_INIT_FILE_MAP: + log.info(dst, '({})'.format(ftype)) + if ftype == 'file': + shutil.copy(src, dst) + else: + fail_hard('Cannot init ftype', ftype) diff --git a/sbws/commands/server.py b/sbws/commands/server.py index 7405731c..f2417fd9 100644 --- a/sbws/commands/server.py +++ b/sbws/commands/server.py @@ -1,10 +1,12 @@ from ..lib.pastlylogger import PastlyLogger from ..util.simpleauth import authenticate_client from ..util.simpleauth import is_good_serverside_password_file +from sbws.globals import is_initted from argparse import ArgumentDefaultsHelpFormatter from threading import Thread import socket import time +import os log = None @@ -108,6 +110,9 @@ def main(args): global log log = PastlyLogger(debug='/dev/stdout', overwrite=['debug'], log_threads=True) + if not is_initted(os.getcwd()): + fail_hard('Directory isn\'t initted') + valid, error_reason = is_good_serverside_password_file(args.password_file) if not valid: fail_hard(error_reason) diff --git a/sbws/globals.py b/sbws/globals.py new file mode 100644 index 00000000..06df7416 --- /dev/null +++ b/sbws/globals.py @@ -0,0 +1,19 @@ +import os + + +G_PKG_DIR = os.path.abspath(os.path.dirname(__file__)) +G_INIT_FILE_MAP = [ + # (source, destination, type) + (os.path.join(G_PKG_DIR, 'passwords.txt.example'), + 'passwords.txt', 'file'), +] + + +def is_initted(d): + dotdir = os.path.join(d, '.sbws') + if not os.path.isdir(dotdir): + return False + for _, fname, _ in G_INIT_FILE_MAP: + if not os.path.exists(fname): + return False + return True diff --git a/passwords.txt.example b/sbws/passwords.txt.example similarity index 100% rename from passwords.txt.example rename to sbws/passwords.txt.example diff --git a/setup.py b/setup.py index 330f9332..fa5c26a3 100755 --- a/setup.py +++ b/setup.py @@ -19,6 +19,10 @@ def get_package_data(): # for f in fs: # other_files.append(os.path.join(r, f)) # return other_files + return ['passwords.txt.example'] + + +def get_data_files(): return [] @@ -49,9 +53,10 @@ setup( 'Development Status :: 3 - Alpha', ], packages=find_packages(), - # package_data={ - # 'foo': get_package_data(), - # }, + package_data={ + 'sbws': get_package_data(), + }, + data_files=get_data_files(), keywords='', python_requires='>=3.5', # test_suite='test', -- GitLab