33import os
44import sys
55from argparse import ArgumentParser
6- from functools import lru_cache
6+ from functools import cached_property
77from importlib .metadata import version
88from pathlib import Path
99from subprocess import CalledProcessError , run
1010
1111
1212class SrcFile :
1313 def __init__ (self , path ):
14- self ._path = path
14+ self .path = path
1515
16- @property
17- @lru_cache
18- def path (self ):
19- return self ._path
20-
21- @property
22- @lru_cache
16+ @cached_property
2317 def abspath (self ):
24- return os .path .abspath (self ._path )
25-
26- @property
27- @lru_cache
28- def abspath_hash (self ):
29- hash_ = hashlib .sha512 ()
30- hash_ .update (self .abspath .encode ("utf8" ))
31- return hash_ .hexdigest ()
18+ return os .path .abspath (self .path )
3219
33- @property
34- @lru_cache
20+ @cached_property
3521 def contents_hash (self ):
22+ hash_ = hashlib .sha512 ()
3623 with open (self .abspath , "rb" ) as file :
37- hash_ = hashlib .sha512 ()
3824 hash_ .update (file .read ())
39- return hash_ .hexdigest ()
25+ return hash_ .hexdigest ()
4026
4127
4228def pip_sync_maybe (src_files , args ):
43- virtualenv = Path (os .environ ["VIRTUAL_ENV" ])
44- cached_hashes_path = virtualenv / "pip_sync_faster.json"
29+ cached_hashes_path = Path (os .environ ["VIRTUAL_ENV" ]) / "pip_sync_faster.json"
4530
4631 try :
4732 with open (cached_hashes_path , "r" , encoding = "utf8" ) as cached_hashes_file :
@@ -52,7 +37,7 @@ def pip_sync_maybe(src_files, args):
5237 src_files = [SrcFile (src_file ) for src_file in src_files ]
5338
5439 for src_file in src_files :
55- if src_file .contents_hash != cached_hashes .get (src_file .abspath_hash ):
40+ if src_file .contents_hash != cached_hashes .get (src_file .abspath ):
5641 break
5742 else :
5843 # All of the source files already had matching hashes in the cache.
@@ -65,17 +50,16 @@ def pip_sync_maybe(src_files, args):
6550 except CalledProcessError as err :
6651 sys .exit (err .returncode )
6752 else :
53+ # pip-sync succeeded so update the cache.
6854 for src_file in src_files :
69- cached_hashes [src_file .abspath_hash ] = src_file .contents_hash
55+ cached_hashes [src_file .abspath ] = src_file .contents_hash
7056
71- # pip-sync succeeded so update the cache.
7257 with open (cached_hashes_path , "w" , encoding = "utf8" ) as cached_hashes_file :
7358 json .dump (cached_hashes , cached_hashes_file )
7459
7560
76- def entry_point (): # pragma: nocover
61+ def entry_point ():
7762 parser = ArgumentParser ()
78- parser .add_argument ("-n" , "--dry-run" , action = "store_true" )
7963 parser .add_argument ("--version" , action = "store_true" )
8064 parser .add_argument ("src_files" , nargs = "*" )
8165
0 commit comments