Giter Site home page Giter Site logo

arpy's Introduction

Arpy

This library can be used to access ar files from python. It's tested to work with python 3.5+ and pypy3. (for earlier pythons see version <2) Travis status: Build Status

It supports both GNU and BSD formats and exposes the archived files using the standard python file interface.

Usage

Standard file usage:

With context managers:

with arpy.Archive('file.ar') as ar:
    print("files: %s" % ar.namelist())
    with ar.open('content.txt') as f:
        print(f.read())

Via headers for duplicate names:

with arpy.Archive('file.ar') as ar:
    for header in ar.infolist():
        print("file: %s" % header.name)
        with ar.open(header) as f:
            print(f.read())

Or directly:

ar = arpy.Archive('file.ar'))
ar.read_all_headers()

# check all available files
ar.archived_files.keys()

# get the contents of the archived file
ar.archived_files[b'some_file'].read()

Stream / pipe / ... usage:

ar = arpy.Archive('file.ar'))
for f in ar:
    print("got file name: %s" % f.header.name)
    print("with contents: %s" % f.read())

Contributions

All contributions welcome. Just make sure that:

  • tests are provided
  • all current platforms are passing (tox configuration is provided)
  • coverage is close to 100% (currently only missing statements are those depending on python version being used)

arpy's People

Contributors

alberthdev avatar frewsxcv avatar grosskur avatar helmutg avatar henningpeters avatar kolanich avatar rich-pixley avatar viraptor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

arpy's Issues

Creating archive files

Hi! Thanks a lot for this library!

Do you have plans to handle creation of ar files as well?

PEP8 and pyflakes compatible code

Hi,

Is there any intent to make the arpy package pep8 or pyflakes compatible?

The main goal for making it pep8 compatible is to facilitate contribution from external developers. With pep8 developers don't have to learn a new style guide for each project they contribute.

pyflakes is a fast static analyzer and can help with keeping the code clean.

Maybe we can also make the source code ascii only, by escaping the unicode characters.

I am willing to contribute a patch if you are happy with this kind of changes

I can also write a small command to automatically check for pep8 or pyflakes warnings.

Thanks!

License for arpy?

What license is arpy under? I would like to use it in some of my proprietaty code, it would be much appreciated if you could license arpy with a permissive license.

arpy raises an exception when handling newer .lib (msvc) archives

With a newer .lib archive (MSVC 2019), arpy is unable to parse this, giving this error:

Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arpy
>>> import pefile
>>> a = arpy.Archive(r"libssl_static.lib")
>>> a.namelist()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...\lib\site-packages\arpy.py", line 412, in namelist
    self.read_all_headers()
  File "...\lib\site-packages\arpy.py", line 397, in read_all_headers
    while self.read_next_header() is not None:
  File "...\lib\site-packages\arpy.py", line 372, in read_next_header
    header = self.__read_file_header(self.next_header_offset)
  File "...\lib\site-packages\arpy.py", line 299, in __read_file_header
    add_len = self.__fix_name(file_header)
  File "...\lib\site-packages\arpy.py", line 346, in __fix_name
    raise ArchiveFormatError("file references a name not present in the index")
arpy.ArchiveFormatError: file references a name not present in the index

I attached the .lib that's causing this issue:
libssl_static.zip

I attempted to address this with a PR (#17) that detects if NULL characters exist in HEADER_GNU_TABLE, and if so, uses that to split and figure out the file names instead of newline. Did some testing as well by dumping the object files out with arpy + binutils ar, and comparing the two - both for files that are oddly marked (e.g. specify /(number) as their filename) and for files that are properly named.

Oddly marked file example (shown with PEView, a very awesome tool used for debugging this issue):
image

Note that the hex does not match the resolved name it comes up with as it's using the HEADER_GNU_TABLE to resolve it.

If you would like to poke at it yourself with 010 Editor, here's a template that you can run on the .lib file. Output of that template:
image

Cannot process windows ar files

The htmlhelp.lib AR file included in http://download.microsoft.com/download/0/A/9/0A939EF6-E31C-430F-A3DF-DFAE7960D564/helpdocs.zip
(from https://msdn.microsoft.com/en-us/library/windows/desktop/ms669985%28v=vs.85%29.aspx )
fails to extract (note that it also contains symbol table most likely)

>>> import arpy
>>> ar = arpy.Archive('x/htmlhelp.lib')
>>> ar.read_all_headers()
Traceback (most recent call last):
  File "/tmp/tk/lib/python3.6/site-packages/arpy.py", line 106, in __init__
    self.uid = int(uid)
ValueError: invalid literal for int() with base 10: b'      '

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/tk/lib/python3.6/site-packages/arpy.py", line 342, in read_all_headers
    while self.read_next_header() is not None:
  File "/tmp/tk/lib/python3.6/site-packages/arpy.py", line 320, in read_next_header
    header = self.__read_file_header(self.next_header_offset)
  File "/tmp/tk/lib/python3.6/site-packages/arpy.py", line 243, in __read_file_header
    file_header = ArchiveFileHeader(header, offset)
  File "/tmp/tk/lib/python3.6/site-packages/arpy.py", line 112, in __init__
    "cannot convert file header fields to integers", err)
arpy.ArchiveFormatError: ('cannot convert file header fields to integers', ValueError("invalid literal for int() with base 10: b' 
  '",))

Support thin Archive

Hey. Just wanted to notify you that I wanted to use your parser but I cant because it does no support thin archives:

Archive creation:

/usr/bin/ar qc libext_lib_normal.a -T CMakeFiles/ext_lib_normal.dir/ext_lib.c.o
/usr/bin/ranlib libext_lib_normal.a

arpy:

ar = arpy.Archive('libext_lib_normal.a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/arpy.py", line 203, in __init__
    raise ArchiveFormatError("file is missing the global header")
arpy.ArchiveFormatError: file is missing the global header

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.