Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Oct 14, 2023
0 parents commit 7f929ad
Show file tree
Hide file tree
Showing 52 changed files with 3,620 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ci/aptPackagesToInstall.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libzip4
Empty file.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
end_of_line = lf

[*.{yml,yaml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .github/.templateMarker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KOLANICH/python_project_boilerplate.py
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
allow:
- dependency-type: "all"
15 changes: 15 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: typical python workflow
uses: KOLANICH-GHActions/typical-python-workflow@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__pycache__
*.py[co]
/*.egg-info
*.srctrlbm
*.srctrldb
build
dist
.eggs
monkeytype.sqlite3
/.ipynb_checkpoints
*.kate-swp
51 changes: 51 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest

variables:
DOCKER_DRIVER: overlay2
SAST_ANALYZER_IMAGE_TAG: latest
SAST_DISABLE_DIND: "true"
SAST_CONFIDENCE_LEVEL: 5
CODECLIMATE_VERSION: latest

include:
- template: SAST.gitlab-ci.yml
- template: Code-Quality.gitlab-ci.yml
- template: License-Management.gitlab-ci.yml

build:
tags:
- shared
- linux
stage: build
variables:
GIT_DEPTH: "1"
PYTHONUSERBASE: ${CI_PROJECT_DIR}/python_user_packages

before_script:
- export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables`
- apt-get update
# todo:
#- apt-get -y install
#- pip3 install --upgrade
#- python3 ./fix_python_modules_paths.py

script:
- python3 -m build -nw bdist_wheel
- mv ./dist/*.whl ./dist/libzip-0.CI-py3-none-any.whl
- pip3 install --upgrade ./dist/*.whl
- coverage run --source=libzip -m --branch pytest --junitxml=./rspec.xml ./tests/test.py
- coverage report -m
- coverage xml

coverage: "/^TOTAL(?:\\s+\\d+){4}\\s+(\\d+%).+/"

cache:
paths:
- $PYTHONUSERBASE

artifacts:
paths:
- dist
reports:
junit: ./rspec.xml
cobertura: ./coverage.xml
1 change: 1 addition & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No codes of conduct!
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include UNLICENSE
include *.md
include tests
include .editorconfig
13 changes: 13 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
libzip.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/)
=========
~~[wheel (GHA via `nightly.link`)](https://nightly.link/KOLANICH-libs/libzip.py/workflows/CI/master/libzip-0.CI-py3-none-any.whl)~~
[![Libraries.io Status](https://img.shields.io/librariesio/github/KOLANICH-libs/libzip.py.svg)](https://libraries.io/github/KOLANICH-libs/libzip.py)
[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://codeberg.org/KOLANICH-tools/antiflash.py)

Python ctypes-based bindings to [`libzip`](https://github.com/nih-at/libzip). You need a `libzip` installed:
* `libzip4` in Debian-based distros.
* `libzip` in RPM-based distros, Arch, and Gentoo.

**Warning: Currently `libzip` is used for updating files witin the archive. It doesn't allow rewriting files in archives without creating a copy of the archive. [It is considered contradicting `libzip` goals by its authors.](https://github.com/nih-at/libzip/issues/304)**. We need a lib allowing to do that.

For the docs see [the tutorial](./tutorial.ipynb)[![NBViewer](https://nbviewer.org/static/ico/ipynb_icon_16x16.png)](https://nbviewer.org/urls/codeberg.org/KOLANICH-libs/libzip.py/raw/branch/master/tutorial.ipynb).
24 changes: 24 additions & 0 deletions UNLICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org/>
78 changes: 78 additions & 0 deletions libzip/Archive/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import typing
from pathlib import Path, PurePath

from .. import Source
from ..ctypes.opaque import zip_source_ptr
from ..enums import EncryptionMethod, OpenFlags, ZipFlags, ZipStat
from ..File import ADD_REPLACE_ARGS, COMMENT_FLAGS, ExistingFile, file_add
from ..Stat import stat
from .ctors import open # pylint:disable=redefined-builtin
from .middleLevel import close, get_archive_comment, get_num_entries, set_archive_comment, set_default_password, zip_ptr

__all__ = ("Archive",)


class Archive:
__slots__ = ("ptr", "path", "mode")

FILE_FLAGS = ZipFlags(0)

def __init__(self, path: Path, mode: OpenFlags) -> None:
self.path = path
self.mode = mode
self.ptr = None

def __enter__(self) -> "Archive":
self.ptr = open(str(self.path), self.mode)
return self

def __exit__(self, ex_type, ex_value, traceback) -> None:
self.close()

def __len__(self) -> int:
return get_num_entries(self.ptr, 0)

@property
def range(self) -> range:
return range(len(self))

def __getitem__(self, k: typing.Union[str, int]) -> ExistingFile:
if isinstance(k, int):
if k not in self.range:
raise KeyError("File with this index doesn't exist in the archive", k)

return ExistingFile(self, idx=k, path=None, flags=self.__class__.FILE_FLAGS, password=None)

if isinstance(k, (str, bytes, PurePath)):
return ExistingFile(self, idx=None, path=k, flags=self.__class__.FILE_FLAGS, password=None)

raise ValueError("Incorrect type of key", k)

def __iter__(self):
for idx in self.range:
yield ExistingFile(self, idx=idx, path=None, flags=self.__class__.FILE_FLAGS, password=None)

@property
def password(self):
raise NotImplementedError

@password.setter
def password(self, pwd):
set_default_password(self.ptr, pwd)

@property
def comment(self) -> typing.Optional[bytes]:
return get_archive_comment(self.ptr, COMMENT_FLAGS)

@comment.setter
def comment(self, v):
set_archive_comment(self.ptr, v)

def add(self, path: PurePath, source: typing.Union[zip_source_ptr, Source.Source], flags: ZipFlags = FILE_FLAGS, addFlags: ZipFlags = ADD_REPLACE_ARGS) -> ExistingFile:
idx = file_add(self.ptr, name=path, source=source, flags=addFlags)
return ExistingFile(self, idx=idx, path=path, flags=flags, password=None)

def close(self) -> None:
if self.ptr is not None:
close(self.ptr)
self.ptr = None
39 changes: 39 additions & 0 deletions libzip/Archive/ctors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import typing
from ctypes import byref, c_char_p, c_int

from ..ctypes import functions as f
from ..Source import OpenFlags, Source, ZipError, zip_ptr, zip_source_ptr
from ..utils import acceptPathOrStrOrBytes

__all__ = ("open", "open_from_source", "fdopen")


def open(src: str, flags: OpenFlags) -> zip_ptr: # pylint:disable=redefined-builtin
err = c_int(0)
res = f.open(c_char_p(acceptPathOrStrOrBytes(src)), c_int(flags), byref(err))
err = ZipError(err.value)
if err:
raise Exception(err)
return res


def open_from_source(src: typing.Union[zip_source_ptr, Source], flags: OpenFlags) -> zip_ptr:
if isinstance(src, Source):
src.recordLeak()
src = src.ptr

err = c_int(0)
res = f.open_from_source(src, c_int(flags), byref(err))
err = ZipError(err.value)
if err:
raise Exception(err)
return res


def fdopen(fd_orig: c_int, _flags: OpenFlags) -> zip_ptr:
err = c_int(0)
res = f.fdopen(fd_orig, c_int(_flags), byref(err))
err = ZipError(err.value)
if err:
raise Exception(err)
return res
70 changes: 70 additions & 0 deletions libzip/Archive/middleLevel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import typing
from ctypes import POINTER, byref, c_byte, c_char_p, c_int, c_int32, c_uint16, cast

from ..ctypes import functions as f
from ..ctypes._inttypes import zip_flags
from ..ctypes.opaque import zip_ptr
from ..enums import ZipFlags
from ..Error import _checkArchiveErrorCode
from ..utils import acceptStrOrBytes

__all__ = ("archive_set_tempdir", "close", "discard", "get_archive_comment", "set_archive_comment", "get_archive_flag", "set_archive_flag", "get_num_entries", "set_default_password", "unchange_all", "unchange_archive")


def archive_set_tempdir(za: zip_ptr, tempdir: c_char_p) -> None:
_checkArchiveErrorCode(za, f.archive_set_tempdir(za, tempdir))


def close(za: zip_ptr) -> None:
_checkArchiveErrorCode(za, f.close(za))


def discard(za: zip_ptr) -> None:
return f.discard(za)


def get_archive_comment(za: zip_ptr, flags: ZipFlags) -> typing.Optional[bytes]:
lenp = c_int32(0)
resPtr = f.get_archive_comment(za, byref(lenp), zip_flags(flags))

lenp = lenp.value

if lenp < 0:
raise ValueError("lenp returned is < 0", lenp)

if resPtr:
bufT = c_byte * lenp
bufPtrT = POINTER(bufT)
return bytes(cast(resPtr, bufPtrT)[0])

return None


def get_archive_flag(za: zip_ptr, flag: ZipFlags, flags: ZipFlags) -> bool:
return bool(_checkArchiveErrorCode(za, f.get_archive_flag(za, zip_flags(flag), zip_flags(flags))))


def get_num_entries(za: zip_ptr, flags: ZipFlags) -> int:
return int(f.get_num_entries(za, zip_flags(flags)))


def set_archive_comment(za: zip_ptr, comment: typing.AnyStr) -> None:
comment = acceptStrOrBytes(comment)

_checkArchiveErrorCode(za, f.set_archive_comment(za, c_char_p(comment), c_uint16(len(comment))))


def set_archive_flag(za: zip_ptr, flag: ZipFlags, value: c_int) -> None:
_checkArchiveErrorCode(za, f.set_archive_flag(za, zip_flags(flag), value))


def set_default_password(za: zip_ptr, passwd: typing.AnyStr) -> None:
_checkArchiveErrorCode(za, f.set_default_password(za, c_char_p(acceptStrOrBytes(passwd))))


def unchange_all(za: zip_ptr) -> None:
_checkArchiveErrorCode(za, f.unchange_all(za))


def unchange_archive(za: zip_ptr) -> None:
_checkArchiveErrorCode(za, f.unchange_archive(za))
Loading

0 comments on commit 7f929ad

Please sign in to comment.