提交 6faf896e authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Move hash_from_code to theano.utils and remove theano.gof.utils.hash_from_file

上级 5a180b58
import hashlib
import logging import logging
import os import os
import shlex import shlex
...@@ -15,7 +14,7 @@ from configparser import ( ...@@ -15,7 +14,7 @@ from configparser import (
from functools import wraps from functools import wraps
from io import StringIO from io import StringIO
from theano.utils import deprecated from theano.utils import deprecated, hash_from_code
_logger = logging.getLogger("theano.configparser") _logger = logging.getLogger("theano.configparser")
...@@ -67,17 +66,6 @@ class _ChangeFlagsDecorator: ...@@ -67,17 +66,6 @@ class _ChangeFlagsDecorator:
v.__set__(self._root, self.old_vals[k]) v.__set__(self._root, self.old_vals[k])
def _hash_from_code(msg):
"""This function was copied from theano.gof.utils to get rid of that import."""
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return "m" + hashlib.sha256(msg).hexdigest()
class _SectionRedirect: class _SectionRedirect:
"""Functions as a mock property on the TheanoConfigParser. """Functions as a mock property on the TheanoConfigParser.
...@@ -136,7 +124,7 @@ class TheanoConfigParser: ...@@ -136,7 +124,7 @@ class TheanoConfigParser:
[c for c in self._config_var_dict.values() if c.in_c_key], [c for c in self._config_var_dict.values() if c.in_c_key],
key=lambda cv: cv.name, key=lambda cv: cv.name,
) )
return _hash_from_code( return hash_from_code(
"\n".join( "\n".join(
[ [
"{} = {}".format(cv.name, cv.__get__(self, self.__class__)) "{} = {}".format(cv.name, cv.__get__(self, self.__class__))
......
import hashlib
import linecache import linecache
import sys import sys
import traceback import traceback
...@@ -462,23 +461,3 @@ def flatten(a): ...@@ -462,23 +461,3 @@ def flatten(a):
return l return l
else: else:
return [a] return [a]
def hash_from_code(msg):
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return "m" + hashlib.sha256(msg).hexdigest()
def hash_from_file(file_path):
"""
Return the SHA256 hash of a file.
"""
with open(file_path, "rb") as f:
file_content = f.read()
return hash_from_code(file_content)
...@@ -26,9 +26,14 @@ from theano.configdefaults import config, gcc_version_str ...@@ -26,9 +26,14 @@ from theano.configdefaults import config, gcc_version_str
# we will abuse the lockfile mechanism when reading and writing the registry # we will abuse the lockfile mechanism when reading and writing the registry
from theano.gof import compilelock from theano.gof import compilelock
from theano.gof.utils import flatten, hash_from_code from theano.gof.utils import flatten
from theano.link.c.exceptions import MissingGXX from theano.link.c.exceptions import MissingGXX
from theano.utils import LOCAL_BITWIDTH, output_subprocess_Popen, subprocess_Popen from theano.utils import (
LOCAL_BITWIDTH,
hash_from_code,
output_subprocess_Popen,
subprocess_Popen,
)
importlib = None importlib = None
......
from theano.gof.utils import hash_from_code from theano.utils import hash_from_code
def hash_from_sparse(data): def hash_from_sparse(data):
......
import numpy as np import numpy as np
import theano import theano
from theano.gof.utils import hash_from_code from theano.utils import hash_from_code
def hash_from_ndarray(data): def hash_from_ndarray(data):
......
"""Utility functions that only depend on the standard library.""" """Utility functions that only depend on the standard library."""
import hashlib
import inspect import inspect
import os import os
import struct import struct
...@@ -284,3 +285,14 @@ def output_subprocess_Popen(command, **params): ...@@ -284,3 +285,14 @@ def output_subprocess_Popen(command, **params):
# the stdout/stderr pipe. # the stdout/stderr pipe.
out = p.communicate() out = p.communicate()
return out + (p.returncode,) return out + (p.returncode,)
def hash_from_code(msg):
"""Return the SHA256 hash of a string or bytes."""
# hashlib.sha256() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return "m" + hashlib.sha256(msg).hexdigest()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论