Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Feb 13, 2023
0 parents commit fcdfc4e
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 0 deletions.
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 }}
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
__pycache__
*.pyc
*.pyo
/*.egg-info
/build
/dist
/.eggs
*.sqlite3
/.mypy_cache
*.py,cover
/.coverage
/rspec.xml
/monkeytype.sqlite3
/*.srctrldb
/*.srctrlbm
/*.srctrlprj
70 changes: 70 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#image: pypy:latest
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest
stages:
- dependencies
- build
- trigger
- test

variables:
DOCKER_DRIVER: overlay2
SAST_ANALYZER_IMAGE_TAG: latest
SAST_DISABLE_DIND: "true"

include:
- template: SAST.gitlab-ci.yml
#- template: DAST.gitlab-ci.yml
#- template: License-Management.gitlab-ci.yml
#- template: Container-Scanning.gitlab-ci.yml
#- template: Dependency-Scanning.gitlab-ci.yml
- template: Code-Quality.gitlab-ci.yml


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

before_script:
- export PYTHON_MODULES_DIR=${PYTHONUSERBASE}/lib/python3.8
- export EXECUTABLE_DEPENDENCIES_DIR=${PYTHONUSERBASE}/bin
- export PATH="$PATH:$EXECUTABLE_DEPENDENCIES_DIR" # don't move into `variables` any of them, it is unordered
- apt-get update
- apt-get install -y python3-apt python3-psutil
- python3 ./fix_python_modules_paths.py
- pip install --user --upgrade --pre git+https://gitlab.com/File2Package.py/File2Package.py.git git+https://github.com/amoffat/sh.git git+https://github.com/KOLANICH/python-debparse.git@fixes
- mkdir ./wheels

script:
- python3 ./setup.py bdist_wheel
- mv ./dist/*.whl ./wheels/File2Package.backend.dpkg-0.CI_python-py3-none-any.whl
- pip3 install --upgrade --pre --user ./wheels/File2Package.backend.dpkg-0.CI_python-py3-none-any.whl
- coverage run --source=File2Package.backend.dpkg --branch -m pytest --junitxml=./rspec.xml ./tests/tests.py
- coverage report -m
- coverage xml

coverage: /^TOTAL\\s+.+?(\\d{1,3}%)$/

cache:
paths:
- $PYTHONUSERBASE

artifacts:
paths:
- wheels
reports:
junit: ./rspec.xml
cobertura: ./coverage.xml

update_prebuilder_dependencies_image:
only:
- master
stage: trigger
allow_failure: true
trigger:
project: KOLANICH-subgroups/docker-images/prebuilder_dependencies
strategy: depend
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!
47 changes: 47 additions & 0 deletions File2Package/backend/dpkg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
import typing
from pathlib import Path

import sh
from AnyVer import AnyVer
from fuckapt import *
from PackageRef import VersionedPackageRef


def init(interfaces):
class DPKGFile2PackagePopulator(interfaces.IFile2PackagePopulator):
__slots__ = ("infoFilesLoc", "defaultArch")

def __init__(self, infoFilesLoc: Path = infoFilesLoc, defaultArch: str = defaultArch):
self.infoFilesLoc = infoFilesLoc
self.defaultArch = defaultArch

def packagesMappingYielder(self, pkgs):
index = {pkg["Package"].text: AnyVer(pkg["Version"].text) for pkg in readStatusFile() if "Version" in pkg}

for p in pkgs:
nameArch = p.stem.split(":")
if len(nameArch) != 1:
name, arch = nameArch
else:
name = nameArch[0]
arch = self.defaultArch

if name in index:
ref = interfaces.VersionedPackageRef(name, arch, version=index[name])
else:
ref = interfaces.BasePackageRef(name, arch)

yield interfaces.FilesPackageMapping(ref, self.__class__.filesYielder(p))

def __call__(self):
pkgs = tuple(self.infoFilesLoc.glob("*.list"))
return len(pkgs), self.packagesMappingYielder(pkgs)

@staticmethod
def filesYielder(p: Path):
with p.open("rt", encoding="utf-8") as f:
for l in f:
yield Path(l.strip())

return DPKGFile2PackagePopulator
Empty file added File2Package/backend/py.typed
Empty file.
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
16 changes: 16 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
File2Package.backend.dpkg [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/)
=========================
[![GitLab Build Status](https://gitlab.com/File2Package.py/File2Package.backend.dpkg/badges/master/pipeline.svg)](https://gitlab.com/File2Package.py/File2Package.backend.dpkg/pipelines/master/latest)
![GitLab Coverage](https://gitlab.com/File2Package.py/File2Package.backend.dpkg/badges/master/coverage.svg)
[![Coveralls Coverage](https://img.shields.io/coveralls/KOLANICH/File2Package.backend.dpkg.svg)](https://coveralls.io/r/KOLANICH/File2Package.backend.dpkg)
[![Libraries.io Status](https://img.shields.io/librariesio/github/KOLANICH/File2Package.backend.dpkg.svg)](https://libraries.io/github/KOLANICH/File2Package.backend.dpkg)
[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://github.com/KOLANICH-tools/antiflash.py)

A backend to fetch the data from dpkg database.

See [`tests/tests.py`](./tests/tests.py) for the examples.

Requirements
------------
* [`datrie`](https://github.com/pytries/datrie) for a prefix tree.
* [`debparse` fork](https://github.com/KOLANICH/python-debparse)
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/>
50 changes: 50 additions & 0 deletions fix_python_modules_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sys
import os
from pathlib import Path
import sysconfig;
import re

"""This tool makes available packages installed via apt to python installed in the original Docker image in /usr/local"""

stdlibDir = Path(sysconfig.get_paths()['platstdlib'])

cextNameRx = re.compile("^(?P<name>.+)\\.(?P<impl>cpython)-(?P<major>\\d)(?P<minor>\\d)m?-(?P<arch>x86_64)-(?P<abi>linux-gnu)\\.so$")

def genNewName(name, impl, major, minor, arch, abi):
return name + "." + impl + "-" + str(major) + str(minor) + ("m" if (major, minor) < (3, 8) else "") + "-" + arch + "-" + abi + ".so"

def symlink(f, to):
print(f, "->", to)
os.symlink(f, to)

def genNewSoPath(parent, name):
m = cextNameRx.match(name)
if m:
d = m.groupdict()
d["major"] = sys.version_info[0]
d["minor"] = sys.version_info[1]
return parent / genNewName(**d)
else:
return parent / name

def fixSoPath(f):
to = genNewSoPath(f.parent, f.name)
if not to.exists():
symlink(f, to)

def fixPaths(fromAptPackagesInstalledPythonModulesDir):
for f in fromAptPackagesInstalledPythonModulesDir.glob("*"):
to = stdlibDir / f.name
if f.is_dir():
for l in f.glob("**/*.so"):
fixSoPath(l)
elif f.suffix == ".so":
to = genNewSoPath(stdlibDir, f.name)
if not to.exists():
symlink(f, to)



if __name__ == "__main__":
print("stdlibDir:", stdlibDir );
fixPaths(Path("/usr/lib/python3/dist-packages/"))
39 changes: 39 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[build-system]
requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"]
build-backend = "setuptools.build_meta"

[project]
name = "File2Package.backend.dpkg"
authors = [{name = "KOLANICH"}]
description = "A dpkg backend for File2Package."
readme = "ReadMe.md"
keywords = ["package", "location", "apt", "dpkg", "debian", "ubuntu", "antix", "kali"]
license = {text = "Unlicense"}
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: Public Domain",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
]
requires-python = ">=3.4"
dependencies = [
"File2Package @ git+https://github.com/File2Package/File2Package.py.git",
"debparse @ git+https://github.com/prebuilder/python-debparse.git@fixes",
]
dynamic = ["version"]

[project.urls]
Homepage = "https://github.com/File2Package/File2Package.backend.dpkg"

[project.entry-points]
file_2_package = {dpkg = "File2Package.backend.dpkg:init"}

[tool.setuptools]
zip-safe = true
py-modules = ["File2Package.backend.dpkg"]

[tool.setuptools_scm]
52 changes: 52 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
import sys
from pathlib import Path
from collections import OrderedDict
import unittest
from debparse.deb_control import parse as debParse

import psutil
import sh
dpkgS = sh.dpkg.bake("-S")
dpkgs = sh.dpkg.bake("-s")

thisFile = Path(__file__).absolute()
thisDir = thisFile.parent.absolute()
repoMainDir = thisDir.parent.absolute()
sys.path.append(str(repoMainDir))

dict = OrderedDict

from File2Package import File2Package
from File2Package.interfaces import BasePackageRef, VersionedPackageRef
from AnyVer import AnyVer
from File2Package.backend.dpkg import defaultArch

class TestDebPkgResolver(unittest.TestCase):
def testResolver(self):
with File2Package("dpkg") as d:
pr = psutil.Process()
for m in pr.memory_maps():
if m.path and m.path[0] == "/":
p = Path(m.path)
etalon = None
try:
res = dpkgS(p)
except:
continue
res = res.rsplit(':', 1)[0].strip().split(":")
if len(res) ==1:
res.append(defaultArch)
etalon = BasePackageRef(*res)
res2 = debParse(data=str(dpkgs(etalon.name)))
pkg = res2.packages[0]
if "Version" in pkg:
etalon = etalon.clone(cls=VersionedPackageRef, version=AnyVer(pkg["Version"].text))

with self.subTest(etalon=etalon, path=p):
ourP = d[p]
self.assertEqual(ourP, etalon)


if __name__ == "__main__":
unittest.main()

0 comments on commit fcdfc4e

Please sign in to comment.