Skip to content

Commit

Permalink
Merge pull request dsoprea#22 from dsoprea/brew_help
Browse files Browse the repository at this point in the history
Added Brew tool.
  • Loading branch information
dsoprea authored Feb 9, 2017
2 parents 57a6b72 + 53617f7 commit c28865f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
49 changes: 42 additions & 7 deletions libarchive/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,48 @@
import ctypes
import ctypes.util

_logger = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)

_FILEPATH = os.environ.get('LA_LIBRARY_FILEPATH', '')
if _FILEPATH == '':
_FILEPATH = ctypes.util.find_library('libarchive')
if _FILEPATH is None:
_FILEPATH = 'libarchive.so'
_LIBRARY_NAME = 'libarchive'
_LIBRARY_FILENAME = 'libarchive.so'

_logger.debug("Using library: [%s]", _FILEPATH)
def find_and_load_library():
search_filepaths = []

# Search for the library using our own environment variable.

filepath = os.environ.get('LA_LIBRARY_FILEPATH', '')
if filepath != '':
search_filepaths.append(filepath)

# Search for the library using the well-defined system library search-path.

_SEARCH_PATH = os.environ.get('LD_LIBRARY_PATH', '')
if _SEARCH_PATH != '':
for path in _SEARCH_PATH.split(":"):
filepath = os.path.join(path, _LIBRARY_FILENAME)
search_filepaths.append(filepath)

# Search for our library using whatever search-path ctypes uses (not the same
# as `LD_LIBRARY_PATH`).

filepath = ctypes.util.find_library(_LIBRARY_NAME)
if filepath is not None:
search_filepaths.append(filepath)

# Load the first one available.

found_filepath = None
for filepath in search_filepaths:
if os.path.exists(filepath) is True:
return filepath

# Fallback on the naively trying to load the filename.

_LOGGER.debug("Using default library file-path: [%s]", _LIBRARY_FILENAME)
return _LIBRARY_FILENAME

_FILEPATH = find_and_load_library()

_LOGGER.debug("Using library file-path: [%s]", _FILEPATH)
libarchive = ctypes.cdll.LoadLibrary(_FILEPATH)
35 changes: 35 additions & 0 deletions libarchive/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
import os
import logging
import logging.handlers

logger = logging.getLogger()

is_debug = bool(int(os.environ.get('DEBUG', '0')))

if is_debug is True:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)

format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(format)

ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)

# TODO(dustin): This all does not work for a Linux system. Just omit the "address" parameter completely.
if sys.platform == 'darwin':
address = '/var/run/syslog'
elif os.path.exists('/dev/log'):
address = '/dev/log'
else:
address = ('localhost', 514)

ch = logging.handlers.SysLogHandler(
address,
facility=logging.handlers.SysLogHandler.LOG_LOCAL0)

ch.setFormatter(formatter)
logger.addHandler(ch)
6 changes: 3 additions & 3 deletions libarchive/resources/README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
**To eliminate some problems with installation, the public API has been moved from the `libarchive` package to `libarchive.public`, and will be reflected in the next release. This is backwards-incompatible, but would otherwise prevent the install due to broken dependencies during install.**

------------
Introduction
------------
Expand Down Expand Up @@ -34,6 +32,8 @@ Notes
- Encryption is not currently supported since it's not supported in the underlying library (*libarchive*). Note `this inquiry <https://github.com/libarchive/libarchive/issues/579>`_ and the `wishlist item <https://github.com/libarchive/libarchive/wiki/WishList#encrypted-backup-support>`_.

- OS X has a system version of `libarchive` that is very old. As a result, many users have encountered issues importing an alternate one. Specifically, often they install a different one via Brew but this will not be [sym]linked into the system like other packages. This is a precaution taken by Brew to prevent undefined behavior in the parts of OS X that depend on the factory version. In order to work around this, you should set `LD_LIBRARY_PATH` (or prepend if `LD_LIBRARY_PATH` is already defined) with the path of the location of the library version you want to use. You'll want to set this from your user-profile script (unless your environment can not support this and you need to prepend something like "LD_LIBRARY_PATH=/some/path" to the front of the command-line or set it via `os.environ` above where you import this package). A `tool <tools/brew_find_libarchive>`_ has been provided that will print the path of the first version of `libarchive` installed via Brew. Just copy-and-paste it. Thanks to @SkyLeach for discussing the issue and treatments.


---------
Task List
Expand Down Expand Up @@ -160,6 +160,6 @@ To create an archive in memory from physical files::
Testing
-------

*libarchive* uses [nose](https://nose.readthedocs.org) for testing::
*libarchive* uses `nose <https://nose.readthedocs.org>`_ for testing::

tests$ ./run.py
6 changes: 0 additions & 6 deletions tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
import sys
sys.path.insert(0, '..')

import os
import os.path
import nose

mac_library_path = '/Users/dustin/build/libarchive/build/libarchive'
if os.path.exists(mac_library_path) is True:
os.environ['DYLD_LIBRARY_PATH'] = mac_library_path

nose.run()
28 changes: 28 additions & 0 deletions tools/brew_find_libarchive
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python2.7

import sys
import os
import json
import subprocess

command = ['brew', 'info', '--json=v1', 'libarchive']

try:
result = subprocess.check_output(command)
except Exception as e:
print("Could not lookup 'libarchive' package info: {0}".format(e.strerror))
sys.exit(1)

info = json.loads(result)
installed_versions = info[0]['installed']
if len(installed_versions) == 0:
print("libarchive is not currently installed via Brew")
sys.exit(2)

version = installed_versions[0]['version']

command = ['brew', '--cellar', 'libarchive']
package_path = subprocess.check_output(command)[:-1]

library_path = os.path.join(package_path, version, 'lib')
print(library_path)

0 comments on commit c28865f

Please sign in to comment.