Ejemplo n.º 1
0
    def save_file_content_and_backup(self, path, content, backup_extension, as_admin = False, admin_password = None):
        # Check if dir, where config file will be stored is writable
        dirname, filename = splitpath(path)

        if not as_admin and not self.is_dir_writable(dirname.strip(" \r\t\n")):
            raise PermissionDeniedError("Cannot write to directory %s" % dirname)

        if self.ssh is not None:
            ## Get temp dir for using as tmpdir
            tmpdir, status = self.process_ops.get_cmd_output("echo %temp%")
            if type(tmpdir) is unicode:
                tmpdir = tmpdir.encode("utf8")
            if type(tmpdir) is str:
                tmpdir = tmpdir.strip(" \r\t\n")
                if tmpdir[1] == ":":
                    tmpdir = tmpdir[2:]
                else:
                    log_debug(_this_file, '%s: Temp directory path "%s" is not in expected form. The expected form is something like "C:\\Windows\\Temp"\n' % (self.__class__.__name__, tmpdir) )
                    tmpdir = None
                log_debug2(_this_file, '%s: Got temp dir: "%s"\n' % (self.__class__.__name__, tmpdir) )
            else:
                tmpdir = None
            
            if not tmpdir:
                tmpdir = dirname

            tmpfilename = tmpdir + r"\workbench-temp-file.ini"

            log_debug(_this_file, '%s: Remotely writing contents to temporary file "%s"\n' % (self.__class__.__name__, tmpfilename) )
            log_debug3(_this_file, '%s: %s\n' % (self.__class__.__name__, content) )
            self.ssh.set_contents(tmpfilename, content)

            if backup_extension:
                log_debug(_this_file, '%s: Backing up "%s"\n' % (self.__class__.__name__, path) )
                backup_cmd = "copy /y " + quote_path_win(path) + " " + quote_path_win(path+backup_extension)
                msg, code = self.process_ops.get_cmd_output(backup_cmd)
                if code != 0:
                    print backup_cmd, "->", msg
                    log_error(_this_file, '%s: Error backing up file: %s\n' % (self.__class__.__name__, backup_cmd+'->'+msg) )
                    raise RuntimeError("Error backing up file: %s" % msg)

            copy_to_dest = "copy /y " + quote_path_win(tmpfilename) + " " + quote_path_win(path)
            delete_tmp = "del " + quote_path_win(tmpfilename)
            log_debug(_this_file, '%s: Copying file to final destination: "%s"\n' % (self.__class__.__name__, copy_to_dest) )
            msg, code = self.process_ops.get_cmd_output(copy_to_dest)
            if code != 0:
                print copy_to_dest, "->", msg
                log_error(_this_file, '%s: Error copying temporary file over destination file: %s\n%s to %s\n' % (self.__class__.__name__, msg, tmpfilename, path) )
                raise RuntimeError("Error copying temporary file over destination file: %s\n%s to %s" % (msg, tmpfilename, path))
            log_debug(_this_file, '%s: Deleting tmp file: "%s"\n' % (self.__class__.__name__, delete_tmp) )
            msg, code = self.process_ops.get_cmd_output(delete_tmp)
            if code != 0:
                print "Could not delete temporary file %s: %s" % (tmpfilename, msg)
                log_info(_this_file, '%s: Could not delete temporary file "%s": %s\n' % (self.__class__.__name__, tmpfilename, msg) )
        else:
            raise Exception("No SSH session active, cannot save file remotely")
Ejemplo n.º 2
0
    def save_file_content_and_backup(self, path, content, backup_extension, as_admin = False, admin_password = None):
        # Check if dir, where config file will be stored is writable
        dirname, filename = splitpath(path)

        if not as_admin and not self.is_dir_writable(dirname.strip(" \r\t\n")):
            raise PermissionDeniedError("Cannot write to directory %s" % dirname)

        if self.ssh is not None:
            ## Get temp dir for using as tmpdir
            tmpdir, status = self.process_ops.get_cmd_output("echo %temp%")
            if type(tmpdir) is unicode:
                tmpdir = tmpdir.encode("utf8")
            if type(tmpdir) is str:
                tmpdir = tmpdir.strip(" \r\t\n")
                if tmpdir[1] == ":":
                    tmpdir = tmpdir[2:]
                else:
                    dprint_ex(1,"Temp directory path '" + tmpdir + " is not in expected form. The expected for is something like 'C:\\Windows\\Temp'")
                    tmpdir = None
                dprint_ex(2, "Got temp dir", tmpdir)
            else:
                tmpdir = None
            
            if not tmpdir:
                tmpdir = dirname

            tmpfilename = tmpdir + r"\workbench-temp-file.ini"

            dprint_ex(1, "Remotely writing contents to temporary file %s" % tmpfilename)
            dprint_ex(1, content)
            self.ssh.set_contents(tmpfilename, content)

            if backup_extension:
                dprint_ex(1, "Backing up %s" % path)
                backup_cmd = "copy /y " + quote_path_win(path) + " " + quote_path_win(path+backup_extension)
                msg, code = self.process_ops.get_cmd_output(backup_cmd)
                if code != 0:
                    print backup_cmd, "->", msg
                    raise RuntimeError("Error backing up file: %s" % msg)

            copy_to_dest = "copy /y " + quote_path_win(tmpfilename) + " " + quote_path_win(path)
            delete_tmp = "del " + quote_path_win(tmpfilename)
            dprint_ex(1, "Copying file to final destination: %s" % copy_to_dest)
            msg, code = self.process_ops.get_cmd_output(copy_to_dest)
            if code != 0:
                print copy_to_dest, "->", msg
                raise RuntimeError("Error copying temporary file over destination file: %s\n%s to %s" % (msg, tmpfilename, path))
            dprint_ex(1, "Deleting tmp file: %s" % delete_tmp)
            msg, code = self.process_ops.get_cmd_output(delete_tmp)
            if code != 0:
                print "Could not delete temporary file %s: %s" % (tmpfilename, msg)
        else:
            raise Exception("No SSH session active, cannot save file remotely")
Ejemplo n.º 3
0
    def save_file_content_and_backup(self,
                                     path,
                                     content,
                                     backup_extension,
                                     as_admin=False,
                                     admin_password=None):
        # Check if dir, where config file will be stored is writable
        dirname, filename = splitpath(path)

        if not as_admin and not self.is_dir_writable(dirname.strip(" \r\t\n")):
            raise PermissionDeniedError("Cannot write to directory %s" %
                                        dirname)

        if self.ssh is not None:
            ## Get home dir for using as tmpdir
            homedir, status = self.process_ops.get_cmd_output("echo ~")
            if type(homedir) is unicode:
                homedir = homedir.encode("utf8")
            if type(homedir) is str:
                homedir = homedir.strip(" \r\t\n")
            else:
                homedir = None
            dprint_ex(2, "Got home dir", homedir)

            if not homedir:
                raise Exception("Unable to get path for remote home directory")

            tmpfilename = homedir + "/.wba.temp"

            dprint_ex(
                1,
                "Remotely writing contents to temporary file %s" % tmpfilename)
            dprint_ex(3, content)
            self.ssh.set_contents(tmpfilename, content)

            if backup_extension:
                dprint_ex(1, "Backing up %s" % path)
                backup_cmd = "/bin/cp " + quote_path(path) + " " + quote_path(
                    path + backup_extension)
                self.process_ops.exec_cmd(backup_cmd, as_admin, admin_password)

            copy_to_dest = "/bin/cp " + quote_path(
                tmpfilename) + " " + quote_path(path)
            delete_tmp = "/bin/rm " + quote_path(tmpfilename)
            dprint_ex(1,
                      "Copying file to final destination: %s" % copy_to_dest)
            self.process_ops.exec_cmd(copy_to_dest, as_admin, admin_password)
            dprint_ex(1, "Deleting tmp file: %s" % delete_tmp)
            self.process_ops.exec_cmd(delete_tmp)
        else:
            raise Exception("No SSH session active, cannot save file remotely")
Ejemplo n.º 4
0
    def save_file_content_and_backup(self, path, content, backup_extension, as_admin = False, admin_password = None):
        # Check if dir, where config file will be stored is writable
        dirname, filename = splitpath(path)

        if not as_admin and not self.is_dir_writable(dirname.strip(" \r\t\n")):
            raise PermissionDeniedError("Cannot write to directory %s" % dirname)

        if self.ssh is not None:
            ## Get home dir for using as tmpdir
            homedir, status = self.process_ops.get_cmd_output("echo ~")
            if type(homedir) is unicode:
                homedir = homedir.encode("utf8")
            if type(homedir) is str:
                homedir = homedir.strip(" \r\t\n")
            else:
                homedir = None
            dprint_ex(2, "Got home dir", homedir)

            if not homedir:
                raise Exception("Unable to get path for remote home directory")

            tmpfilename = homedir + "/.wba.temp"

            dprint_ex(1, "Remotely writing contents to temporary file %s" % tmpfilename)
            dprint_ex(3, content)
            self.ssh.set_contents(tmpfilename, content)

            if backup_extension:
                dprint_ex(1, "Backing up %s" % path)
                backup_cmd = "/bin/cp " + quote_path(path) + " " + quote_path(path+backup_extension)
                self.process_ops.exec_cmd(backup_cmd, as_admin, admin_password)

            copy_to_dest = "/bin/cp " + quote_path(tmpfilename) + " " + quote_path(path)
            delete_tmp = "/bin/rm " + quote_path(tmpfilename)
            dprint_ex(1, "Copying file to final destination: %s" % copy_to_dest)
            self.process_ops.exec_cmd(copy_to_dest, as_admin, admin_password)
            dprint_ex(1, "Deleting tmp file: %s" % delete_tmp)
            self.process_ops.exec_cmd(delete_tmp)
        else:
            raise Exception("No SSH session active, cannot save file remotely")
Ejemplo n.º 5
0
    def save_file_content_and_backup(self,
                                     path,
                                     content,
                                     backup_extension,
                                     as_admin=False,
                                     admin_password=None):
        # Check if dir, where config file will be stored is writable
        dirname, filename = splitpath(path)

        if not as_admin and not self.is_dir_writable(dirname.strip(" \r\t\n")):
            raise PermissionDeniedError("Cannot write to directory %s" %
                                        dirname)

        if self.ssh is not None:
            ## Get temp dir for using as tmpdir
            tmpdir, status = self.process_ops.get_cmd_output("echo %temp%")
            if type(tmpdir) is unicode:
                tmpdir = tmpdir.encode("utf8")
            if type(tmpdir) is str:
                tmpdir = tmpdir.strip(" \r\t\n")
                if tmpdir[1] == ":":
                    tmpdir = tmpdir[2:]
                else:
                    dprint_ex(
                        1, "Temp directory path '" + tmpdir +
                        " is not in expected form. The expected for is something like 'C:\\Windows\\Temp'"
                    )
                    tmpdir = None
                dprint_ex(2, "Got temp dir", tmpdir)
            else:
                tmpdir = None

            if not tmpdir:
                tmpdir = dirname

            tmpfilename = tmpdir + r"\workbench-temp-file.ini"

            dprint_ex(
                1,
                "Remotely writing contents to temporary file %s" % tmpfilename)
            dprint_ex(1, content)
            self.ssh.set_contents(tmpfilename, content)

            if backup_extension:
                dprint_ex(1, "Backing up %s" % path)
                backup_cmd = "copy /y " + quote_path_win(
                    path) + " " + quote_path_win(path + backup_extension)
                msg, code = self.process_ops.get_cmd_output(backup_cmd)
                if code != 0:
                    print backup_cmd, "->", msg
                    raise RuntimeError("Error backing up file: %s" % msg)

            copy_to_dest = "copy /y " + quote_path_win(
                tmpfilename) + " " + quote_path_win(path)
            delete_tmp = "del " + quote_path_win(tmpfilename)
            dprint_ex(1,
                      "Copying file to final destination: %s" % copy_to_dest)
            msg, code = self.process_ops.get_cmd_output(copy_to_dest)
            if code != 0:
                print copy_to_dest, "->", msg
                raise RuntimeError(
                    "Error copying temporary file over destination file: %s\n%s to %s"
                    % (msg, tmpfilename, path))
            dprint_ex(1, "Deleting tmp file: %s" % delete_tmp)
            msg, code = self.process_ops.get_cmd_output(delete_tmp)
            if code != 0:
                print "Could not delete temporary file %s: %s" % (tmpfilename,
                                                                  msg)
        else:
            raise Exception("No SSH session active, cannot save file remotely")
Ejemplo n.º 6
0
                    1,
                    "Copying over tmp file to final filename using sudo: %s -> %s"
                    % (tmp_name, filename))
                self._copy_file(source=tmp_name,
                                dest=filename,
                                as_admin=as_admin,
                                admin_password=admin_password)
                dprint_ex(1, "Done")
                tmp.close()
            except Exception, exc:
                dprint_ex(1, str(exc))
                if tmp:
                    tmp.close()
                raise
        else:
            target_dir = splitpath(filename)[0]

            if not os.path.exists(target_dir):
                dprint_ex(1, "Target directory %s does not exist" % target_dir)
                raise InvalidPathError("The directory %s does not exist" %
                                       target_dir)

            if not self.is_dir_writable(target_dir):
                dprint_ex(1,
                          "Target directory %s is not writable" % target_dir)
                raise PermissionDeniedError("Cannot write to target directory")

            if os.path.exists(filename) and backup_extension:
                dprint_ex(1,
                          "Target file %s exists, creating backup" % filename)
                # backup config file
Ejemplo n.º 7
0
                if backup_extension and os.path.exists(filename):
                    dprint_ex(1, "Creating backup of %s to %s" % (filename, filename+backup_extension))
                    self._copy_file(source = filename, dest = filename + backup_extension, 
                                    as_admin = as_admin, admin_password = admin_password)

                dprint_ex(1, "Copying over tmp file to final filename using sudo: %s -> %s" % (tmp_name, filename))
                self._copy_file(source = tmp_name, dest = filename, as_admin = as_admin, admin_password = admin_password)
                dprint_ex(1, "Done")
                tmp.close()
            except Exception, exc:
                dprint_ex(1, str(exc))
                if tmp:
                  tmp.close()
                raise
        else:
            target_dir = splitpath(filename)[0]
            
            if not os.path.exists(target_dir):
                dprint_ex(1, "Target directory %s does not exist" % target_dir)
                raise InvalidPathError("The directory %s does not exist" % target_dir)
            
            if not self.is_dir_writable(target_dir):
                dprint_ex(1, "Target directory %s is not writable" % target_dir)
                raise PermissionDeniedError("Cannot write to target directory")
            
            if os.path.exists(filename) and backup_extension:
                dprint_ex(1, "Target file %s exists, creating backup" % filename)
                # backup config file
                self._copy_file(filename, filename+backup_extension)            
            try:
                f = open(filename, 'w')
Ejemplo n.º 8
0
 def test_split_path_linux(self):
     self.assertEqual(wb_common.splitpath('hello/world'),
                      ('hello', 'world'))
     self.assertEqual(wb_common.splitpath('hello\\world'),
                      ('hello', 'world'))
 def test_split_path_linux(self):
     self.assertEqual(wb_common.splitpath('hello/world'), ('hello', 'world'))
     self.assertEqual(wb_common.splitpath('hello\\world'), ('hello', 'world'))