dirutility.permissions   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 165
Duplicated Lines 76.97 %

Importance

Changes 0
Metric Value
eloc 83
dl 127
loc 165
rs 9.92
c 0
b 0
f 0
wmc 31

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Permissions.allow_readonly() 3 3 1
A Permissions.allow_rwe() 4 4 1
A Permissions.allow() 5 5 1
A Permissions.octal() 4 4 1
A Permissions.__init__() 2 2 1
A Permissions.revoke_access() 3 4 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
F make_octal_permissions_mode() 48 48 15
A set_permissions_mode_from_octal() 14 14 2
B get_permissions_mode() 37 37 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import os
2
import stat
3
4
PERMISSIONS = {
5
    # User permissions
6
    'user': {
7
        'read': stat.S_IRUSR,
8
        'write': stat.S_IWUSR,
9
        'execute': stat.S_IXUSR,
10
        'all': stat.S_IRWXU,
11
    },
12
13
    # Group permissions
14
    'group': {
15
        'read': stat.S_IRGRP,
16
        'write': stat.S_IWGRP,
17
        'execute': stat.S_IXGRP,
18
        'all': stat.S_IRWXG,
19
    },
20
21
    # Other permissions
22
    'other': {
23
        'read': stat.S_IROTH,
24
        'write': stat.S_IWOTH,
25
        'execute': stat.S_IXOTH,
26
        'all': stat.S_IRWXO,
27
    }
28
}
29
30
31 View Code Duplication
def make_octal_permissions_mode(user=(False, False, False), group=(False, False, False), other=(False, False, False)):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
32
    """
33
    Create a permissions bit in absolute notation (octal).
34
35
    The user, group and other parameters are tuples representing Read, Write and Execute values.
36
    All are set disallowed (set to false) by default and can be individually permitted.
37
38
    :param user: User permissions
39
    :param group: Group permissions
40
    :param other: Other permissions
41
    :return: Permissions bit
42
    """
43
    # Create single digit code for each name
44
    mode = ''
45
    for name in (user, group, other):
46
        read, write, execute = name
47
48
        # Execute
49
        if execute and not all(i for i in (read, write)):
50
            code = 1
51
52
        # Write
53
        elif write and not all(i for i in (read, execute)):
54
            code = 2
55
56
        # Write & Execute
57
        elif all(i for i in (write, execute)) and not read:
58
            code = 3
59
60
        # Read
61
        elif read and not all(i for i in (write, execute)):
62
            code = 4
63
64
        # Read & Execute
65
        elif all(i for i in (read, execute)) and not write:
66
            code = 5
67
68
        # Read & Write
69
        elif all(i for i in (read, write)) and not execute:
70
            code = 6
71
72
        # Read, Write & Execute
73
        elif all(i for i in (read, write, execute)):
74
            code = 7
75
        else:
76
            code = 0
77
        mode += str(code)
78
    return int(mode)
79
80
81 View Code Duplication
def set_permissions_mode_from_octal(file_path, code):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
    """
83
    Set permissions for a file or directory.
84
85
    :param file_path: Path to a file or directory
86
    :param code: Permission code in absolute notation (octal)
87
    :return:
88
    """
89
    # Unpack permissions tuple
90
    user, group, other = tuple(str(code[-3:])) if len(str(code)) > 3 else tuple(str(code))
91
    user, group, other = int(user), int(group), int(other)
92
    mode = get_permissions_mode(user, 'user') & get_permissions_mode(group, 'group') & get_permissions_mode(
93
        other, 'other')
94
    os.chmod(file_path, mode)
95
96
97 View Code Duplication
def get_permissions_mode(permission_octal, name):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
    """Retrieve a user name group permissions bitwise code."""
99
    read = PERMISSIONS[name]['read']
100
    write = PERMISSIONS[name]['write']
101
    execute = PERMISSIONS[name]['execute']
102
103
    # Read
104
    if permission_octal == 4:
105
        return read & ~write & ~execute
106
107
    # Write
108
    elif permission_octal == 2:
109
        return ~read & write & ~execute
110
111
    # Execute
112
    elif permission_octal == 1:
113
        return ~read & ~write & execute
114
115
    # Read & Write
116
    elif permission_octal == 6:
117
        return read & write & ~execute
118
119
    # Read & Execute
120
    elif permission_octal == 5:
121
        return read & ~write & execute
122
123
    # Write & Execute
124
    elif permission_octal == 3:
125
        return ~read & write & execute
126
127
    # Read, Write & Execute
128
    elif permission_octal == 7:
129
        return read & write & execute
130
131
    # No read, write or execute by default
132
    else:
133
        return ~read & ~write & ~execute
134
135
136 View Code Duplication
class Permissions:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
137
138
    def __init__(self, file_path):
139
        self.file_path = file_path
140
141
    @property
142
    def octal(self):
143
        """Return file permissions in absolute notation (octal) format."""
144
        return stat.S_IMODE(os.lstat(self.file_path).st_mode)
145
146
    def allow(self, privilege):
147
        """Add an allowed privilege (read, write, execute, all)."""
148
        assert privilege in PERMISSIONS['user'].keys()
149
        reading = PERMISSIONS['user'][privilege] + PERMISSIONS['group'][privilege] + PERMISSIONS['other'][privilege]
150
        os.chmod(self.file_path, reading)
151
152
    def allow_readonly(self):
153
        """Add an allowed privilege (read, write, execute, all)."""
154
        os.chmod(self.file_path, 0o777)
155
156
    def allow_rwe(self, name):
157
        """Allow all privileges for a particular name group (user, group, other)."""
158
        assert name in PERMISSIONS.keys()
159
        os.chmod(self.file_path, PERMISSIONS[name]['all'])
160
161
    def revoke_access(self):
162
        """Revoke all access to this path."""
163
        reading = PERMISSIONS['user']['execute'] + PERMISSIONS['group']['execute'] + PERMISSIONS['other']['execute']
164
        os.chmod(self.file_path, reading)
165