It looks like my workaround doesn't quite work. And the reason is not snapcraft which correctly passes the environment variable down.
I found that pip is also not a problem. I was using a simple package installation as a test case and found that py_compile correctly gets the SOURCE_DATE_EPOCH variable and decides that SOURCE_DATE_EPOCH method should be used. https://paste.ubuntu.com/p/63d5wD3DsZ/
However, the importlib._bootstrap_external._code_to_hash_pyc function returns a different value for the same source during different executions (change of a process):
It looks like my workaround doesn't quite work. And the reason is not snapcraft which correctly passes the environment variable down.
I found that pip is also not a problem. I was using a simple package installation as a test case and found that py_compile correctly gets the SOURCE_DATE_EPOCH variable and decides that SOURCE_DATE_EPOCH method should be used. /paste. ubuntu. com/p/63d5wD3Ds Z/
https:/
So, in the end, we correctly get to this point where the .pyc content is generated: /github. com/python/ cpython/ blob/0f5a28f834 bdac2da8a04597d c0fc5b71e50da9d /Lib/py_ compile. py#L164- L172
https:/
However, the importlib. _bootstrap_ external. _code_to_ hash_pyc function returns a different value for the same source during different executions (change of a process):
root@ftest:~/test# rm -r ./*
root@ftest:~/test# pip3 install -t ./ setuptools
> /usr/lib/ python3. 8/py_compile. py(169) compile( ) _bootstrap_ external. _code_to_ hash_pyc( ion_mode) ; import hashlib ; hashlib. sha256( source_ bytes). hexdigest( ) ; hashlib. sha256( source_ hash).hexdigest () ; hashlib. sha256( importlib. _bootstrap_ external. _code_to_ hash_pyc( code, source_hash, (invalidation_mode == PycInvalidation Mode.CHECKED_ HASH))) .hexdigest( ) ; importlib. _bootstrap_ external. MAGIC_NUMBER Mode.CHECKED_ HASH 1cedbec418662b2 9f4226a91e9f579 dfc4218722f2a82 6a2b5' e3ad48843f9d615 ece9310755cbfb9 deb037c3c9ce2d5 e249f' d2e875030d27179 61f319c4da55b0d 012fcb09f38d91e da6cd'
-> bytecode = importlib.
(Pdb) print(invalidat
PycInvalidation
'a33213258b106b
'6750d9e9657c3c
'4e537d6107457e
b'U\r\r\n'
root@ftest:~/test# rm -r ./*
root@ftest:~/test# pip3 install -t ./ setuptools
(Pdb) print(invalidat ion_mode) ; import hashlib ; hashlib. sha256( source_ bytes). hexdigest( ) ; hashlib. sha256( source_ hash).hexdigest () ; hashlib. sha256( importlib. _bootstrap_ external. _code_to_ hash_pyc( code, source_hash, (invalidation_mode == PycInvalidation Mode.CHECKED_ HASH))) .hexdigest( ) ; importlib. _bootstrap_ external. MAGIC_NUMBER Mode.CHECKED_ HASH 1cedbec418662b2 9f4226a91e9f579 dfc4218722f2a82 6a2b5' e3ad48843f9d615 ece9310755cbfb9 deb037c3c9ce2d5 e249f' f4f5c4ee8d29722 8241c6488b0e39b 6d71cd2de9d0bb7 a9790'
PycInvalidation
'a33213258b106b
'6750d9e9657c3c
'8d7d7ab7695b88
b'U\r\r\n'
(Pdb) cfile unpacked- wheel-lx3lxwld/ setuptools/ command/ __pycache_ _/bdist_ egg.cpython- 38.pyc'
'/tmp/pip-
Within the same process the value returned by importlib. _bootstrap_ external. _code_to_ hash_pyc for the same source does not change:
> /usr/lib/ python3. 8/py_compile. py(169) compile( ) _bootstrap_ external. _code_to_ hash_pyc( ion_mode) ; import hashlib ; hashlib. sha256( source_ bytes). hexdigest( ) ; hashlib. sha256( source_ hash).hexdigest () ; hashlib. sha256( importlib. _bootstrap_ external. _code_to_ hash_pyc( code, source_hash, (invalidation_mode == PycInvalidation Mode.CHECKED_ HASH))) .hexdigest( ) ; importlib. _bootstrap_ external. MAGIC_NUMBER Mode.CHECKED_ HASH 1cedbec418662b2 9f4226a91e9f579 dfc4218722f2a82 6a2b5' e3ad48843f9d615 ece9310755cbfb9 deb037c3c9ce2d5 e249f' 1e7072be45d7235 3ee21db9b2b28f5 a17bd6c957b0181 baa64' ion_mode) ; import hashlib ; hashlib. sha256( source_ bytes). hexdigest( ) ; hashlib. sha256( source_ hash).hexdigest () ; hashlib. sha256( importlib. _bootstrap_ external. _code_to_ hash_pyc( code, source_hash, (invalidation_mode == PycInvalidation Mode.CHECKED_ HASH))) .hexdigest( ) ; importlib. _bootstrap_ external. MAGIC_NUMBER Mode.CHECKED_ HASH 1cedbec418662b2 9f4226a91e9f579 dfc4218722f2a82 6a2b5' e3ad48843f9d615 ece9310755cbfb9 deb037c3c9ce2d5 e249f' 1e7072be45d7235 3ee21db9b2b28f5 a17bd6c957b0181 baa64'
-> bytecode = importlib.
print(invalidat
PycInvalidation
'a33213258b106b
'6750d9e9657c3c
'46827abbb669a5
b'U\r\r\n'
(Pdb) print(invalidat
PycInvalidation
'a33213258b106b
'6750d9e9657c3c
'46827abbb669a5
b'U\r\r\n'
importlib. _bootstrap_ external. _code_to_ hash_pyc /github. com/python/ cpython/ blob/0f5a28f834 bdac2da8a04597d c0fc5b71e50da9d /Lib/importlib/ _bootstrap_ external. py#L601- L609 hash_pyc( code, source_hash, checked=True): MAGIC_NUMBER) extend( _pack_uint32( flags)) extend( source_ hash) extend( marshal. dumps(code) )
https:/
def _code_to_
"Produce the data for a hash-based pyc."
data = bytearray(
flags = 0b1 | checked << 1
data.
assert len(source_hash) == 8
data.
data.
return data