Ejemplo n.º 1
0
 def test_Pathname(self):
     path = Pathname("file with \n char")
     self.assertIsInstance(path, Pathname)
     self.assertEqual(path, "file with \\n char")
     self.assertEqual(path.raw, "file with \n char")
     path = Pathname("no funky chars")
     self.assertEqual(path, "no funky chars")
     path = Pathname(None)
     self.assertIsNone(path)
Ejemplo n.º 2
0
 def mountinfo(self):
     """
     Parses /proc/<pid>/mountinfo and returns a list of AttrDict's
     """
     fields = ('mnt_id', 'parent_id', 'major', 'minor', 'root', 'mount',
               'options', 'optional', 'fstype', 'source', 'super_options')
     with open("mountinfo", opener=self._opener) as file:
         lines = file.read().splitlines()
     regex = r'(\S+) (\S+) (\d+):(\d+) (\S+) (\S+) (\S+) (.*? - )(\S+) (\S+) (\S+)'
     entries = [
         AttrDict(zip(fields,
                      re.findall(regex, _)[0])) for _ in lines
     ]
     for entry in entries:
         entry.update(
             mount=Pathname(entry.mount),
             optional=AttrDict([
                 _.split(':') for _ in entry.optional[:-len(" - ")].split()
             ]),
             options=AttrDict({
                 k: try_int(v[0]) if v else None
                 for k, *v in
                 [_.split('=', 1) for _ in entry.options.split(',')]
             }),
             super_options=AttrDict({
                 k: try_int(v[0]) if v else None
                 for k, *v in
                 [_.split('=', 1) for _ in entry.super_options.split(',')]
             }))
     return entries
Ejemplo n.º 3
0
 def _swaps(self):
     """
     Parses /proc/swaps and returns a list of AttrDict's
     """
     with open("swaps", opener=self._opener, encoding="utf-8") as file:
         keys, *values = file.read().splitlines()
     entries = [AttrDict(zip(keys.split(), _.rsplit(maxsplit=5))) for _ in values]
     for entry in entries:
         entry.update(Filename=Pathname(entry.Filename))
     return entries
Ejemplo n.º 4
0
 def test_CustomJSONEncoder(self):
     d = {
         'time': Time(0.0),
         'uid': Uid(0),
         'gid': Gid(0),
         'ip': IPAddr('0' * 8),
         'path': Pathname("/etc")
     }
     s = '{"gid": 0, "ip": "0.0.0.0", "path": "/etc", "time": "Thu Jan  1 00:00:00 1970", "uid": 0}'
     self.assertEqual(
         s, json.dumps(AttrDict(d), cls=CustomJSONEncoder, sort_keys=True))
Ejemplo n.º 5
0
 def _unix(self):
     """
     Parse /proc/net/unix
     """
     with open("net/unix", opener=self._opener, encoding="utf-8") as file:
         keys, *lines = file.read().splitlines()
     # Ignore "Num"
     entries = [AttrDict(zip_longest(keys.split()[1:], _.split(maxsplit=7)[1:])) for _ in lines]
     for entry in entries:
         entry.update(Path=Pathname(entry.Path))
     return entries
Ejemplo n.º 6
0
 def _numa_maps(self):   # pylint: disable=star-needs-assignment-target  # Make Pylint happy on Python 3.4
     """
     Parses /proc/<pid>/numa_maps and returns an AttrDict
     """
     with open("numa_maps", opener=self._opener, encoding="utf-8") as file:
         lines = file.read().splitlines()
     entry = AttrDict({
         address: AttrDict({
             k: try_int(v[0]) if v else None
             for k, *v in [_.split('=', 1) for _ in ['policy=%s' % policy] + values]})
         for line in lines
         for address, policy, *values in [line.split()]})
     for key in entry:
         if 'file' in entry[key]:
             entry[key].update(file=Pathname(entry[key].file))
     return entry
Ejemplo n.º 7
0
 def _mounts(self):
     """
     Parses /proc/<pid>/mounts and returns a list of AttrDict's
     """
     fields = ('spec', 'file', 'vfstype', 'mntops', 'freq', 'passno')
     with open("mounts", opener=self._opener, encoding="utf-8") as file:
         lines = file.read().splitlines()
     entries = [
         AttrDict(zip(fields, re.findall(r'^(\S+) (.*?) (\S+) (\S+) (\d+) (\d+)$', _)[0]))
         for _ in lines]
     for entry in entries:
         entry.update(
             file=Pathname(entry.file),
             mntops=AttrDict({
                 k: try_int(v[0]) if v else None
                 for k, *v in [_.split('=', 1) for _ in entry.mntops.split(',')]}))
     return entries
Ejemplo n.º 8
0
 def _maps(self):
     """
     Parses /proc/<pid>/maps and returns a list of AttrDict's
     """
     fields = ('address', 'perms', 'offset', 'dev', 'inode', 'pathname')
     with open("maps", opener=self._opener, encoding="utf-8") as file:
         lines = file.read().splitlines()
     entries = [
         AttrDict(zip_longest(fields, _.split(maxsplit=5))) for _ in lines]
     for entry in entries:
         # From the proc(5) manpage:
         #  pathname is shown unescaped except for newline characters,
         #  which are replaced with an octal escape sequence. As a result,
         #  it is not possible to determine whether the original pathname
         #  contained a newline character or the literal \012 character sequence
         # So let's readlink() the address in the map_files directory
         pathname = entry.pathname
         if pathname and '\\012' in pathname:
             pathname = os.readlink(os.path.join("map_files", entry.address), dir_fd=self._dir_fd)
         entry.update(pathname=Pathname(pathname))
     return entries