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
Loading history...
|
|||
19 | return string |
||
20 | except NameError: |
||
21 | pass |
||
22 | |||
23 | return string.decode(encoding) |
||
24 | |||
25 | return string |
||
26 | |||
27 | def _bytes(string): |
||
28 | if constants.python_version == 3: |
||
29 | return bytes(string, 'utf8') |
||
30 | else: |
||
31 | return bytes(string) |
||
32 | |||
33 | def _copyfile(src, dst): |
||
34 | # shutil.copy seems slow, changing to streaming according to |
||
35 | # http://stackoverflow.com/questions/22078621/python-how-to-copy-files-fast # noqa |
||
36 | # Python 3 hangs using open/write method so we proceed with shutil.copy |
||
37 | # and only perform the optimized write for Python 2. |
||
38 | if (constants.python_version == 3): |
||
0 ignored issues
–
show
|
|||
39 | # Do not use copy2(), it will have an issue when copying to a |
||
40 | # network/mounted drive. |
||
41 | # Using copy and manual set_date_from_filename gets the job done. |
||
42 | # The calling function is responsible for setting the time. |
||
43 | shutil.copy(src, dst) |
||
44 | return |
||
45 | |||
46 | try: |
||
47 | O_BINARY = os.O_BINARY |
||
48 | except: |
||
49 | O_BINARY = 0 |
||
50 | |||
51 | READ_FLAGS = os.O_RDONLY | O_BINARY |
||
52 | WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY |
||
53 | TEN_MEGABYTES = 10485760 |
||
54 | BUFFER_SIZE = min(TEN_MEGABYTES, os.path.getsize(src)) |
||
55 | |||
56 | try: |
||
57 | fin = os.open(src, READ_FLAGS) |
||
58 | stat = os.fstat(fin) |
||
59 | fout = os.open(dst, WRITE_FLAGS, stat.st_mode) |
||
60 | for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""): |
||
61 | os.write(fout, x) |
||
62 | finally: |
||
63 | try: |
||
64 | os.close(fin) |
||
65 | except: |
||
66 | pass |
||
67 | try: |
||
68 | os.close(fout) |
||
69 | except: |
||
70 | pass |
||
71 | |||
72 | |||
73 | # If you want cross-platform overwriting of the destination, |
||
74 | # use os.replace() instead of rename(). |
||
75 | # https://docs.python.org/3/library/os.html#os.rename |
||
76 | def _rename(src, dst): |
||
77 | if (constants.python_version == 3): |
||
0 ignored issues
–
show
|
|||
78 | return os.replace(src, dst) |
||
79 | else: |
||
80 | return os.rename(src, dst) |
||
81 |