Skip to content

Instantly share code, notes, and snippets.

@bitprophet
Created August 20, 2011 00:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
diff -Bbru fabric-0-9-3/main.py fabric-0-9-3-p01/main.py
--- fabric-0-9-3/main.py 2010-11-12 21:57:34.000000000 +0100
+++ fabric-0-9-3-p01/main.py 2011-01-16 14:31:44.053231561 +0100
@@ -19,7 +19,7 @@
from fabric.network import denormalize, normalize
from fabric import state # For easily-mockable access to roles, env and etc
from fabric.state import commands, connections, env_options
-from fabric.utils import abort, indent
+from fabric.utils import abort, indent, prefixprint
# One-time calculation of "all internal callables" to avoid doing this on every
@@ -535,7 +535,7 @@
state.env.port = port
# Log to stdout
if state.output.running:
- print("[%s] Executing task '%s'" % (host, name))
+ print("[%s] Executing task '%s'" % (prefixprint(host,'.'), name))
# Actually run command
commands[name](*args, **kwargs)
# Put old user back
diff -Bbru fabric-0-9-3/operations.py fabric-0-9-3-p01/operations.py
--- fabric-0-9-3/operations.py 2010-11-12 21:57:34.000000000 +0100
+++ fabric-0-9-3-p01/operations.py 2011-01-16 14:44:28.205730621 +0100
@@ -17,7 +17,7 @@
from fabric.network import output_thread, needs_host
from fabric.state import env, connections, output
-from fabric.utils import abort, indent, warn, puts
+from fabric.utils import abort, indent, warn, puts, prefixprint
def _handle_failure(message, exception=None):
@@ -293,9 +293,7 @@
)
# Print
if output.running:
- print("[%s] put: %s -> %s" % (
- env.host_string, lpath, _remote_path
- ))
+ print("[%s] put: %s -> %s" % (prefixprint(env.host_string, _remote_path ), lpath, _remote_path))
# Try to catch raised exceptions (which is the only way to tell if
# this operation had problems; there's no return code) during upload
try:
@@ -405,9 +403,9 @@
"""
# Set up new var so original argument can be displayed verbatim later.
real_command = command
+ cwd = env.get('cwd', '')
if shell:
# Handle cwd munging via 'cd' context manager
- cwd = env.get('cwd', '')
if cwd:
# TODO: see if there is any nice way to quote this, given that it
# ends up inside double quotes down below...
@@ -416,9 +414,9 @@
real_command = '%s "%s"' % (env.shell,
_shell_escape(cwd + real_command))
if output.debug:
- print("[%s] run: %s" % (env.host_string, real_command))
+ print("[%s] run: %s" % (prefixprint(env.host_string,cwd if cwd else '.'), real_command))
elif output.running:
- print("[%s] run: %s" % (env.host_string, command))
+ print("[%s] run: %s" % (prefixprint(env.host_string,cwd if cwd else '.'), command))
channel = connections[env.host_string]._transport.open_session()
# Create pty if necessary (using Paramiko default options, which as of
# 1.7.4 is vt100 $TERM @ 80x24 characters)
@@ -428,9 +426,9 @@
capture = []
capture_stderr = []
- out_thread = output_thread("[%s] out" % env.host_string, channel,
+ out_thread = output_thread("[%s] out" % prefixprint(env.host_string,cwd if cwd else '.'), channel,
capture=capture)
- err_thread = output_thread("[%s] err" % env.host_string, channel,
+ err_thread = output_thread("[%s] err" % prefixprint(env.host_string,cwd if cwd else '.'), channel,
stderr=True, capture=capture_stderr)
# Close when done
@@ -509,6 +507,7 @@
# Put in explicit sudo prompt string (so we know what to look for when
# detecting prompts)
sudo_prefix = sudo_prefix % env.sudo_prompt
+ cwd = ''
# Without using a shell, we just do 'sudo -u blah my_command'
if (not env.use_shell) or (not shell):
real_command = "%s %s" % (sudo_prefix, _shell_escape(command))
@@ -522,10 +521,11 @@
cwd = 'cd %s && ' % _shell_escape(cwd)
real_command = '%s %s "%s"' % (sudo_prefix, env.shell,
_shell_escape(cwd + command))
+
if output.debug:
- print("[%s] sudo: %s" % (env.host_string, real_command))
+ print("[%s] sudo: %s" % (prefixprint(env.host_string, cwd), real_command))
elif output.running:
- print("[%s] sudo: %s" % (env.host_string, command))
+ print("[%s] sudo: %s" % (prefixprint(env.host_string, cwd), command))
channel = connections[env.host_string]._transport.open_session()
# Create pty if necessary (using Paramiko default options, which as of
# 1.7.4 is vt100 $TERM @ 80x24 characters)
@@ -535,10 +535,11 @@
channel.exec_command(real_command)
capture = []
capture_stderr = []
+ # Check time again
- out_thread = output_thread("[%s] out" % env.host_string, channel,
+ out_thread = output_thread("[%s] out" % prefixprint(env.host_string, cwd), channel,
capture=capture)
- err_thread = output_thread("[%s] err" % env.host_string, channel,
+ err_thread = output_thread("[%s] err" % prefixprint(env.host_string, cwd), channel,
stderr=True, capture=capture_stderr)
# Close channel when done
@@ -613,10 +614,11 @@
cwd = 'cd %s && ' % _shell_escape(cwd)
# Construct real command
real_command = cwd + command
+
if output.debug:
- print("[localhost] run: %s" % (real_command))
+ print("[%s] run: %s" % (prefixprint('localhost',cwd),real_command))
elif output.running:
- print("[localhost] run: " + command)
+ print("[%s] run: %s" % (prefixprint('localhost',cwd),command))
# By default, capture both stdout and stderr
PIPE = subprocess.PIPE
out_stream = PIPE
diff -Bbru fabric-0-9-3/utils.py fabric-0-9-3-p01/utils.py
--- fabric-0-9-3/utils.py 2010-11-12 21:57:34.000000000 +0100
+++ fabric-0-9-3-p01/utils.py 2011-01-16 14:50:50.203221991 +0100
@@ -97,6 +97,29 @@
if flush:
sys.stdout.flush()
+def prefixprint(host_string,cwd=None):
+ """
+ Function to customize prefix text of each task. Provide generic characters to replace
+ witch variables data. Avaible special characters:
+ \h - current hostname
+ \u - current username
+ \p - current machine port number
+ \s - working direcotyr
+ date format - POSIX compatible date format
+ """
+ from fabric.state import env
+ from fabric.network import normalize
+ from datetime import datetime
+
+ user,host, port = normalize(host_string)
+
+ format = env.prefix if env.prefix else '\h'
+ format = format.replace('\h',host)
+ format = format.replace('\u',user)
+ format = format.replace('\p',port)
+ format = format.replace('\s',cwd)
+
+ return datetime.now().strftime(format)
def fastprint(text, show_prefix=False, end="", flush=True):
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment