Skip to content

Commit

Permalink
Use argument parser from pusto.py in manage.py;
Browse files Browse the repository at this point in the history
  • Loading branch information
naspeh committed Oct 16, 2013
1 parent 107dc6c commit 82c8c01
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Requires:

Steps:
- create virtualenv and activate it;
- run ``./manage.py bootsrap``.
- run ``./bootsrap``.


Usage
Expand Down
2 changes: 2 additions & 0 deletions bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
pip install -r requirements.txt --no-index
27 changes: 7 additions & 20 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#!/usr/bin/env python
import argparse
import subprocess

import pusto

sh = lambda cmd: print(cmd) or subprocess.call(cmd, shell=True)
ssh = lambda cmd: sh(
'ssh yadro.org "%s"'
% cmd.replace('"', '\"').replace('$', '\$')
)


def process_args(args=None):
parser = argparse.ArgumentParser()
subs = parser.add_subparsers(title='subcommands')

def sub(name, **kw):
s = subs.add_parser(name, **kw)
s.set_defaults(sub=name)
s.arg = lambda *a, **kw: s.add_argument(*a, **kw) and s
s.exe = lambda f: s.set_defaults(exe=f) and s
return s
def process_args():
parser = pusto.get_parser()
sub = parser.sub

sub('deploy', help='deploy to server')\
.arg(
Expand All @@ -30,17 +24,14 @@ def sub(name, **kw):
'&& git fetch origin'
'&& git checkout {target}'
'&& source $(cat .venv)/bin/activate'
'&& ./manage.py bootstrap'
'&& ./bootstrap'
'&& ./pusto.py build -b build-tmp'
'&& rm -rf build'
'&& mv build-tmp build'
'&& systemctl restart nginx.service'
.format(target=a.target)
))

sub('bootstrap', help='install dependencies')\
.exe(lambda a: sh('pip install -r requirements.txt --no-index'))

sub('wheels', help='prepare wheels')\
.exe(lambda a: sh(
'pip wheel -r requirements.txt -w wheels'
Expand All @@ -57,11 +48,7 @@ def sub(name, **kw):
% ('push' if a.push else 'pull --squash')
))

args = parser.parse_args(args)
if not hasattr(args, 'sub'):
parser.print_usage()
else:
args.exe(args)
pusto.process(parser=parser)


if __name__ == '__main__':
Expand Down
55 changes: 33 additions & 22 deletions pusto.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ def save_urls(pages, filename):
f.write(urlmap.encode())


def run(src_dir, build_dir, no_build=False, port=5000):
if not no_build:
build(src_dir, build_dir)

watcher = Thread(target=watch_files, args=(src_dir, build_dir))
watcher.daemon = True
watcher.start()

os.chdir(build_dir)
http.server.test(
port=port, HandlerClass=http.server.SimpleHTTPRequestHandler
)


def list_files(path):
files = set()
for dirpath, dirnames, filenames in os.walk(path):
Expand Down Expand Up @@ -403,52 +417,49 @@ def get(url, expected_code=200, indent=''):
print('OK')


def process(*args):
def get_parser():
parser = argparse.ArgumentParser()
subs = parser.add_subparsers(title='subcommands')

def sub(name, **kw):
s = subs.add_parser(name, **kw)
s.set_defaults(sub=name)
s.arg = lambda *a, **kw: s.add_argument(*a, **kw) and s
s.exe = lambda f: s.set_defaults(exe=f) and s
return s

sub('run', help='start dev server')\
.arg('--port', type=int, default=5000)\
.arg('--no-build', action='store_true')
.arg('--no-build', action='store_true')\
.exe(lambda a: (
run(SRC_DIR, BUILD_DIR, no_build=a.no_build, port=a.port)
))

sub('build', help='build static content from `data` directory')\
.arg('-b', '--bdir', default=BUILD_DIR, help='build directory')\
.arg('-n', '--nginx-file', help='file nginx rules')\
.arg('--port', type=int, default=8000)
.arg('--port', type=int, default=8000)\
.exe(lambda a: build(SRC_DIR, a.bdir, a.nginx_file))

sub('test_urls', help='test url responses')\
.arg('-v', '--verbose', action='store_true')\
.arg('--host', help='use host for test')\
.exe(lambda a: check_urls(host=a.host, verbose=a.verbose))

args = parser.parse_args(args or None)
if not hasattr(args, 'sub'):
parser.print_usage()

elif args.sub == 'run':
if not args.no_build:
build(SRC_DIR, BUILD_DIR)
parser.sub = sub
return parser

watcher = Thread(target=watch_files, args=(SRC_DIR, BUILD_DIR))
watcher.daemon = True
watcher.start()

os.chdir(BUILD_DIR)
http.server.test(
port=args.port,
HandlerClass=http.server.SimpleHTTPRequestHandler
)
def process(*args, parser=None):
if parser is None:
parser = get_parser()

elif args.sub == 'build':
build(SRC_DIR, args.bdir, args.nginx_file)
args = parser.parse_args(args or None)
if not hasattr(args, 'sub'):
parser.print_usage()

elif args.sub == 'test_urls':
check_urls(host=args.host, verbose=args.verbose)
elif hasattr(args, 'exe'):
args.exe(args)
else:
raise ValueError('Wrong subcommand')

Expand Down

0 comments on commit 82c8c01

Please sign in to comment.