]> git.donarmstrong.com Git - lilypond.git/blob - cygwin/lily-wins.py
Imported sources
[lilypond.git] / cygwin / lily-wins.py
1 #!@PYTHON@
2 # lily-wins.py -- LilyPond command for .ly on Windows
3
4 import os
5 import re
6 import sys
7
8 def usage ():
9         print 'Usage [-h,--help] lily-wins LY-FILE'
10
11 # print debugging stuff for now
12 def debug (s):
13         print s
14
15 def read_pipe (command):
16         debug ('command:' + command)
17         s = os.popen (command).read ()
18         if s and s[-1] == '\n':
19                 return s[:-1]
20         return s 
21
22 def system (command):
23         debug ('command:' + command)
24         os.system (command)
25
26 def strip_extension (f, ext):
27         (p, e) = os.path.splitext (f)
28         if e == ext:
29                 e = ''
30         return p + e
31
32 def escape_shell (x):
33         return re.sub ("(\s|[`'\"\\\\])", r'\\\1',x)
34 #       return re.sub (r'''([^\\])([`'"\\\s])''', r'\1\\\2', x)
35         # help emacs'" broken python mode
36         
37 debug (`sys.argv`)
38
39 if len (sys.argv) != 2 \
40    or sys.argv[1] == '-h' or sys.argv[1] == '--help':
41         usage ()
42         sys.exit (0)
43         
44 native_file = sys.argv[1]
45         
46 file = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_file))
47 if not file:
48         file = native_file
49
50 cwd = os.getcwd ()
51
52 dir = os.path.dirname (file)
53 if not dir:
54         dir = '.'
55 base = os.path.basename (file)
56 stem = strip_extension (base, '.ly')
57 print `vars ()`
58
59 native_base = '%(dir)s/%(stem)s' % vars ()
60 native_base = read_pipe ('/usr/bin/cygpath -aw %s' % escape_shell (native_base))
61                          
62 if not native_base:
63         native_base = '%(dir)s/%(stem)s' % vars ()
64
65 pdfname = read_pipe ('/usr/bin/regtool get /root/.pdf/')
66 pdfopencommand = read_pipe ('/usr/bin/regtool get /root/%s/shell/open/command/' % escape_shell (pdfname))
67
68 # hmm
69 native_view = re.sub ('"([^"]*).*"', '\\1', pdfopencommand)
70 if not native_view:
71         native_view = 'acrobat'
72         
73 if native_view:
74         pdfview = read_pipe ('/usr/bin/cygpath -au %s' % escape_shell (native_view))
75 if not pdfview:
76         # message box?
77         sys.stderr.write ('no pdf viewer found\n')
78         pdfview = 'xpdf'
79
80 os.chdir (dir)
81 if os.path.exists ('/usr/bin/ly2dvi'):
82         system ('/usr/bin/ly2dvi -p %s > %s.log 2>&1' % (escape_shell (base),
83                                                          escape_shell (stem)))
84 else:
85         system ('/usr/bin/lilypond %s > %s.log 2>&1' % (escape_shell (base),
86                                                         escape_shell (stem)))
87 if not os.path.exists ('%(stem)s.pdf' % vars ()):
88         # message box?
89         sys.stderr.write ('pdf output not found\n')
90
91 system ('%s %s.pdf' % (escape_shell (pdfview), escape_shell (native_base)))