Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rjdbcm committed Aug 22, 2021
1 parent f4c3ac2 commit 54121d2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 77 deletions.
72 changes: 0 additions & 72 deletions Aspidites/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,77 +97,5 @@ def create_warning(func, args, kwargs, stack, exc=Exception()):
)


MD5 = ".sha256"
code = new_contract("code", lambda x: isinstance(x, ParseResults))


@contextlib.contextmanager
def working_directory(path):
"""
A context manager which changes the working directory to the given
path, and then changes it back to its previous value on exit.
Usage:
> # Do something in original directory
> with working_directory('/my/new/path'):
> # Do something in new directory
> # Back to old directory
"""

prev_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev_cwd)


def checksum(fname, write=True, check=False):
base, name = os.path.split(fname)
fname_sha256 = os.path.join(base, "." + name) + ".sha256"

def read_sha256(data):
curr_hash = sha256()
chunk = data.read(8192)
while chunk:
curr_hash.update(chunk)
chunk = data.read(8192)
return curr_hash

if write:
with open(fname, "rb") as data:
curr_hash = read_sha256(data)
with open(fname_sha256, "wb") as digest:
digest.write(curr_hash.digest())
return pmap({curr_hash.digest(): fname}).items()[0] # immutable
if check:
with open(fname_sha256, "rb") as digest:
with open(fname, "rb") as data:
curr_hash = read_sha256(data)
old = digest.read()
new = curr_hash.digest()
if new == old:
print(
"sha256 digest check successful: %s, %s == %s"
% (fname, new.hex(), old.hex())
)
return new
else:
print(
"sha256 digest failure: %s, %s != %s"
% (fname, new.hex(), old.hex())
)
return ""


class CheckedFileStack:
def __init__(self, initial=None, pre_size=8192):
if initial is None:
initial = {}
self._files = pmap(initial, pre_size)
self.all_files = self._files.evolver()

def register(self, fname):
self.all_files.set(*checksum(fname))

def finalize(self):
return self.all_files.persistent()
6 changes: 1 addition & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
**unreleased**
**v0.10.1**

**v0.10.0**

- 598f86a patch setup.py woma compile
- c39ac21 changelog updated



-


**v0.9.34**

- a11958b refactor compiler.py for better decoupling of various writes
Expand Down

0 comments on commit 54121d2

Please sign in to comment.