|
27 | 27 |
|
28 | 28 | from pip._internal.exceptions import InstallationError
|
29 | 29 | from pip._internal.locations import get_major_minor_version
|
| 30 | +from pip._internal.utils.filesystem import adjacent_tmp_file, replace |
30 | 31 | from pip._internal.utils.misc import captured_stdout, ensure_dir, hash_file
|
31 | 32 | from pip._internal.utils.temp_dir import TempDirectory
|
32 | 33 | from pip._internal.utils.typing import MYPY_CHECK_RUNNING
|
|
36 | 37 | if MYPY_CHECK_RUNNING:
|
37 | 38 | from email.message import Message
|
38 | 39 | from typing import (
|
39 |
| - Dict, List, Optional, Sequence, Tuple, IO, Text, Any, |
| 40 | + Dict, List, Optional, Sequence, Tuple, Any, |
40 | 41 | Iterable, Callable, Set,
|
41 | 42 | )
|
42 | 43 |
|
@@ -64,15 +65,15 @@ def rehash(path, blocksize=1 << 20):
|
64 | 65 | return (digest, str(length)) # type: ignore
|
65 | 66 |
|
66 | 67 |
|
67 |
| -def open_for_csv(name, mode): |
68 |
| - # type: (str, Text) -> IO[Any] |
69 |
| - if sys.version_info[0] < 3: |
70 |
| - nl = {} # type: Dict[str, Any] |
71 |
| - bin = 'b' |
| 68 | +def csv_io_kwargs(mode): |
| 69 | + # type: (str) -> Dict[str, Any] |
| 70 | + """Return keyword arguments to properly open a CSV file |
| 71 | + in the given mode. |
| 72 | + """ |
| 73 | + if sys.version_info.major < 3: |
| 74 | + return {'mode': '{}b'.format(mode)} |
72 | 75 | else:
|
73 |
| - nl = {'newline': ''} # type: Dict[str, Any] |
74 |
| - bin = '' |
75 |
| - return open(name, mode + bin, **nl) |
| 76 | + return {'mode': mode, 'newline': ''} |
76 | 77 |
|
77 | 78 |
|
78 | 79 | def fix_script(path):
|
@@ -563,28 +564,25 @@ def is_entrypoint_wrapper(name):
|
563 | 564 | logger.warning(msg)
|
564 | 565 |
|
565 | 566 | # Record pip as the installer
|
566 |
| - installer = os.path.join(dest_info_dir, 'INSTALLER') |
567 |
| - temp_installer = os.path.join(dest_info_dir, 'INSTALLER.pip') |
568 |
| - with open(temp_installer, 'wb') as installer_file: |
| 567 | + installer_path = os.path.join(dest_info_dir, 'INSTALLER') |
| 568 | + with adjacent_tmp_file(installer_path) as installer_file: |
569 | 569 | installer_file.write(b'pip\n')
|
570 |
| - shutil.move(temp_installer, installer) |
571 |
| - generated.append(installer) |
| 570 | + replace(installer_file.name, installer_path) |
| 571 | + generated.append(installer_path) |
572 | 572 |
|
573 | 573 | # Record details of all files installed
|
574 |
| - record = os.path.join(dest_info_dir, 'RECORD') |
575 |
| - temp_record = os.path.join(dest_info_dir, 'RECORD.pip') |
576 |
| - with open_for_csv(record, 'r') as record_in: |
577 |
| - with open_for_csv(temp_record, 'w+') as record_out: |
578 |
| - reader = csv.reader(record_in) |
579 |
| - outrows = get_csv_rows_for_installed( |
580 |
| - reader, installed=installed, changed=changed, |
581 |
| - generated=generated, lib_dir=lib_dir, |
582 |
| - ) |
583 |
| - writer = csv.writer(record_out) |
584 |
| - # Sort to simplify testing. |
585 |
| - for row in sorted_outrows(outrows): |
586 |
| - writer.writerow(row) |
587 |
| - shutil.move(temp_record, record) |
| 574 | + record_path = os.path.join(dest_info_dir, 'RECORD') |
| 575 | + with open(record_path, **csv_io_kwargs('r')) as record_file: |
| 576 | + rows = get_csv_rows_for_installed( |
| 577 | + csv.reader(record_file), |
| 578 | + installed=installed, |
| 579 | + changed=changed, |
| 580 | + generated=generated, |
| 581 | + lib_dir=lib_dir) |
| 582 | + with adjacent_tmp_file(record_path, **csv_io_kwargs('w')) as record_file: |
| 583 | + writer = csv.writer(record_file) |
| 584 | + writer.writerows(sorted_outrows(rows)) # sort to simplify testing |
| 585 | + replace(record_file.name, record_path) |
588 | 586 |
|
589 | 587 |
|
590 | 588 | def install_wheel(
|
|
0 commit comments