File 18.patch of Package jefferson

From 6b870ff03006f8d0270edce442cd0e8fd7b61ee8 Mon Sep 17 00:00:00 2001
From: ach5948 <ach5948>
Date: Tue, 30 Jun 2020 15:04:57 -0400
Subject: [PATCH] Converted to python3

README.md: Updated build instructions for python3
setup.py: Updated version to 0.3
src/scripts/jefferson: Changed all python2 specific code to python3 alternative
---
 README.md             |  2 --
 setup.py              |  4 +--
 src/scripts/jefferson | 74 +++++++++++++++++++++----------------------
 3 files changed, 39 insertions(+), 41 deletions(-)

diff --git a/README.md b/README.md
index d56d618..7292f90 100755
--- a/README.md
+++ b/README.md
@@ -11,11 +11,9 @@ $ sudo python setup.py install
 Dependencies
 ============
 - `cstruct`
-- `pyliblzma`
 
 ```bash
 $ sudo pip install cstruct
-$ sudo apt-get install python-lzma
 ```
 
 Features
diff --git a/setup.py b/setup.py
index 38bc245..4814b96 100755
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
 
 from distutils.core import setup
 
-version = '0.2'
+version = '0.3'
 
 setup(
     name='jefferson',
@@ -17,4 +17,4 @@
     packages=['jefferson'],
     package_dir={'jefferson': 'src/jefferson'},
     scripts=['src/scripts/jefferson'],
-)
\ No newline at end of file
+)
diff --git a/src/scripts/jefferson b/src/scripts/jefferson
index 891d0cc..9fd8b6d 100755
--- a/src/scripts/jefferson
+++ b/src/scripts/jefferson
@@ -1,4 +1,4 @@
-#! /usr/bin/python2
+#! /usr/bin/python3
 
 import struct
 import stat
@@ -74,7 +74,7 @@ class Jffs2_unknown_node(cstruct.CStruct):
         if comp_hrd_crc == self.hdr_crc:
             self.hdr_crc_match = True
         else:
-            #print 'hdr_crc does not match!'
+            #print('hdr_crc does not match!')
             self.hdr_crc_match = False
 
 
@@ -153,13 +153,13 @@ class Jffs2_raw_dirent(cstruct.CStruct):
         if mtd_crc(data[:self.size - 8]) == self.node_crc:
             self.node_crc_match = True
         else:
-            print 'node_crc does not match!'
+            print('node_crc does not match!')
             self.node_crc_match = False
 
         if mtd_crc(self.name) == self.name_crc:
             self.name_crc_match = True
         else:
-            print 'data_crc does not match!'
+            print('data_crc does not match!')
             self.name_crc_match = False
 
     def __str__(self):
@@ -211,23 +211,23 @@ class Jffs2_raw_inode(cstruct.CStruct):
         elif self.compr == JFFS2_COMPR_LZMA:
             self.data = jffs2_lzma.decompress(node_data, self.dsize)
         else:
-            print 'compression not implemented', self
-            print node_data.encode('hex')[:20]
+            print('compression not implemented', self)
+            print(node_data.hex()[:20])
             self.data = node_data
 
         if len(self.data) != self.dsize:
-            print 'data length mismatch!'
+            print('data length mismatch!')
 
         if mtd_crc(data[:self.size - 8]) == self.node_crc:
             self.node_crc_match = True
         else:
-            print 'hdr_crc does not match!'
+            print('hdr_crc does not match!')
             self.node_crc_match = False
 
         if mtd_crc(node_data) == self.data_crc:
             self.data_crc_match = True
         else:
-            print 'data_crc does not match!'
+            print('data_crc does not match!')
             self.data_crc_match = False
 
 class Jffs2_device_node_old(cstruct.CStruct):
@@ -301,7 +301,7 @@ def scan_fs(content, endianness, verbose=False):
                     dirent = Jffs2_raw_dirent()
                     dirent.unpack(content[0 + offset:], offset)
                     if dirent.ino in dirent_dict:
-                        print 'duplicate inode use detected!!!'
+                        print('duplicate inode use detected!!!')
                         fs_index += 1
                         fs[fs_index] = {}
                         fs[fs_index]["endianness"] = endianness
@@ -316,39 +316,39 @@ def scan_fs(content, endianness, verbose=False):
 
                     fs[fs_index][JFFS2_NODETYPE_DIRENT].append(dirent)
                     if verbose:
-                        print '0x%08X:' % (offset), dirent
+                        print('0x%08X:' % (offset), dirent)
                 elif unknown_node.nodetype == JFFS2_NODETYPE_INODE:
                     inode = Jffs2_raw_inode()
                     inode.unpack(content[0 + offset:])
                     fs[fs_index][JFFS2_NODETYPE_INODE].append(inode)
                     if verbose:
-                        print '0x%08X:' % (offset), inode
+                        print('0x%08X:' % (offset), inode)
                 elif unknown_node.nodetype == JFFS2_NODETYPE_XREF:
                     xref = Jffs2_raw_xref()
                     xref.unpack(content[offset:offset + xref.size])
                     fs[fs_index][JFFS2_NODETYPE_XREF].append(xref)
                     if verbose:
-                        print '0x%08X:' % (offset), xref
+                        print('0x%08X:' % (offset), xref)
                 elif unknown_node.nodetype == JFFS2_NODETYPE_XATTR:
                     xattr = Jffs2_raw_xattr()
                     xattr.unpack(content[offset:offset + xattr.size])
                     fs[fs_index][JFFS2_NODETYPE_XREF].append(xattr)
                     if verbose:
-                        print '0x%08X:' % (offset), xattr
+                        print('0x%08X:' % (offset), xattr)
                 elif unknown_node.nodetype == JFFS2_NODETYPE_SUMMARY:
                     summary = Jffs2_raw_summary()
                     summary.unpack(content[offset:offset + summary.size])
                     summaries.append(summary)
                     fs[fs_index][JFFS2_NODETYPE_SUMMARY].append(summary)
                     if verbose:
-                        print '0x%08X:' % (offset), summary
+                        print('0x%08X:' % (offset), summary)
                 elif unknown_node.nodetype == JFFS2_NODETYPE_CLEANMARKER:
                     pass
                 elif unknown_node.nodetype == JFFS2_NODETYPE_PADDING:
                     pass
                 else:
-                    print 'Unhandled node type', unknown_node.nodetype, unknown_node
-    return fs.values()
+                    print('Unhandled node type', unknown_node.nodetype, unknown_node)
+    return list(fs.values())
 
 
 def get_device(inode):
@@ -377,7 +377,7 @@ def dump_fs(fs, target):
             if inode.ino == dirent.ino:
                 dirent.inodes.append(inode)
         if dirent.ino in node_dict:
-            print 'duplicate dirent.ino use detected!!!', dirent
+            print('duplicate dirent.ino use detected!!!', dirent)
         node_dict[dirent.ino] = dirent
 
     for dirent in fs[JFFS2_NODETYPE_DIRENT]:
@@ -396,24 +396,24 @@ def dump_fs(fs, target):
         for pnode in pnodes:
             node_names.append(pnode.name)
         node_names.append(dirent.name)
-        path = '/'.join(node_names)
+        path = str(b'/'.join(node_names), 'utf-8')
 
         target_path = os.path.join(os.getcwd(), target, path)
         for inode in dirent.inodes:
             try:
                 if stat.S_ISDIR(inode.mode):
-                    print 'writing S_ISDIR', path
+                    print('writing S_ISDIR', path)
                     if not os.path.isdir(target_path):
                         os.makedirs(target_path)
                 elif stat.S_ISLNK(inode.mode):
-                    print 'writing S_ISLNK', path
+                    print('writing S_ISLNK', path)
                     if not os.path.islink(target_path):
                         if os.path.exists(target_path):
-                            print 'file already exists as', inode.data
+                            print('file already exists as', inode.data)
                             continue
                         os.symlink(inode.data, target_path)
                 elif stat.S_ISREG(inode.mode):
-                    print 'writing S_ISREG', path
+                    print('writing S_ISREG', path)
                     if not os.path.isfile(target_path):
                         if not os.path.isdir(os.path.dirname(target_path)):
                             os.makedirs(os.path.dirname(target_path))
@@ -424,23 +424,23 @@ def dump_fs(fs, target):
                     os.chmod(target_path, stat.S_IMODE(inode.mode))
                     break
                 elif stat.S_ISCHR(inode.mode):
-                    print 'writing S_ISBLK', path
+                    print('writing S_ISBLK', path)
                     os.mknod(target_path, inode.mode, get_device(inode))
                 elif stat.S_ISBLK(inode.mode):
-                    print 'writing S_ISBLK', path
+                    print('writing S_ISBLK', path)
                     os.mknod(target_path, inode.mode, get_device(inode))
                 elif stat.S_ISFIFO(inode.mode):
-                    print 'skipping S_ISFIFO', path
+                    print('skipping S_ISFIFO', path)
                 elif stat.S_ISSOCK(inode.mode):
-                    print 'skipping S_ISSOCK', path
+                    print('skipping S_ISSOCK', path)
                 else:
-                    print 'unhandled inode.mode: %o' % inode.mode, inode, dirent
+                    print('unhandled inode.mode: %o' % inode.mode, inode, dirent)
 
             except IOError as e:
-                print "I/O error(%i): %s" % (e.errno, e.strerror), inode, dirent
+                print("I/O error(%i): %s" % (e.errno, e.strerror), inode, dirent)
 
             except OSError as e:
-                print "OS error(%i): %s" % (e.errno, e.strerror), inode, dirent
+                print("OS error(%i): %s" % (e.errno, e.strerror), inode, dirent)
 
 
 def main():
@@ -461,7 +461,7 @@ def main():
 
     if os.path.exists(dest_path):
         if not args.force:
-            print 'Destination path already exists!'
+            print('Destination path already exists!')
             return
     else:
         os.mkdir(dest_path)
@@ -476,22 +476,22 @@ def main():
             continue
 
         dest_path_fs = os.path.join(dest_path, 'fs_%i' % fs_index)
-        print 'dumping fs #%i to %s' % (fs_index, dest_path_fs)
-        for key, value in fs.iteritems():
+        print('dumping fs #%i to %s' % (fs_index, dest_path_fs))
+        for key, value in fs.items():
             if key == "endianness":
                 if value == cstruct.BIG_ENDIAN:
-                    print 'Endianness: Big'
+                    print('Endianness: Big')
                 elif value == cstruct.LITTLE_ENDIAN:
-                    print 'Endianness: Little'
+                    print('Endianness: Little')
                 continue
 
-            print '%s count: %i' % (NODETYPES[key].__name__, len(value))
+            print('%s count: %i' % (NODETYPES[key].__name__, len(value)))
 
         if not os.path.exists(dest_path_fs):
             os.mkdir(dest_path_fs)
 
         dump_fs(fs, dest_path_fs)
-        print '-' * 10
+        print('-' * 10)
         fs_index += 1
 
 if __name__ == '__main__':
openSUSE Build Service is sponsored by