Skip to content

Commit

Permalink
Minor syntax cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien committed Mar 14, 2012
1 parent b9699fd commit 3eb43d5
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/cuisine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# License : Revised BSD License
# -----------------------------------------------------------------------------
# Creation : 26-Apr-2010
# Last mod : 13-Feb-2012
# Last mod : 14-Mar-2012
# -----------------------------------------------------------------------------

"""
Expand Down Expand Up @@ -41,7 +41,7 @@
import base64, bz2, crypt, hashlib, os, random, sys, re, string, tempfile, subprocess, types, functools
import fabric, fabric.api, fabric.operations, fabric.context_managers

VERSION = "0.2.3"
VERSION = "0.2.4"

RE_SPACES = re.compile("[\s\t]+")
MAC_EOL = "\n"
Expand Down Expand Up @@ -105,7 +105,6 @@ def mode_remote():
else:
return False


class mode_user(object):
"""Cuisine functions will be executed as the current user."""

Expand Down Expand Up @@ -149,7 +148,6 @@ def select_package( option=None ):
fabric.api.env["option_package"] = option
return (fabric.api.env["option_package"], supported)


# =============================================================================
#
# RUN/SUDO METHODS
Expand Down Expand Up @@ -238,7 +236,6 @@ def wrapper(*args, **kwargs):
else:
return dispatch_wrapper


# =============================================================================
#
# TEXT PROCESSING
Expand Down Expand Up @@ -386,14 +383,15 @@ def file_write(location, content, mode=None, owner=None, group=None, sudo=None):
os.write(fd, content)
# Upload the content if necessary
if not file_exists(location) or sig != file_sha256(location):
if MODE_SUDO: sudo = MODE_SUDO
if MODE_LOCAL:
run('cp "%s" "%s"'%(local_path,location))
else:
try:
fabric.operations.put(local_path, location, use_sudo=sudo)
except Exception, e:
print "cuisine.file_write exception:"
if MODE_LOCAL:
run('cp "%s" "%s"'%(local_path,location))
else:
if MODE_SUDO:
sudo = MODE_SUDO
try:
fabric.operations.put(local_path, location, use_sudo=sudo)
except Exception, e:
print "cuisine.file_write exception:"
# Remove the local temp file
os.close(fd)
os.unlink(local_path)
Expand All @@ -419,7 +417,13 @@ def file_upload(remote, local, sudo=None):
sig = hashlib.sha256(content).hexdigest()
if MODE_SUDO: sudo = MODE_SUDO
if not file_exists(remote) or sig != file_sha256(remote):
fabric.operations.put(local, remote, use_sudo=sudo)
if MODE_LOCAL:
if sudo:
sudo('cp "%s" "%s"'%(local,remote))
else:
run('cp "%s" "%s"'%(local,remote))
else:
fabric.operations.put(local, remote, use_sudo=sudo)

def file_update(location, updater=lambda x: x):
"""Updates the content of the given by passing the existing
Expand All @@ -434,14 +438,12 @@ def file_update(location, updater=lambda x: x):
assert file_exists(location), "File does not exists: " + location
new_content = updater(file_read(location))
# assert type(new_content) in (str, unicode, fabric.operations._AttributeString), "Updater must be like (string)->string, got: %s() = %s" % (updater, type(new_content))
run('echo "%s" | base64 -d > "%s"' %
(base64.b64encode(new_content), location))
run('echo "%s" | base64 -d > "%s"' % (base64.b64encode(new_content), location))

def file_append(location, content, mode=None, owner=None, group=None):
"""Appends the given content to the remote file at the given
location, optionally updating its mode/owner/group."""
run('echo "%s" | base64 -d >> "%s"' %
(base64.b64encode(content), location))
run('echo "%s" | base64 -d >> "%s"' % (base64.b64encode(content), location))
file_attribs(location, mode, owner, group)

def file_unlink(path):
Expand All @@ -468,11 +470,6 @@ def file_sha256(location):
# be on the safe side.
return run('sha256sum "%s" | cut -d" " -f1' % (location)).split("\n")[-1]

# TODO: From McCoy's version, consider merging
# def file_append( location, content, use_sudo=False, partial=False, escape=True):
# """Wrapper for fabric.contrib.files.append."""
# fabric.contrib.files.append(location, content, use_sudo, partial, escape)

# =============================================================================
#
# DIRECTORY OPERATIONS
Expand Down Expand Up @@ -554,7 +551,6 @@ def package_install_apt(package, update=False):
package = " ".join(package)
sudo("apt-get --yes install %s" % (package))


def package_ensure_apt(package, update=False):
status = run("dpkg-query -W -f='${Status}' %s ; true" % package)
if status.find("not-installed") != -1 or status.find("installed") == -1:
Expand All @@ -574,7 +570,6 @@ def command_check(command):
"""Tests if the given command is available on the system."""
return run("which '%s' >& /dev/null && echo OK ; true" % command).endswith("OK")


def command_ensure(command, package=None):
"""Ensures that the given command is present, if not installs the
package with the given name, which is the same as the command by
Expand Down Expand Up @@ -811,7 +806,4 @@ def locale_ensure(locale):
for option, value in DEFAULT_OPTIONS.items():
eval("select_" + option)(value)




# EOF - vim: ts=4 sw=4 noet

0 comments on commit 3eb43d5

Please sign in to comment.