Passed
Push — master ( 65ba32...c0e0be )
by russianidiot
01:13
created

_cp_dir()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
1
#!/usr/bin/env python
2
from distutils import dir_util
3
import os
4
import shutil
5
from assert_exists import assert_exists
6
from public import public
7
8
9
def _cp_file(source, target):
10
    if (os.path.exists(target) or os.path.lexists(target)):
11
        if os.path.isfile(source) != os.path.isfile(target):
12
            os.unlink(target)
13
    shutil.copy(source, target)
14
15
16
def _cp_dir(source, target):
17
    if not os.path.exists(target):
18
        os.makedirs(target)
19
    dir_util.copy_tree(source, target)
20
21
22
def _get_target(source, target):
23
    if os.path.isfile(source) and os.path.isdir(target):
24
        return os.path.join(target, os.path.basename(source))
25
    return target
26
27
28
def _copy(source, target):
29
    target = _get_target(source, target)
30
    if os.path.isfile(source) or os.path.islink(source):
31
        _cp_file(source, target)
32
    if os.path.isdir(source):
33
        _cp_dir(source, target)
34
35
36
@public
37
def cp(source, target, force=True):
38
    if (os.path.exists(target) and not force) or source == target:
39
        return
40
    assert_exists(source)
41
    _copy(source, target)
42