Of course, this was supposed to go to the list...

Kevin Horn

---------- Forwarded message ----------
From: Kevin Horn <[email protected]>
Date: Tue, Mar 1, 2011 at 2:28 PM
Subject: Re: [Fab-user] Putting Folders
To: Bruno Clermont <[email protected]>


On Tue, Feb 1, 2011 at 7:27 AM, Bruno Clermont <[email protected]>wrote:

> # here is my library I wrote for that (look for putdir function)
> # never tested on Windows
>
> import os
> import tempfile
> import tarfile
> import uuid
> import logging
>
> import fabric.api
> import fabric.operations
> import fabric.contrib.files
> import fabric.context_managers
>
> def mktemp(prefix='/tmp'):
>    "return a random remote filename"
>    return '/'.join((prefix, str(uuid.uuid4())))
>
> def change_permissions(filename, uid=None, gid=None, mode=None,
>                       recursive=False):
>    "change uid and gid of specified file"
>
>    def command(command, option, filename):
>        "run command with options recursively or not"
>        if recursive:
>            template = '%s -R %s %s'
>        else:
>            template = '%s %s %s'
>        fabric.operations.run(template % (command, option, filename))
>
>    if fabric.contrib.files.exists(filename):
>        logging.debug("Found remote file '%s', change permissions",
> filename)
>        if uid:
>            command('chown', uid, filename)
>        if gid:
>            command('chgrp', gid, filename)
>        if mode:
>            command('chmod', mode, filename)
>    else:
>        logging.warning("Can't change ownership of '%s', file does not
> exists",
>                        filename)
>
> def putdir(directory, target, uid=None, gid=None):
>    "copy specified directory content to remote target directory"
>    if not os.path.exists(directory):
>        raise IOError("Invalid directory '%s'" % directory)
>    if not os.path.isdir(directory):
>        raise IOError("'%s' is not a directory" % directory)
>
>    tmp_tar = tempfile.mkstemp()
>    filename = tmp_tar[1]
>    curdir = os.path.abspath(os.curdir)
>    os.chdir(directory)
>
>    contents = os.listdir(directory)
>
>    tarfh = tarfile.open(filename, 'w:bz2')
>    for content in contents:
>        tarfh.add(content)
>    tarfh.close()
>    os.chdir(curdir)
>
>    remote_filename = mktemp()
>    fabric.operations.put(filename, remote_filename)
>    os.unlink(filename)
>    if not fabric.contrib.files.exists(target):
>        fabric.operations.run("mkdir %s" % target)
>    with fabric.context_managers.cd(target):
>        fabric.operations.run("tar -xjf %s" % remote_filename)
>        for content in contents:
>            change_permissions(content, uid, gid, recursive=True)
>    fabric.operations.run('rm -f %s' % remote_filename)
>
> # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
>
>

Here's a version I use to move files from a Windows machine to a Linux
server:
https://bitbucket.org/khorn/lore2sphinx/src/71729e79b94b/put_dir.py

I think it might be modified from an older version of Bruno's code above.

It's not very generic, but it works for me. YMMV.

Kevin Horn
_______________________________________________
Fab-user mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/fab-user

Reply via email to