def _open_file_browser_for_folder(path):
    """
    This method will take a path to a folder and open it in
    an OS's file browser.

    :param path: A folder path
    :raises: RuntimeError: If the Platform is not supported or if
        the file browser couldn't be launched.
    :raises: ValueError: If the path is not a valid directory.
    """
    log.debug("Launching file system browser for folder %s" % path)

    # Check that we don't have a file path.
    if not os.path.isdir(path):
        raise ValueError("The path \"%s\" is not a valid directory." % path)

    # get the setting
    system = sys.platform

    # build the commands for opening the folder on the various OS's
    if system.startswith("linux"):
        cmd_args = ["xdg-open", path]
    elif system == "darwin":
        cmd_args = ["open", path]
    elif system == "win32":
        cmd_args = ["cmd.exe", "/C", "start", path]
    else:
        raise RuntimeError("Platform '%s' is not supported." % system)

    log.debug("Executing command '%s'" % cmd_args)
    exit_code = subprocess.call(cmd_args)
    if exit_code != 0:
        raise RuntimeError("Failed to launch a file browser for folder '%s'. "
                           "Error code %s" % (path, exit_code))
Beispiel #2
0
    def lock_file(self, path, chowned_set=None, utime=False):
        log.debug("Locking %s" % path)
        wm_path = path_utils.conform_path(path, os_name="linux")
        log.debug("stat [%c %#o] %s:%s (%d:%d): %s" % self._curstat(path))

        lock_file(wm_path, preserveTime=not utime)

        # The shared set from the current session should be passed in,
        # to keep track of which files
        # have been chown'd to in watchman case of a rollback.
        # This is a keyword argument to stay compatible
        # with spk.
        if chowned_set is not None:
            chowned_set.add(path)
Beispiel #3
0
    def lock_dir_recursive(self, path, chowned_set=None, utime=False):
        log.debug("Locking recursively %s" % path)
        # wm_path = path_utils.conform_path(path, os_name="linux")
        log.debug("stat [%c %#o] %s:%s (%d:%d): %s" % self._curstat(path))

        file_utils.lock_dir_recursive(path, preserveTime=not utime)

        for root, dirs, files in os.walk(path):
            for d in dirs:
                this_dir = os.path.join(root, d)
                if chowned_set is not None:
                    chowned_set.add(this_dir)
            for f in files:
                this_file = os.path.join(root, f)
                if chowned_set is not None:
                    chowned_set.add(this_file)
def auto_created_yml(path):
    """
    A context manager for opening/closing an auto-generated yaml file.

    - clears umask
    - any existing files will be removed
    - the given path will be open for writing in text mode
    - a standard header will be added

    Usage example::

        with filesystem.auto_created_yml(yaml_path) as fh:
            fh.write("foobar: blah")

        # fh is automatically closed upon exiting the context

    :param path: path to yml file to open for writing
    :return: file handle.
    """

    log.debug("Creating auto-generated config file %s" % path)
    # clean out any existing file and replace it with a new one.

    safe_delete_file(path)

    with open(path, "w+") as fh:

        fh.write("# This file was auto generated by the Dsk.\n")
        fh.write("# Please do not modify by hand as it may be ")
        fh.write("# overwritten at any point.\n")
        fh.write("# Created %s\n" %
                 (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
        fh.write("# \n")

        # on entering the context
        yield fh

        # on exiting the context
        fh.write("\n")
        fh.write("# End of file.\n")
def _open_file_browser_for_file(path):
    """
    This method will take a path to a file and open it in
    an OS's file browser and attempt to highlight it.

    :param path: A file path
    :raises: RuntimeError: If the Platform is not supported or if
        the file browser couldn't be launched.
    :raises: ValueError: If the path is not a valid file path.
    """
    log.debug("Launching file system browser for file %s" % path)

    if not os.path.isfile(path):
        raise ValueError("The path \"%s\" is not a valid file path." % path)

    # get the setting
    system = sys.platform

    if system.startswith("linux"):
        # note: there isn't a straight forward way to do
        # this on linux, so just open the directory instead.
        cmd_args = ["xdg-open", os.path.dirname(path)]
    elif system == "darwin":
        cmd_args = ["open", "-R", path]
    elif system == "win32":
        # /select makes windows select the file within the explorer window
        # The problem with this approach is that it always returns back an
        # error code of 1 even if it does behave correctly.
        cmd_args = ["explorer", "/select,", path]
    else:
        raise Exception("Platform '%s' is not supported." % system)

    log.debug("Executing command '%s'" % cmd_args)
    exit_code = subprocess.call(cmd_args)

    # cannot trust exit code from windows, see above
    if system != "win32" and exit_code != 0:
        raise RuntimeError("Failed to launch a file browser for file '%s'. "
                           "Error code %s" % (path, exit_code))
    def get_unused_path(base_path):
        """
        Return an unused file path from the given base path by appending
        if needed a number at the end of the basename of the path, right
        before the first ".", if any.

        For example, ``/tmp/foo_1.bar.blah`` would be returned for
        ``/tmp/foo.bar.blah`` if it already exists.

        If the given path does not exist, the original path is returned.

        .. note::
            The returned path is not _reserved_, so it is possible that
            other processes could create the returned path before it is used
            by the caller.

        :param str base_path: Target path.
        :returns: A string.
        """
        if not os.path.exists(base_path):
            # Bail out quickly if everything is fine with the path
            return base_path

        # Split the base path and find an unused path
        folder, basename = os.path.split(base_path)

        # two entries.
        base_parts = basename.split(".", 1) + [""]
        numbering = 0
        while True:
            numbering += 1
            name = "%s_%d%s" % (base_parts[0], numbering,
                                ".%s" % base_parts[1] if base_parts[1] else "")
            path = os.path.join(folder, name)
            log.debug("Checking if %s exists..." % path)
            if not os.path.exists(path):
                break
        return path
    def move_folder(cls,
                    src,
                    dst,
                    folder_permissions=0o775,
                    rm_dir=False,
                    do_log=True):
        """Moves a directory.
        First copies all content into target. Then deletes
        all content from sources. Skips files that won't delete.

        :param src: Source path to copy from
        :param dst: Destination to copy to
        :param folder_permissions: permissions to use for new folders
        """
        if os.path.exists(src):
            if do_log is True:
                log.debug("Moving directory: %s -> %s" % (src, dst))

            # first copy the content in the core folder
            src_files = cls.copy_folder_nocheck(src, dst, folder_permissions)

            # now clear out the install location
            if do_log is True:
                log.debug("Clearing out source location...")
            for f in src_files:
                try:
                    # on windows, ensure all files are writable
                    if sys.platform == "win32":
                        attr = os.stat(f)[0]
                        if (not attr & stat.S_IWRITE):
                            # file is readonly! - turn off this attribute
                            os.chmod(f, stat.S_IWRITE)
                    os.remove(f)
                except Exception as e:
                    log.error("Could not delete file %s: %s" % (f, e))
            if rm_dir:
                try:
                    all_files = os.listdir(src)
                    if len(all_files) == 0:
                        os.rmdir(src)
                        if do_log is True:
                            log.debug("dir %s: removed" % (src))
                    else:
                        if do_log is True:
                            log.info("no Empty %s" % all_files)
                except Exception as e:
                    log.error("Could not delete dir %s: %s" % (src, e))