Exemplo n.º 1
0
    def _generate_schema_diagram(self, show_fk_only):

        tempfiles = []
        output_handle, sql_schema_filepath = open_temp_file('/tmp', ftype = 'w')
        tempfiles.append(sql_schema_filepath)
        try:
            #output_handle.write('%s\n\n' % self.db_schema)
            output_handle.write('%s\n\n' % self.sanitize_schema())#mysqldump_schema)
            output_handle.close()
        except:
            output_handle.close()

        try:
            png_handle, png_filepath = open_temp_file('/tmp', ftype = 'w')
            png_handle.close()
            tempfiles.append(png_filepath)

            c = [
                "sqlt-diagram",
                "-d=MySQL",
                "-i=png",
                "-t=%s database on %s" % (self.db, self.host),
                "-o=%s" % png_filepath,
                "--color",
                sql_schema_filepath,
                ]
            if show_fk_only:
                # Useful to print a smaller schema of just the primary/foreign keys
                c.append("--show-fk-only")

            p = subprocess.Popen(c, stdout=subprocess.PIPE)
            stdout, stderr = p.communicate()
            if not p.returncode == 0:
                if stderr:
                    raise colortext.Exception("Error - sqlt-diagram exited with %d: '%s'." % (p.returncode, stderr))
                else:
                    raise colortext.Exception("Error - sqlt-diagram exited with %d." % (p.returncode))

        except Exception, e:
            colortext.error('Failed!')
            print(str(e))
Exemplo n.º 2
0
 def get_schema(self, host, username, passwd, database_name):
     try:
         outfile, outfilename = open_temp_file('/tmp', "w")
         p = subprocess.Popen(shlex.split("mysqldump -h %s -u %s -p%s --skip-add-drop-table --no-data %s" % (host, username, passwd, database_name)), stdout=outfile)
         p.wait()
         outfile.close()
         contents = read_file(outfilename)
         os.remove(outfilename)
         return contents
     except Exception, e:
         if os.path.exists(outfilename):
             os.remove(outfilename)
         raise
Exemplo n.º 3
0
def get_insecure_resource(host, resource, port = 21, output_filepath = None, timeout = None):

    using_temp_file = output_filepath == None
    output_handle = None
    ftp = None
    ftp_has_quit = True
    if timeout:
        ftp = FTP()
    else:
        ftp = FTP(timeout = 20)
    try:
        # Create an FTP connection and navigate to the correct directory
        ftp.connect(host, port)
        ftp_has_quit = False
        ftp.login()
        local_dirname, local_filename = os.path.split(resource)
        if local_dirname:
            ftp.cwd(local_dirname)

        #ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/1xyz.xml.gz
        #ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/1a2c.xml.gz

        # Create
        if not output_filepath:
            output_handle, output_filepath = open_temp_file('/tmp', ftype = 'wb')
        else:
            output_handle = open(output_filepath, 'wb')
        try:
            contents = ftp.retrbinary('RETR %s' % local_filename, output_handle.write, 1024)
        except Exception, e:
            if str(e).find('Failed to open file') != -1:
                raise FTPException550('Resource could not be located. FTP error: "%s"' % str(e))
            raise Exception('Unknown FTP error: "%s"' % str(e))

        output_handle.close()

        ftp.quit()
        ftp_has_quit = True

        if contents.find('Transfer complete') == -1:
            raise Exception('FTP error: "%s"' % contents)