-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
executable file
·49 lines (40 loc) · 1.8 KB
/
serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
import os
import sys
import argparse
import subprocess
# SSH Deploy Destination:
destination = '[email protected]:webapps/reidransom/'
def parseArgs():
parser = argparse.ArgumentParser(description="Run a static site development http server.")
parser.add_argument('--deploy', action='store_const', default=False, const=True, dest='deploy', help='sync with the production server')
parser.add_argument('-s', '--serve', action='store_const', default=False, const=True, dest='serve', help='start the http server')
parser.add_argument('-o', '--open', action='store_const', default=False, const=True, dest='open', help='open the root url in the default browser')
parser.add_argument('-p', '--port', action='store', default=8000, metavar='PORT', dest='port', help='port number to run the http server on')
parser.add_argument('-d', '--docroot', action='store', default="./public", metavar='DOCROOT', dest='docroot', help='directory where files will be served from')
return parser.parse_args()
def getURL(port):
return 'http://localhost:%d/' % port
def serve(port, docroot):
os.chdir(docroot)
subprocess.call(['http-server', '-p', str(port)])
def openInBrowser(url):
try:
subprocess.call(['open', url])
except FileNotFoundError:
pass
def deploy():
subprocess.call(['rsync', '-av', '--delete', '--delete-excluded', '--exclude', '*.DS_Store', '-e', 'ssh', './public/', destination])
def run():
args = parseArgs()
if args.deploy == True:
deploy()
if args.open == True:
openInBrowser(getURL(args.port))
if args.serve == True:
serve(args.port, args.docroot)
if len(sys.argv) < 2:
openInBrowser(getURL(args.port))
serve(args.port, args.docroot)
if __name__ == "__main__":
run()