url2pkg: extract code for analyzing the distfile URL

In a few commits, this will allow url2pkg to be run from a category
directory, thereby omitting the need to create the package directory
manually.

No functional change.
This commit is contained in:
rillig 2022-02-06 18:00:08 +00:00
parent 55b85e08ff
commit 877fdb4a8e
2 changed files with 50 additions and 45 deletions

View file

@ -1,5 +1,5 @@
#! @PYTHONBIN@
# $NetBSD: url2pkg.py,v 1.36 2022/02/06 17:11:37 rillig Exp $
# $NetBSD: url2pkg.py,v 1.37 2022/02/06 18:00:08 rillig Exp $
# Copyright (c) 2019 The NetBSD Foundation, Inc.
# All rights reserved.
@ -315,8 +315,8 @@ class Lines:
return False
class Generator:
""" Generates the initial package Makefile. """
class PackageVars:
""" Determines the package variables from a distfile URL. """
url: str
master_sites: str
distfile: str
@ -350,6 +350,14 @@ class Generator:
self.distname = ''
self.pkgname = ''
self.adjust_site_SourceForge()
self.adjust_site_GitHub_archive()
self.adjust_site_GitHub_release()
self.foreach_site_from_sites_mk(self.adjust_site_from_sites_mk)
self.adjust_site_PyPI()
self.adjust_site_other()
self.adjust_everything_else()
def foreach_site_from_sites_mk(self, action: Callable[[str, str], None]):
if self.master_sites != '':
return
@ -433,7 +441,6 @@ class Generator:
self.homepage = f'https://pypi.org/project/{project}/'
self.distfile = filename
def adjust_site_GitHub_archive(self):
pattern = r'''(?x)
^
@ -533,26 +540,35 @@ class Generator:
os.getenv('PKGMAINTAINER') or os.getenv('REPLYTO') \
or 'INSERT_YOUR_MAIL_ADDRESS_HERE # or use pkgsrc-users@NetBSD.org'
def generate_lines(self) -> Lines:
class Generator:
""" Generates the initial package Makefile. """
vars: PackageVars
def __init__(self, url: str) -> None:
self.vars = PackageVars(url)
def generate_Makefile(self) -> Lines:
vars = self.vars
lines = Lines()
lines.add('# $''NetBSD$')
lines.add('')
lines.add_vars(
Var('GITHUB_PROJECT', '=', self.github_project),
Var('GITHUB_TAG', '=', self.github_tag),
Var('DISTNAME', '=', self.distname),
Var('PKGNAME', '=', self.pkgname),
Var('CATEGORIES', '=', self.categories),
Var('MASTER_SITES', '=', self.master_sites),
Var('GITHUB_RELEASE', '=', self.github_release),
Var('EXTRACT_SUFX', '=', self.extract_sufx),
Var('DIST_SUBDIR', '=', self.dist_subdir),
Var('GITHUB_PROJECT', '=', vars.github_project),
Var('GITHUB_TAG', '=', vars.github_tag),
Var('DISTNAME', '=', vars.distname),
Var('PKGNAME', '=', vars.pkgname),
Var('CATEGORIES', '=', vars.categories),
Var('MASTER_SITES', '=', vars.master_sites),
Var('GITHUB_RELEASE', '=', vars.github_release),
Var('EXTRACT_SUFX', '=', vars.extract_sufx),
Var('DIST_SUBDIR', '=', vars.dist_subdir),
)
lines.add_vars(
Var('MAINTAINER', '=', self.maintainer),
Var('HOMEPAGE', '=', self.homepage),
Var('MAINTAINER', '=', vars.maintainer),
Var('HOMEPAGE', '=', vars.homepage),
Var('COMMENT', '=', 'TODO: Short description of the package'),
Var('#LICENSE', '=', '# TODO: (see mk/license.mk)'),
)
@ -562,16 +578,6 @@ class Generator:
return lines
def generate_Makefile(self) -> Lines:
self.adjust_site_SourceForge()
self.adjust_site_GitHub_archive()
self.adjust_site_GitHub_release()
self.foreach_site_from_sites_mk(self.adjust_site_from_sites_mk)
self.adjust_site_PyPI()
self.adjust_site_other()
self.adjust_everything_else()
return self.generate_lines()
def generate_package(self, g: Globals) -> Lines:
pkgdir = g.pkgdir
makefile = pkgdir / 'Makefile'
@ -1082,7 +1088,7 @@ class Adjuster:
if lines.get('GITHUB_PROJECT') == '':
return
# don't risk to overwrite any changes made by the package developer.
# don't risk overwriting any changes made by the package developer.
if edited_lines.lines != initial_lines.lines:
lines.lines.insert(-2, '# TODO: Migrate MASTER_SITES '
'to MASTER_SITE_PYPI')

View file

@ -1,4 +1,4 @@
# $NetBSD: url2pkg_test.py,v 1.36 2022/01/01 15:29:14 rillig Exp $
# $NetBSD: url2pkg_test.py,v 1.37 2022/02/06 18:00:08 rillig Exp $
import pytest
from url2pkg import *
@ -384,7 +384,7 @@ def test_Lines_remove_if__multiple():
assert lines.lines == []
def test_Generator_adjust_site_SourceForge():
def test_PackageVars_adjust_site_SourceForge():
url = 'http://downloads.sourceforge.net/sourceforge/rfcascade/cascade-1.4.tar.gz'
lines = Generator(url).generate_Makefile()
@ -406,7 +406,7 @@ def test_Generator_adjust_site_SourceForge():
]
def test_Generator_adjust_site_GitHub_archive():
def test_PackageVars_adjust_site_GitHub_archive():
url = 'https://github.com/org/proj/archive/v1.0.0.tar.gz'
lines = Generator(url).generate_Makefile()
@ -431,7 +431,7 @@ def test_Generator_adjust_site_GitHub_archive():
]
def test_Generator_adjust_site_GitHub_archive__tag():
def test_PackageVars_adjust_site_GitHub_archive__tag():
url = 'https://github.com/org/proj/archive/refs/tags/1.0.0.tar.gz'
lines = Generator(url).generate_Makefile()
@ -456,7 +456,7 @@ def test_Generator_adjust_site_GitHub_archive__tag():
]
def test_Generator_adjust_site_GitHub_archive__tag_v():
def test_PackageVars_adjust_site_GitHub_archive__tag_v():
url = 'https://github.com/org/proj/archive/refs/tags/v1.0.0.tar.gz'
lines = Generator(url).generate_Makefile()
@ -478,7 +478,7 @@ def test_Generator_adjust_site_GitHub_archive__tag_v():
]
def test_Generator_adjust_site_GitHub_release__containing_project_name():
def test_PackageVars_adjust_site_GitHub_release__containing_project_name():
url = 'https://github.com/org/proj/releases/download/1.0.0/proj.zip'
lines = Generator(url).generate_Makefile()
@ -503,7 +503,7 @@ def test_Generator_adjust_site_GitHub_release__containing_project_name():
]
def test_Generator_adjust_site_GitHub_release__not_containing_project_name():
def test_PackageVars_adjust_site_GitHub_release__not_containing_project_name():
url = 'https://github.com/org/proj/releases/download/1.0.0/data.zip'
lines = Generator(url).generate_Makefile()
@ -529,7 +529,7 @@ def test_Generator_adjust_site_GitHub_release__not_containing_project_name():
]
def test_Generator_adjust_site_from_sites_mk__with_subdir():
def test_PackageVars_adjust_site_from_sites_mk__with_subdir():
url = 'https://files.pythonhosted.org/packages/source/i/irc/irc-11.1.1.zip'
generator = Generator(url)
@ -553,7 +553,7 @@ def test_Generator_adjust_site_from_sites_mk__with_subdir():
]
def test_Generator_adjust_site_from_sites_mk__without_subdir():
def test_PackageVars_adjust_site_from_sites_mk__without_subdir():
url = 'https://files.pythonhosted.org/packages/source/irc-11.1.1.zip'
generator = Generator(url)
@ -577,7 +577,7 @@ def test_Generator_adjust_site_from_sites_mk__without_subdir():
]
def test_Generator_adjust_site_from_sites_mk__GNU():
def test_PackageVars_adjust_site_from_sites_mk__GNU():
url = 'https://ftp.gnu.org/pub/gnu/cflow/cflow-1.6.tar.gz'
generator = Generator(url)
@ -600,7 +600,7 @@ def test_Generator_adjust_site_from_sites_mk__GNU():
]
def test_Generator_adjust_site_from_sites_mk__PyPI():
def test_PackageVars_adjust_site_from_sites_mk__PyPI():
url = ('https://files.pythonhosted.org/'
+ 'packages/da/8b/218264f5ce91df1ad27ce8021d51b747ef287627338fe05d170565358546/'
+ 'apprise-0.9.6.tar.gz')
@ -625,18 +625,17 @@ def test_Generator_adjust_site_from_sites_mk__PyPI():
]
def test_Generator_adjust_site_from_sites_mk__R(tmp_path: Path):
def test_PackageVars_adjust_site_from_sites_mk__R(tmp_path: Path):
g.pkgdir = tmp_path
url = 'http://cran.r-project.org/src/contrib/forecast_8.7.tar.gz'
generator = Generator(url)
with pytest.raises(SystemExit, match='^url2pkg: to create R packages, use pkgtools/R2pkg instead$'):
generator.generate_Makefile()
_ = Generator(url)
assert list(tmp_path.glob('*')) == []
def test_Generator_adjust_site_other__malformed_URL():
def test_PackageVars_adjust_site_other__malformed_URL():
# This error is supposed to be handled by the URL check in main.
error = "'NoneType' object has no attribute 'groups'"
@ -644,7 +643,7 @@ def test_Generator_adjust_site_other__malformed_URL():
Generator('localhost').generate_Makefile()
def test_Generator_adjust_everything_else__distname_version_with_v():
def test_PackageVars_adjust_everything_else__distname_version_with_v():
# Some version numbers have a leading 'v', derived from the Git tag name.
url = 'https://cpan.example.org/Algorithm-CheckDigits-v1.3.2.tar.gz'
@ -669,7 +668,7 @@ def test_Generator_adjust_everything_else__distname_version_with_v():
]
def test_Generator_adjust_everything_else__distfile_without_extension():
def test_PackageVars_adjust_everything_else__distfile_without_extension():
url = 'https://example.org/app-2019-10-05'
lines = Generator(url).generate_Makefile()
@ -692,7 +691,7 @@ def test_Generator_adjust_everything_else__distfile_without_extension():
]
def test_Generator_adjust_everything_else__v8():
def test_PackageVars_adjust_everything_else__v8():
generator = Generator('https://example.org/v8-1.0.zip')
lines = generator.generate_Makefile()