Dulwich.io dulwich / 62d7752
Avoid errno. Jelmer Vernooń≥ 14 days ago
11 changed file(s) with 63 addition(s) and 131 deletion(s). Raw diff Collapse all Expand all
3939
4040 from contextlib import closing
4141 from io import BytesIO, BufferedReader
42 import errno
4342 import os
4443 import select
4544 import socket
18881887 (username is None or
18891888 parsed_line.username == username)):
18901889 return parsed_line.username, parsed_line.password
1891 except OSError as e:
1892 if e.errno != errno.ENOENT:
1893 raise
1890 except FileNotFoundError:
18941891 # If the file doesn't exist, try the next one.
18951892 continue
2525 subsections
2626 """
2727
28 import errno
2928 import os
3029 import sys
3130
524523 for path in paths:
525524 try:
526525 cf = ConfigFile.from_path(path)
527 except (IOError, OSError) as e:
528 if e.errno != errno.ENOENT:
529 raise
530 else:
531 continue
526 except FileNotFoundError:
527 continue
532528 backends.append(cf)
533529 return backends
534530
1919
2020 """Safe access to git files."""
2121
22 import errno
2322 import io
2423 import os
2524 import sys
3029 """Ensure a directory exists, creating if necessary."""
3130 try:
3231 os.makedirs(dirname)
33 except OSError as e:
34 if e.errno != errno.EEXIST:
35 raise
32 except FileExistsError:
33 pass
3634
3735
3836 def _fancy_rename(oldname, newname):
126124 self._lockfilename,
127125 os.O_RDWR | os.O_CREAT | os.O_EXCL |
128126 getattr(os, "O_BINARY", 0))
129 except OSError as e:
130 if e.errno == errno.EEXIST:
131 raise FileLocked(filename, self._lockfilename)
132 raise
127 except FileExistsError:
128 raise FileLocked(filename, self._lockfilename)
133129 self._file = os.fdopen(fd, mode, bufsize)
134130 self._closed = False
135131
147143 try:
148144 os.remove(self._lockfilename)
149145 self._closed = True
150 except OSError as e:
146 except FileNotFoundError:
151147 # The file may have been removed already, which is ok.
152 if e.errno != errno.ENOENT:
153 raise
154148 self._closed = True
155149
156150 def close(self):
2020 """Parser for the git index file format."""
2121
2222 import collections
23 import errno
2423 import os
2524 import stat
2625 import struct
467466 """
468467 try:
469468 oldstat = os.lstat(target_path)
470 except OSError as e:
471 if e.errno == errno.ENOENT:
472 oldstat = None
473 else:
474 raise
469 except FileNotFoundError:
470 oldstat = None
475471 contents = blob.as_raw_string()
476472 if stat.S_ISLNK(mode):
477473 # FIXME: This will fail on Windows. What should we do instead?
678674
679675 if filter_blob_callback is not None:
680676 blob = filter_blob_callback(blob, tree_path)
681 except EnvironmentError as e:
682 if e.errno == errno.ENOENT:
683 # The file was removed, so we assume that counts as
684 # different from whatever file used to exist.
685 yield tree_path
686 else:
687 raise
677 except FileNotFoundError:
678 # The file was removed, so we assume that counts as
679 # different from whatever file used to exist.
680 yield tree_path
688681 else:
689682 if blob.id != entry.sha:
690683 yield tree_path
776769 p = _tree_to_fs_path(root_path, path)
777770 try:
778771 entry = index_entry_from_path(p, object_store=object_store)
779 except EnvironmentError as e:
780 if e.errno in (errno.ENOENT, errno.EISDIR):
781 entry = None
782 else:
783 raise
772 except (FileNotFoundError, IsADirectoryError):
773 entry = None
784774 yield path, entry
785775
786776
1717 # License, Version 2.0.
1818 #
1919
20 import errno
2120 import hashlib
2221 import os
2322 import tempfile
5150 """Open an object by sha."""
5251 try:
5352 return open(self._sha_path(sha), 'rb')
54 except (OSError, IOError) as e:
55 if e.errno == errno.ENOENT:
56 raise KeyError(sha)
57 raise
53 except FileNotFoundError:
54 raise KeyError(sha)
5855
5956 def write_object(self, chunks):
6057 """Write an object.
2222 """Git object store interfaces and implementation."""
2323
2424 from io import BytesIO
25 import errno
2625 import os
2726 import stat
2827 import sys
572571 def _read_alternate_paths(self):
573572 try:
574573 f = GitFile(os.path.join(self.path, INFODIR, "alternates"), 'rb')
575 except (OSError, IOError) as e:
576 if e.errno == errno.ENOENT:
577 return
578 raise
574 except FileNotFoundError:
575 return
579576 with f:
580577 for line in f.readlines():
581578 line = line.rstrip(b"\n")
591588 """
592589 try:
593590 os.mkdir(os.path.join(self.path, INFODIR))
594 except OSError as e:
595 if e.errno != errno.EEXIST:
596 raise
591 except FileExistsError:
592 pass
597593 alternates_path = os.path.join(self.path, INFODIR, "alternates")
598594 with GitFile(alternates_path, 'wb') as f:
599595 try:
600596 orig_f = open(alternates_path, 'rb')
601 except (OSError, IOError) as e:
602 if e.errno != errno.ENOENT:
603 raise
597 except FileNotFoundError:
598 pass
604599 else:
605600 with orig_f:
606601 f.write(orig_f.read())
614609 """Read and iterate over new pack files and cache them."""
615610 try:
616611 pack_dir_contents = os.listdir(self.pack_dir)
617 except OSError as e:
618 if e.errno == errno.ENOENT:
619 self.close()
620 return []
621 raise
612 except FileNotFoundError:
613 self.close()
614 return []
622615 pack_files = set()
623616 for name in pack_dir_contents:
624617 if name.startswith("pack-") and name.endswith(".pack"):
656649 path = self._get_shafile_path(sha)
657650 try:
658651 return ShaFile.from_path(path)
659 except (OSError, IOError) as e:
660 if e.errno == errno.ENOENT:
661 return None
662 raise
652 except FileNotFoundError:
653 return None
663654
664655 def _remove_loose_object(self, sha):
665656 os.remove(self._get_shafile_path(sha))
728719 # removal, silently passing if the target does not exist.
729720 try:
730721 os.remove(target_pack)
731 except (IOError, OSError) as e:
732 if e.errno != errno.ENOENT:
733 raise
722 except FileNotFoundError:
723 pass
734724 os.rename(path, target_pack)
735725
736726 # Write the index.
795785 # removal, silently passing if the target does not exist.
796786 try:
797787 os.remove(target_pack)
798 except (IOError, OSError) as e:
799 if e.errno != errno.ENOENT:
800 raise
788 except FileNotFoundError:
789 pass
801790 os.rename(path, target_pack)
802791 final_pack = Pack(basename)
803792 self._add_cached_pack(basename, final_pack)
838827 dir = os.path.dirname(path)
839828 try:
840829 os.mkdir(dir)
841 except OSError as e:
842 if e.errno != errno.EEXIST:
843 raise
830 except FileExistsError:
831 pass
844832 if os.path.exists(path):
845833 return # Already there, no need to write again
846834 with GitFile(path, 'wb') as f:
851839 def init(cls, path):
852840 try:
853841 os.mkdir(path)
854 except OSError as e:
855 if e.errno != errno.EEXIST:
856 raise
842 except FileExistsError:
843 pass
857844 os.mkdir(os.path.join(path, "info"))
858845 os.mkdir(os.path.join(path, PACKDIR))
859846 return cls(path)
14511451 """
14521452 with open_repo_closing(repo) as r:
14531453 from dulwich.mailmap import Mailmap
1454 import errno
14551454 try:
14561455 mailmap = Mailmap.from_path(os.path.join(r.path, '.mailmap'))
1457 except IOError as e:
1458 if e.errno != errno.ENOENT:
1459 raise
1456 except FileNotFoundError:
14601457 mailmap = Mailmap()
14611458 return mailmap.lookup(contact)
14621459
2121 """Ref handling.
2222
2323 """
24 import errno
2524 import os
2625
2726 from dulwich.errors import (
583582 path = os.path.join(self.path, b'packed-refs')
584583 try:
585584 f = GitFile(path, 'rb')
586 except IOError as e:
587 if e.errno == errno.ENOENT:
588 return {}
589 raise
585 except FileNotFoundError:
586 return {}
590587 with f:
591588 first_line = next(iter(f)).rstrip()
592589 if (first_line.startswith(b'# pack-refs') and b' peeled' in
643640 else:
644641 # Read only the first 40 bytes
645642 return header + f.read(40 - len(SYMREF))
646 except IOError as e:
647 if e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR):
648 return None
649 raise
643 except (FileNotFoundError, IsADirectoryError, NotADirectoryError):
644 return None
650645
651646 def _remove_packed_ref(self, name):
652647 if self._packed_refs is None:
722717 packed_refs = self.get_packed_refs()
723718 while probe_ref:
724719 if packed_refs.get(probe_ref, None) is not None:
725 raise OSError(errno.ENOTDIR,
726 'Not a directory: {}'.format(filename))
720 raise NotADirectoryError(filename)
727721 probe_ref = os.path.dirname(probe_ref)
728722
729723 ensure_dir_exists(os.path.dirname(filename))
817811 # remove the reference file itself
818812 try:
819813 os.remove(filename)
820 except OSError as e:
821 if e.errno != errno.ENOENT: # may only be packed
822 raise
814 except FileNotFoundError:
815 pass # may only be packed
823816
824817 self._remove_packed_ref(name)
825818 self._log(name, old_ref, None, committer=committer,
2828 """
2929
3030 from io import BytesIO
31 import errno
3231 import os
3332 import sys
3433 import stat
977976 path = os.path.join(self.controldir(), 'logs', os.fsdecode(ref))
978977 try:
979978 os.makedirs(os.path.dirname(path))
980 except OSError as e:
981 if e.errno != errno.EEXIST:
982 raise
979 except FileExistsError:
980 pass
983981 if committer is None:
984982 config = self.get_config_stack()
985983 committer = self._get_user_identity(config)
10391037 st1 = os.lstat(fname)
10401038 try:
10411039 os.chmod(fname, st1.st_mode ^ stat.S_IXUSR)
1042 except EnvironmentError as e:
1043 if e.errno == errno.EPERM:
1044 return False
1045 raise
1040 except PermissionError:
1041 return False
10461042 st2 = os.lstat(fname)
10471043
10481044 os.unlink(fname)
10661062 def _del_named_file(self, path):
10671063 try:
10681064 os.unlink(os.path.join(self.controldir(), path))
1069 except (IOError, OSError) as e:
1070 if e.errno == errno.ENOENT:
1071 return
1072 raise
1065 except FileNotFoundError:
1066 return
10731067
10741068 def get_named_file(self, path, basedir=None):
10751069 """Get a file from the control dir with a specific name.
10911085 path = path.lstrip(os.path.sep)
10921086 try:
10931087 return open(os.path.join(basedir, path), 'rb')
1094 except (IOError, OSError) as e:
1095 if e.errno == errno.ENOENT:
1096 return None
1097 raise
1088 except FileNotFoundError:
1089 return None
10981090
10991091 def index_path(self):
11001092 """Return path to the index file."""
12571249 path = os.path.join(self._controldir, 'config')
12581250 try:
12591251 return ConfigFile.from_path(path)
1260 except (IOError, OSError) as e:
1261 if e.errno != errno.ENOENT:
1262 raise
1252 except FileNotFoundError:
12631253 ret = ConfigFile()
12641254 ret.path = path
12651255 return ret
12731263 try:
12741264 with GitFile(path, 'rb') as f:
12751265 return f.read()
1276 except (IOError, OSError) as e:
1277 if e.errno != errno.ENOENT:
1278 raise
1266 except FileNotFoundError:
12791267 return None
12801268
12811269 def __repr__(self):
13401328 f.write(b'gitdir: ' + os.fsencode(worktree_controldir) + b'\n')
13411329 try:
13421330 os.mkdir(main_worktreesdir)
1343 except OSError as e:
1344 if e.errno != errno.EEXIST:
1345 raise
1331 except FileExistsError:
1332 pass
13461333 try:
13471334 os.mkdir(worktree_controldir)
1348 except OSError as e:
1349 if e.errno != errno.EEXIST:
1350 raise
1335 except FileExistsError:
1336 pass
13511337 with open(os.path.join(worktree_controldir, GITDIR), 'wb') as f:
13521338 f.write(os.fsencode(gitdirfile) + b'\n')
13531339 with open(os.path.join(worktree_controldir, COMMONDIR), 'wb') as f:
2121
2222 from __future__ import absolute_import
2323
24 import errno
2524 import os
2625
2726 from dulwich.file import GitFile
5150 try:
5251 with GitFile(reflog_path, 'rb') as f:
5352 return reversed(list(read_reflog(f)))
54 except EnvironmentError as e:
55 if e.errno == errno.ENOENT:
56 return []
57 raise
53 except FileNotFoundError:
54 return []
5855
5956 @classmethod
6057 def from_repo(cls, repo):
2020 """Tests for dulwich.porcelain."""
2121
2222 from io import BytesIO, StringIO
23 import errno
2423 import os
2524 import shutil
2625 import tarfile
138137 parent_dir = os.path.dirname(abs_path)
139138 try:
140139 os.makedirs(parent_dir)
141 except OSError as err:
142 if not err.errno == errno.EEXIST:
143 raise err
140 except FileExistsError:
141 pass
144142 with open(abs_path, 'w') as f:
145143 f.write('')
146144