Go to file
TheOnePath 939cf3c0da
Updated extractar
- removed `on_setattr` argument in attr.ib() call for `name` attribute
  in `class@ArchiveInfo`.
- update docstring for class@Archive.
- if `file=` provided with None, ValueError is raised instead of
  RuntimeError.
- changed attributes of class@Archive to be private.
- added setattr dunder method similar to class@ArchiveInfo, but all
  attributes are defined as constant. Any attempt to modify constants
  throws an AttributeError
- added property for attribute `__file` in class@Archive.
- refactored code to use os.path instead of pathlib. Pathlib was
  restrictive on not allowing byte-like strings, and is an inappropriate
  module for the task (os is just simpler).
- ensured that writing the byte contents from an archive to a new file
  occurs within a try-except statement, and raises an IOError if the
  write fails in some way due to IO.
- other minor amendments.
2023-06-30 22:51:36 +01:00
.gitignore Initial commit 2023-06-07 19:39:16 +01:00
extractar.py Updated extractar 2023-06-30 22:51:36 +01:00
LICENSE Initial commit 2023-06-07 19:39:16 +01:00
README.md Added README.md 2023-06-07 19:48:58 +01:00

ExtractAr

A basic Python implementation of the Unix ar command which can extract files from a Debian binary package archive.

ExtractAr is a rework of the unix_ar single Python module library by remram44 but no longer supported and maintained. It is still a single module library, but with half the content; ExtractAr, meaning "Extract Archive" only reads and extracts files archived, not creating archives.

As Python is cross-platform, making use of the standard library, it means archive files can be extracted on Windows or Linux systems.

Usage

The below code snippet is a basic example of how to use ExtractAr:

import extractar 

if __name__ == "__main__":
    ar = extractar.Archive("/path/to/deb/file.deb")
    ar.open() # load the archive into memory
    
    print("Extracting content from archive...")
    ar.extractall()
    
    print("Archive extracted.")

However, ExtractAr employs strict handling of content through many stages of the reading and extraction process. It's possible that an I/O error occurs when dealing with reading the archive file. When extracting files, it's possible that the archive content is corrupt or not in the Debian binary package format. The following is a more robust method of operating with ExtractAr:

import extractar 
import sys

if __name__ == "__main__":
    ar = extractar.Archive("/path/to/deb/file.deb")

    try:
        ar.open() # load the archive into memory
    except:
        print("There was an issue when reading the archive file:", sys.exc_info()[0])
    
    print("Extracting content from archive...")

    try:
        ar.extractall()
    except extractar.ArchiveMagicByteError:
        print("Looks like the archive isn't of Debian binary package format.")
    except extractar.ArchiveBufferReadError as err:
        print("The archive might be corrupt. The following was raised when extracting files:", err)
    except:
        print("An issue occurred during archive extraction:", sys.exc_info()[0])
    
    print("Archive extracted.")

Code may not be required specific handling in this manner, but is an example of dealing with exceptions that may arise during the runtime of the file.

Licence

The original source code was licence under Modified BSD licence, which has been acknowledged appropriately. For licence agreement on ExtractAr, see LICENCE.txt for licence agreement.