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

Packagedcode to handle Ruby(.gemspec) and Cocoapods(.podspec) #2075

Merged
merged 19 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def read(*names, **kwargs):
'xmltodict >= 0.11.0',
'javaproperties >= 0.5',
'toml >= 0.10.0',
'gemfileparser >= 0.7.0',
'pkginfo >= 1.5.0.1',
'dparse >= 0.4.1',

Expand Down
10 changes: 6 additions & 4 deletions src/packagedcode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from packagedcode import build
from packagedcode import chef
from packagedcode import models
from packagedcode import about
from packagedcode import bower
from packagedcode import conda
from packagedcode import build
from packagedcode import cargo
from packagedcode import chef
from packagedcode import conda
from packagedcode import cocoapods
from packagedcode import freebsd
from packagedcode import golang
from packagedcode import haxe
from packagedcode import maven
from packagedcode import models
from packagedcode import npm
from packagedcode import nuget
from packagedcode import opam
Expand Down Expand Up @@ -64,6 +65,7 @@
phpcomposer.PHPComposerPackage,
haxe.HaxePackage,
cargo.RustCargoCrate,
cocoapods.CocoapodsPackage,
opam.OpamPackage,
models.MeteorPackage,
bower.BowerPackage,
Expand Down
190 changes: 190 additions & 0 deletions src/packagedcode/cocoapods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# Data generated with ScanCode require an acknowledgment.
# ScanCode 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 ScanCode or any ScanCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# ScanCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import logging
import re

import attr
from packageurl import PackageURL

from commoncode.fileutils import py2
from commoncode import filetype
from commoncode import fileutils
from packagedcode import models
from packagedcode.spec import Spec


"""
Handle cocoapods packages manifests for macOS and iOS
including .podspec, Podfile and Podfile.lock files.
See https://cocoapods.org
"""

# TODO: override the license detection to detect declared_license correctly.


TRACE = False

logger = logging.getLogger(__name__)

if TRACE:
import sys
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)


@attr.s()
class CocoapodsPackage(models.Package):
metafiles = ('*.podspec',)
extensions = ('.podspec',)
default_type = 'pods'
default_primary_language = 'Objective-C'
default_web_baseurl = 'https://cocoapods.org'
default_download_baseurl = None
default_api_baseurl = None

@classmethod
def recognize(cls, location):
yield parse(location)

def repository_homepage_url(self, baseurl=default_web_baseurl):
return '{}/pods/{}'.format(baseurl, self.name)

def repository_download_url(self):
return '{}/archive/{}.zip'.format(self.homepage_url, self.version)


def is_podspec(location):
"""
Checks if the file is actually a podspec file
"""
return (filetype.is_file(location) and location.endswith('.podspec'))


def parse(location):
"""
Return a Package object from a .podspec file or None.
"""
if not is_podspec(location):
return

podspec_object = Spec()
podspec_data = podspec_object.parse_spec(location)
return build_package(podspec_data)


def build_package(podspec_data):
"""
Return a Package object from a package data mapping or None.
"""
name = podspec_data.get('name')
version = podspec_data.get('version')
declared_license = podspec_data.get('license')
summary = podspec_data.get('summary')
description = podspec_data.get('description')
homepage_url = podspec_data.get('homepage_url')
source = podspec_data.get('source')
authors = podspec_data.get('author') or []

author_names = []
author_email = []
if authors:
for split_author in authors:
split_author = split_author.strip()
author, email = parse_person(split_author)
author_names.append(author)
author_email.append(email)

parties = list(party_mapper(author_names, author_email))

package = CocoapodsPackage(
name=name,
version=version,
vcs_url=source,
source_packages=list(source.split('\n')),
description=description,
declared_license=declared_license,
homepage_url=homepage_url,
parties=parties
)

return package


def party_mapper(author, email):
"""
Yields a Party object with author and email.
"""
for person in author:
yield models.Party(
type=models.party_person,
name=person,
role='author')

for person in email:
yield models.Party(
type=models.party_person,
email=person,
role='email')


person_parser = re.compile(
r'^(?P<name>[\w\s(),-_.,]+)'
r'=>'
r'(?P<email>[\S+]+$)'
).match

person_parser_only_name = re.compile(
r'^(?P<name>[\w\s(),-_.,]+)'
).match


def parse_person(person):
"""
Return name and email from person string.

https://guides.cocoapods.org/syntax/podspec.html#authors
Author can be in the form:
s.author = 'Rohit Potter'
or
s.author = 'Rohit Potter=>rohit@gmail.com'
Author check:
>>> p = parse_person('Rohit Potter=>rohit@gmail.com')
>>> assert p == ('Rohit Potter', 'rohit@gmail.com')
>>> p = parse_person('Rohit Potter')
>>> assert p == ('Rohit Potter', None)
"""
parsed = person_parser(person)
if not parsed:
parsed = person_parser_only_name(person)
name = parsed.group('name')
email = None
else:
name = parsed.group('name')
email = parsed.group('email')

return name, email
Loading