Python os.stat() Examples

The following are code examples for showing how to use os.stat(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: fs_image   Author: facebookincubator   File: test_package_image.py    MIT License 6 votes vote down vote up
def test_package_image_as_btrfs_loopback_writable(self):
        with self._package_image(
            self._sibling_path('create_ops.layer'),
            'btrfs',
            writable_subvolume=True,
        ) as out_path, \
                Unshare([Namespace.MOUNT, Namespace.PID]) as unshare, \
                tempfile.TemporaryDirectory() as mount_dir:
            os.chmod(
                out_path,
                stat.S_IMODE(os.stat(out_path).st_mode)
                | (stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH),
            )
            subprocess.check_call(nsenter_as_root(
                unshare, 'mount', '-t', 'btrfs', '-o', 'loop,discard,nobarrier',
                out_path, mount_dir,
            ))
            try:
                subprocess.check_call(nsenter_as_root(
                    unshare, 'touch', os.path.join(mount_dir,
                                                   'create_ops',
                                                   'foo'),
                ))
            finally:
                nsenter_as_root(unshare, 'umount', mount_dir) 
Example 2
Project: fs_image   Author: facebookincubator   File: package_image.py    MIT License 6 votes vote down vote up
def package_image(argv):
    args = parse_args(argv)
    assert not os.path.exists(args.output_path)
    Format.make(args.format).package_full(
        find_built_subvol(args.layer_path, subvolumes_dir=args.subvolumes_dir),
        output_path=args.output_path,
        opts=_Opts(
            subvol_opts=SubvolOpts(readonly=not args.writable_subvolume),
        ),
    )
    # Paranoia: images are read-only after being built
    os.chmod(
        args.output_path,
        stat.S_IMODE(os.stat(args.output_path).st_mode)
            & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH),
    ) 
Example 3
Project: leapp-repository   Author: oamg   File: test_lib_backup.py    Apache License 2.0 6 votes vote down vote up
def test_backup_file():
    tmpdir = tempfile.mkdtemp()
    try:
        file_path = os.path.join(tmpdir, 'foo-bar')
        content = 'test content\n'
        with open(file_path, 'w') as f:
            f.write(content)

        backup_path = backup_file(file_path)

        assert os.path.basename(backup_path) == 'foo-bar.leapp-backup'
        assert os.path.dirname(backup_path) == tmpdir
        assert len(os.listdir(tmpdir)) == 2
        st = os.stat(backup_path)
        assert stat.S_IMODE(st.st_mode) == (stat.S_IRUSR | stat.S_IWUSR)
        with open(backup_path, 'r') as f:
            backup_content = f.read()
        assert backup_content == content
        with open(file_path, 'r') as f:
            orig_content = f.read()
        assert orig_content == content
    finally:
        shutil.rmtree(tmpdir) 
Example 4
Project: leapp-repository   Author: oamg   File: test_lib_backup.py    Apache License 2.0 6 votes vote down vote up
def test_backup_file_target_exists():
    tmpdir = tempfile.mkdtemp()
    try:
        file_path = os.path.join(tmpdir, 'foo-bar')
        primary_target_path = '%s.leapp-backup' % file_path
        primary_target_content = 'do not overwrite me'
        content = 'test_content\n'
        with open(file_path, 'w') as f:
            f.write(content)
        with open(primary_target_path, 'w') as f:
            f.write(primary_target_content)

        backup_path = backup_file(file_path)

        assert os.path.basename(backup_path).startswith('foo-bar.leapp-backup.')
        assert os.path.dirname(backup_path) == tmpdir
        assert len(os.listdir(tmpdir)) == 3
        st = os.stat(backup_path)
        assert stat.S_IMODE(st.st_mode) == (stat.S_IRUSR | stat.S_IWUSR)
        with open(backup_path, 'r') as f:
            assert f.read() == content
        with open(primary_target_path, 'r') as f:
            assert f.read() == primary_target_content
    finally:
        shutil.rmtree(tmpdir) 
Example 5
Project: pyblish-win   Author: pyblish   File: pstats.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def load_stats(self, arg):
        if not arg:  self.stats = {}
        elif isinstance(arg, basestring):
            f = open(arg, 'rb')
            self.stats = marshal.load(f)
            f.close()
            try:
                file_stats = os.stat(arg)
                arg = time.ctime(file_stats.st_mtime) + "    " + arg
            except:  # in case this is not unix
                pass
            self.files = [ arg ]
        elif hasattr(arg, 'create_stats'):
            arg.create_stats()
            self.stats = arg.stats
            arg.stats = {}
        if not self.stats:
            raise TypeError("Cannot create or construct a %r object from %r"
                            % (self.__class__, arg))
        return 
Example 6
Project: pyblish-win   Author: pyblish   File: shutil.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def copystat(src, dst):
    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
    st = os.stat(src)
    mode = stat.S_IMODE(st.st_mode)
    if hasattr(os, 'utime'):
        os.utime(dst, (st.st_atime, st.st_mtime))
    if hasattr(os, 'chmod'):
        os.chmod(dst, mode)
    if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        try:
            os.chflags(dst, st.st_flags)
        except OSError, why:
            for err in 'EOPNOTSUPP', 'ENOTSUP':
                if hasattr(errno, err) and why.errno == getattr(errno, err):
                    break
            else:
                raise 
Example 7
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _get_cputimes_fields():
    """Return a namedtuple of variable fields depending on the
    CPU times available on this Linux kernel version which may be:
    (user, nice, system, idle, iowait, irq, softirq, [steal, [guest,
     [guest_nice]]])
    """
    with open('/proc/stat', 'rb') as f:
        values = f.readline().split()[1:]
    fields = ['user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq']
    vlen = len(values)
    if vlen >= 8:
        # Linux >= 2.6.11
        fields.append('steal')
    if vlen >= 9:
        # Linux >= 2.6.24
        fields.append('guest')
    if vlen >= 10:
        # Linux >= 3.2.0
        fields.append('guest_nice')
    return fields 
Example 8
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_proc_inodes(self, pid):
        inodes = defaultdict(list)
        for fd in os.listdir("/proc/%s/fd" % pid):
            try:
                inode = os.readlink("/proc/%s/fd/%s" % (pid, fd))
            except OSError as err:
                # ENOENT == file which is gone in the meantime;
                # os.stat('/proc/%s' % self.pid) will be done later
                # to force NSP (if it's the case)
                if err.errno in (errno.ENOENT, errno.ESRCH):
                    continue
                elif err.errno == errno.EINVAL:
                    # not a link
                    continue
                else:
                    raise
            else:
                if inode.startswith('socket:['):
                    # the process is using a socket
                    inode = inode[8:][:-1]
                    inodes[inode].append((pid, int(fd)))
        return inodes 
Example 9
Project: pyblish-win   Author: pyblish   File: _pssunos.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def threads(self):
        ret = []
        tids = os.listdir('/proc/%d/lwp' % self.pid)
        hit_enoent = False
        for tid in tids:
            tid = int(tid)
            try:
                utime, stime = cext.query_process_thread(
                    self.pid, tid)
            except EnvironmentError as err:
                # ENOENT == thread gone in meantime
                if err.errno == errno.ENOENT:
                    hit_enoent = True
                    continue
                raise
            else:
                nt = _common.pthread(tid, utime, stime)
                ret.append(nt)
        if hit_enoent:
            # raise NSP if the process disappeared on us
            os.stat('/proc/%s' % self.pid)
        return ret 
Example 10
Project: pyblish-win   Author: pyblish   File: _pssunos.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
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 
Example 11
Project: pyblish-win   Author: pyblish   File: filecmp.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def phase2(self): # Distinguish files, directories, funnies
        self.common_dirs = []
        self.common_files = []
        self.common_funny = []

        for x in self.common:
            a_path = os.path.join(self.left, x)
            b_path = os.path.join(self.right, x)

            ok = 1
            try:
                a_stat = os.stat(a_path)
            except os.error, why:
                # print 'Can\'t stat', a_path, ':', why[1]
                ok = 0
            try:
                b_stat = os.stat(b_path)
            except os.error, why:
                # print 'Can\'t stat', b_path, ':', why[1]
                ok = 0 
Example 12
Project: pyblish-win   Author: pyblish   File: dep_util.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def newer(source, target):
    """Tells if the target is newer than the source.

    Return true if 'source' exists and is more recently modified than
    'target', or if 'source' exists and 'target' doesn't.

    Return false if both exist and 'target' is the same age or younger
    than 'source'. Raise DistutilsFileError if 'source' does not exist.

    Note that this test is not very accurate: files created in the same second
    will have the same "age".
    """
    if not os.path.exists(source):
        raise DistutilsFileError("file '%s' does not exist" %
                                 os.path.abspath(source))
    if not os.path.exists(target):
        return True

    return os.stat(source)[ST_MTIME] > os.stat(target)[ST_MTIME] 
Example 13
Project: pyblish-win   Author: pyblish   File: test_file_util.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_copy_file_hard_link_failure(self):
        # If hard linking fails, copy_file() falls back on copying file
        # (some special filesystems don't support hard linking even under
        #  Unix, see issue #8876).
        with open(self.source, 'w') as f:
            f.write('some content')
        st = os.stat(self.source)
        def _os_link(*args):
            raise OSError(0, "linking unsupported")
        old_link = os.link
        os.link = _os_link
        try:
            copy_file(self.source, self.target, link='hard')
        finally:
            os.link = old_link
        st2 = os.stat(self.source)
        st3 = os.stat(self.target)
        self.assertTrue(os.path.samestat(st, st2), (st, st2))
        self.assertFalse(os.path.samestat(st2, st3), (st2, st3))
        for fn in (self.source, self.target):
            with open(fn, 'r') as f:
                self.assertEqual(f.read(), 'some content') 
Example 14
Project: pyblish-win   Author: pyblish   File: mhlib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def listsubfolders(self, name):
        """Return the names of the subfolders in a given folder
        (prefixed with the given folder name)."""
        fullname = os.path.join(self.path, name)
        # Get the link count so we can avoid listing folders
        # that have no subfolders.
        nlinks = os.stat(fullname).st_nlink
        if nlinks <= 2:
            return []
        subfolders = []
        subnames = os.listdir(fullname)
        for subname in subnames:
            fullsubname = os.path.join(fullname, subname)
            if os.path.isdir(fullsubname):
                name_subname = os.path.join(name, subname)
                subfolders.append(name_subname)
                # Stop looking for subfolders when
                # we've seen them all
                nlinks = nlinks - 1
                if nlinks <= 2:
                    break
        subfolders.sort()
        return subfolders 
Example 15
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _create_tmp(self):
        """Create a file in the tmp subdirectory and open and return it."""
        now = time.time()
        hostname = socket.gethostname()
        if '/' in hostname:
            hostname = hostname.replace('/', r'\057')
        if ':' in hostname:
            hostname = hostname.replace(':', r'\072')
        uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(),
                                    Maildir._count, hostname)
        path = os.path.join(self._path, 'tmp', uniq)
        try:
            os.stat(path)
        except OSError, e:
            if e.errno == errno.ENOENT:
                Maildir._count += 1
                try:
                    return _create_carefully(path)
                except OSError, e:
                    if e.errno != errno.EEXIST:
                        raise 
Example 16
Project: pyblish-win   Author: pyblish   File: zipfile.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _get_codename(self, pathname, basename):
        """Return (filename, archivename) for the path.

        Given a module name path, return the correct file path and
        archive name, compiling if necessary.  For example, given
        /python/lib/string, return (/python/lib/string.pyc, string).
        """
        file_py  = pathname + ".py"
        file_pyc = pathname + ".pyc"
        file_pyo = pathname + ".pyo"
        if os.path.isfile(file_pyo) and \
                            os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
            fname = file_pyo    # Use .pyo file
        elif not os.path.isfile(file_pyc) or \
             os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
            import py_compile
            if self.debug:
                print "Compiling", file_py
            try:
                py_compile.compile(file_py, file_pyc, None, True)
            except py_compile.PyCompileError,err:
                print err.msg
            fname = file_pyc 
Example 17
Project: pyblish-win   Author: pyblish   File: test_stat.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def assertS_IS(self, name, mode):
        # test format, lstrip is for S_IFIFO
        fmt = getattr(stat, "S_IF" + name.lstrip("F"))
        self.assertEqual(stat.S_IFMT(mode), fmt)
        # test that just one function returns true
        testname = "S_IS" + name
        for funcname in self.format_funcs:
            func = getattr(stat, funcname, None)
            if func is None:
                if funcname == testname:
                    raise ValueError(funcname)
                continue
            if funcname == testname:
                self.assertTrue(func(mode))
            else:
                self.assertFalse(func(mode)) 
Example 18
Project: pyblish-win   Author: pyblish   File: test_stat.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_module_attributes(self):
        for key, value in self.stat_struct.items():
            modvalue = getattr(stat, key)
            self.assertEqual(value, modvalue, key)
        for key, value in self.permission_bits.items():
            modvalue = getattr(stat, key)
            self.assertEqual(value, modvalue, key)
        for key in self.file_flags:
            modvalue = getattr(stat, key)
            self.assertIsInstance(modvalue, int)
        for key in self.formats:
            modvalue = getattr(stat, key)
            self.assertIsInstance(modvalue, int)
        for key in self.format_funcs:
            func = getattr(stat, key)
            self.assertTrue(callable(func))
            self.assertEqual(func(0), 0) 
Example 19
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 5 votes vote down vote up
def _path_is_btrfs_subvol(path):
    'Ensure that there is a btrfs subvolume at this path. As per @kdave at '
    'https://stackoverflow.com/a/32865333'
    # You'd think I could just `os.statvfs`, but no, not until Py3.7
    # https://bugs.python.org/issue32143
    fs_type = subprocess.run(
        ['stat', '-f', '--format=%T', path],
        stdout=subprocess.PIPE,
    ).stdout.decode().strip()
    ino = os.stat(path).st_ino
    return fs_type == 'btrfs' and ino == 256 
Example 20
Project: fs_image   Author: facebookincubator   File: test_volume_for_repo.py    MIT License 5 votes vote down vote up
def test_volume_repo(self):
        artifacts_dir = tempfile.mkdtemp(prefix='test_volume_repo')
        volume_dir = os.path.join(artifacts_dir, vfr.VOLUME_DIR)
        image_file = os.path.join(artifacts_dir, vfr.IMAGE_FILE)
        min_free_bytes = 250e6

        try:
            self.assertEqual(
                vfr.get_volume_for_current_repo(
                    min_free_bytes=min_free_bytes,
                    artifacts_dir=artifacts_dir,
                ),
                volume_dir,
            )
            self.assertGreaterEqual(
                os.stat(image_file).st_size, min_free_bytes,
            )

            fstype_and_avail = subprocess.check_output([
                'findmnt', '--noheadings', '--output', 'FSTYPE,AVAIL',
                '--bytes', volume_dir
            ])
            fstype, avail = fstype_and_avail.strip().split()
            self.assertEqual(b'btrfs', fstype)
            self.assertGreaterEqual(int(avail), min_free_bytes)
        finally:
            try:
                subprocess.call(['sudo', 'umount', volume_dir])
            except Exception:
                pass  # Might not have been mounted in case of an earlier error
            shutil.rmtree(artifacts_dir) 
Example 21
Project: fs_image   Author: facebookincubator   File: subvolume_garbage_collector.py    MIT License 5 votes vote down vote up
def list_refcounts(refcounts_dir):
    # The the first part of the name may contain 0 or more colons.
    reg = re.compile('^(.+):([^:]+).json$')
    for p in glob.glob(f'{refcounts_dir}/*:*.json'):
        m = reg.match(os.path.basename(p))
        # Only fails if glob does not work.
        assert m is not None, f'Bad refcount item {p} in {refcounts_dir}'
        st = os.stat(p)
        if not stat.S_ISREG(st.st_mode):
            raise RuntimeError(f'Refcount {p} is not a regular file')
        # It is tempting to check that the subvolume name & version match
        # `SubvolumeOnDisk.from_json_file`, but we cannot do that because
        # our GC pass might be running concurrently with another build, and
        # the refcount file might be empty or half-written.
        yield (f'{m.group(1)}:{m.group(2)}', st.st_nlink) 
Example 22
Project: alfred-yubikey-otp   Author: robertoriv   File: workflow.py    MIT License 5 votes vote down vote up
def cached_data_age(self, name):
        """Return age in seconds of cache `name` or 0 if cache doesn't exist.

        :param name: name of datastore
        :type name: ``unicode``
        :returns: age of datastore in seconds
        :rtype: ``int``

        """
        cache_path = self.cachefile('%s.%s' % (name, self.cache_serializer))

        if not os.path.exists(cache_path):
            return 0

        return time.time() - os.stat(cache_path).st_mtime 
Example 23
Project: phrydy   Author: Josef-Friedrich   File: test_mediafile_edge.py    MIT License 5 votes vote down vote up
def test_opening_tagless_file_leaves_untouched(self):
        old_mtime = os.stat(self.empty).st_mtime
        mediafile.MediaFile(self.empty)
        new_mtime = os.stat(self.empty).st_mtime
        self.assertEqual(old_mtime, new_mtime) 
Example 24
Project: phrydy   Author: Josef-Friedrich   File: test_mediafile.py    MIT License 5 votes vote down vote up
def test_unmodified(self):
        mediafile = self._mediafile_fixture('full')
        mtime = self._set_past_mtime(mediafile.path)
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime)

        mediafile.save()
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime) 
Example 25
Project: phrydy   Author: Josef-Friedrich   File: test_mediafile.py    MIT License 5 votes vote down vote up
def test_same_tag_value(self):
        mediafile = self._mediafile_fixture('full')
        mtime = self._set_past_mtime(mediafile.path)
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime)

        mediafile.title = mediafile.title
        mediafile.save()
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime) 
Example 26
Project: phrydy   Author: Josef-Friedrich   File: test_mediafile.py    MIT License 5 votes vote down vote up
def test_update_same_tag_value(self):
        mediafile = self._mediafile_fixture('full')
        mtime = self._set_past_mtime(mediafile.path)
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime)

        mediafile.update({'title': mediafile.title})
        mediafile.save()
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime) 
Example 27
Project: phrydy   Author: Josef-Friedrich   File: test_mediafile.py    MIT License 5 votes vote down vote up
def test_tag_value_change(self):
        mediafile = self._mediafile_fixture('full')
        mtime = self._set_past_mtime(mediafile.path)
        self.assertEqual(os.stat(mediafile.path).st_mtime, mtime)

        mediafile.title = mediafile.title
        mediafile.album = u'another'
        mediafile.save()
        self.assertNotEqual(os.stat(mediafile.path).st_mtime, mtime) 
Example 28
Project: pyblish-win   Author: pyblish   File: mkreal.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def mkrealfile(name):
    st = os.stat(name) # Get the mode
    mode = S_IMODE(st[ST_MODE])
    linkto = os.readlink(name) # Make sure again it's a symlink
    f_in = open(name, 'r') # This ensures it's a file
    os.unlink(name)
    f_out = open(name, 'w')
    while 1:
        buf = f_in.read(BUFSIZE)
        if not buf: break
        f_out.write(buf)
    del f_out # Flush data to disk before changing mode
    os.chmod(name, mode) 
Example 29
Project: pyblish-win   Author: pyblish   File: mkreal.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def mkrealdir(name):
    st = os.stat(name) # Get the mode
    mode = S_IMODE(st[ST_MODE])
    linkto = os.readlink(name)
    files = os.listdir(name)
    os.unlink(name)
    os.mkdir(name, mode)
    os.chmod(name, mode)
    linkto = join(os.pardir, linkto)
    #
    for filename in files:
        if filename not in (os.curdir, os.pardir):
            os.symlink(join(linkto, filename), join(name, filename)) 
Example 30
Project: pyblish-win   Author: pyblish   File: cvsfiles.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def getmtime(filename):
    try:
        st = os.stat(filename)
    except os.error:
        return 0
    return st[stat.ST_MTIME] 
Example 31
Project: pyblish-win   Author: pyblish   File: pysource.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _open(fullpath):
    try:
        size = os.stat(fullpath).st_size
    except OSError, err: # Permission denied - ignore the file
        print_debug("%s: permission denied: %s" % (fullpath, err))
        return None 
Example 32
Project: pyblish-win   Author: pyblish   File: xxci.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def getmtime(file):
    try:
        st = os.stat(file)
        return st[ST_MTIME]
    except os.error:
        return -1 
Example 33
Project: pyblish-win   Author: pyblish   File: diff.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def main():

    usage = "usage: %prog [options] fromfile tofile"
    parser = optparse.OptionParser(usage)
    parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
    parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
    parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')
    parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
    parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
    (options, args) = parser.parse_args()

    if len(args) == 0:
        parser.print_help()
        sys.exit(1)
    if len(args) != 2:
        parser.error("need to specify both a fromfile and tofile")

    n = options.lines
    fromfile, tofile = args

    fromdate = time.ctime(os.stat(fromfile).st_mtime)
    todate = time.ctime(os.stat(tofile).st_mtime)
    fromlines = open(fromfile, 'U').readlines()
    tolines = open(tofile, 'U').readlines()

    if options.u:
        diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
    elif options.n:
        diff = difflib.ndiff(fromlines, tolines)
    elif options.m:
        diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
    else:
        diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)

    sys.stdout.writelines(diff) 
Example 34
Project: pyblish-win   Author: pyblish   File: pstats.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def help_callees(self):
            print >> self.stream, "Print callees statistics from the current stat object."
            self.generic_help() 
Example 35
Project: pyblish-win   Author: pyblish   File: pstats.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def help_callers(self):
            print >> self.stream, "Print callers statistics from the current stat object."
            self.generic_help() 
Example 36
Project: pyblish-win   Author: pyblish   File: pstats.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def help_stats(self):
            print >> self.stream, "Print statistics from the current stat object."
            self.generic_help() 
Example 37
Project: pyblish-win   Author: pyblish   File: dircache.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def listdir(path):
    """List directory contents, using cache."""
    try:
        cached_mtime, list = cache[path]
        del cache[path]
    except KeyError:
        cached_mtime, list = -1, []
    mtime = os.stat(path).st_mtime
    if mtime != cached_mtime:
        list = os.listdir(path)
        list.sort()
    cache[path] = mtime, list
    return list 
Example 38
Project: pyblish-win   Author: pyblish   File: shutil.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def copymode(src, dst):
    """Copy mode bits from src to dst"""
    if hasattr(os, 'chmod'):
        st = os.stat(src)
        mode = stat.S_IMODE(st.st_mode)
        os.chmod(dst, mode) 
Example 39
Project: pyblish-win   Author: pyblish   File: shutil.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def copy2(src, dst):
    """Copy data and all stat info ("cp -p src dst").

    The destination may be a directory.

    """
    if os.path.isdir(dst):
        dst = os.path.join(dst, os.path.basename(src))
    copyfile(src, dst)
    copystat(src, dst) 
Example 40
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def emit(self, record):
        """
        Emit a record.

        First check if the underlying file has changed, and if it
        has, close the old stream and reopen the file to get the
        current stream.
        """
        # Reduce the chance of race conditions by stat'ing by path only
        # once and then fstat'ing our new fd if we opened a new log stream.
        # See issue #14632: Thanks to John Mulligan for the problem report
        # and patch.
        try:
            # stat the file by path, checking for existence
            sres = os.stat(self.baseFilename)
        except OSError as err:
            if err.errno == errno.ENOENT:
                sres = None
            else:
                raise
        # compare file system stat with that of our stream file handle
        if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:
            if self.stream is not None:
                # we have an open file handle, clean it up
                self.stream.flush()
                self.stream.close()
                self.stream = None  # See Issue #21742: _open () might fail.
                # open a new file handle and get new stat info from that fd
                self.stream = self._open()
                self._statstream()
        logging.FileHandler.emit(self, record) 
Example 41
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def open_local_file(self, url):
        """Use local file."""
        import mimetypes, mimetools, email.utils
        try:
            from cStringIO import StringIO
        except ImportError:
            from StringIO import StringIO
        host, file = splithost(url)
        localname = url2pathname(file)
        try:
            stats = os.stat(localname)
        except OSError, e:
            raise IOError(e.errno, e.strerror, e.filename) 
Example 42
Project: pyblish-win   Author: pyblish   File: _psposix.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _get_terminal_map():
    ret = {}
    ls = glob.glob('/dev/tty*') + glob.glob('/dev/pts/*')
    for name in ls:
        assert name not in ret
        try:
            ret[os.stat(name).st_rdev] = name
        except OSError as err:
            if err.errno != errno.ENOENT:
                raise
    return ret 
Example 43
Project: pyblish-win   Author: pyblish   File: _common.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def isfile_strict(path):
    """Same as os.path.isfile() but does not swallow EACCES / EPERM
    exceptions, see:
    http://mail.python.org/pipermail/python-dev/2012-June/120787.html
    """
    try:
        st = os.stat(path)
    except OSError as err:
        if err.errno in (errno.EPERM, errno.EACCES):
            raise
        return False
    else:
        return stat.S_ISREG(st.st_mode) 
Example 44
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def cpu_times():
    """Return a named tuple representing the following system-wide
    CPU times:
    (user, nice, system, idle, iowait, irq, softirq [steal, [guest,
     [guest_nice]]])
    Last 3 fields may not be available on all Linux kernel versions.
    """
    with open('/proc/stat', 'rb') as f:
        values = f.readline().split()
    fields = values[1:len(scputimes._fields) + 1]
    fields = [float(x) / CLOCK_TICKS for x in fields]
    return scputimes(*fields) 
Example 45
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def cpu_count_logical():
    """Return the number of logical CPUs in the system."""
    try:
        return os.sysconf("SC_NPROCESSORS_ONLN")
    except ValueError:
        # as a second fallback we try to parse /proc/cpuinfo
        num = 0
        with open('/proc/cpuinfo', 'rb') as f:
            for line in f:
                if line.lower().startswith(b'processor'):
                    num += 1

        # unknown format (e.g. amrel/sparc architectures), see:
        # https://github.com/giampaolo/psutil/issues/200
        # try to parse /proc/stat as a last resort
        if num == 0:
            search = re.compile('cpu\d')
            with open('/proc/stat', 'rt') as f:
                for line in f:
                    line = line.split(' ')[0]
                    if search.match(line):
                        num += 1

        if num == 0:
            # mimic os.cpu_count()
            return None
        return num 
Example 46
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def boot_time():
    """Return the system boot time expressed in seconds since the epoch."""
    global BOOT_TIME
    with open('/proc/stat', 'rb') as f:
        for line in f:
            if line.startswith(b'btime'):
                ret = float(line.strip().split()[1])
                BOOT_TIME = ret
                return ret
        raise RuntimeError("line 'btime' not found in /proc/stat")


# --- processes 
Example 47
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def name(self):
        fname = "/proc/%s/stat" % self.pid
        kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
        with open(fname, "rt", **kw) as f:
            data = f.read()
        # XXX - gets changed later and probably needs refactoring
        return data[data.find('(') + 1:data.rfind(')')] 
Example 48
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def terminal(self):
        tmap = _psposix._get_terminal_map()
        with open("/proc/%s/stat" % self.pid, 'rb') as f:
            tty_nr = int(f.read().split(b' ')[6])
        try:
            return tmap[tty_nr]
        except KeyError:
            return None 
Example 49
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def create_time(self):
        with open("/proc/%s/stat" % self.pid, 'rb') as f:
            st = f.read().strip()
        # ignore the first two values ("pid (exe)")
        st = st[st.rfind(b')') + 2:]
        values = st.split(b' ')
        # According to documentation, starttime is in field 21 and the
        # unit is jiffies (clock ticks).
        # We first divide it for clock ticks and then add uptime returning
        # seconds since the epoch, in UTC.
        # Also use cached value if available.
        bt = BOOT_TIME or boot_time()
        return (float(values[19]) / CLOCK_TICKS) + bt 
Example 50
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def threads(self):
        thread_ids = os.listdir("/proc/%s/task" % self.pid)
        thread_ids.sort()
        retlist = []
        hit_enoent = False
        for thread_id in thread_ids:
            fname = "/proc/%s/task/%s/stat" % (self.pid, thread_id)
            try:
                with open(fname, 'rb') as f:
                    st = f.read().strip()
            except IOError as err:
                if err.errno == errno.ENOENT:
                    # no such file or directory; it means thread
                    # disappeared on us
                    hit_enoent = True
                    continue
                raise
            # ignore the first two values ("pid (exe)")
            st = st[st.find(b')') + 2:]
            values = st.split(b' ')
            utime = float(values[11]) / CLOCK_TICKS
            stime = float(values[12]) / CLOCK_TICKS
            ntuple = _common.pthread(int(thread_id), utime, stime)
            retlist.append(ntuple)
        if hit_enoent:
            # raise NSP if the process disappeared on us
            os.stat('/proc/%s' % self.pid)
        return retlist