Re: Bug 11101: Document with xfig figures fail with python3

2018-04-04 Thread José Abílio Matos
After looking into all the python scripts I have fixed most of the issues.

The only possible issues are files that are not in utf-8 encoding, I think that 
in all those cases we are on the safe side but I could be wrong.

The patch as it is should be compatible with both python 2 and python 3, so 
it should be safe both for lyx 2.3 and 2.4.

I would appreciate some testing before committing it. :-)

Regards,
-- 
José Abílio
diff --git a/lib/scripts/convertDefault.py b/lib/scripts/convertDefault.py
index e54b066888..9a460b7cf3 100644
--- a/lib/scripts/convertDefault.py
+++ b/lib/scripts/convertDefault.py
@@ -16,8 +16,11 @@
 # replacement in ~/.lyx/scripts
 
 # converts an image $2 (format $1) to $4 (format $3)
+from __future__ import print_function
 import os, re, sys
 
+PY2 = sys.version_info[0] == 2
+
 # We may need some extra options only supported by recent convert versions
 re_version = re.compile(r'^Version:.*ImageMagick\s*(\d*)\.(\d*)\.(\d*).*$')
 # imagemagick 7
@@ -31,6 +34,9 @@ if fout.close() != None:
 fout = os.popen('convert -version 2>&1')
 output = fout.readline()
 fout.close()
+if not PY2:
+output = output.decode()
+
 version = re_version.match(output)
 
 # Imagemagick by default
@@ -63,12 +69,12 @@ if sys.argv[1] == 'pdf' and (version >= 0x060206 or gm):
 if sys.argv[3] == 'ppm' and (im and version >= 0x060305 or gm):
 opts = opts + ' -flatten'
 
-# print >> sys.stdout, command, sys.argv[2], sys.argv[4]
+# print (command, sys.argv[2], sys.argv[4], file= sys.stdout)
 if (im or gm) and os.system(r'%s %s "%s" "%s"' % (command, opts, sys.argv[2], sys.argv[3] + ':' + sys.argv[4])) != 0:
-print >> sys.stderr, sys.argv[0], 'ERROR'
-print >> sys.stderr, ('Execution of "%s" failed.' % command)
+print (sys.argv[0], 'ERROR', file= sys.stderr)
+print ('Execution of "%s" failed.' % command, file= sys.stderr)
 sys.exit(1)
 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:
-print >> sys.stderr, sys.argv[0], 'ERROR'
-print >> sys.stderr, ('Execution of "%s" failed.' % command)
+print (sys.argv[0], 'ERROR', file= sys.stderr)
+print ('Execution of "%s" failed.' % command, file= sys.stderr)
 sys.exit(1)
diff --git a/lib/scripts/fen2ascii.py b/lib/scripts/fen2ascii.py
index d7f0fb3d77..74087440e2 100644
--- a/lib/scripts/fen2ascii.py
+++ b/lib/scripts/fen2ascii.py
@@ -9,6 +9,7 @@
 # This script will convert a chess position in the FEN
 # format to an ascii representation of the position.
 
+from __future__ import print_function
 import sys,string,os
 
 os.close(0)
@@ -26,7 +27,7 @@ comp=string.split(line,'/')
 cont=1
 margin= " "*6
 
-print margin+'   +'+"-"*15+'+'
+print (margin+'   +'+"-"*15+'+')
 for i in range(8):
 cont = cont + 1
 tmp=""
@@ -42,7 +43,7 @@ for i in range(8):
 cont = cont + 1
 
 row = 8 - i
-print margin, row, tmp+"|"
+print (margin, row, tmp+"|")
 
-print margin+'   +'+"-"*15+'+'
-print margin+'a b c d e f g h '
+print (margin+'   +'+"-"*15+'+')
+print (margin+'a b c d e f g h ')
diff --git a/lib/scripts/fig2pdftex.py b/lib/scripts/fig2pdftex.py
index 603fd31103..b458ccd8f3 100644
--- a/lib/scripts/fig2pdftex.py
+++ b/lib/scripts/fig2pdftex.py
@@ -26,7 +26,7 @@
 #   the real pdf file will be overwritten by a tex file named file.pdf.
 #
 
-
+from __future__ import print_function
 import os, sys, re
 
 
@@ -35,7 +35,7 @@ def runCommand(cmd):
 run a command, quit if fails
 '''
 if os.system(cmd) != 0:
-print "Command '%s' fails." % cmd
+print("Command '%s' fails." % cmd)
 sys.exit(1)
 
 
@@ -78,15 +78,15 @@ else:
 # with tetex.
 epsfile = outbase + '.pstex'
 tmp = mkstemp()
-boundingboxline = re.compile('%%BoundingBox:\s+(\d*)\s+(\d*)\s+(\d*)\s+(\d*)')
-for line in open(epsfile).xreadlines():
-if line[:13] == '%%BoundingBox':
-(llx, lly, urx, ury) = map(int, boundingboxline.search(line).groups())
+boundingboxline = re.compile(b'%%BoundingBox:\s+(\d*)\s+(\d*)\s+(\d*)\s+(\d*)')
+for line in open(epsfile, 'rb'):
+if line[:13] == b'%%BoundingBox':
+(llx, lly, urx, ury) = list(map(int, boundingboxline.search(line).groups()))
 width = urx - llx
 height = ury - lly
 xoffset = - llx
 yoffset = - lly
-tmp.write('''BoundingBox: 0 0 %d %d
+tmp.write(b'''BoundingBox: 0 0 %d %d
 << /PageSize  [%d %d] >> setpagedevice
 gsave %d %d translate
 ''' % (width, height, width, height, xoffset, yoffset))
diff --git a/lib/scripts/fig2pstex.py b/lib/scripts/fig2pstex.py
index aaf3a1bd3a..90e163de40 100644
--- a/lib/scripts/fig2pstex.py
+++ b/lib/scripts/fig2pstex.py
@@ -26,6 +26,7 @@
 #   the real eps file will be overwritten by a tex file named file.eps.
 #
 
+from __future__ import print_function
 import os, sys
 
 # We expect two args, the names of the i

Re: Bug 11101: Document with xfig figures fail with python3

2018-04-03 Thread José Abílio Matos
On Tuesday, 3 April 2018 12.55.20 WEST Helge Hafting wrote:
> Existing documents with xfig figures cannot be opened, because LyX then
> invokes /usr/share/lyx/scripts/fig_copy.py which python3 does not
> understand. In other words, backward compatibility seems broken.
> 
> On arch linux, "python" is now python3.  Python version 2 is available,
> but is called "python2".
> 
> I can work around this by creating a /usr/local/bin/python script that does:
> 
> exec python2 "$@"
> 
> This is a bit heavy-handed, as other sw may want python3.
> 
> 
> python2 has no problems with fig_copy.py, but LyX 2.3.0 does not seem to
> take advantage of python2.
> 
> 
> Helge Hafting

Here follows the complete patch after a cursory test. The difference now is 
that this patch 
should work for files that have a non utf-8 enconding if they are in different 
directories.

All the other cases, if the files were in the same directory or if the encoding 
was utf-8 (from 
which ascii is a subset), already should have worked with the previous patch.

Regards,
-- 
José Abílio
diff --git a/lib/scripts/fig_copy.py b/lib/scripts/fig_copy.py
index d5e0421668..a398c1dbf5 100644
--- a/lib/scripts/fig_copy.py
+++ b/lib/scripts/fig_copy.py
@@ -17,14 +17,15 @@
 # picture files that are stored as relative paths are replaced
 # with the absolute path.
 
+from __future__ import print_function
 import os, sys
 
 if len(sys.argv) != 3:
-print >> sys.stderr, "Usage: fig_copy.py  "
+print ("Usage: fig_copy.py  ", file=sys.stderr)
 sys.exit(1)
 
 if not os.path.isfile(sys.argv[1]):
-print >> sys.stderr, "Unable to read", sys.argv[1]
+print ("Unable to read", sys.argv[1], file=sys.stderr)
 sys.exit(1)
 
 from_dir = os.path.split(os.path.realpath(sys.argv[1]))[0]
@@ -45,14 +46,14 @@ import re
 # We're looking for a line of text that defines an entry of
 # type '2' (a polyline), subtype '5' (an external picture file).
 # The line has 14 other data fields.
-patternline = re.compile(r'^\s*2\s+5(\s+[0-9.+-]+){14}\s*$')
-emptyline   = re.compile(r'^\s*$')
-commentline = re.compile(r'^\s*#.*$')
+patternline = re.compile(br'^\s*2\s+5(\s+[0-9.+-]+){14}\s*$')
+emptyline   = re.compile(br'^\s*$')
+commentline = re.compile(br'^\s*#.*$')
 # we allow space in path name
-figureline  = re.compile(r'^(\s*[01]\s*)(\S[\S ]*)(\s*)$')
+figureline  = re.compile(br'^(\s*[01]\s*)(\S[\S ]*)(\s*)$')
 
-input = open(sys.argv[1], 'r')
-output = open(sys.argv[2], 'w')
+input = open(sys.argv[1], 'rb')
+output = open(sys.argv[2], 'wb')
 
 # path in the fig is relative to this path
 os.chdir(from_dir)
@@ -68,7 +69,7 @@ for line in input:
 found = False
 elif patternline.match(line):
 found = True
-print >> output, line,
+output.write(line)
 
 input.close()
 output.close()


Re: Bug 11101: Document with xfig figures fail with python3

2018-04-03 Thread José Abílio Matos
On Tuesday, 3 April 2018 13.28.55 WEST José Abílio Matos 
wrote:
> Could you test the attached patch?

There was a typo in the last patch...

-- 
José Abílio
diff --git a/lib/scripts/fig_copy.py b/lib/scripts/fig_copy.py
index d5e0421668..c6235195c2 100644
--- a/lib/scripts/fig_copy.py
+++ b/lib/scripts/fig_copy.py
@@ -17,14 +17,15 @@
 # picture files that are stored as relative paths are replaced
 # with the absolute path.
 
+from __future__ import print_function
 import os, sys
 
 if len(sys.argv) != 3:
-print >> sys.stderr, "Usage: fig_copy.py  "
+print ("Usage: fig_copy.py  ", file=sys.stderr)
 sys.exit(1)
 
 if not os.path.isfile(sys.argv[1]):
-print >> sys.stderr, "Unable to read", sys.argv[1]
+print ("Unable to read", sys.argv[1], file=sys.stderr)
 sys.exit(1)
 
 from_dir = os.path.split(os.path.realpath(sys.argv[1]))[0]
@@ -68,7 +69,7 @@ for line in input:
 found = False
 elif patternline.match(line):
 found = True
-print >> output, line,
+print (line, end="", file=output)
 
 input.close()
 output.close()


Re: Bug 11101: Document with xfig figures fail with python3

2018-04-03 Thread José Abílio Matos
On Tuesday, 3 April 2018 12.55.20 WEST Helge Hafting wrote:
> Existing documents with xfig figures cannot be opened, because LyX then
> invokes /usr/share/lyx/scripts/fig_copy.py which python3 does not
> understand. In other words, backward compatibility seems broken.
> 
> On arch linux, "python" is now python3.  Python version 2 is available,
> but is called "python2".
> 
> I can work around this by creating a /usr/local/bin/python script that does:
> 
> exec python2 "$@"
> 
> This is a bit heavy-handed, as other sw may want python3.
> 
> 
> python2 has no problems with fig_copy.py, but LyX 2.3.0 does not seem to
> take advantage of python2.
> 
> 
> Helge Hafting

That was on oversight from me. :-(

Could you test the attached patch?

I suspect that this should not be enough, basically the question is about the 
encoding of the fig 
files. I will test this and send later a more complete patch.

Regards,
-- 
José Abílio
--- fig_copy.py	(original)
+++ fig_copy.py	(refactored)
@@ -17,14 +17,15 @@
 # picture files that are stored as relative paths are replaced
 # with the absolute path.
 
+from __future__ import print_function
 import os, sys
 
 if len(sys.argv) != 3:
-print >> sys.stderr, "Usage: fig_copy.py  "
+print("Usage: fig_copy.py  ", file=sys.stderr)
 sys.exit(1)
 
 if not os.path.isfile(sys.argv[1]):
-print >> sys.stderr, "Unable to read", sys.argv[1]
+print("Unable to read", sys.argv[1], file=sys.stderr)
 sys.exit(1)
 
 from_dir = os.path.split(os.path.realpath(sys.argv[1]))[0]
@@ -68,7 +69,7 @@
 found = False
 elif patternline.match(line):
 found = True
-print >> output, line,
+print(line, end=' ', file=output)
 
 input.close()
 output.close()