Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add goresym support #4

Merged
merged 17 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 6 additions & 60 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,62 +1,8 @@
A Simple Python Project Skeleton
Go-Inspector
================================
This repo attempts to standardize the structure of the Python-based project's
repositories using modern Python packaging and configuration techniques.
Using this `blog post`_ as inspiration, this repository serves as the base for
all new Python projects and is mergeable in existing repositories as well.

.. _blog post: https://blog.jaraco.com/a-project-skeleton-for-python-projects/


Usage
=====

A brand new project
-------------------
.. code-block:: bash

git init my-new-repo
cd my-new-repo
git pull [email protected]:nexB/skeleton

# Create the new repo on GitHub, then update your remote
git remote set-url origin [email protected]:nexB/your-new-repo.git

From here, you can make the appropriate changes to the files for your specific project.

Update an existing project
---------------------------
.. code-block:: bash

cd my-existing-project
git remote add skeleton [email protected]:nexB/skeleton
git fetch skeleton
git merge skeleton/main --allow-unrelated-histories

This is also the workflow to use when updating the skeleton files in any given repository.

More usage instructions can be found in ``docs/skeleton-usage.rst``.


Release Notes
=============

- 2023-07-18:
- Add macOS-13 job in azure-pipelines.yml

- 2022-03-04:
- Synchronize configure and configure.bat scripts for sanity
- Update CI operating system support with latest Azure OS images
- Streamline utility scripts in etc/scripts/ to create, fetch and manage third-party dependencies
There are now fewer scripts. See etc/scripts/README.rst for details

- 2021-09-03:
- ``configure`` now requires pinned dependencies via the use of ``requirements.txt`` and ``requirements-dev.txt``
- ``configure`` can now accept multiple options at once
- Add utility scripts from scancode-toolkit/etc/release/ for use in generating project files
- Rename virtual environment directory from ``tmp`` to ``venv``
- Update README.rst with instructions for generating ``requirements.txt`` and ``requirements-dev.txt``,
as well as collecting dependencies as wheels and generating ABOUT files for them.

- 2021-05-11:
- Adopt new configure scripts from ScanCode TK that allows correct configuration of which Python version is used.
- To enable the GoReSym plugin, first you need to install goresym from https://github.com/mandiant/GoReSym/releases/download/v2.6.4/GoReSym.zip
- Unzip the GoReSym.zip, extract goresym for linux and add it in src/go_inspector/bin.
- then change it to executable ```chmod u+x src/go_inspector/bin/GoReSym_lin```
- Install requirements and dependencies using ```make dev```
- Use ```scancode --json-pp - --go-symbol <PATH> --verbose``` to get debug symbols.
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ setup_requires = setuptools_scm[toml] >= 4
python_requires = >=3.7

install_requires =
plugincode
scancode-toolkit

[options.entry_points]

scancode_scan =
go_symbol = go_inspector.plugin:GoSymbolScannerPlugin

[options.packages.find]
where = src
Expand Down
2 changes: 0 additions & 2 deletions src/README.rst

This file was deleted.

Empty file added src/go_inspector/__init__.py
Empty file.
Binary file added src/go_inspector/bin/GoReSym_lin
Binary file not shown.
130 changes: 130 additions & 0 deletions src/go_inspector/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-plugins for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

import json
import logging
import os

import attr
from commoncode import command
from commoncode import fileutils
from commoncode.cliutils import SCAN_GROUP
from commoncode.cliutils import PluggableCommandLineOption
from commoncode.functional import flatten
from plugincode.scan import ScanPlugin
from plugincode.scan import scan_impl
from typecode import contenttype

"""
Extract symbols information from source code files with ctags.
"""
LOG = logging.getLogger(__name__)

from os.path import abspath
from os.path import dirname
from os.path import join

"""
https://github.com/mandiant/GoReSym/archive/refs/tags/v2.6.4.tar.gz
Download this zip at:
https://github.com/mandiant/GoReSym/releases/download/v2.6.4/GoReSym.zip
Extract goresym for linux
And then change it to executable
chmod u+x src/go_inspector/bin/GoReSym_lin
"""


def get_goresym_location():
curr_dir = dirname(abspath(__file__))
return join(curr_dir, "bin", "GoReSym_lin")


@scan_impl
class GoSymbolScannerPlugin(ScanPlugin):
"""
Scan a Go binary for symbols using GoReSym.
"""

resource_attributes = dict(
go_symbols=attr.ib(default=attr.Factory(dict), repr=False),
)

options = [
PluggableCommandLineOption(
("--go-symbol",),
is_flag=True,
default=False,
help="Collect Go symbols.",
help_group=SCAN_GROUP,
sort_order=100,
),
]

def is_enabled(self, go_symbol, **kwargs):
return go_symbol

def get_scanner(self, **kwargs):
return collect_and_parse_symbols


def is_executable_binary(location):
"""
Return True if the file at ``location`` is an executable binary.
"""

if not os.path.exists(location):
return False

if not os.path.isfile(location):
return False

type = contenttype.Type(location)

if not (type.is_elf or type.is_winexe):
return False

return True


def collect_and_parse_symbols(location, **kwargs):
"""
Run GoReSym and return a mapping of symbols of interest for the Go binary file
at ``location``.
"""
if not is_executable_binary(location):
print("Not an executable binary")
return

goresym_args = ["-p", location]
goresym_temp_dir = fileutils.get_temp_dir()
envt = {"TMPDIR": goresym_temp_dir}

try:
rc, stdo, err = command.execute(
cmd_loc=get_goresym_location(),
args=goresym_args,
env=envt,
to_files=True,
)

if rc != 0:
print("Error running goresym ", open(err).read())
raise Exception(open(err).read())

with open(stdo) as syms:
symbols = json.load(syms)
return dict(
go_symbols=dict(
build_info=symbols.get("BuildInfo") or [], file_paths=symbols.get("Files") or []
)
)

finally:
fileutils.delete(goresym_temp_dir)
Binary file added tests/data/GoReSym_lin
Binary file not shown.
Binary file added tests/data/arm_gentoo_elf
Binary file not shown.
Loading
Loading