Mercurial > repos > geert-vandeweyer > files_to_ftp
annotate DownloadFiles.py @ 3:fe42761670f1 draft default tip
Uploaded
author | geert-vandeweyer |
---|---|
date | Tue, 25 Mar 2014 13:15:25 -0400 |
parents | 837fafa672d2 |
children |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
2 #by Geert Vandeweyer. | |
3 | |
4 #from galaxy import eggs | |
5 import sys, os | |
6 import os.path | |
7 import datetime | |
8 | |
9 def stop_err(msg): | |
10 sys.stderr.write(msg) | |
11 sys.exit() | |
12 | |
13 def __main__(): | |
14 htmloutfile = sys.argv[1] | |
15 useremail = sys.argv[2] | |
16 folder = sys.argv[3] | |
17 ftpdirconfig = sys.argv[4] | |
18 try: | |
19 fout = open(sys.argv[1],'w') | |
20 except: | |
21 stop_err("Output file cannot be opened for writing.") | |
22 | |
23 if len(sys.argv) < 5: | |
24 stop_err("No datafiles specified!"); | |
25 | |
26 # write header to outfile | |
27 fout.write("<html><body><h2>Files copied to your FTP account.</h2>") | |
28 filecounter = 5 | |
29 fout.write("<p><ol> ") | |
30 now = datetime.datetime.now() | |
31 date=str(now)[:10] | |
2
837fafa672d2
common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents:
1
diff
changeset
|
32 #fout.write("<li>%s</li>" %(len(sys.argv))) |
1 | 33 # compose ftp storage location |
34 ftpdir="%s/%s/" %(ftpdirconfig,useremail) | |
35 if len(folder) == 0: | |
36 subdir = date | |
37 storedir="%s/%s/%s/" %(ftpdirconfig,useremail,date) | |
38 else: | |
39 storedir="%s/%s/%s/" %(ftpdirconfig,useremail,folder) | |
40 subdir = folder | |
41 if not os.path.exists(ftpdir): | |
3 | 42 try: |
43 os.makedirs(ftpdir) | |
44 except: | |
45 stop_err("User dir on FTP could not be created: %s" % (ftpdir)) | |
1 | 46 if not os.path.exists(storedir): |
3 | 47 |
48 try: | |
49 os.makedirs(storedir) | |
50 except: | |
51 stop_err("Target dir could not be created: %s" % (storedir)) | |
1 | 52 |
53 while filecounter < len(sys.argv): | |
54 inp = sys.argv[filecounter] | |
55 name = sys.argv[(filecounter+1)] | |
2
837fafa672d2
common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents:
1
diff
changeset
|
56 name = name.replace(':','_') |
837fafa672d2
common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents:
1
diff
changeset
|
57 name = name.replace(' ','_') |
837fafa672d2
common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents:
1
diff
changeset
|
58 name = name.replace('@','_AT_') |
1 | 59 ext = sys.argv[(filecounter+2)] |
60 # copy files to tmp dir & filenames | |
3 | 61 try: |
62 os.system("cp '%s' '%s/%s.%s'" %(inp, storedir,name,ext)) | |
63 except: | |
64 stop_err("Copy failed for target file : %s/%s.%s. Command was\ncp '%s' '%s/%s.%s'" % (storedir,name,ext,inp,storedir,name,ext)) | |
2
837fafa672d2
common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents:
1
diff
changeset
|
65 ## gzip if fastq/fastqsanger/vcf |
837fafa672d2
common uncompressed files are now gzipped upons transfer to the ftp directory to save space.
geert-vandeweyer
parents:
1
diff
changeset
|
66 if ( ext == "fastq" or ext == "fastqsanger" or ext == "vcf" or ext == "sam" or ext == "txt"): |
3 | 67 try: |
68 os.system("gzip -9 '%s/%s.%s'" %(storedir,name,ext)) | |
69 except: | |
70 stop_err("Compression of %s/%s.%s failed." % (storedir,name,ext)) | |
1 | 71 fout.write("<li>%s.%s</li>" %(name,ext)) |
72 filecounter +=3 | |
73 | |
74 fout.write("</ol></p>") | |
75 fout.write("<p>Info: Files written to the '%s' subdirectory in your FTP account</p></body></html>" %(subdir)) | |
76 # close file | |
77 fout.close | |
78 | |
79 | |
80 if __name__ == "__main__": __main__() | |
81 |