Skip to content

Commit

Permalink
meta(py): Configure black as formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-auer committed Jan 27, 2020
1 parent e76276c commit bc5246b
Show file tree
Hide file tree
Showing 26 changed files with 914 additions and 780 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

// Python configuration
"python.pythonPath": ".venv/bin/python",
"python.linting.enabled": false,
"python.formatting.provider": "black",

// Created by CPP Extension
"files.associations": {
Expand Down
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ test-python: .venv/bin/python

# Style checking

style: style-rust
style: style-rust style-python
.PHONY: style

# TODO: Style rust

style-rust:
@rustup component add rustfmt --toolchain stable 2> /dev/null
cargo +stable fmt -- --check
cd cabi && cargo +stable fmt -- --check
.PHONY: style-rust

style-python: .venv/bin/python
.venv/bin/pip install -U black
.venv/bin/black --check py

# Linting

lint: lint-rust lint-python
Expand Down Expand Up @@ -84,12 +86,12 @@ format-rust:

format-python: .venv/bin/python
.venv/bin/pip install -U black
.venv/bin/black tests
.venv/bin/black py
.PHONY: format-python

# Dependencies

.venv/bin/python: Makefile
@rm -rf .venv
@which virtualenv || sudo easy_install virtualenv
virtualenv -p python2 .venv
virtualenv -p python3 .venv
84 changes: 37 additions & 47 deletions py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@
_version_re = re.compile(r'(?m)^version\s*=\s*"(.*?)"\s*$')


DEBUG_BUILD = os.environ.get('SYMBOLIC_DEBUG') == '1'
DEBUG_BUILD = os.environ.get("SYMBOLIC_DEBUG") == "1"

with open('README') as f:
with open("README") as f:
readme = f.read()


if os.path.isfile('../cabi/Cargo.toml'):
with open('../cabi/Cargo.toml') as f:
if os.path.isfile("../cabi/Cargo.toml"):
with open("../cabi/Cargo.toml") as f:
version = _version_re.search(f.read()).group(1)
else:
with open('version.txt') as f:
with open("version.txt") as f:
version = f.readline().strip()


def vendor_rust_deps():
subprocess.Popen(['scripts/git-archive-all', 'py/rustsrc.zip'],
cwd='..').wait()
subprocess.Popen(["scripts/git-archive-all", "py/rustsrc.zip"], cwd="..").wait()


def write_version():
with open('version.txt', 'w') as f:
f.write('%s\n' % version)
with open("version.txt", "w") as f:
f.write("%s\n" % version)


class CustomSDist(sdist):
Expand All @@ -45,69 +44,60 @@ def run(self):


def build_native(spec):
cmd = ['cargo', 'build']
cmd = ["cargo", "build"]
if not DEBUG_BUILD:
cmd.append('--release')
target = 'release'
cmd.append("--release")
target = "release"
else:
target = 'debug'
target = "debug"

# Step 0: find rust sources
if not os.path.isfile('../cabi/Cargo.toml'):
if not os.path.isfile("../cabi/Cargo.toml"):
scratchpad = tempfile.mkdtemp()

@atexit.register
def delete_scratchpad():
try:
shutil.rmtree(scratchpad)
except (IOError, OSError):
pass
zf = zipfile.ZipFile('rustsrc.zip')

zf = zipfile.ZipFile("rustsrc.zip")
zf.extractall(scratchpad)
rust_path = scratchpad + '/rustsrc/cabi'
rust_path = scratchpad + "/rustsrc/cabi"
else:
rust_path = '../cabi'
rust_path = "../cabi"
scratchpad = None

# Step 1: build the rust library
print('running `%s` (%s target)' % (' '.join(cmd), target))
build = spec.add_external_build(
cmd=cmd,
path=rust_path
)
print("running `%s` (%s target)" % (" ".join(cmd), target))
build = spec.add_external_build(cmd=cmd, path=rust_path)

rtld_flags = ['NOW']
if sys.platform == 'darwin':
rtld_flags.append('NODELETE')
rtld_flags = ["NOW"]
if sys.platform == "darwin":
rtld_flags.append("NODELETE")
spec.add_cffi_module(
module_path='symbolic._lowlevel',
dylib=lambda: build.find_dylib('symbolic', in_path='target/%s' % target),
header_filename=lambda: build.find_header('symbolic.h', in_path='include'),
rtld_flags=rtld_flags
module_path="symbolic._lowlevel",
dylib=lambda: build.find_dylib("symbolic", in_path="target/%s" % target),
header_filename=lambda: build.find_header("symbolic.h", in_path="include"),
rtld_flags=rtld_flags,
)


setup(
name='symbolic',
name="symbolic",
version=version,
packages=find_packages(),
author='Sentry',
license='MIT',
author_email='hello@sentry.io',
description='A python library for dealing with symbol files and more.',
author="Sentry",
license="MIT",
author_email="hello@sentry.io",
description="A python library for dealing with symbol files and more.",
long_description=readme,
include_package_data=True,
zip_safe=False,
platforms='any',
install_requires=[
'milksnake>=0.1.2',
],
setup_requires=[
'milksnake>=0.1.2',
],
milksnake_tasks=[
build_native,
],
cmdclass={
'sdist': CustomSDist,
}
platforms="any",
install_requires=["milksnake>=0.1.2",],
setup_requires=["milksnake>=0.1.2",],
milksnake_tasks=[build_native,],
cmdclass={"sdist": CustomSDist,},
)
11 changes: 6 additions & 5 deletions py/symbolic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

def _import_all():
import pkgutil

glob = globals()
for _, modname, _ in pkgutil.iter_modules(__path__):
if modname[:1] == '_':
if modname[:1] == "_":
continue
mod = __import__('symbolic.%s' % modname, glob, glob, ['__name__'])
if not hasattr(mod, '__all__'):
mod = __import__("symbolic.%s" % modname, glob, glob, ["__name__"])
if not hasattr(mod, "__all__"):
continue
__all__.extend(mod.__all__)
for name in mod.__all__:
obj = getattr(mod, name)
if hasattr(obj, '__module__'):
obj.__module__ = 'symbolic'
if hasattr(obj, "__module__"):
obj.__module__ = "symbolic"
glob[name] = obj


Expand Down
7 changes: 5 additions & 2 deletions py/symbolic/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
string_types = (str, unicode)
range_type = xrange
itervalues = lambda x: x.itervalues()
NUL = '\x00'
NUL = "\x00"

def implements_to_string(cls):
cls.__unicode__ = cls.__str__
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
cls.__str__ = lambda x: x.__unicode__().encode("utf-8")
return cls


else:
text_type = str
int_types = (int,)
Expand Down
17 changes: 9 additions & 8 deletions py/symbolic/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from symbolic import exceptions


__all__ = ['arch_is_known', 'arch_get_ip_reg_name', 'normalize_arch', 'parse_addr']
__all__ = ["arch_is_known", "arch_get_ip_reg_name", "normalize_arch", "parse_addr"]


ignore_arch_exc = (exceptions.UnknownArchError,)


# Make sure we init the lib and turn on rust backtraces
os.environ['RUST_BACKTRACE'] = '1'
ffi.init_once(lib.symbolic_init, 'init')
os.environ["RUST_BACKTRACE"] = "1"
ffi.init_once(lib.symbolic_init, "init")


def arch_is_known(arch):
Expand All @@ -29,7 +29,7 @@ def normalize_arch(arch):
if arch is None:
return None
if not isinstance(arch, string_types):
raise ValueError('Invalid architecture: expected string')
raise ValueError("Invalid architecture: expected string")

normalized = rustcall(lib.symbolic_normalize_arch, encode_str(arch))
return decode_str(normalized)
Expand All @@ -38,8 +38,9 @@ def normalize_arch(arch):
def arch_get_ip_reg_name(arch):
"""Returns the ip register if known for this arch."""
try:
return str(decode_str(rustcall(
lib.symbolic_arch_ip_reg_name, encode_str(arch))))
return str(
decode_str(rustcall(lib.symbolic_arch_ip_reg_name, encode_str(arch)))
)
except ignore_arch_exc:
pass

Expand All @@ -51,7 +52,7 @@ def parse_addr(x):
if isinstance(x, int_types):
return x
if isinstance(x, string_types):
if x[:2] == '0x':
if x[:2] == "0x":
return int(x[2:], 16)
return int(x)
raise ValueError('Unsupported address format %r' % (x,))
raise ValueError("Unsupported address format %r" % (x,))
62 changes: 29 additions & 33 deletions py/symbolic/debuginfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@


__all__ = [
'Archive',
'Object',
'ObjectLookup',
'id_from_breakpad',
'normalize_code_id',
'normalize_debug_id',
"Archive",
"Object",
"ObjectLookup",
"id_from_breakpad",
"normalize_code_id",
"normalize_debug_id",
]


Expand All @@ -26,9 +26,8 @@ class Archive(RustObject):
def open(self, path):
"""Opens an archive from a given path."""
if isinstance(path, text_type):
path = path.encode('utf-8')
return Archive._from_objptr(
rustcall(lib.symbolic_archive_open, path))
path = path.encode("utf-8")
return Archive._from_objptr(rustcall(lib.symbolic_archive_open, path))

@property
def object_count(self):
Expand All @@ -50,19 +49,19 @@ def get_object(self, debug_id=None, arch=None):
for obj in self.iter_objects():
if obj.debug_id == debug_id or obj.arch == arch:
return obj
raise LookupError('Object not found')
raise LookupError("Object not found")

def _get_object(self, idx):
"""Returns the object at a certain index."""
cache = getattr(self, '_objcache', None)
cache = getattr(self, "_objcache", None)
if cache is None:
cache = self._objcache = WeakValueDictionary()
rv = cache.get(idx)
if rv is not None:
return rv
ptr = self._methodcall(lib.symbolic_archive_get_object, idx)
if ptr == ffi.NULL:
raise LookupError('No object #%d' % idx)
raise LookupError("No object #%d" % idx)
rv = cache[idx] = Object._from_objptr(ptr)
return rv

Expand Down Expand Up @@ -116,48 +115,45 @@ def features(self):

def make_symcache(self):
"""Creates a symcache from the object."""
return SymCache._from_objptr(self._methodcall(
lib.symbolic_symcache_from_object))
return SymCache._from_objptr(
self._methodcall(lib.symbolic_symcache_from_object)
)

def make_cficache(self):
"""Creates a cficache from the object."""
return CfiCache._from_objptr(self._methodcall(
lib.symbolic_cficache_from_object))
return CfiCache._from_objptr(
self._methodcall(lib.symbolic_cficache_from_object)
)

def __repr__(self):
return '<Object %s %r>' % (
self.debug_id,
self.arch,
)
return "<Object %s %r>" % (self.debug_id, self.arch,)


class ObjectRef(object):
"""Holds a reference to an object in a format."""

def __init__(self, data):
self.addr = parse_addr(data.get('image_addr'))
self.addr = parse_addr(data.get("image_addr"))
# not a real address but why handle it differently
self.size = parse_addr(data.get('image_size'))
self.vmaddr = data.get('image_vmaddr')
self.code_id = data.get('code_id')
self.code_file = data.get('code_file') or data.get('name')
self.size = parse_addr(data.get("image_size"))
self.vmaddr = data.get("image_vmaddr")
self.code_id = data.get("code_id")
self.code_file = data.get("code_file") or data.get("name")
self.debug_id = normalize_debug_id(
data.get('debug_id') or data.get('id') or data.get('uuid') or None)
self.debug_file = data.get('debug_file')
data.get("debug_id") or data.get("id") or data.get("uuid") or None
)
self.debug_file = data.get("debug_file")

if data.get('arch') is not None and arch_is_known(data['arch']):
self.arch = data['arch']
if data.get("arch") is not None and arch_is_known(data["arch"]):
self.arch = data["arch"]
else:
self.arch = None

# Legacy alias for backwards compatibility
self.name = self.code_file

def __repr__(self):
return '<ObjectRef %s %r>' % (
self.debug_id,
self.arch,
)
return "<ObjectRef %s %r>" % (self.debug_id, self.arch,)


class ObjectLookup(object):
Expand Down
Loading

0 comments on commit bc5246b

Please sign in to comment.