Skip to contentSkip to conversation

docs: add python version in shebang

PATCH In review Stefan Lengfeld 3 participants 7 messages Sep 15, 2019 – Sep 18, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 docs: add python version in shebang

On my machine 'make docs' fails with

Traceback (most recent call last):
File "/home/stc/git/barebox/Documentation/gen_commands.py", line 156, in <module>
parse_c(source)
File "/home/stc/git/barebox/Documentation/gen_commands.py", line 34, in parse_c
for line in file(name, 'r'):
NameError: name 'file' is not defined

because /usr/bin/python points to python3 instead of python2. Fix this by including the language version in the shebang of the script.

---
 Documentation/gen_commands.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/gen_commands.py +1 -1
index 6251b4f22..b46165882 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python2
 
 import errno
 import os
Signature · 7 lines
2.21.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

Hello Stefan,

Quoted 11 lines · Stefan Lengfeld

On my machine 'make docs' fails with

Traceback (most recent call last):
File "/home/stc/git/barebox/Documentation/gen_commands.py", line 156, in <module>
parse_c(source)
File "/home/stc/git/barebox/Documentation/gen_commands.py", line 34, in parse_c
for line in file(name, 'r'):
NameError: name 'file' is not defined

because /usr/bin/python points to python3 instead of python2. Fix this by including the language version in the shebang of the script.

It would be much preferable to convert this code to Python3. I looked into that and it wasn't that hard to even make it compatible to both Python 2 and Python 3. (One downside is that Python 3 is more picky about file encodings, so I have to insert a patch that converts the few latin1 file left in the tree to utf-8 first. But as this is a good thing on its own (IMHO) that's not too bad.)

Find the two patches in reply to this mail.

Best regards
Uwe

Signature · 7 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 1/2 Convert latin1 files to utf-8

The source files are parsed by $(make docs). Currently this is done using python2 (actually 'python' which is still python2 on most machines). When using python3 however (which has a stricter encoding handling) it is easier if all input is in utf-8 encoding. So convert the few remaining latin1 encoded files to utf-8. This only affects code comments.

---
 .../arm/boards/chumby_falconwing/falconwing.c |   2 +-
 lib/fonts/font_7x14.c                         | 256 +++++++++---------
 lib/fonts/font_8x16.c                         | 256 +++++++++---------
 lib/fonts/font_8x8.c                          | 256 +++++++++---------
 4 files changed, 385 insertions(+), 385 deletions(-)
arch/arm/boards/chumby_falconwing/falconwing.c +1 -1
index 5554b78d6d49..42dd838d758e 100644
@@ -65,7 +65,7 @@ static struct fb_videomode falconwing_vmode = {
 	.xres = 320,
 	.yres = 240,
 	.pixclock = KHZ2PICOS(6250),    /* max. 10 MHz */
-	/* line lenght should be 64 �s */
+	/* line lenght should be 64 µs */
 	.left_margin = 28,
 	.hsync_len = 24,
 	.right_margin = 28,
lib/fonts/font_7x14.c +128 -128

1,024 lines not on this page · raw message

lib/fonts/font_8x16.c +128 -128

1,024 lines not on this page · raw message

lib/fonts/font_8x8.c +79 -79

631 lines not on this page · raw message

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Applied
#3 lore raw

Re: [PATCH 1/2] Convert latin1 files to utf-8

Quoted 7 lines · Uwe Kleine-König

The source files are parsed by $(make docs). Currently this is done using python2 (actually 'python' which is still python2 on most machines). When using python3 however (which has a stricter encoding handling) it is easier if all input is in utf-8 encoding. So convert the few remaining latin1 encoded files to utf-8. This only affects code comments.

Quoted 7 lines
---
 .../arm/boards/chumby_falconwing/falconwing.c |   2 +-
 lib/fonts/font_7x14.c                         | 256 +++++++++---------
 lib/fonts/font_8x16.c                         | 256 +++++++++---------
 lib/fonts/font_8x8.c                          | 256 +++++++++---------
 4 files changed, 385 insertions(+), 385 deletions(-)

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH 2/2 Documentation: make gen_commands helper python3 compatible

In reply to Uwe Kleine-König

On some machines the python command is provided by Python 3 while on most (at least in my bubble) it is still Python 2. Modify the code to make it usable by both Python versions.

print_function is available in __future__ since Python 2.6.0a2, which shouldn't be a relevant restriction.

The modified script generates the same documentation as the old one; independent if the script is called using Python 2 (here: 2.7.16) or Python 3 (here: 3.7.3).

---
 Documentation/gen_commands.py | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
Documentation/gen_commands.py +18 -12
index 6251b4f22ed9..ff07ee0297f1 100755
@@ -1,5 +1,7 @@
 #!/usr/bin/python
 
+from __future__ import print_function
+
 import errno
 import os
 import re
@@ -28,10 +30,15 @@ CONT = re.compile(r"""\s*"(.*?)"\s*\)?\s*$""")
 
 CMDS = {}
 
+def string_escape(s):
+  # This used to do s.decode("string_escape") which isn't available on Python 3.
+  # Actually we only need to drop '\t', so do this here.
+  return s.replace(r'\t', '')
+
 def parse_c(name):
   cmd = None
   last = None
-  for line in file(name, 'r'):
+  for line in open(name, 'r'):
     x = HELP_START.match(line)
     if x:
       cmd = CMDS.setdefault(x.group(1), defaultdict(list))
@@ -50,14 +57,14 @@ def parse_c(name):
         last = cmd['h_pre']
       else:
         last = cmd['h_post']
-      last.append(x.group(1).decode("string_escape").strip())
+      last.append(string_escape(x.group(1)).strip())
       continue
     x = HELP_OPT.match(line)
     if x:
       last = cmd['h_opts']
       last.append([
-        x.group(1).decode("string_escape"),
-        x.group(2).decode("string_escape")
+        string_escape(x.group(1)),
+        string_escape(x.group(2)),
       ])
       continue
     x = CMD_FUNC.match(line)
@@ -68,12 +75,12 @@ def parse_c(name):
     x = CMD_DESC.match(line)
     if x:
       last = cmd['c_desc']
-      last.append(x.group(1).decode("string_escape"))
+      last.append(string_escape(x.group(1)))
       continue
     x = CMD_OPTS.match(line)
     if x:
       last = cmd['c_opts']
-      last.append(x.group(1).decode("string_escape"))
+      last.append(string_escape(x.group(1)))
       continue
     x = CMD_GROUP.match(line)
     if x:
@@ -85,9 +92,9 @@ def parse_c(name):
       if last is None:
         raise Exception("Parse error in %s: %r" % (name, line))
       if isinstance(last[-1], str):
-        last[-1] += x.group(1).decode("string_escape")
+        last[-1] += string_escape(x.group(1))
       elif isinstance(last[-1], list):
-        last[-1][1] += x.group(1).decode("string_escape")
+        last[-1][1] += string_escape(x.group(1))
       continue
     x = HELP_END.match(line)
     if x:
@@ -163,7 +170,7 @@ for name, cmd in CMDS.items():
   rst = gen_rst(name, cmd)
   group = cmd.get('c_group')
   if group is None:
-    print >> sys.stderr, "gen_commands: warning: using default group 'misc' for command '%s'" % name
+    print("gen_commands: warning: using default group 'misc' for command '%s'" % name, file=sys.stderr)
     group = ['misc']
   subdir = os.path.join(sys.argv[2], group[0])
   try:
@@ -183,9 +190,8 @@ for name, cmd in CMDS.items():
   except:
     pass
   hash_new = hashlib.sha1()
-  hash_new.update(rst)
+  hash_new.update(rst.encode('utf-8'))
   if hash_old.hexdigest() == hash_new.hexdigest():
     continue
 
-  file(target, 'w').write(rst)
-
+  open(target, 'w').write(rst)
Signature · 7 lines
2.23.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

Re: [PATCH 2/2] Documentation: make gen_commands helper python3 compatible

Quoted 29 lines · Uwe Kleine-König

On some machines the python command is provided by Python 3 while on most (at least in my bubble) it is still Python 2. Modify the code to make it usable by both Python versions.

print_function is available in __future__ since Python 2.6.0a2, which shouldn't be a relevant restriction.

The modified script generates the same documentation as the old one; independent if the script is called using Python 2 (here: 2.7.16) or Python 3 (here: 3.7.3).

---
 Documentation/gen_commands.py | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
Documentation/gen_commands.py +2 -0
index 6251b4f22ed9..ff07ee0297f1 100755
@@ -1,5 +1,7 @@
 #!/usr/bin/python
 
+from __future__ import print_function
+
 import errno
 import os
 import re

Documentation/gen_commands.py CONT = re.compile(r"""\s*"(.*?)"\s*\)?\s*$""") · line 36

Quoted 7 lines
Documentation/gen_commands.py +4 -0
@@ -28,10 +30,15 @@ CONT = re.compile(r"""\s*"(.*?)"\s*\)?\s*$""")
 
 CMDS = {}
 
+def string_escape(s):
+  # This used to do s.decode("string_escape") which isn't available on Python 3.
+  # Actually we only need to drop '\t', so do this here.
+  return s.replace(r'\t', '')

I just found a change in my working copy that didn't make it into my patch:

-  # Actually we only need to drop '\t', so do this here.
-  return s.replace(r'\t', '')
+  # Actually we only need to drop '\t' and '\n', so do this here.
+  return s.replace(r'\t', '').replace(r'\n', '')

With this the claim that the generated code is identical actually holds.

Best regards
Uwe

Signature · 7 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author Tested-by
#6 lore raw
In reply to Uwe Kleine-König

Hi Uwe,

Quoted 15 lines · Uwe Kleine-König

Hello Stefan,

Quoted 11 lines · Stefan Lengfeld

On my machine 'make docs' fails with

Traceback (most recent call last):
File "/home/stc/git/barebox/Documentation/gen_commands.py", line 156, in <module>
parse_c(source)
File "/home/stc/git/barebox/Documentation/gen_commands.py", line 34, in parse_c
for line in file(name, 'r'):
NameError: name 'file' is not defined

because /usr/bin/python points to python3 instead of python2. Fix this by including the language version in the shebang of the script.

Quoted 8 lines

It would be much preferable to convert this code to Python3. I looked into that and it wasn't that hard to even make it compatible to both Python 2 and Python 3. (One downside is that Python 3 is more picky about file encodings, so I have to insert a patch that converts the few latin1 file left in the tree to utf-8 first. But as this is a good thing on its own (IMHO) that's not too bad.)

Find the two patches in reply to this mail.

Indeed, converting the script to support Python 2 and 3 at the same time is the better fix.

Kind regards,
Stefan

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox