]> git.lyx.org Git - lyx.git/blob - lib/scripts/convertDefault.py
Fix the remaing issues with comparisons with objects of different types.
[lyx.git] / lib / scripts / convertDefault.py
1 # -*- coding: utf-8 -*-
2
3 # file convertDefault.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # \author Herbert Voß
8 # \author Bo Peng
9
10 # Full author contact details are available in file CREDITS.
11
12 # The default converter if no other has been defined by the user from the
13 # Conversion->Converter tab of the Preferences dialog.
14
15 # The user can also redefine this default converter, placing their
16 # replacement in ~/.lyx/scripts
17
18 # converts an image $2 (format $1) to $4 (format $3)
19 from __future__ import print_function
20 import os, re, sys
21
22 PY2 = sys.version_info[0] == 2
23
24 # We may need some extra options only supported by recent convert versions
25 re_version = re.compile(r'^Version:.*ImageMagick\s*(\d*)\.(\d*)\.(\d*).*$')
26 # imagemagick 7
27 command = 'magick'
28 fout = os.popen('magick -version 2>&1')
29 output = fout.readline()
30 if fout.close() != None:
31     # older versions
32     # caution: windows has a convert.exe for converting file systems
33     command = 'convert'
34     fout = os.popen('convert -version 2>&1')
35     output = fout.readline()
36     fout.close()
37 if not PY2:
38     # Ensure we have a (unicode) string object in Python3
39     # (not required for version >= 3.5).
40     # FIXME: Check whether this is required with any supported 3.x version!
41     output = str(output)
42
43 version = re_version.match(output)
44
45 # Imagemagick by default
46 im = False
47 gm = False
48
49 if version != None:
50     major = int(version.group(1))
51     minor = int(version.group(2))
52     patch = int(version.group(3))
53     version = (major, minor, patch)
54     im = True
55 else:
56     # Try GraphicsMagick
57     re_version = re.compile(r'^GraphicsMagick.*http:..www.GraphicsMagick.org.*$')
58     version = re_version.match(output)
59     if version != None:
60         gm = True
61         # we need version to be a valid integer 3-tuple
62         version = (1,0,0)
63
64 # IM >= 5.5.8 separates options for source and target files
65 # See http://www.imagemagick.org/Usage/basics/#why
66 if im or gm:
67     sopts = ""
68     topts = ""
69 elif sys.platform == 'darwin':
70     command = 'lyxconvert'
71
72 # If supported, add the -define option for pdf source formats
73 if sys.argv[1] == 'pdf' and (version >= (6,2,6) or gm):
74     sopts = '-define pdf:use-cropbox=true ' + sopts
75
76 # If supported, add the -flatten option for ppm target formats (see bug 4749)
77 if sys.argv[3] == 'ppm' and (im and version >= (6,3,5) or gm):
78     topts = '-flatten'
79
80 # print (command, sys.argv[2], sys.argv[4], file= sys.stdout)
81 if (im or gm) and os.system(r'%s %s "%s" %s "%s"' % (command, sopts, sys.argv[2], topts, sys.argv[3] + ':' + sys.argv[4])) != 0:
82     print (sys.argv[0], 'ERROR', file= sys.stderr)
83     print ('Execution of "%s" failed.' % command, file= sys.stderr)
84     sys.exit(1)
85 elif not im and not gm and sys.platform == 'darwin' and os.system(r'%s "%s" "%s"' % (command, sys.argv[2], sys.argv[4])) != 0:
86     print (sys.argv[0], 'ERROR', file= sys.stderr)
87     print ('Execution of "%s" failed.' % command, file= sys.stderr)
88     sys.exit(1)