~ubuntu-branches/ubuntu/oneiric/weave/oneiric

« back to all changes in this revision

Viewing changes to tools/scripts/share.py

  • Committer: Bazaar Package Importer
  • Author(s): Micah Gersten
  • Date: 2010-08-11 00:35:15 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100811003515-o3jbh826bnd1syjv
Tags: 1.4.3-1ubuntu1
* Add -fshort-wchar to CXXFLAGS to fix FTBFS in Ubuntu
  - update debian/rules 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
    Usage: python share.py <root-dir> <owner> <cmd-json>
3
 
 
4
 
    This utility can be used in conjunction with an Apache-based
5
 
    WebDAV server to create .htaccess files with readability
6
 
    permissions that are set by a privileged remote client.
7
 
 
8
 
    <root-dir> is the local root directory where per-user
9
 
    directories are kept.
10
 
 
11
 
    <owner> is the name of the user who is sharing a
12
 
    directory.  Their user directory is assumed to be in
13
 
    <root-dir>/<owner>.
14
 
 
15
 
    <cmd-json> is JSON that contains information about the
16
 
    share.  It must have the following keys:
17
 
 
18
 
        version: This must be 1.
19
 
 
20
 
        directory: This is the directory to be shared, relative to
21
 
                   the owner's user directory.
22
 
 
23
 
        share_to_users: This is a list of the users who should be
24
 
                        given access to read the directory.  If it is
25
 
                        a list with "all" as its only element, then
26
 
                        the directory is readable by anyone.
27
 
 
28
 
    If successful, the script displays nothing and exits with a
29
 
    return code of 0.  Otherwise, it displays an error and exits
30
 
    with a nonzero return code.
31
 
"""
32
 
 
33
 
import os
34
 
import sys
35
 
import json
36
 
 
37
 
def make_read_share_htaccess(owner, users):
38
 
    if users == ["all"]:
39
 
        read_require_perms = "valid-user"
40
 
    else:
41
 
        users = set(users + [owner])
42
 
        users = list(users)
43
 
        users.sort()
44
 
        read_require_perms = "user %s" % " ".join(users)
45
 
    lines = [
46
 
        "Options +Indexes",
47
 
        "<Limit GET PROPFIND>",
48
 
        "Require %s" % read_require_perms,
49
 
        "</Limit>",
50
 
        "<LimitExcept GET PROPFIND>",
51
 
        "Require user %s" % owner,
52
 
        "</LimitExcept>",
53
 
        ""
54
 
        ]
55
 
    return "\n".join(lines)
56
 
 
57
 
def write_htaccess(root_dir, owner, cmd, file_open = open):
58
 
    htaccess = make_read_share_htaccess(owner,
59
 
                                        cmd["share_to_users"])
60
 
    user_root_dir = os.path.join(root_dir, owner)
61
 
    path = os.path.join(user_root_dir, cmd["directory"], ".htaccess")
62
 
    path = os.path.normpath(path)
63
 
    if not path.startswith(user_root_dir):
64
 
        raise Exception("Path doesn't start with user root dir: %s" % path)
65
 
    file_open(path, "w").write(htaccess)
66
 
 
67
 
def write_htaccess_from_json(root_dir, owner, cmd_json,
68
 
                             write_htaccess = write_htaccess):
69
 
    write_htaccess(root_dir, owner, json.read(cmd_json))
70
 
 
71
 
if __name__ == "__main__":
72
 
    args = sys.argv[1:]
73
 
    if len(args) < 3:
74
 
        __main__ = __import__("__main__")
75
 
        print __main__.__doc__
76
 
        sys.exit(1)
77
 
    root_dir, owner, cmd_json = args
78
 
    write_htaccess_from_json(root_dir, owner, cmd_json)