Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make manifest.py 2/3 compatible #35

Merged
2 commits merged into from
Jul 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions smdx/manifest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import print_function

import os
import hashlib
import sys
import xml.etree.ElementTree as ET

PY2 = (2,) <= sys.version_info < (3,)

MANIFEST_ROOT = 'manifest'
MANIFEST_FILE = 'file'
MANIFEST_NAME = 'name'
Expand Down Expand Up @@ -58,17 +62,17 @@ def create(self):
f.close()

def md5(self):
return hashlib.md5(self.to_xml_str()).hexdigest()
return hashlib.md5(self.to_xml_bytes()).hexdigest()

def diff(self, m):
diff_str = ''
for f, md5 in self.files.iteritems():
for f, md5 in self.files.items():
m_md5 = m.files.get(f)
if md5 != m_md5:
diff_str += '%s: %s %s\n' % (f, md5, m_md5)

if len(self.files) != len(m.files):
for f, md5 in m.files.iteritems():
for f, md5 in m.files.items():
if self.files.get(f) is None:
diff_str += '%s: "None" %s\n' % (f, md5)
return diff_str
Expand All @@ -81,12 +85,12 @@ def scan(self):
if f in manifest_files or (ext == '.xml' and f.startswith('smdx_')):
filename = os.path.join(self.path, f)
content = open(filename, 'rb').read()
# if content.find('\r\n') >= 0:
# print 'windows file ', self.path
content = content.replace('\r\n', '\n')
# if content.find(b'\r\n') >= 0:
# print('windows file ', self.path)
content = content.replace(b'\r\n', b'\n')
md5 = hashlib.md5(content).hexdigest()
self.files[f] = md5
except Exception, e:
except Exception as e:
raise ManifestError('Error scanning directory %s: %s' % (self.path, str(e)))

def scan_strip(self):
Expand All @@ -97,14 +101,14 @@ def scan_strip(self):
if f in manifest_files or (ext == '.xml' and f.startswith('smdx_')):
filename = os.path.join(self.path, f)
content = open(filename, 'rb').read()
if content.find('\r\n') >= 0:
content = content.replace('\r\n', '\n')
if content.find(b'\r\n') >= 0:
content = content.replace(b'\r\n', b'\n')
fc = open(filename, 'w')
fc.write(content)
fc.close()
md5 = hashlib.md5(content).hexdigest()
self.files[f] = md5
except Exception, e:
except Exception as e:
raise ManifestError('Error scanning directory %s: %s' % (self.path, str(e)))

def to_xml(self, parent=None, filename=None):
Expand All @@ -119,7 +123,7 @@ def to_xml(self, parent=None, filename=None):

return e

def to_xml_str(self, pretty_print=True):
def to_xml_bytes(self, pretty_print=True):
e = self.to_xml()

if pretty_print:
Expand All @@ -128,16 +132,19 @@ def to_xml_str(self, pretty_print=True):
return ET.tostring(e)

def to_xml_file(self, filename=None, pretty_print=True, replace_existing=True):
xml = self.to_xml_str(pretty_print)
xml = self.to_xml_bytes(pretty_print)

if filename is not None:
if replace_existing is False and os.path.exists(filename):
raise ManifestError('File %s already exists' % (filename))
f = open(filename, 'w')
f = open(filename, 'wb')
f.write(xml)
f.close()
else:
print xml
if PY2:
print(xml)
else:
print(xml.decode())

def from_xml(self, element=None, filename=None):
if element is None and filename is not None:
Expand Down