Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use memory management functions #22

Draft
wants to merge 40 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e2d1fff
fix import error
Fadila82 Jul 4, 2022
cbf3877
free errors
Fadila82 Jul 6, 2022
fac90ec
access: deallocate unused c struct
Fadila82 Jul 6, 2022
09d173d
free unused structs
Fadila82 Jul 7, 2022
c0e0c9c
fix codacy issues
Fadila82 Jul 7, 2022
a8da7bb
linting
Fadila82 Jul 8, 2022
7f74da2
remove duplicate code
Fadila82 Aug 8, 2022
80ccdc5
correct according to codacy checks
Fadila82 Aug 8, 2022
a19bcaa
update satellite address
Fadila82 Sep 6, 2022
dbbed15
argstypes replaced by argtypes
Fadila82 Oct 29, 2022
2fd62c8
do not free upload result
Fadila82 Oct 30, 2022
b015729
test
Fadila82 Oct 31, 2022
e3e6934
test
Fadila82 Oct 31, 2022
0711d1e
test
Fadila82 Oct 31, 2022
6547aa2
use unwrap_libuplink_result
Fadila82 Nov 3, 2022
ea0d41b
code clean-up
Fadila82 Nov 3, 2022
02721a7
code clean-up
Fadila82 Nov 3, 2022
493d6d6
code clean-up
Fadila82 Nov 3, 2022
d3c4f48
code clean-up
Fadila82 Nov 3, 2022
139c51a
code cleanup
Fadila82 Nov 4, 2022
2162e46
code cleanup
Fadila82 Nov 4, 2022
83607ab
code cleanup
Fadila82 Nov 4, 2022
12851fc
code cleanup
Fadila82 Nov 4, 2022
d083724
code cleanup
Fadila82 Nov 4, 2022
86e51ab
code cleanup
Fadila82 Nov 4, 2022
4f6c64f
add __del__ to free c memory
Fadila82 Nov 10, 2022
b8f98a2
add __del__ to free c memory
Fadila82 Nov 10, 2022
c30c650
typo
Fadila82 Nov 11, 2022
d4cbf8a
storj-sim for test
Fadila82 Nov 20, 2022
4021f41
storj-sim for test
Fadila82 Nov 20, 2022
30fc6ae
storj-sim for test
Fadila82 Nov 20, 2022
ed9b284
storj-sim for test
Fadila82 Nov 20, 2022
a9e605b
storj-sim for test
Fadila82 Nov 20, 2022
69035b7
storj-sim for test
Fadila82 Nov 20, 2022
9aba7aa
storj-sim for test
Fadila82 Nov 20, 2022
2a9579e
storj-sim for test
Fadila82 Nov 20, 2022
09e200d
storj-sim for test
Fadila82 Nov 20, 2022
888345e
ci back to original
Fadila82 Nov 22, 2022
a589059
code cleanup
Fadila82 Nov 24, 2022
90970c0
code cleanup
Fadila82 Nov 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

FROM python:3.8

RUN curl -O https://dl.google.com/go/go1.14.6.linux-amd64.tar.gz
RUN sha256sum go1.14.6.linux-amd64.tar.gz
RUN tar xvf go1.14.6.linux-amd64.tar.gz
RUN curl -O "https://dl.google.com/go/go1.16.6.linux-amd64.tar.gz"
RUN sha256sum go1.16.6.linux-amd64.tar.gz
RUN tar xvf go1.16.6.linux-amd64.tar.gz
RUN chown -R root:root ./go
RUN mv go /usr/local
ENV PATH=$PATH:/usr/local/go/bin
Expand Down
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ steps:
args: ["-c", "git clone -b v1.2.2 https://github.com/storj/uplink-c"]
- name: 'gcr.io/${PROJECT_ID}/python3'
entrypoint: 'bash'
args: ["-c", "cd uplink-c && go build -o libuplinkc.so -buildmode=c-shared && cp *.so ../uplink_python/"]
args: ["-c", "cd uplink-c && go build -o libuplinkc.so -buildmode=c-shared && cp *.so ../uplink_python/"]
- name: 'gcr.io/${PROJECT_ID}/python3'
entrypoint: 'python3'
args: ['-m', 'unittest', 'test/test_cases.py', '-v']
Expand Down
2 changes: 1 addition & 1 deletion test/test_data/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self):
self.api_key = file_handle.read()
file_handle.close()

self.satellite = "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us-central-1.tardigrade.io:7777"
self.satellite = "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us1.storj.io:7777"
self.encryption_phrase = "test"

self.uplink = Uplink()
Expand Down
1 change: 0 additions & 1 deletion test/test_data/object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import string
import unittest

from uplink_python.errors import StorjException, ERROR_OBJECT_NOT_FOUND
from .helper import TestPy


Expand Down
61 changes: 28 additions & 33 deletions uplink_python/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
_AccessStruct, _ProjectResult, _StringResult, _AccessResult, _EncryptionKeyResult,\
_EncryptionKeyStruct
from uplink_python.project import Project
from uplink_python.errors import _storj_exception


class Access:
Expand Down Expand Up @@ -77,12 +76,7 @@ def derive_encryption_key(self, passphrase: str, salt: str):
encryption_key_result = self.uplink.m_libuplink.uplink_derive_encryption_key(passphrase_ptr,
salt_ptr,
length_ptr)
#
# if error occurred
if bool(encryption_key_result.error):
raise _storj_exception(encryption_key_result.error.contents.code,
encryption_key_result.error.contents.message.decode("utf-8"))
return encryption_key_result.encryption_key
return self.uplink.unwrap_encryption_key_result(encryption_key_result)

def override_encryption_key(self, bucket_name: str, prefix: str, encryption_key):
"""
Expand Down Expand Up @@ -115,8 +109,7 @@ def override_encryption_key(self, bucket_name: str, prefix: str, encryption_key)
#
# if error occurred
if bool(error_result):
raise _storj_exception(error_result.contents.code,
error_result.contents.message.decode("utf-8"))
self.uplink.free_error_and_raise_exception(error_result)

def open_project(self):
"""
Expand All @@ -134,12 +127,10 @@ def open_project(self):
#
# open project by calling the exported golang function
project_result = self.uplink.m_libuplink.uplink_open_project(self.access)
#
# if error occurred
if bool(project_result.error):
raise _storj_exception(project_result.error.contents.code,
project_result.error.contents.message.decode("utf-8"))
return Project(project_result.project, self.uplink)

_unwrapped_project = self.uplink.unwrap_project_result(project_result)

return Project(_unwrapped_project, self.uplink)

def config_open_project(self, config: Config):
"""
Expand All @@ -159,6 +150,7 @@ def config_open_project(self, config: Config):
self.uplink.m_libuplink.uplink_config_open_project.argtypes =\
[_ConfigStruct, ctypes.POINTER(_AccessStruct)]
self.uplink.m_libuplink.uplink_config_open_project.restype = _ProjectResult
self.uplink.m_libuplink.uplink_free_project_result.argtypes = [_ProjectResult]
#
# prepare the input for the function
if config is None:
Expand All @@ -168,12 +160,11 @@ def config_open_project(self, config: Config):
#
# open project by calling the exported golang function
project_result = self.uplink.m_libuplink.uplink_config_open_project(config_obj, self.access)
#
# if error occurred
if bool(project_result.error):
raise _storj_exception(project_result.error.contents.code,
project_result.error.contents.message.decode("utf-8"))
return Project(project_result.project, self.uplink)

_unwrapped_project = self.uplink.unwrap_project_result(project_result)

return Project(_unwrapped_project, self.uplink)


def serialize(self):
"""
Expand All @@ -189,15 +180,16 @@ def serialize(self):
# declare types of arguments and response of the corresponding golang function
self.uplink.m_libuplink.uplink_access_serialize.argtypes = [ctypes.POINTER(_AccessStruct)]
self.uplink.m_libuplink.uplink_access_serialize.restype = _StringResult
self.uplink.m_libuplink.uplink_free_string_result.argtypes = [_StringResult]
#
# get serialized access by calling the exported golang function
string_result = self.uplink.m_libuplink.uplink_access_serialize(self.access)
#
# if error occurred
if bool(string_result.error):
raise _storj_exception(string_result.error.contents.code,
string_result.error.contents.message.decode("utf-8"))
return string_result.string.decode("utf-8")

_unwrapped_string = self.uplink.unwrap_string_result(string_result)

serialized_access = _unwrapped_string.decode("utf-8")
self.uplink.m_libuplink.uplink_free_string_result(string_result)
return serialized_access

def share(self, permission: Permission = None, shared_prefix: [SharePrefix] = None):
"""
Expand Down Expand Up @@ -227,6 +219,7 @@ def share(self, permission: Permission = None, shared_prefix: [SharePrefix] = No
ctypes.POINTER(_SharePrefixStruct),
ctypes.c_size_t]
self.uplink.m_libuplink.uplink_access_share.restype = _AccessResult
self.uplink.m_libuplink.uplink_free_access_result.argtypes = [_AccessResult]
#
# prepare the input for the function
# check and create valid _PermissionStruct parameter
Expand All @@ -251,9 +244,11 @@ def share(self, permission: Permission = None, shared_prefix: [SharePrefix] = No
# get shareable access by calling the exported golang function
access_result = self.uplink.m_libuplink.uplink_access_share(self.access, permission_obj,
shared_prefix_obj, array_size)
#
# if error occurred
if bool(access_result.error):
raise _storj_exception(access_result.error.contents.code,
access_result.error.contents.message.decode("utf-8"))
return Access(access_result.access, self.uplink)

_unwrapped_access = self.uplink.unwrap_access_result(access_result)
return Access(_unwrapped_access, self.uplink)


def __del__(self):
"""Free memory associated to this Access"""
self.uplink.free_access_struct(self.access)
52 changes: 28 additions & 24 deletions uplink_python/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from uplink_python.module_def import _DownloadStruct, _ReadResult, _ProjectStruct,\
_ObjectResult, _Error
from uplink_python.errors import _storj_exception

_WINDOWS = os.name == 'nt'
COPY_BUFSIZE = 1024 * 1024 if _WINDOWS else 64 * 1024
Expand Down Expand Up @@ -72,6 +71,7 @@ def read(self, size_to_read: int):
ctypes.POINTER(ctypes.c_uint8),
ctypes.c_size_t]
self.uplink.m_libuplink.uplink_download_read.restype = _ReadResult
self.uplink.m_libuplink.uplink_free_read_result.argtypes = [_ReadResult]
#
# prepare the inputs for the function
data_size = ctypes.c_int32(size_to_read)
Expand All @@ -83,20 +83,20 @@ def read(self, size_to_read: int):
# read data from Storj by calling the exported golang function
read_result = self.uplink.m_libuplink.uplink_download_read(self.download, data_to_write_ptr,
size_to_read)
#
# if error occurred
if bool(read_result.error):
raise _storj_exception(read_result.error.contents.code,
read_result.error.contents.message.decode("utf-8"))

bytes_read = self.uplink.unwrap_read_result(read_result)

data_read = bytes()
if int(read_result.bytes_read) != 0:
if bytes_read != 0:
#
# --------------------------------------------
# data conversion to type python readable form
# conversion of LP_c_ubyte to python readable data variable
data_read = ctypes.string_at(data_to_write_ptr, int(read_result.bytes_read))
return data_read, int(read_result.bytes_read)

self.uplink.m_libuplink.uplink_free_read_result(read_result)

return data_read, bytes_read

def read_file(self, file_handle, buffer_size: int = 0):
"""
Expand All @@ -120,8 +120,7 @@ def read_file(self, file_handle, buffer_size: int = 0):
if not buffer_size:
buffer_size = COPY_BUFSIZE
file_size = self.file_size()
if buffer_size > file_size:
buffer_size = file_size
buffer_size = min(buffer_size, file_size)
while file_size:
buf, bytes_read = self.read(buffer_size)
if buf:
Expand All @@ -141,16 +140,18 @@ def file_size(self):
self.uplink.m_libuplink.uplink_stat_object.argtypes = [ctypes.POINTER(_ProjectStruct),
ctypes.c_char_p, ctypes.c_char_p]
self.uplink.m_libuplink.uplink_stat_object.restype = _ObjectResult
self.uplink.m_libuplink.uplink_free_object_result.argtypes = [_ObjectResult]
#
# get object information by calling the exported golang function
object_result = self.uplink.m_libuplink.uplink_stat_object(self.project, self.bucket_name,
self.storj_path)
# if error occurred
if bool(object_result.error):
raise _storj_exception(object_result.error.contents.code,
object_result.error.contents.message.decode("utf-8"))
# find object size
return int(object_result.object.contents.system.content_length)

_object = self.uplink.unwrap_object_result(object_result)

file_size = int(_object.contents.system.content_length)

self.uplink.m_libuplink.uplink_free_object_result(object_result)
return file_size

def close(self):
"""
Expand All @@ -170,8 +171,7 @@ def close(self):
#
# if error occurred
if bool(error):
raise _storj_exception(error.contents.code,
error.contents.message.decode("utf-8"))
self.uplink.free_error_and_raise_exception(error)

def info(self):
"""
Expand All @@ -188,9 +188,13 @@ def info(self):
#
# get last download info by calling the exported golang function
object_result = self.uplink.m_libuplink.uplink_download_info(self.download)
#
# if error occurred
if bool(object_result.error):
raise _storj_exception(object_result.error.contents.code,
object_result.error.contents.message.decode("utf-8"))
return self.uplink.object_from_result(object_result.object)

_unwrapped_object = self.uplink.unwrap_object_result(object_result)
_object = self.uplink.object_from_result(_unwrapped_object)

self.uplink.m_libuplink.uplink_free_object_result(object_result)

return _object

def __del__(self):
self.uplink.free_download_struct(self.download)
6 changes: 3 additions & 3 deletions uplink_python/hello_storj.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from datetime import datetime

from .errors import StorjException, BucketNotEmptyError, BucketNotFoundError
from .module_classes import ListObjectsOptions, Permission, SharePrefix
from .uplink import Uplink
from uplink_python.errors import StorjException, BucketNotEmptyError, BucketNotFoundError
from uplink_python.module_classes import ListObjectsOptions, Permission, SharePrefix
from uplink_python.uplink import Uplink

if __name__ == "__main__":

Expand Down
Loading