Ejemplo n.º 1
0
 def test_isfile_strict(self):
     this_file = os.path.abspath(__file__)
     assert isfile_strict(this_file)
     assert not isfile_strict(os.path.dirname(this_file))
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EPERM, "foo")):
         self.assertRaises(OSError, isfile_strict, this_file)
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EACCES, "foo")):
         self.assertRaises(OSError, isfile_strict, this_file)
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.ENOENT, "foo")):
         assert not isfile_strict(this_file)
     with mock.patch('psutil._common.stat.S_ISREG', return_value=False):
         assert not isfile_strict(this_file)
Ejemplo n.º 2
0
 def open_files(self):
     retlist = []
     files = os.listdir("/proc/%s/fd" % self.pid)
     hit_enoent = False
     for fd in files:
         file = "/proc/%s/fd/%s" % (self.pid, fd)
         try:
             file = os.readlink(file)
         except OSError as err:
             # ENOENT == file which is gone in the meantime
             if err.errno in (errno.ENOENT, errno.ESRCH):
                 hit_enoent = True
                 continue
             elif err.errno == errno.EINVAL:
                 # not a link
                 continue
             else:
                 raise
         else:
             # If file is not an absolute path there's no way
             # to tell whether it's a regular file or not,
             # so we skip it. A regular file is always supposed
             # to be absolutized though.
             if file.startswith('/') and isfile_strict(file):
                 ntuple = _common.popenfile(file, int(fd))
                 retlist.append(ntuple)
     if hit_enoent:
         # raise NSP if the process disappeared on us
         os.stat('/proc/%s' % self.pid)
     return retlist
Ejemplo n.º 3
0
 def test_isfile_strict(self):
     from psutil._common import isfile_strict
     this_file = os.path.abspath(__file__)
     assert isfile_strict(this_file)
     assert not isfile_strict(os.path.dirname(this_file))
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EPERM, "foo")):
         self.assertRaises(OSError, isfile_strict, this_file)
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EACCES, "foo")):
         self.assertRaises(OSError, isfile_strict, this_file)
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EINVAL, "foo")):
         assert not isfile_strict(this_file)
     with mock.patch('psutil._common.stat.S_ISREG', return_value=False):
         assert not isfile_strict(this_file)
Ejemplo n.º 4
0
 def open_files(self):
     retlist = []
     files = os.listdir("/proc/%s/fd" % self.pid)
     hit_enoent = False
     for fd in files:
         file = "/proc/%s/fd/%s" % (self.pid, fd)
         if os.path.islink(file):
             try:
                 file = os.readlink(file)
             except OSError:
                 # ENOENT == file which is gone in the meantime
                 err = sys.exc_info()[1]
                 if err.errno == errno.ENOENT:
                     hit_enoent = True
                     continue
                 raise
             else:
                 # If file is not an absolute path there's no way
                 # to tell whether it's a regular file or not,
                 # so we skip it. A regular file is always supposed
                 # to be absolutized though.
                 if file.startswith('/') and isfile_strict(file):
                     ntuple = _common.popenfile(file, int(fd))
                     retlist.append(ntuple)
     if hit_enoent:
         # raise NSP if the process disappeared on us
         os.stat('/proc/%s' % self.pid)
     return retlist
Ejemplo n.º 5
0
 def open_files(self):
     if self.pid == 0:
         return []
     files = []
     rawlist = cext.proc_open_files(self.pid)
     for path, fd in rawlist:
         if isfile_strict(path):
             ntuple = _common.popenfile(path, fd)
             files.append(ntuple)
     return files
Ejemplo n.º 6
0
 def open_files(self):
     if self.pid == 0:
         return []
     files = []
     rawlist = cext.proc_open_files(self.pid)
     for path, fd in rawlist:
         if isfile_strict(path):
             ntuple = _common.popenfile(path, fd)
             files.append(ntuple)
     return files
Ejemplo n.º 7
0
 def open_files(self):
     if self.pid in (0, 4):
         return []
     retlist = []
     # Filenames come in in native format like:
     # "\Device\HarddiskVolume1\Windows\systemew\file.txt"
     # Convert the first part in the corresponding drive letter
     # (e.g. "C:\") by using Windows's QueryDosDevice()
     raw_file_names = cext.proc_open_files(self.pid)
     for file in raw_file_names:
         file = _convert_raw_path(file)
         if isfile_strict(file) and file not in retlist:
             ntuple = _common.popenfile(file, -1)
             retlist.append(ntuple)
     return retlist
Ejemplo n.º 8
0
 def open_files(self):
     if self.pid in (0, 4):
         return []
     retlist = []
     # Filenames come in in native format like:
     # "\Device\HarddiskVolume1\Windows\systemew\file.txt"
     # Convert the first part in the corresponding drive letter
     # (e.g. "C:\") by using Windows's QueryDosDevice()
     raw_file_names = cext.proc_open_files(self.pid)
     for file in raw_file_names:
         file = _convert_raw_path(file)
         if isfile_strict(file) and file not in retlist:
             ntuple = _common.popenfile(file, -1)
             retlist.append(ntuple)
     return retlist
Ejemplo n.º 9
0
 def open_files(self):
     retlist = []
     hit_enoent = False
     pathdir = '/proc/%d/path' % self.pid
     for fd in os.listdir('/proc/%d/fd' % self.pid):
         path = os.path.join(pathdir, fd)
         if os.path.islink(path):
             try:
                 file = os.readlink(path)
             except OSError as err:
                 # ENOENT == file which is gone in the meantime
                 if err.errno == errno.ENOENT:
                     hit_enoent = True
                     continue
                 raise
             else:
                 if isfile_strict(file):
                     retlist.append(_common.popenfile(file, int(fd)))
     if hit_enoent:
         # raise NSP if the process disappeared on us
         os.stat('/proc/%s' % self.pid)
     return retlist
Ejemplo n.º 10
0
 def open_files(self, ret, info):
     self.assertIsInstance(ret, list)
     for f in ret:
         self.assertIsInstance(f.fd, int)
         self.assertIsInstance(f.path, str)
         if WINDOWS:
             self.assertEqual(f.fd, -1)
         elif LINUX:
             self.assertIsInstance(f.position, int)
             self.assertIsInstance(f.mode, str)
             self.assertIsInstance(f.flags, int)
             self.assertGreaterEqual(f.position, 0)
             self.assertIn(f.mode, ('r', 'w', 'a', 'r+', 'a+'))
             self.assertGreater(f.flags, 0)
         elif BSD and not f.path:
             # XXX see: https://github.com/giampaolo/psutil/issues/595
             continue
         assert os.path.isabs(f.path), f
         try:
             assert isfile_strict(f.path), f
         except FileNotFoundError:
             pass
Ejemplo n.º 11
0
 def open_files(self):
     retlist = []
     hit_enoent = False
     pathdir = '/proc/%d/path' % self.pid
     for fd in os.listdir('/proc/%d/fd' % self.pid):
         path = os.path.join(pathdir, fd)
         if os.path.islink(path):
             try:
                 file = os.readlink(path)
             except OSError as err:
                 # ENOENT == file which is gone in the meantime
                 if err.errno == errno.ENOENT:
                     hit_enoent = True
                     continue
                 raise
             else:
                 if isfile_strict(file):
                     retlist.append(_common.popenfile(file, int(fd)))
     if hit_enoent:
         # raise NSP if the process disappeared on us
         os.stat('/proc/%s' % self.pid)
     return retlist