Completed
Push — master ( 5d083e...d7579e )
by russianidiot
05:48
created

cp()   F

Complexity

Conditions 15

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 15
dl 0
loc 26
rs 2.7451

How to fix   Complexity   

Complexity

Complex classes like cp() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
from distutils import dir_util
3
import os
4
from os.path import *
5
import shutil
6
# me
7
from assert_exists import *
8
from public import public
9
10
@public
11
def cp(source,target,force=True):
12
    if isinstance(source,list): # list
13
        # copy files to dir
14
        targets = []
15
        for s in source:
16
            t = cp(s,target,force)
17
            targets.append(t)
18
        return targets
19
    assert_exists(source) # assert exists
20
    if not force and exists(target): return
21
    if source==target:
22
        return target
23
    if isfile(source) and isdir(target):
24
        # target is DIR
25
        target = join(target,basename(source))
26
    if isfile(source) or islink(source):
27
        if (exists(target) or lexists(target)) and isfile(source)!=isfile(target):
28
            os.unlink(target)
29
        shutil.copy(source,target)
30
    if isdir(source):
31
        # first create dirs
32
        if not exists(target):
33
            os.makedirs(target)
34
        dir_util.copy_tree(source,target)
35
    return target
36
37
if __name__=="__main__":
38
    dst = join(os.environ["HOME"],".cp_test")
39
    cp(__file__,dst)
40
    os.unlink(dst)
41
    import shutil
42
    cp(dirname(__file__),dst)
43
    shutil.rmtree(dst)
44
    try:
45
        cp("not-existing","dst") # IOError
46
    except IOError:
47
        pass
48
49
50