Skip to content

Commit

Permalink
Create PackageManifest class for Haxe
Browse files Browse the repository at this point in the history
Creates PackageManifest classes for haxe manifests
and overrides the methods for detection and PackageManifest creation.

Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
  • Loading branch information
AyanSinhaMahapatra committed Nov 4, 2021
1 parent a0d0010 commit 2ab8558
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 342 deletions.
2 changes: 1 addition & 1 deletion src/packagedcode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
npm.NPMYarnLockJSON,
phpcomposer.PHPComposerJSON,
phpcomposer.PHPComposerLock,
haxe.HaxePackage,
haxe.HaxelibJSON,
cargo.RustCargoCrate,
cocoapods.CocoapodsPodspec,
cocoapods.CocoapodsPodfileLock,
Expand Down
144 changes: 71 additions & 73 deletions src/packagedcode/haxe.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,15 @@


@attr.s()
class HaxePackage(models.Package, models.PackageManifest):
file_patterns = ('haxelib.json',)
class HaxePackage(models.Package):

filetypes = tuple()
mimetypes = tuple()
default_type = 'haxe'
default_primary_language = 'Haxe'
default_web_baseurl = 'https://lib.haxe.org/p/'
default_download_baseurl = 'https://lib.haxe.org/p/'

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

@classmethod
def get_package_root(cls, manifest_resource, codebase):
return manifest_resource.parent(codebase)
Expand All @@ -72,6 +68,75 @@ def repository_download_url(self, baseurl=default_download_baseurl):
return haxelib_download_url(self.name, self.version, baseurl=baseurl)


@attr.s()
class HaxelibJSON(HaxePackage, models.PackageManifest):

file_patterns = ('haxelib.json',)
extensions = ('.json',)
manifest_type = 'haxlibjson'

@classmethod
def is_manifest(cls, location):
"""
Return True if the file at ``location`` is likely a manifest of this type.
"""
return (filetype.is_file(location)
and fileutils.file_name(location).lower() == 'haxelib.json')

@classmethod
def recognize(cls, location):
"""
Yield one or more Package manifest objects given a file ``location`` pointing to a
package archive, manifest or similar.
{
"name": "haxelib",
"url" : "https://lib.haxe.org/documentation/",
"license": "GPL",
"tags": ["haxelib", "core"],
"description": "The haxelib client",
"classPath": "src",
"version": "3.4.0",
"releasenote": " * Fix password input issue in Windows (#421).\n * ....",
"contributors": ["back2dos", "ncannasse", "jason", "Simn", "nadako", "andyli"]
}
"""
with io.open(location, encoding='utf-8') as loc:
package_data = json.load(loc)

package = HaxePackage(
name=package_data.get('name'),
version=package_data.get('version'),
homepage_url=package_data.get('url'),
declared_license=package_data.get('license'),
keywords=package_data.get('tags'),
description=package_data.get('description'),
)

package.download_url = package.repository_download_url()

for contrib in package_data.get('contributors', []):
party = models.Party(
type=models.party_person,
name=contrib,
role='contributor',
url='https://lib.haxe.org/u/{}'.format(contrib))
package.parties.append(party)

for dep_name, dep_version in package_data.get('dependencies', {}).items():
dep_version = dep_version and dep_version.strip()
is_resolved = bool(dep_version)
dep_purl = PackageURL(
type='haxe',
name=dep_name,
version=dep_version
).to_string()
dep = models.DependentPackage(purl=dep_purl, is_resolved=is_resolved,)
package.dependencies.append(dep)

yield package


def haxelib_homepage_url(name, baseurl='https://lib.haxe.org/p/'):
"""
Return an haxelib package homepage URL given a name and a base registry web
Expand All @@ -97,73 +162,6 @@ def haxelib_download_url(name, version, baseurl='https://lib.haxe.org/p'):
return '{baseurl}/{name}/{version}/download/'.format(**locals())


def is_haxelib_json(location):
return (filetype.is_file(location)
and fileutils.file_name(location).lower() == 'haxelib.json')


def parse(location):
"""
Return a Package object from a haxelib.json file or None.
"""
if not is_haxelib_json(location):
return

with io.open(location, encoding='utf-8') as loc:
package_data = json.load(loc)
return build_package(package_data)


def build_package(package_data):
"""
Return a Package object from a package_data mapping (from a
haxelib.json or similar) or None.
{
"name": "haxelib",
"url" : "https://lib.haxe.org/documentation/",
"license": "GPL",
"tags": ["haxelib", "core"],
"description": "The haxelib client",
"classPath": "src",
"version": "3.4.0",
"releasenote": " * Fix password input issue in Windows (#421).\n * ....",
"contributors": ["back2dos", "ncannasse", "jason", "Simn", "nadako", "andyli"]
}
"""
package = HaxePackage(
name=package_data.get('name'),
version=package_data.get('version'),
homepage_url=package_data.get('url'),
declared_license=package_data.get('license'),
keywords=package_data.get('tags'),
description=package_data.get('description'),
)

package.download_url = package.repository_download_url()

for contrib in package_data.get('contributors', []):
party = models.Party(
type=models.party_person,
name=contrib,
role='contributor',
url='https://lib.haxe.org/u/{}'.format(contrib))
package.parties.append(party)

for dep_name, dep_version in package_data.get('dependencies', {}).items():
dep_version = dep_version and dep_version.strip()
is_resolved = bool(dep_version)
dep_purl = PackageURL(
type='haxe',
name=dep_name,
version=dep_version
).to_string()
dep = models.DependentPackage(purl=dep_purl, is_resolved=is_resolved,)
package.dependencies.append(dep)

return package


def map_license(package):
"""
Update the license based on a mapping:
Expand Down
166 changes: 84 additions & 82 deletions tests/packagedcode/data/haxe/basic/haxelib.json.expected
Original file line number Diff line number Diff line change
@@ -1,82 +1,84 @@
{
"type": "haxe",
"namespace": null,
"name": "haxelib",
"version": "3.4.0",
"qualifiers": {},
"subpath": null,
"primary_language": "Haxe",
"description": "The haxelib client",
"release_date": null,
"parties": [
{
"type": "person",
"role": "contributor",
"name": "back2dos",
"email": null,
"url": "https://lib.haxe.org/u/back2dos"
},
{
"type": "person",
"role": "contributor",
"name": "ncannasse",
"email": null,
"url": "https://lib.haxe.org/u/ncannasse"
},
{
"type": "person",
"role": "contributor",
"name": "jason",
"email": null,
"url": "https://lib.haxe.org/u/jason"
},
{
"type": "person",
"role": "contributor",
"name": "Simn",
"email": null,
"url": "https://lib.haxe.org/u/Simn"
},
{
"type": "person",
"role": "contributor",
"name": "nadako",
"email": null,
"url": "https://lib.haxe.org/u/nadako"
},
{
"type": "person",
"role": "contributor",
"name": "andyli",
"email": null,
"url": "https://lib.haxe.org/u/andyli"
}
],
"keywords": [
"haxelib",
"core"
],
"homepage_url": "https://lib.haxe.org/documentation/",
"download_url": "https://lib.haxe.org/p/haxelib/3.4.0/download/",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": null,
"code_view_url": null,
"vcs_url": null,
"copyright": null,
"license_expression": "gpl-1.0-plus",
"declared_license": "GPL",
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"extra_data": {},
"purl": "pkg:haxe/haxelib@3.4.0",
"repository_homepage_url": "https://lib.haxe.org/p/haxelib",
"repository_download_url": "https://lib.haxe.org/p/haxelib/3.4.0/download/",
"api_data_url": null
}
[
{
"type": "haxe",
"namespace": null,
"name": "haxelib",
"version": "3.4.0",
"qualifiers": {},
"subpath": null,
"primary_language": "Haxe",
"description": "The haxelib client",
"release_date": null,
"parties": [
{
"type": "person",
"role": "contributor",
"name": "back2dos",
"email": null,
"url": "https://lib.haxe.org/u/back2dos"
},
{
"type": "person",
"role": "contributor",
"name": "ncannasse",
"email": null,
"url": "https://lib.haxe.org/u/ncannasse"
},
{
"type": "person",
"role": "contributor",
"name": "jason",
"email": null,
"url": "https://lib.haxe.org/u/jason"
},
{
"type": "person",
"role": "contributor",
"name": "Simn",
"email": null,
"url": "https://lib.haxe.org/u/Simn"
},
{
"type": "person",
"role": "contributor",
"name": "nadako",
"email": null,
"url": "https://lib.haxe.org/u/nadako"
},
{
"type": "person",
"role": "contributor",
"name": "andyli",
"email": null,
"url": "https://lib.haxe.org/u/andyli"
}
],
"keywords": [
"haxelib",
"core"
],
"homepage_url": "https://lib.haxe.org/documentation/",
"download_url": "https://lib.haxe.org/p/haxelib/3.4.0/download/",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": null,
"code_view_url": null,
"vcs_url": null,
"copyright": null,
"license_expression": "gpl-1.0-plus",
"declared_license": "GPL",
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"extra_data": {},
"purl": "pkg:haxe/haxelib@3.4.0",
"repository_homepage_url": "https://lib.haxe.org/p/haxelib",
"repository_download_url": "https://lib.haxe.org/p/haxelib/3.4.0/download/",
"api_data_url": null
}
]
Loading

0 comments on commit 2ab8558

Please sign in to comment.