pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/url2pkg url2pkg: allow url2pkg to be run from...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/615e1751ba5b
branches:  trunk
changeset: 373116:615e1751ba5b
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sun Feb 06 18:42:26 2022 +0000

description:
url2pkg: allow url2pkg to be run from a category directory as well

Previously, it was necessary to create the package directory, change
into it and then run url2pkg from there.  Since the name of the package
directory usually corresponds to DISTNAME without the version number,
all necessary data is readily available, so automate this.

Update version to 21.4.2.

diffstat:

 pkgtools/url2pkg/Makefile              |   4 +-
 pkgtools/url2pkg/files/url2pkg.py      |  36 +++++++++++++++++++++------------
 pkgtools/url2pkg/files/url2pkg_test.py |   6 ++--
 3 files changed, 28 insertions(+), 18 deletions(-)

diffs (129 lines):

diff -r 5313e50cacde -r 615e1751ba5b pkgtools/url2pkg/Makefile
--- a/pkgtools/url2pkg/Makefile Sun Feb 06 18:04:50 2022 +0000
+++ b/pkgtools/url2pkg/Makefile Sun Feb 06 18:42:26 2022 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: Makefile,v 1.123 2022/01/01 15:29:14 rillig Exp $
+# $NetBSD: Makefile,v 1.124 2022/02/06 18:42:26 rillig Exp $
 
-PKGNAME=       url2pkg-21.4.1
+PKGNAME=       url2pkg-21.4.2
 CATEGORIES=    pkgtools
 
 MAINTAINER=    rillig%NetBSD.org@localhost
diff -r 5313e50cacde -r 615e1751ba5b pkgtools/url2pkg/files/url2pkg.py
--- a/pkgtools/url2pkg/files/url2pkg.py Sun Feb 06 18:04:50 2022 +0000
+++ b/pkgtools/url2pkg/files/url2pkg.py Sun Feb 06 18:42:26 2022 +0000
@@ -1,5 +1,5 @@
 #! @PYTHONBIN@
-# $NetBSD: url2pkg.py,v 1.38 2022/02/06 18:04:50 rillig Exp $
+# $NetBSD: url2pkg.py,v 1.39 2022/02/06 18:42:26 rillig Exp $
 
 # Copyright (c) 2019 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -333,7 +333,7 @@
     distname: str
     pkgname: str
 
-    def __init__(self, url: str) -> None:
+    def __init__(self, url: str, pkgsrcdir: Path) -> None:
         self.url = url
         self.master_sites = ''
         self.distfile = ''
@@ -353,17 +353,17 @@
         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_from_sites_mk(pkgsrcdir)
         self.adjust_site_PyPI()
         self.adjust_site_other()
         self.adjust_everything_else()
 
-    def foreach_site_from_sites_mk(self, action: Callable[[str, str], None]):
+    def adjust_site_from_sites_mk(self, pkgsrcdir: Path):
         if self.master_sites != '':
             return
 
         varname = ''
-        with open('../../mk/fetch/sites.mk') as sites_mk:
+        with open(pkgsrcdir / 'mk/fetch/sites.mk') as sites_mk:
             for line in sites_mk:
                 m = re.search(r'^(MASTER_SITE_.*)\+=', line)
                 if m:
@@ -372,9 +372,9 @@
 
                 m = re.search(r'^\t(.*?)(?:\s+\\)?$', line)
                 if m:
-                    action(varname, m[1])
+                    self.adjust_site_from_site_var(varname, m[1])
 
-    def adjust_site_from_sites_mk(self, varname: str, site_url: str):
+    def adjust_site_from_site_var(self, varname: str, site_url: str):
 
         url_noproto = re.sub(r'^\w+://', '', self.url)
         site_url_noproto = re.sub(r'^\w+://', '', site_url)
@@ -545,8 +545,8 @@
     """ Generates the initial package Makefile. """
     vars: PackageVars
 
-    def __init__(self, url: str) -> None:
-        self.vars = PackageVars(url)
+    def __init__(self, url: str):
+        self.vars = PackageVars(url, Path('../..'))
 
     def generate_Makefile(self) -> Lines:
         vars = self.vars
@@ -1214,10 +1214,6 @@
 
 
 def main(argv: List[str], g: Globals):
-    if not os.path.isfile('../../mk/bsd.pkg.mk'):
-        sys.exit(f'{argv[0]}: must be run from a package directory '
-                 f'(.../pkgsrc/category/package)')
-
     try:
         opts, args = getopt.getopt(argv[1:], 'v', ['verbose'])
         for (opt, _) in opts:
@@ -1230,6 +1226,20 @@
     if not re.fullmatch(r'\w+://[!-~]+?/[!-~]+', url):
         sys.exit(f'url2pkg: invalid URL: {url}')
 
+    if os.path.isfile('../mk/bsd.pkg.mk'):
+        vars = PackageVars(url, Path('..'))
+        m = re.fullmatch(r'(.*?)-[0-9].*', vars.distname)
+        if not m:
+            sys.exit(f'url2pkg: cannot determine package directory from distname \'{vars.distname}\'')
+        if Path(m[1]).exists():
+            sys.exit(f'url2pkg: package directory \'{m[1]}\' already exists')
+        os.mkdir(m[1])
+        os.chdir(m[1])
+
+    if not os.path.isfile('../../mk/bsd.pkg.mk'):
+        sys.exit(f'{argv[0]}: must be run from a package or category directory '
+                 f'(.../pkgsrc/category[/package])')
+
     initial_lines = Generator(url).generate_package(g)
     Adjuster(g, url, initial_lines).adjust()
 
diff -r 5313e50cacde -r 615e1751ba5b pkgtools/url2pkg/files/url2pkg_test.py
--- a/pkgtools/url2pkg/files/url2pkg_test.py    Sun Feb 06 18:04:50 2022 +0000
+++ b/pkgtools/url2pkg/files/url2pkg_test.py    Sun Feb 06 18:42:26 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: url2pkg_test.py,v 1.37 2022/02/06 18:00:08 rillig Exp $
+# $NetBSD: url2pkg_test.py,v 1.38 2022/02/06 18:42:26 rillig Exp $
 
 import pytest
 from url2pkg import *
@@ -1565,10 +1565,10 @@
 
 def test_main__wrong_dir(tmp_path):
     os.chdir(tmp_path)
-    error = r'url2pkg: must be run from a package directory'
+    error = r'url2pkg: must be run from a package or category directory'
 
     with pytest.raises(SystemExit, match=error):
-        main(['url2pkg'], g)
+        main(['url2pkg', 'https://example.org/distfile-1.0.tar.gz'], g)
 
 
 def test_main__unknown_option():



Home | Main Index | Thread Index | Old Index