Skip to content

Commit

Permalink
Added some more functions to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Ruml committed Feb 6, 2012
1 parent ab99cda commit d08bdb1
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 69 deletions.
29 changes: 4 additions & 25 deletions pythondrop
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import sys
import platform
from optparse import OptionParser

from src import app_manager
from src import config
from optparse import OptionParser
from src import cli

__appName__ = 'PythonDrop'
__version__ = '0.2.0'
Expand Down Expand Up @@ -52,34 +52,13 @@ def main():
if HELP:
sys.exit(0)

# Check for the correct numbers of arguments
if len(args) != 1:
print "usage: PythonDrop.py start|stop|restart"
sys.exit(2)

daemon = app_manager.AppManager("/tmp/pythondrop.pid", debug=options.debugmode)
# Parse args
if args[0] == "start":
if options.debugmode:
daemon.run()
else:
daemon.start()
elif args[0] == "stop":
daemon.stop()
elif args[0] == "restart":
daemon.restart()
else:
print "Unknown command"
print "usage: PythonDrop.py start|stop|restart"

print "Exit..."
sys.exit(0)
cli.Cli(args=args, options=options)

if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
except: # BaseException doesn't exist in python 2.4
except:
import traceback
traceback.print_exc()
24 changes: 7 additions & 17 deletions src/app_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

class AppManager(Daemon):
def __init__(self, pidfile, debug=False):
Daemon.__init__(self, pidfile=pidfile)
Daemon.__init__(self, pidfile=pidfile, verbose=0)

self._systray = None

Expand All @@ -59,16 +59,15 @@ def __init__(self, pidfile, debug=False):
# Set the log level
self._logger.set_level(self._config.logLevel)

def run(self):
self._logger.info("Starting PythonDrop v" + self._globals.version + "...")


def run(self):
# Create and start the API server
self._api_server = ApiServer(self, self._config.tcpListenIp, self._config.tcpListenPort)

# Start the web server
if self._config.enableWebServer:
self.web_server = WebServer()
self._web_server = WebServer()

# Check if the systray should be shown
if self._config.enableSystray:
Expand All @@ -92,7 +91,7 @@ def start(self):

def stop(self):
# TODO: Stop the fswatcher and the web server

self.exit()
Daemon.stop(self)

def restart(self):
Expand All @@ -103,15 +102,6 @@ def pause(self):
pass

def exit(self):
if self._config.get_option('enableGui', 'general'):
self._systray.exit()


if __name__ == '__main__':
try:
PythonDrop()
except SystemExit:
raise
except:
import traceback
traceback.print_exc()
#if self._config.get_option('enableGui', 'general'):
# self._systray.exit()
pass
61 changes: 47 additions & 14 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import sys
from optparse import OptionParser

from app_manager import AppManager
from configuration import Configuration
import globals

pythondrop_app = None

Expand All @@ -31,8 +30,9 @@ def __init__(self, args, options):

self._usage = "usage: %prog start|stop|restart"

self._app = None
self._config = Configuration()
self._app = AppManager(self.pid_file(), debug=self._options.debugmode)
self._config = self._app._config
self._globs = globals.Globals()

self.parseArgs()

Expand All @@ -48,38 +48,71 @@ def parseArgs(self):
self.stop()
elif self._args[0] == 'restart':
self.restart()
elif self._args[0] == 'create':
self.create()
elif self._args[0] == 'status':
self.status()
elif self._args[0] == 'config':
self.config()
elif self.args[0] == 'factoryreset':
pass
else:
print "Unknown command"
print self._usage
sys.exit(2)

def start(self):
app = AppManager(self.pid_file, pidfile=self._options.debugmode)
if self._options.debugmode:
app.run()
print "Starting PythonDrop..."
if self.stopped() and not self._options.debugmode:
self._app.start()
elif self.stopped() and self._options.debugmode:
# Start in debug mode
self._app.run()
else:
app.start()
print "PythonDrop is already running, please use restart"

def stop(self):
pass
print "Stopping PythonDrop..."
if self.running:
self._app.stop()
print "PythonDrop stopped"
else:
print "PythonDrop is not running"

def restart(self):
self.stop()
self.start()

def create(self):
pass
if len(self._args) < 4:
print "Error"

def status(self):
pass
print "PythonDrop v" + self._globs.version
print "Running: True" if self.running() else "Running: False"
print "Shares:"
print "\n".join(" - " + share.sync_folder for share in self._config.get_shares())

def config(self):
pass
if len(self._args) == 1:
# TODO: Print configuration
print "Configuration:"
print " - Log Level: " + self._config.logLevel
print " - API Listen IP: " + self._config.tcpListenIp
print " - API Listen Port: " + str(self._config.tcpListenPort)
print " - Web Server Enabled: " + str(self._config.enableWebServer)
print " - Web Server Listen IP: " + self._config.webServerListenIp
print " - Web Server Listen Port: " + str(self._config.webServerListenPort)
print " - Systray Enabled: " + str(self._config.enableSystray)

def help(self):
pass

def running(self):
pass
return self._app.running()

def stopped(self):
pass
return self._app.stopped()

def pid_file(self):
return "/tmp/pythondrop.pid"
3 changes: 3 additions & 0 deletions src/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ def set_webServerListenPort(self, port):
debugEnabled = False

def add_test_share(self):
"""
Adds some test shares
"""
self.add_share("/Users/sruml/PythonDrop", "sebastianruml.com",
"PythonDrop/PythonDrop.git", "pythondrop")

Expand Down
20 changes: 19 additions & 1 deletion src/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ def stop(self):
# Try killing the daemon process
try:
while 1:
os.kill(pid, signal.SIGTERM)
#os.kill(pid, signal.SIGTERM)
os.kill(pid, signal.SIGKILL)
time.sleep(0.1)
except OSError, err:
err = str(err)
Expand All @@ -187,3 +188,20 @@ def run(self):
daemonized by start() or restart().
"""

def running(self):
# Check for a pidfile to see if the daemon already runs
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
except SystemExit:
pid = None

if pid is not None: return True
else: return False

def stopped(self):
return not self.running()

24 changes: 12 additions & 12 deletions src/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@
# Configuration
configuration = None

def init_db():
pass

def connect_db():
# Connect to the database
pass

@app.before_request
def before_request():
# TODO: Make sure that we're connect to the database
Expand All @@ -62,9 +55,20 @@ def shares(share_id=None, dir=None):
shares = configuration.shares
error = None

if share_id == None:
error = "No share id given"
return render_template('dir.html', error=error)

share = get_share_by_id(share_id)
share_dir = share.sync_folder
if dir != None:
root_path = os.path.join(share_dir, dir)
else:
root_path = share_dir

if request.method == 'POST':
# FIXME: Add support for mode type
if request.form.get('action', None) == 'ok':
if request.form.get('action', None) == 'ok': # Create folder
dirName = request.form['dirName']
share = get_share_by_id(share_id)
if not None and len(dirName) > 0 and share != None:
Expand All @@ -79,10 +83,6 @@ def shares(share_id=None, dir=None):
else:
error = "Invalid filename"

if share_id == None:
error = "No share id given"
return render_template('dir.html', error=error)

share = get_share_by_id(share_id)
if share is not None:
share_path = share.sync_folder
Expand Down

0 comments on commit d08bdb1

Please sign in to comment.