Skip to content

Commit

Permalink
Merge pull request #50 from nexB/46-add-notice-to-json-output-file
Browse files Browse the repository at this point in the history
Add notice etc. to JSON output, collect errors #6 #46
  • Loading branch information
steven-esser committed Feb 1, 2018
2 parents e7c5385 + 0ddd322 commit 900cec1
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 93 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
@@ -1,8 +1,9 @@
language: python
python:
- '2.7'
install: source configure
script: py.test -n 2
install:
- ./configure
script: bin/py.test -n 2
notifications:
slack:
on_success: always
Expand Down
50 changes: 49 additions & 1 deletion setup.py
Expand Up @@ -11,11 +11,59 @@
from os.path import join
from os.path import relpath
from os.path import splitext
import sys

from setuptools import find_packages
from setuptools import setup


version = '0.0.1.beta'


#### Small hack to force using a plain version number if the option
#### --plain-version is passed to setup.py

USE_DEFAULT_VERSION = False
try:
sys.argv.remove('--use-default-version')
USE_DEFAULT_VERSION = True
except ValueError:
pass
####


def get_version(default=version, template='{commit}',
use_default=USE_DEFAULT_VERSION):
"""
Return a version collected from git. If `use_default` is True,
always use the default version.
"""
if use_default:
return default

try:
# TODO: When we add git tags, we will need to adjust this code
commit = get_git_version()
commit = '{}-{}'.format(version, commit)

return template.format(**locals())
except:
# no git data: use default version
return default


def get_git_version():
"""
Return version parts from Git or raise an exception.
"""
from subprocess import check_output, STDOUT
cmd = 'git', 'describe', '--tags', '--long', '--dirty', '--always'
# TODO: When we add git tags, we will need to adjust this code
version = check_output(cmd, stderr=STDOUT).strip()

return version


def read(*names, **kwargs):
return io.open(
join(dirname(__file__), *names),
Expand All @@ -25,7 +73,7 @@ def read(*names, **kwargs):

setup(
name='deltacode',
version='0.0.1.beta',
version=get_version(),
license='Apache-2.0',
description='Delta-related utilities.',
long_description='Delta-related utilities.',
Expand Down
48 changes: 48 additions & 0 deletions src/deltacode/NOTICE
@@ -0,0 +1,48 @@
Software license
================

Copyright (c) 2017 nexB Inc. and others. All rights reserved.
http://nexb.com and https://github.com/nexB/deltacode/
The DeltaCode software is licensed under the Apache License version 2.0.
Data generated with DeltaCode require an acknowledgment.
DeltaCode is a trademark of nexB Inc.

You may not use this software except in compliance with the License.
You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

When you publish or redistribute any data created with DeltaCode or any DeltaCode
derivative work, you must accompany this data with the following acknowledgment:

Generated with DeltaCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied. No content created from
DeltaCode should be considered or used as legal advice. Consult an Attorney
for any legal advice.
DeltaCode is a free software codebase-comparison tool from nexB Inc. and others.
Visit https://github.com/nexB/deltacode/ for support and download.


Third-party software licenses
=============================

DeltaCode embeds third-party free and open source software packages under various
licenses including copyleft licenses. Some of the third-party software packages
are delivered as pre-built binaries. The origin and license of these packages is
documented by .ABOUT files.

The corresponding source code for pre-compiled third-party software is available
for immediate download from the same release page where you obtained DeltaCode at:
https://github.com/nexB/deltacode/
or https://github.com/nexB/deltacode-thirdparty-src/

You may also contact us to request the source code by email at info@nexb.com or
by postal mail at:

nexB Inc., DeltaCode open source code request
735 Industrial Road, Suite #101, 94070 San Carlos, CA, USA

Please indicate in your communication the DeltaCode version for which you are
requesting source code.
11 changes: 9 additions & 2 deletions src/deltacode/__init__.py
Expand Up @@ -31,7 +31,13 @@
from deltacode.models import Scan
from deltacode import utils

__version__ = '0.0.1.beta'

from pkg_resources import get_distribution, DistributionNotFound
try:
__version__ = get_distribution('deltacode').version
except DistributionNotFound:
# package is not installed ??
__version__ = '0.0.1.beta'


class DeltaCode(object):
Expand All @@ -40,9 +46,10 @@ class DeltaCode(object):
format) and the Delta objects created from a comparison of the files (in
the form of File objects) contained in those scans.
"""
def __init__(self, new_path, old_path):
def __init__(self, new_path, old_path, options):
self.new = Scan(new_path)
self.old = Scan(old_path)
self.options = options
self.deltas = OrderedDict([
('added', []),
('removed', []),
Expand Down
20 changes: 13 additions & 7 deletions src/deltacode/cli.py
Expand Up @@ -28,14 +28,13 @@
from collections import OrderedDict

import csv
import json

import click
import simplejson

from deltacode import DeltaCode
from deltacode import __version__
from deltacode.utils import deltas
from deltacode.utils import deltas, get_notice


# FIXME: update the function argument delta to deltacode
Expand Down Expand Up @@ -65,12 +64,14 @@ def write_csv(delta, result_file, all_delta_types=False):

def write_json(deltacode, outfile, all_delta_types=False):
"""
Using the DeltaCode object, create a .json file
containing the primary information from the Delta objects. Omit all Delta
objects whose 'category' is 'unmodified' unless the user selects the
'-a'/'--all' option.
Using the DeltaCode object, create a .json file containing the primary
information from the Delta objects. Omit all Delta objects whose
'category' is 'unmodified' unless the user selects the
'-a'/'--all-delta-types' option.
"""
results = OrderedDict([
('deltacode_notice', get_notice()),
('deltacode_options', deltacode.options),
('deltacode_version', __version__),
('deltacode_stats', deltacode.get_stats()),
('deltas', deltas(deltacode, all_delta_types)),
Expand All @@ -96,8 +97,13 @@ def cli(new, old, csv_file, json_file, all_delta_types):
.json file (-j or -json-file) at a user-designated location. If no file
option is selected, print the JSON results to the console.
"""
# retrieve the option selections
options = OrderedDict([
('--all-delta-types', all_delta_types)
])

# do the delta
deltacode = DeltaCode(new, old)
deltacode = DeltaCode(new, old, options)

# output to csv
if csv_file:
Expand Down
30 changes: 10 additions & 20 deletions src/deltacode/models.py
Expand Up @@ -84,13 +84,16 @@ def is_valid_scan(self, location):
scan = json.loads(open(location).read())

if not scan.get('scancode_version'):
raise ScancodeVersionAttributeException
msg = ('JSON file \'' + location + '\' is missing the \'scancode_version\' attribute.')
raise ScanException(msg)

if int(scan.get('scancode_version').split('.').pop(0)) < 2:
raise ScancodeOldVersionException
msg = ('JSON file \'' + location + '\' was created with an old version of ScanCode.')
raise ScanException(msg)

if not scan.get('scancode_options').get('--info'):
raise ScancodeInfoAttributeException
msg = ('JSON file \'' + location + '\' is missing the \'scancode_options/--info\' attribute.')
raise ScanException(msg)

return True

Expand Down Expand Up @@ -230,23 +233,10 @@ def __repr__(self):
return "%s" % self.__dict__


class ScancodeVersionAttributeException(Exception):
class ScanException(Exception):
"""
Named exception for JSON file containing no 'scancode_version' attribute.
"""
pass


class ScancodeOldVersionException(Exception):
"""
Named exception for JSON file containing old version of ScanCode.
"""
pass


class ScancodeInfoAttributeException(Exception):
"""
Named exception for JSON file containing no 'scancode_options'/'--info'
attribute.
Named exception for JSON file (1) containing no 'scancode_version'
attribute, (2) containing old version of ScanCode, or (3) containing no
'scancode_options'/'--info' attribute.
"""
pass
22 changes: 22 additions & 0 deletions src/deltacode/utils.py
Expand Up @@ -27,6 +27,8 @@

from collections import defaultdict

import os


from commoncode import paths

Expand Down Expand Up @@ -119,3 +121,23 @@ def check_moved(added_sha1, added_deltas, removed_sha1, removed_deltas):
return False
if added_deltas[0].new_file.name == removed_deltas[0].old_file.name:
return True


def get_notice():
"""
Retrieve the notice text from the NOTICE file for display in the JSON output.
"""
notice_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'NOTICE')
notice_text = open(notice_path).read()

delimiter = '\n\n\n'
[notice_text, extra_notice_text] = notice_text.split(delimiter, 1)
extra_notice_text = delimiter + extra_notice_text

delimiter = '\n\n '
[notice_text, acknowledgment_text] = notice_text.split(delimiter, 1)
acknowledgment_text = delimiter + acknowledgment_text

notice = acknowledgment_text.strip().replace(' ', '')

return notice

0 comments on commit 900cec1

Please sign in to comment.