~ubuntu-branches/ubuntu/utopic/mysql-workbench/utopic

« back to all changes in this revision

Viewing changes to plugins/wb.admin/backend/wb_server_management.py

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2014-05-31 12:03:58 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20140531120358-cjik5ofkmj0fxsn8
Tags: 6.1.6+dfsg-1
* New upstream release [May 2014].
* Dropped "prtcl.patch".
* "debian/clean": better clean-up.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import subprocess
28
28
import time
29
29
import inspect
 
30
import random
 
31
import string
30
32
 
31
33
default_sudo_prefix       = '/usr/bin/sudo -S -p EnterPasswordHere'
32
34
 
107
109
            self.mtx.release()
108
110
        return ret
109
111
 
110
 
    def set_contents(self, filename, data):
 
112
    def set_contents(self, filename, data, mode="w"):
111
113
        self.mtx.acquire()
112
114
        try:
113
 
            ret = WbAdminSSH.set_contents(self, filename, data)
 
115
            ret = WbAdminSSH.set_contents(self, filename, data, mode)
114
116
        finally:
115
117
            self.mtx.release()
116
118
        return ret
898
900
        output = StringIO.StringIO()
899
901
 
900
902
        if skip_lines == 0:
901
 
            command = 'cat %s' % quote_path(filename)
 
903
            command = 'LC_ALL=C cat %s' % quote_path(filename)
902
904
        else:
903
 
            command = 'tail -n+%d %s' % (skip_lines+1, quote_path(filename))
 
905
            command = 'LC_ALL=C tail -n+%d %s' % (skip_lines+1, quote_path(filename))
904
906
 
905
907
        res = self.process_ops.exec_cmd(command,
906
908
                                        as_user   = as_user,
1476
1478
            raise err
1477
1479
    
1478
1480
    def _create_temp_file(self, content):
 
1481
 
1479
1482
        tmpfilename = ''
 
1483
 
1480
1484
        if self.ssh is not None:
1481
 
            homedir, status = self.process_ops.get_cmd_output("echo ~")
1482
 
            if type(homedir) is unicode:
1483
 
                homedir = homedir.encode("utf8")
1484
 
            if type(homedir) is str:
1485
 
                homedir = homedir.strip(" \r\t\n")
1486
 
            else:
1487
 
                homedir = None
1488
 
            log_debug2('%s: Got home dir: "%s"\n' % (self.__class__.__name__, homedir) )
1489
 
 
1490
 
            if not homedir:
1491
 
                raise Exception("Unable to get path for remote home directory")
1492
 
 
1493
 
            tmpfilename = homedir + "/.wba.temp"
1494
 
            
1495
 
            self.ssh.set_contents(tmpfilename, content)
 
1485
            done = False
 
1486
            attempts = 0
 
1487
            while not done:
 
1488
                tmpfilename = '/tmp/' + ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(16))
 
1489
                try:
 
1490
                    # This uses file open mode as wx to make sure the temporary has been created
 
1491
                    # on this open attempt to avoid writing to an existing file.
 
1492
                    self.ssh.set_contents(tmpfilename, content, "wx")
 
1493
                    log_debug2('Created temp file: "%s".\n' % tmpfilename)
 
1494
                    done = True
 
1495
                except IOError, exc:
 
1496
                    # This is the only hting reported on a failure due to an attempt to
 
1497
                    # create a file that already exists
 
1498
                    if exc.message == "Failure":
 
1499
                        log_warning('WARNING: Unable to create temp file: "%s", trying a different name.\n' % tmpfilename)
 
1500
 
 
1501
                        if attempts < 10:
 
1502
                            attempts += 1
 
1503
                        else:
 
1504
                            log_warning('ERROR: Unable to create temp file max number of attempts reached.\n')
 
1505
                            raise IOError('Unable to create temp file max number of attempts reached.')
 
1506
                    else:
 
1507
                        log_warning('ERROR: Unable to create temp file: "%s" : %s.\n' % (tmpfilename, exc))
 
1508
                        raise exc
 
1509
                except Exception, exc:
 
1510
                    log_warning('ERROR: Unable to create temp file: "%s" : %s.\n' % (tmpfilename, exc))
 
1511
                    raise exc
1496
1512
        else:
1497
1513
            raise Exception("No SSH session active, cannot save file remotely")
1498
 
        
 
1514
 
1499
1515
        return tmpfilename
1500
1516
        
1501
1517