Skip to content

Commit

Permalink
(python3 migration) Fix using str and bytes on the replicator
Browse files Browse the repository at this point in the history
  • Loading branch information
neoclust committed Nov 16, 2023
1 parent a123e71 commit 7bfd131
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions pulse_xmpp_agent/replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,25 @@ def install_key_register_windows(version):
return True


def file_get_contents(filename, use_include_path=0, context=None, offset=-1, maxlen=-1):
if filename.find("://") > 0:
def file_get_contents(
filename, use_include_path=0, context=None, offset=-1, maxlen=-1, encoding=None
):
if isinstance(filename, bytes):
filename = filename.decode()

print(filename)
if filename.find('://') > 0:
ret = urllib.request.urlopen(filename).read()
if offset > 0:
ret = ret[offset:]
if maxlen > 0:
ret = ret[:maxlen]
return ret
else:
fp = open(filename, "r")
if encoding:
fp = open(filename, "r", encoding=encoding)
else:
fp = open(filename, "r")
try:
if offset > 0:
fp.seek(offset)
Expand Down Expand Up @@ -198,8 +207,9 @@ def load_list_md5_agentbase(self):
.replace("\r", "")
.strip()
)

self.directory["version_agent"] = hashlib.md5(
self.directory["version"]
self.directory["version"].encode('utf-8')
).hexdigest()
listmd5 = [self.directory["version_agent"]]
list_script_python_for_update = [
Expand All @@ -211,7 +221,7 @@ def load_list_md5_agentbase(self):

for fichiername in list_script_python_for_update:
self.directory["program_agent"][fichiername] = hashlib.md5(
file_get_contents(os.path.join(self.dir_agent_base, fichiername))
file_get_contents(os.path.join(self.dir_agent_base, fichiername)).encode('utf-8')
).hexdigest()
listmd5.append(self.directory["program_agent"][fichiername])
for fichiername in [
Expand All @@ -220,7 +230,7 @@ def load_list_md5_agentbase(self):
if x[-3:] == ".py"
]:
self.directory["lib_agent"][fichiername] = hashlib.md5(
file_get_contents(os.path.join(self.dir_agent_base, "lib", fichiername))
file_get_contents(os.path.join(self.dir_agent_base, "lib", fichiername)).encode('utf-8')
).hexdigest()
listmd5.append(self.directory["lib_agent"][fichiername])
for fichiername in [
Expand All @@ -231,11 +241,11 @@ def load_list_md5_agentbase(self):
self.directory["script_agent"][fichiername] = hashlib.md5(
file_get_contents(
os.path.join(self.dir_agent_base, "script", fichiername)
)
).encode('utf-8')
).hexdigest()
listmd5.append(self.directory["script_agent"][fichiername])
listmd5.sort()
self.directory["fingerprint"] = hashlib.md5(json.dumps(listmd5)).hexdigest()
self.directory["fingerprint"] = hashlib.md5(json.dumps(listmd5).encode('utf-8')).hexdigest()


def restorationfolder(rollback_pulse_xmpp_agent, agent_folder):
Expand Down

0 comments on commit 7bfd131

Please sign in to comment.