Skip to content

Commit

Permalink
Add archiving option, fix exceptions, caused by multiline content in …
Browse files Browse the repository at this point in the history
…tag in spec files
  • Loading branch information
u committed Jan 8, 2025
1 parent 4eccd09 commit 743682c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 212 deletions.
40 changes: 34 additions & 6 deletions py2pack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013, Sascha Peilicke <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -40,6 +37,11 @@
get_metadata)

from email import parser
try:
import libarchive
except ModuleNotFoundError:
libarchive = None
import io


def replace_string(output_string, replaces):
Expand All @@ -51,6 +53,7 @@ def replace_string(output_string, replaces):

warnings.simplefilter('always', DeprecationWarning)


SPDX_LICENSES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'spdx_license_map.json')
with open(SPDX_LICENSES_FILE, 'r') as fp:
SPDX_LICENSES = json.load(fp)
Expand All @@ -69,7 +72,11 @@ def pypi_json(project, release=None):

def pypi_text_file(pkg_info_path):
with open(pkg_info_path, 'r') as pkg_info_file:
pkg_info_lines = parser.Parser().parse(pkg_info_file)
return pypi_text_stream(pkg_info_file)


def pypi_text_stream(pkg_info_stream):
pkg_info_lines = parser.Parser().parse(pkg_info_stream)
pkg_info_dict = {}
for key, value in pkg_info_lines.items():
key = key.lower().replace('-', '_')
Expand All @@ -86,14 +93,33 @@ def pypi_text_file(pkg_info_path):

def pypi_json_file(file_path):
with open(file_path, 'r') as json_file:
js = json.load(json_file)
return pypi_json_stream(json_file)


def pypi_json_stream(json_stream):
js = json.load(json_stream)
if 'info' not in js:
js = {'info': js}
if 'urls' not in js:
js['urls'] = []
return js


def pypi_archive_file(file_path):
if libarchive is None:
return None
try:
with libarchive.file_reader(file_path) as archive:
for entry in archive:
# Check if the entry's pathname matches the target filename
if entry.pathname == 'PKG-INFO':
return pypi_text_stream(io.StringIO(entry.read().decode()))
else:
return None
except Exception:
return None


def _get_template_dirs():
"""existing directories where to search for jinja2 templates. The order
is important. The first found template from the first found dir wins!"""
Expand Down Expand Up @@ -427,7 +453,9 @@ def fetch_local_data(args):
try:
data = pypi_json_file(localfile)
except json.decoder.JSONDecodeError:
data = pypi_text_file(localfile)
data = pypi_archive_file(localfile)
if data is None:
data = pypi_text_file(localfile)
args.fetched_data = data
args.version = args.fetched_data['info']['version']
return
Expand Down
8 changes: 4 additions & 4 deletions py2pack/templates/fedora.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Name: python-%{pypi_name}
Version: {{ version }}
Release: %autorelease
Summary: {{ summary }}
Summary: {{ summary|replace('\n','') }}

# Check if the automatically generated License and its spelling is correct for Fedora
# https://docs.fedoraproject.org/en-US/packaging-guidelines/LicensingGuidelines/
License: {{ license }}
URL: {{ home_page }}
Source: {{ source_url|replace(version, '%{version}') }}
License: {{ license|replace('\n','') }}
URL: {{ home_page|replace('\n','') }}
Source: {{ source_url|replace(version, '%{version}')|replace('\n','') }}

BuildRequires: pyproject-rpm-macros
BuildRequires: python-devel
Expand Down
8 changes: 4 additions & 4 deletions py2pack/templates/mageia.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
Name: python-%{mod_name}
Version: {{ version }}
Release: %mkrel 1
Url: {{ home_page }}
Summary: {{ summary }}
License: {{ license }}
Url: {{ home_page|replace('\n','') }}
Summary: {{ summary|replace('\n','') }}
License: {{ license|replace('\n','') }}
Group: Development/Python
Source: {{ source_url|replace(version, '%{version}') }}
Source: {{ source_url|replace(version, '%{version}')|replace('\n','') }}
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
BuildRequires: python-devel
{%- for req in requires %}
Expand Down
8 changes: 4 additions & 4 deletions py2pack/templates/opensuse-legacy.spec
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
Name: python-{{ name }}
Version: {{ version }}
Release: 0
Summary: {{ summary_no_ending_dot|default(summary, true) }}
License: {{ license }}
URL: {{ home_page }}
Source: {{ source_url|replace(version, '%{version}') }}
Summary: {{ summary_no_ending_dot|default(summary, true)|replace('\n','') }}
License: {{ license|replace('\n','') }}
URL: {{ home_page|replace('\n','') }}
Source: {{ source_url|replace(version, '%{version}')|replace('\n','') }}
BuildRequires: python-setuptools
{%- if install_requires and install_requires is not none %}
{%- for req in install_requires|sort %}
Expand Down
8 changes: 4 additions & 4 deletions py2pack/templates/opensuse.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
Name: python-{{ name }}
Version: {{ version }}
Release: 0
Summary: {{ summary_no_ending_dot|default(summary, true) }}
License: {{ license }}
URL: {{ home_page }}
Source: {{ source_url|replace(version, '%{version}') }}
Summary: {{ summary_no_ending_dot|default(summary, true)|replace('\n','') }}
License: {{ license|replace('\n','') }}
URL: {{ home_page|replace('\n','') }}
Source: {{ source_url|replace(version, '%{version}')|replace('\n','') }}
BuildRequires: python-rpm-macros
{%- set build_requires_plus_pip = ((build_requires if build_requires and build_requires is not none else []) +
['pip']) %}
Expand Down
2 changes: 1 addition & 1 deletion test/examples/poetry-opensuse-augmented.spec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# spec file for package python-poetry
#
# Copyright (c) 2024 SUSE LLC
# Copyright (c) __YEAR__ SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
Expand Down
74 changes: 0 additions & 74 deletions test/examples/py2pack-fedora-augmented.spec

This file was deleted.

75 changes: 0 additions & 75 deletions test/examples/py2pack-opensuse-augmented.spec

This file was deleted.

21 changes: 1 addition & 20 deletions test/examples/sampleproject-fedora-augmented.spec
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,7 @@ Summary: A sample Python project

# Check if the automatically generated License and its spelling is correct for Fedora
# https://docs.fedoraproject.org/en-US/packaging-guidelines/LicensingGuidelines/
License: Copyright (c) 2016 The Python Packaging Authority (PyPA)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
(FIXME:No SPDX)
License: Copyright (c) 2016 The Python Packaging Authority (PyPA)Permission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copiesof the Software, and to permit persons to whom the Software is furnished to doso, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE. (FIXME:No SPDX)
URL: https://github.com/pypa/sampleproject
Source: https://files.pythonhosted.org/packages/source/s/sampleproject/sampleproject-%{version}.tar.gz

Expand Down
21 changes: 1 addition & 20 deletions test/examples/sampleproject-opensuse-augmented.spec
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,7 @@ Name: python-sampleproject
Version: 3.0.0
Release: 0
Summary: A sample Python project
License: Copyright (c) 2016 The Python Packaging Authority (PyPA)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
(FIXME:No SPDX)
License: Copyright (c) 2016 The Python Packaging Authority (PyPA)Permission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copiesof the Software, and to permit persons to whom the Software is furnished to doso, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE. (FIXME:No SPDX)
URL: https://github.com/pypa/sampleproject
Source: https://files.pythonhosted.org/packages/source/s/sampleproject/sampleproject-%{version}.tar.gz
BuildRequires: python-rpm-macros
Expand Down

0 comments on commit 743682c

Please sign in to comment.