comparison protxml2html_wrapper.py @ 0:9156a440afed draft default tip

Improved some datatype handling
author galaxyp
date Thu, 20 Jun 2013 11:09:24 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9156a440afed
1 #!/usr/bin/env python
2 import optparse
3 import os
4 import sys
5 import tempfile
6 import shutil
7 import subprocess
8 import re
9 from os.path import basename
10 import logging
11
12 assert sys.version_info[:2] >= ( 2, 6 )
13
14 log = logging.getLogger(__name__)
15 working_directory = os.getcwd()
16 tmp_stderr_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stderr').name
17 tmp_stdout_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stdout').name
18
19 def stop_err( msg ):
20 sys.stderr.write( "%s\n" % msg )
21 sys.exit()
22
23 def read_stderr():
24 stderr = ''
25 if(os.path.exists(tmp_stderr_name)):
26 with open(tmp_stderr_name, 'rb') as tmp_stderr:
27 buffsize = 1048576
28 try:
29 while True:
30 stderr += tmp_stderr.read(buffsize)
31 if not stderr or len(stderr) % buffsize != 0:
32 break
33 except OverflowError:
34 pass
35 return stderr
36
37 def execute(command, stdin=None):
38 with open(tmp_stderr_name, 'wb') as tmp_stderr:
39 with open(tmp_stdout_name, 'wb') as tmp_stdout:
40 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
41 returncode = proc.wait()
42 if returncode != 0:
43 raise Exception, "Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())
44
45 def delete_file(path):
46 if os.path.exists(path):
47 try:
48 os.remove(path)
49 except:
50 pass
51
52 def delete_directory(directory):
53 if os.path.exists(directory):
54 try:
55 shutil.rmtree(directory)
56 except:
57 pass
58
59 def symlink(source, link_name):
60 import platform
61 if platform.system() == 'Windows':
62 import win32file
63 win32file.CreateSymbolicLink(source, link_name, 1)
64 else:
65 os.symlink(source, link_name)
66
67
68 def copy_to_working_directory(data_file, relative_path):
69 if os.path.abspath(data_file) != os.path.abspath(relative_path):
70 shutil.copy(data_file, relative_path)
71 return relative_path
72
73 def __main__():
74 run_script()
75
76 #ENDTEMPLATE
77
78
79 def run_script():
80 parser = optparse.OptionParser()
81 parser.add_option("--input")
82 parser.add_option("--export_spreadsheet", action="store_true", dest="export_spreadsheet")
83 parser.set_defaults(export_spreadsheet=False)
84 (options, args) = parser.parse_args()
85
86 copy_to_working_directory(options.input, "input.prot.xml")
87 cmd = "protxml2html.pl -file ./input.prot.xml"
88 if options.export_spreadsheet:
89 cmd = "%s FORMAT EXCEL" % cmd
90 else:
91 cmd = "%s FORMAT HTML" % cmd
92 execute(cmd)
93
94 if __name__ == '__main__': __main__()