[elbe-devel] [PATCH] commands: gen_update: fixed handling of pre/post.sh scripts

Kurt Kanzenbach kurt at linutronix.de
Wed Nov 29 14:52:53 CET 2017


The current code throws an exception if the generation of update package
includes a pre and/or post shell script. Example:

root at elbe-daemon:# elbe gen_update -t . -p pre.sh -o test.pkg source.xml
[...]
Traceback (most recent call last):
  File "/usr/bin/elbe", line 61, in <module>
    cmdmod.run_command( sys.argv[2:] )
  File "/usr/lib/python2.7/dist-packages/elbepack/commands/gen_update.py", line 103, in run_command
    cfg_dir = opt.cfg_dir, cmd_dir = opt.cmd_dir )
  File "/usr/lib/python2.7/dist-packages/elbepack/updatepkg.py", line 136, in gen_update_pkg
    copyfile (project.presh_file, update + '/pre.sh')
  File "/usr/lib/python2.7/shutil.py", line 68, in copyfile
    if _samefile(src, dst):
  File "/usr/lib/python2.7/shutil.py", line 58, in _samefile
    return os.path.samefile(src, dst)
  File "/usr/lib/python2.7/posixpath.py", line 162, in samefile
    s1 = os.stat(f1)
TypeError: coercing to Unicode: need string or buffer, file found

The problem is, the code expects presh_file and postsh_file to be
strings. But they're file objects. So using strings fixes the issue.

Furthermore, adding a sanity check if the files actually exist and
printing a useful message to the user.

Signed-off-by: Kurt Kanzenbach <kurt at linutronix.de>
---
 elbepack/commands/gen_update.py | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/elbepack/commands/gen_update.py b/elbepack/commands/gen_update.py
index 9aa4a000..7d7e00d0 100644
--- a/elbepack/commands/gen_update.py
+++ b/elbepack/commands/gen_update.py
@@ -20,6 +20,7 @@
 
 from optparse import OptionParser
 import sys
+import os.path
 
 from elbepack.elbeproject import ElbeProject
 from elbepack.elbexml import ValidationError
@@ -80,18 +81,16 @@ def run_command( argv ):
         sys.exit(20)
 
     if opt.presh_file:
-        try:
-            project.presh_file = open (opt.presh_file)
-        except IOError as e:
-            print 'pre.sh file invalid: %s' % str (e)
+        if not os.path.isfile(opt.presh_file):
+            print 'pre.sh file does not exist'
             sys.exit(20)
+        project.presh_file = opt.presh_file
 
     if opt.postsh_file:
-        try:
-            project.postsh_file = open (opt.postsh_file)
-        except IOError as e:
-            print 'post.sh file invalid: %s' % str (e)
+        if not os.path.isfile(opt.postsh_file):
+            print 'post.sh file does not exist'
             sys.exit(20)
+        project.postsh_file = opt.postsh_file
 
     update_xml = None
     if len(args) >= 1:
@@ -109,9 +108,3 @@ def run_command( argv ):
     except MissingData as e:
         print str(e)
         sys.exit(20)
-
-    if project.postsh_file:
-        project.postsh_file.close ()
-
-    if project.presh_file:
-        project.presh_file.close ()
-- 
2.11.0




More information about the elbe-devel mailing list