Skip to content

Commit

Permalink
Implement helper arguments functionality; Close #21
Browse files Browse the repository at this point in the history
For this purpose was introduced concept of the helpers list instead
of just one helper.
  • Loading branch information
FrostyX committed Nov 16, 2014
1 parent 06c46a9 commit 340e188
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
13 changes: 13 additions & 0 deletions data/applications.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@
- <app name="app_name3" />
- </group>
-
- Helpers:
- Obviously you can specify the helper and set the command for restarting
- the application. I.e. helper="myapp restart".
- What is actually more interesting is that you can add various arguments.
-
- <app name="app_name" helper="kill {PID}" />
-
- Helper arguments:
- * {PNAME} - Name of the process
- * {NAME} - Name of the application
- * {PID} - Process ID
- * {EXE} - Process binary
-
-->

<applications>
Expand Down
32 changes: 32 additions & 0 deletions tracer/resources/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tracer.resources.lang import _
from tracer.resources.processes import Processes
import os
import re


class Applications:
Expand Down Expand Up @@ -174,6 +175,7 @@ def update(self, values):
values = values._attributes
self._attributes.update(values)

# @TODO rename to helper_format
@property
def helper(self):
helper = self._attributes["helper"]
Expand All @@ -182,6 +184,36 @@ def helper(self):
helper = "sudo " + helper
return helper

@property
def helper_contains_formating(self):
return bool(re.search("\{.*\}", self.helper))

@property
def helper_contains_name(self):
return bool(re.search("\{NAME\}", self.helper))

@property
def helpers(self):
"""
Return the list of helpers which describes how to restart the application.
When no ``helper_format`` was described, empty list will be returned.
If ``helper_format`` contains process specific arguments such a {PID}, etc.
list will contain helper for every application instance.
In other cases, there will be just one helper in the list.
"""
helpers = []
if not self.helper_contains_formating:
helpers.append(self.helper)
else:
for process in self.instances:
helpers.append(self.helper.format(
NAME=self.name,
PNAME=process.name,
PID=process.pid,
EXE=process.exe,
))
return helpers

@property
def instances(self):
return Processes.all().filtered(lambda process: process.name == self.name)
6 changes: 5 additions & 1 deletion tracer/views/default.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import View
from tracer.resources.lang import _
from tracer.views.note_for_hidden import NoteForHiddenView
import re


class DefaultView(View):
Expand All @@ -16,7 +17,10 @@ def render(self):
if len(with_helpers):
print " " + _("restart_using_helpers")
for application in with_helpers.sorted("helper"):
print " " + application.helper
helpers = "; ".join(application.helpers)
if application.helper_contains_formating and not application.helper_contains_name:
helpers += " # {}".format(application.name)
print " " + helpers

if without_helpers:
print ""
Expand Down
3 changes: 2 additions & 1 deletion tracer/views/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def render(self):
if not self.args.affected_by:
print " {app_name} does not need restarting".format(app_name=self.args.application.name)
else:
print " {how_to_restart}".format(how_to_restart=self.args.application.helper)
for helper in self.args.application.helpers:
print " {how_to_restart}".format(how_to_restart=helper)

def render_affected_by(self):

Expand Down

0 comments on commit 340e188

Please sign in to comment.