Passed
Push — master ( 9ddb09...7fc5b5 )
by Jaisen
01:14
created

elodie.compatability._rename()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import os
2
import shutil
3
import sys
4
5
from elodie import constants
6
7
8
def _decode(string, encoding=sys.getfilesystemencoding()):
9
    """Return a utf8 encoded unicode string.
10
11
    Python2 and Python3 differ in how they handle strings.
12
    So we do a few checks to see if the string is ascii or unicode.
13
    Then we decode it if needed.
14
    """
15
    if hasattr(string, 'decode'):
16
        # If the string is already unicode we return it.
17
        try:
18
            if isinstance(string, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaisen Mathai
The variable unicode does not seem to be defined.
Loading history...
19
                return string
20
        except NameError:
21
            pass
22
23
        return string.decode(encoding)
24
25
    return string
26
27
28
def _copyfile(src, dst):
29
    # shutil.copy seems slow, changing to streaming according to
30
    # http://stackoverflow.com/questions/22078621/python-how-to-copy-files-fast  # noqa
31
    # Python 3 hangs using open/write method so we proceed with shutil.copy
32
    #  and only perform the optimized write for Python 2.
33
    if (constants.python_version == 3):
34
        # Do not use copy2(), it will have an issue when copying to a
35
        #  network/mounted drive.
36
        # Using copy and manual set_date_from_filename gets the job done.
37
        # The calling function is responsible for setting the time.
38
        shutil.copy(src, dst)
39
        return
40
41
    try:
42
        O_BINARY = os.O_BINARY
43
    except:
44
        O_BINARY = 0
45
46
    READ_FLAGS = os.O_RDONLY | O_BINARY
47
    WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
48
    TEN_MEGABYTES = 10485760
49
    BUFFER_SIZE = min(TEN_MEGABYTES, os.path.getsize(src))
50
51
    try:
52
        fin = os.open(src, READ_FLAGS)
53
        stat = os.fstat(fin)
54
        fout = os.open(dst, WRITE_FLAGS, stat.st_mode)
55
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
0 ignored issues
show
introduced by Jaisen Mathai
The variable BUFFER_SIZE does not seem to be defined for all execution paths.
Loading history...
introduced by Jaisen Mathai
The variable fin does not seem to be defined for all execution paths.
Loading history...
56
            os.write(fout, x)
57
    finally:
58
        try:
59
            os.close(fin)
60
        except:
61
            pass
62
        try:
63
            os.close(fout)
64
        except:
65
            pass
66
67
68
# If you want cross-platform overwriting of the destination, 
69
# use os.replace() instead of rename().
70
# https://docs.python.org/3/library/os.html#os.rename
71
def _rename(src, dst):
72
    if (constants.python_version == 3):
73
        return os.replace(src, dst)
74
    else:
75
        return os.rename(src, dst)
76