view DownloadFiles.py @ 3:fe42761670f1 draft default tip

Uploaded
author geert-vandeweyer
date Tue, 25 Mar 2014 13:15:25 -0400
parents 837fafa672d2
children
line wrap: on
line source

#!/usr/bin/env python
#by Geert Vandeweyer.

#from galaxy import eggs
import sys, os
import os.path
import datetime

def stop_err(msg):
    sys.stderr.write(msg)
    sys.exit()
    
def __main__():
    htmloutfile = sys.argv[1]
    useremail = sys.argv[2]
    folder = sys.argv[3] 
    ftpdirconfig = sys.argv[4]
    try:
        fout = open(sys.argv[1],'w')
    except:
        stop_err("Output file cannot be opened for writing.")
    
    if len(sys.argv) < 5:
   	stop_err("No datafiles specified!"); 

    # write header to outfile	
    fout.write("<html><body><h2>Files copied to your FTP account.</h2>")
    filecounter = 5 
    fout.write("<p><ol> ")
    now = datetime.datetime.now()
    date=str(now)[:10]
    #fout.write("<li>%s</li>" %(len(sys.argv)))
    # compose ftp storage location
    ftpdir="%s/%s/" %(ftpdirconfig,useremail)
    if len(folder) == 0:
	    subdir = date
	    storedir="%s/%s/%s/" %(ftpdirconfig,useremail,date)
    else:
	    storedir="%s/%s/%s/" %(ftpdirconfig,useremail,folder)
	    subdir = folder
    if not os.path.exists(ftpdir):
    	try:
		os.makedirs(ftpdir)
	except:
		stop_err("User dir on FTP could not be created: %s" % (ftpdir))
    if not os.path.exists(storedir):
    	
	try:
		os.makedirs(storedir)
	except:
		stop_err("Target dir could not be created: %s" % (storedir))
	
    while filecounter < len(sys.argv):
	inp = sys.argv[filecounter]
	name = sys.argv[(filecounter+1)]
	name = name.replace(':','_')
	name = name.replace(' ','_')
	name = name.replace('@','_AT_')
	ext = sys.argv[(filecounter+2)]
	# copy files to tmp dir & filenames
	try:
		os.system("cp '%s' '%s/%s.%s'" %(inp, storedir,name,ext))
	except:
		stop_err("Copy failed for target file : %s/%s.%s. Command was\ncp '%s' '%s/%s.%s'" % (storedir,name,ext,inp,storedir,name,ext))
	## gzip if fastq/fastqsanger/vcf
	if ( ext == "fastq" or ext == "fastqsanger" or ext == "vcf" or ext == "sam" or ext == "txt"): 
		try:
			os.system("gzip -9 '%s/%s.%s'" %(storedir,name,ext))
		except:
			stop_err("Compression of %s/%s.%s failed." % (storedir,name,ext))
	fout.write("<li>%s.%s</li>" %(name,ext))
	filecounter +=3
	
    fout.write("</ol></p>")
    fout.write("<p>Info: Files written to the '%s' subdirectory in your FTP account</p></body></html>" %(subdir))
    # close file
    fout.close 

        
if __name__ == "__main__": __main__()