Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 340bd04

Browse files
committed
Add new deploy command. serve now takes a port option, and commands can be chained i.e. build server or build deploy
1 parent f7e8d6c commit 340bd04

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

runestone/__main__.py

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
import getpass
66
import six
77
import click
8+
from paver.easy import sh
89
from pkg_resources import resource_string, resource_filename
910

11+
@click.group(chain=True)
12+
def cli():
13+
pass
1014

15+
@cli.command()
1116
def init():
1217
template_base_dir = resource_filename('runestone', 'common/project_template')
1318
config_stuff = resource_string('runestone','common/project_template/conf.tmpl')
@@ -20,6 +25,7 @@ def init():
2025
while ' ' in conf_dict['project_name']:
2126
conf_dict['project_name'] = click.prompt("Project name: (one word, NO SPACES)")
2227
conf_dict['build_dir'] = click.prompt("Path to build dir ", default="./build")
28+
conf_dict['dest'] = click.prompt("Path to deploy built site ", default="../../static")
2329
conf_dict['use_services'] = click.prompt("Use Runestone Web Services ", type=click.Choice(['true', 'false']), default="false")
2430
conf_dict['author'] = click.prompt("Your Name ", default=getpass.getuser())
2531
conf_dict['project_title'] = click.prompt("Title for this project ", default="Runestone Default")
@@ -50,13 +56,22 @@ def init():
5056

5157
print("Done. Type runestone build to build your project")
5258

53-
def build():
59+
@cli.command()
60+
@click.option('--all/--not-all', default=False, help="build all")
61+
def build(all):
5462
from paver.tasks import main as paver_main
5563
os.chdir(findProjectRoot())
56-
sys.argv[0] = "build"
57-
paver_main()
58-
59-
def serve():
64+
65+
myargs = ['build']
66+
if all:
67+
myargs.append('--all')
68+
69+
paver_main(args=myargs)
70+
71+
@cli.command()
72+
@click.option('--port', default=8000, help="port for server to listen on")
73+
@click.option('--listen', default="", help="address for server to listen on")
74+
def serve(port,listen):
6075
os.chdir(findProjectRoot())
6176
sys.path.insert(0,os.getcwd())
6277
try:
@@ -68,35 +83,47 @@ def serve():
6883
os.chdir(pavement.serving_dir)
6984

7085

71-
PORT = 8000
7286
if six.PY2:
7387
import SimpleHTTPServer
7488
import SocketServer
7589
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
76-
httpd = SocketServer.TCPServer(("", PORT), Handler)
90+
httpd = SocketServer.TCPServer((listen, port), Handler)
7791
else:
7892
import http.server
7993
import socketserver
8094
Handler = http.server.SimpleHTTPRequestHandler
81-
httpd = socketserver.TCPServer(("", PORT), Handler)
95+
httpd = socketserver.TCPServer((listen, port), Handler)
8296

83-
print("serving at port", PORT)
97+
print("serving at port", port)
8498
httpd.serve_forever()
8599

100+
@cli.command()
101+
@click.option("--dest", default="", help="destination for deploy")
102+
def deploy(dest):
103+
os.chdir(findProjectRoot())
104+
sys.path.insert(0,os.getcwd())
105+
try:
106+
import pavement
107+
except ImportError as ie:
108+
print("Error, you must be in your project root directory")
109+
110+
if not dest:
111+
try:
112+
dest = pavement.dest
113+
except:
114+
raise IOError("No destination configured add dest to your pavement.py or use --dest")
115+
116+
click.echo('Deploying from ' + pavement.serving_dir + ' to ' + dest)
117+
sh("rsync -rav --delete {} {}".format(pavement.serving_dir,dest))
118+
86119
def main(args=None):
87120
if not args:
88121
args = sys.argv[1:]
89-
foo_config = resource_filename('runestone', 'common')
90-
# foo_string = resource_string('runestone', 'project/template/conf.tmpl')
91-
92-
if args[0] == "init":
93-
init()
94-
elif args[0] == "build":
95-
build()
96-
elif args[0] == "serve":
97-
serve()
98-
else:
99-
print("Error: I only understand init, build, and serve")
122+
cli.add_command(init)
123+
cli.add_command(build)
124+
cli.add_command(serve)
125+
cli.add_command(deploy)
126+
cli()
100127

101128
def findProjectRoot():
102129
start = os.getcwd()
@@ -106,7 +133,8 @@ def findProjectRoot():
106133
return start
107134
prevdir = start
108135
start = os.path.dirname(start)
109-
raise NotADirectoryError("You must be in a runestone project to run runestone")
136+
raise IOError("You must be in a runestone project to run runestone")
137+
110138

111139
if __name__ == "__main__":
112-
sys.exit(main(sys.argv[1:]))
140+
sys.exit(main(sys.argv[1:]))

runestone/common/project_template/pavement.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ home_dir = os.getcwd()
1212
master_url = '%(master_url)s'
1313
master_app = 'runestone'
1414
serving_dir = "%(build_dir)s/%(project_name)s"
15+
dest = "%(dest)s"
1516

1617
options(
1718
sphinx = Bunch(docroot=".",),

0 commit comments

Comments
 (0)