From 379df044b324c06dd85b237fdd28ca201e8eceee Mon Sep 17 00:00:00 2001 From: Alexey Nikitin Date: Mon, 4 Nov 2019 00:34:40 +0300 Subject: [PATCH 1/2] add setup.py --- .gitignore | 3 +++ setup.py | 13 +++++++++++++ src/disk_reader/__init__.py | 9 +++++++++ fat.py => src/disk_reader/fat.py | 4 ++-- fat12.py => src/disk_reader/fat12.py | 15 +++++++++++---- fat16.py => src/disk_reader/fat16.py | 15 +++++++++++---- fat32.py => src/disk_reader/fat32.py | 14 ++++++++++---- reader.py => src/disk_reader/reader.py | 0 signatures.py => src/disk_reader/signatures.py | 18 +++++++++++++++++- utils.py => src/disk_reader/utils.py | 0 10 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 setup.py create mode 100644 src/disk_reader/__init__.py rename fat.py => src/disk_reader/fat.py (98%) rename fat12.py => src/disk_reader/fat12.py (92%) rename fat16.py => src/disk_reader/fat16.py (92%) rename fat32.py => src/disk_reader/fat32.py (92%) rename reader.py => src/disk_reader/reader.py (100%) rename signatures.py => src/disk_reader/signatures.py (92%) rename utils.py => src/disk_reader/utils.py (100%) diff --git a/.gitignore b/.gitignore index 5b4d8cb..1c9e4d8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ __pycache__/ *.py[cod] *$py.class +# IDE +.vscode + # C extensions *.so diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..5975c90 --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup, find_packages + + +setup( + name="disk_reader", + version="0.0.1", + description="Python low-level disk reader", + long_description=open("README.md").read(), + long_description_content_type="text/markdown", + packages=find_packages("src"), + package_dir={"": "src"}, + python_requires=">=3.6.*" +) \ No newline at end of file diff --git a/src/disk_reader/__init__.py b/src/disk_reader/__init__.py new file mode 100644 index 0000000..bf2fb54 --- /dev/null +++ b/src/disk_reader/__init__.py @@ -0,0 +1,9 @@ +from disk_reader.fat12 import FAT12Reader +from disk_reader.fat16 import FAT16Reader +from disk_reader.fat32 import FAT32Reader + +__all__ = [ + 'FAT12Reader', + 'FAT16Reader', + 'FAT32Reader' +] diff --git a/fat.py b/src/disk_reader/fat.py similarity index 98% rename from fat.py rename to src/disk_reader/fat.py index 6e59f5b..6c4435d 100644 --- a/fat.py +++ b/src/disk_reader/fat.py @@ -1,8 +1,8 @@ from functools import reduce from itertools import accumulate, takewhile, repeat -from reader import Reader -from utils import decode_sfn, decode_lfn, groupby +from disk_reader.reader import Reader +from disk_reader.utils import decode_sfn, decode_lfn, groupby class FATException(Exception): diff --git a/fat12.py b/src/disk_reader/fat12.py similarity index 92% rename from fat12.py rename to src/disk_reader/fat12.py index 3e9af65..0a94b60 100644 --- a/fat12.py +++ b/src/disk_reader/fat12.py @@ -1,8 +1,15 @@ from struct import unpack - -from fat import FATTable, FATEntryReader, FATReader, FATEntry, FATDir -from signatures import * -from utils import slice_len +from collections import namedtuple + +from disk_reader.fat import FATTable, FATEntryReader, FATReader, FATEntry, FATDir +from disk_reader.signatures import ( + FAT12_SIGN, + FAT12_ENTRY_PERMS, + FAT12_ENTRY, + FAT12_LFN, + FAT12_ENTRY_SIZE +) +from disk_reader.utils import slice_len FAT12_STRUCT = namedtuple("FAT12", (it[2] for it in FAT12_SIGN)) FAT12_ENTRY_STRUCT = namedtuple("FAT12Directory", (it[2] for it in FAT12_ENTRY)) diff --git a/fat16.py b/src/disk_reader/fat16.py similarity index 92% rename from fat16.py rename to src/disk_reader/fat16.py index b514c9b..5d54504 100644 --- a/fat16.py +++ b/src/disk_reader/fat16.py @@ -1,8 +1,15 @@ from struct import unpack - -from fat import FATTable, FATEntryReader, FATReader, FATEntry, FATDir -from signatures import * -from utils import slice_len +from collections import namedtuple +from disk_reader.fat import FATTable, FATEntryReader, FATReader, FATEntry, FATDir +from disk_reader.signatures import ( + FAT12_ENTRY_SIZE, + FAT16_SIGN, + FAT16_ENTRY_PERMS, + FAT16_ENTRY, + FAT16_LFN, + FAT16_ENTRY_SIZE +) +from disk_reader.utils import slice_len SECTOR_SIZE = 512 diff --git a/fat32.py b/src/disk_reader/fat32.py similarity index 92% rename from fat32.py rename to src/disk_reader/fat32.py index 7010701..1b28cb9 100644 --- a/fat32.py +++ b/src/disk_reader/fat32.py @@ -1,8 +1,14 @@ from struct import unpack - -from fat import FATTable, FATEntryReader, FATReader, FATEntry, FATDir -from signatures import * -from utils import slice_len +from collections import namedtuple +from disk_reader.fat import FATTable, FATEntryReader, FATReader, FATEntry, FATDir +from disk_reader.signatures import ( + FAT32_SIGN, + FAT32_ENTRY_PERMS, + FAT32_ENTRY, + FAT32_LFN, + FAT32_ENTRY_SIZE +) +from disk_reader.utils import slice_len SECTOR_SIZE = 512 diff --git a/reader.py b/src/disk_reader/reader.py similarity index 100% rename from reader.py rename to src/disk_reader/reader.py diff --git a/signatures.py b/src/disk_reader/signatures.py similarity index 92% rename from signatures.py rename to src/disk_reader/signatures.py index b348951..eef6893 100644 --- a/signatures.py +++ b/src/disk_reader/signatures.py @@ -1,5 +1,21 @@ from collections import namedtuple +__all__ = [ + 'FAT12_SIGN', + 'FAT12_ENTRY_PERMS', + 'FAT12_ENTRY', + 'FAT12_LFN', + 'FAT16_SIGN', + 'FAT16_ENTRY_PERMS', + 'FAT16_ENTRY', + 'FAT16_LFN', + 'FAT32_SIGN', + 'FAT32_ENTRY_PERMS', + 'FAT32_ENTRY', + 'FAT32_LFN' +] + + FAT12_SIGN = ( # ( offset, size, name, unpack string) (0x00, 3, 'JumpInstruction', '3s'), @@ -106,4 +122,4 @@ FAT32_ENTRY = FAT12_ENTRY FAT32_ENTRY_SIZE = FAT12_ENTRY_SIZE -FAT32_ENTRY_PERMS = FAT12_ENTRY_PERMS \ No newline at end of file +FAT32_ENTRY_PERMS = FAT12_ENTRY_PERMS diff --git a/utils.py b/src/disk_reader/utils.py similarity index 100% rename from utils.py rename to src/disk_reader/utils.py From f57ffb7999974e4ba390d2bcd359618de31748b1 Mon Sep 17 00:00:00 2001 From: Alexey Nikitin Date: Mon, 4 Nov 2019 00:41:12 +0300 Subject: [PATCH 2/2] update README --- README.md | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index dc1a65d..4ca2cff 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,20 @@ TL;DR: Python implementation for low-level disk (and disk images) reading, parse MBR, file systems, etc. + +# Installation +py-disk-reader is temporarily unavailable on PyPI. + +Use github link to install: +``` +pip install git+https://github.com/bashkirtsevich/py-disk-reader +``` # Examples ## FAT12 ```python +from disk_reader import FAT12Reader from hashlib import sha1 from reader import FileReader @@ -36,35 +45,37 @@ with open("images/floppy2.img", "rb") as f: ## FAT16 ```python - from hashlib import sha1 - from reader import FileReader +from disk_reader import FAT16Reader +from hashlib import sha1 +from reader import FileReader - with open("images/fat16.img", "rb") as f: - img = FAT16Reader(FileReader(f)) +with open("images/fat16.img", "rb") as f: + img = FAT16Reader(FileReader(f)) - files = list(img.root_dir) + files = list(img.root_dir) - for i, n in enumerate(files): - print(i, n.name) + for i, n in enumerate(files): + print(i, n.name) - bar = files[1] - baz = bar.read() - print(bar.name, sha1(baz).hexdigest(), baz) + bar = files[1] + baz = bar.read() + print(bar.name, sha1(baz).hexdigest(), baz) - print("----") + print("----") - bar = list(files[0]) - for i, n in enumerate(bar): - print(i, n.name) + bar = list(files[0]) + for i, n in enumerate(bar): + print(i, n.name) - baz = bar[7].read() - print(bar[7].name, sha1(baz).hexdigest(), baz) + baz = bar[7].read() + print(bar[7].name, sha1(baz).hexdigest(), baz) ``` ## FAT32 ```python +from disk_reader import FAT32Reader from hashlib import sha1 from reader import FileReader