Skip to content

Instantly share code, notes, and snippets.

@abubelinha
Last active December 30, 2022 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abubelinha/c0bf4f6b9ec50149c1df73566785d0c0 to your computer and use it in GitHub Desktop.
Save abubelinha/c0bf4f6b9ec50149c1df73566785d0c0 to your computer and use it in GitHub Desktop.
def pma_import_resubmit(br, sqlfile, target_dbname):
""" To be used in phpmyadmin dump.sql file imports using python-mechanize
as described in <https://github.com/phpmyadmin/phpmyadmin/issues/17423#issuecomment-1268271930>
Only call this function when you have already found a 'timeout_passed=' string in browser response
(so you are sure the page contains a resubmit link)
"""
# https://stackoverflow.com/questions/3569622/python-mechanize-following-link-by-url-and-what-is-the-nr-parameter/3569707#3569707
#link = br.find_link(url = resumeurl)
link = br.find_link(text = "resubmit the same file")
link.absolute_url = link.absolute_url.replace("&amp;","&").replace("?", "?db="+target_dbname+"&")
resumeurl = link.absolute_url
print("This is the resubmission link absolute_url:",resumeurl)
offset = resumeurl.split("offset=")[1]
# Open server import
# response = br.follow_link(text_regex=re.compile('Import'))
response = br.follow_link(link)
assert(b'after resubmitting will continue from position' in response.read())
# Upload SQL file with python mechanize:
# https://stackoverflow.com/questions/1299855/upload-file-with-python-mechanize/1305860#1305860
br.select_form('import')
print("... about to reupload '{}' (offset {}) to {}".format(sqlfile, offset, resumeurl))
if ".".join(sqlfile.lower().split(".")[-2:])=="sql.zip": # if compressed, don't use encoding:
# binary open: https://splunktool.com/python-mechanize-file-upload-keeps-uploading-1kb-files
# https://stackoverflow.com/questions/55136066/file-upload-through-python-mechanize/55136271#55136271
br.form.add_file(open(sqlfile, 'rb'), 'text/plain', sqlfile)
print("file RE-added to import form (using zipped mode)")
else: # abubelinha introduces encoding:
encoding="" # set whatever you want, or pass it as a parameter to current function
br.form.add_file(open(sqlfile, encoding=encoding), 'text/plain', sqlfile)
print("file RE-added to import form (using text encoding={})".format(encoding))
response = br.submit()
htmlstring = str(response.read())
if len(htmlstring.split("timeout_passed="))>1: # timeout resubmit link found in response:
sample_error = """<div class="error"><img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> Script timeout passed, if you want to finish import, please <a href="db_import.php?db=dbname&amp;timeout_passed=1&amp;offset=76543210">resubmit the same file</a> and import will resume.</div>"""
newresumeurl = htmlstring.split('Script timeout passed, if you want to finish import, please <a href="')[1].split('">resubmit the same file</a>')[0]
print("\nWarning: new call to pma_import_resubmit(sqlfile='{}', target_dbname='{}', resumeurl='{}')\n".format(sqlfile,target_dbname,newresumeurl))
br = pma_import_resubmit(br, sqlfile, target_dbname) # , resumeurl=newresumeurl
print("\n... back in pma_import_resubmit(resumeurl='{}') from pma_import_resubmit(resumeurl='{}')\n".format(resumeurl,newresumeurl))
else: # no timeout resubmit link in response. All should have finished. Let's check response content:
if len(htmlstring.split('<div class="error">'))>1: # red ERROR divs found:
print("\nERRORS PROCESSING {} BY PHPMYADMIN:".format(sqlfile))
for ediv in htmlstring.split('<div class="error">')[1:]:
errordiv = ediv.split("</div>")[0]
print("\n----- ERROR DIV CONTENT: -----\n", errordiv, "\n","-"*25)
if False:
if errordiv != '<img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> Javascript must be enabled past this point!':
for error in errordiv.split("</ol>")[0].split("<ol>")[1].split("<li>")[1:]:
print("\n- ",error.split("</li>")[0])
print("\n")
if len(htmlstring.split('<div class="success">'))>1: # green SUCCESS divs found:
print("\nSUCCESS PROCESSING {} BY PHPMYADMIN:".format(sqlfile))
for sdiv in htmlstring.split('<div class="success">')[1:]:
successdiv = sdiv.split("</div>")[0]
print("\n----- SUCCESS DIV CONTENT: -----\n", successdiv, "\n","-"*25)
print("\n... pma_import_resubmit(resumeurl='{}') finished\n".format(resumeurl))
return(br)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment