#759 Don't require editing of Source0 in Python spec template
Closed: accepted 9 months ago Opened a year ago by mavit.

Guideline page needing clarification:

https://fedoraproject.org/wiki/Packaging:Python#Example_common_spec_file

Explanation

As previously noted in https://pagure.io/packaging-committee/issue/741, it is easy to overlook the lone "e" that needs replacing in this example:

Source0:        https://files.pythonhosted.org/packages/source/e/%{srcname}/%{srcname}-%{version}.tar.gz

The following form would make this line copy-and-pasteable without any editing being required:

Source0:        https://files.pythonhosted.org/packages/source/%(c=%{srcname}; echo ${c:0:1})/%{srcname}/%{srcname}-%{version}.tar.gz

I'd rather prep the macro for this.

Something like

Source0: %{pypi_source %{srcname} %{version} .tar.gz}

I just haven't got the time for it yet.

Indeed, that's a better solution.

I whipped this one up. It's currently in a format that you can paste into your specfile to test it. Let me know if this does or doesn't do what you're looking for.

%global srcname example
%global __pypi_url https://files.pythonhosted.org/packages/source/
%global __pypi_default_extension tar.gz

%define pypi_source() %{lua:
    local src = rpm.expand('%1')
    local ver = rpm.expand('%2')
    local ext = rpm.expand('%3')
    local url = rpm.expand('%__pypi_url')
\
    if src == '%1' or ver == '%2' then
        rpm.expand('%{error:pypi_source macro: Improper arguments.}')
    end
\
    if ext == '%3' then
        ext = rpm.expand('%__pypi_default_extension')
    end
\
    local first = string.sub(src, 1, 1)
\
    print(url .. first .. '/' .. src .. '-' .. ver .. '.' .. ext)
}

Edit: I took a look and found that "tar.gz" is overwhelmingly used (about 579 using that to 19 specs using something else, mostly "zip" or "tar.bz2", so I updated to a version that defaults to "tar.gz" if no third argument is supplied.

That should be:

print(url .. first .. '/' .. src .. '/' .. src .. '-' .. ver .. '.' .. ext)

Otherwise, looks good to me.

Yeah, I'm talking with @churchyard on IRC; more fixes incoming.

Updated version. This fixes the generated URL to include src twice, and provides a default for the source file name by using %srcname if defined, then %pypi_name, then just %name. Dumb greps show that this would get the majority of packages. A few (48) would want %name with a leading "python-" stripped, but 13 would want it in there, and trying to strip things seems a bit too clever.

So here's a more final version. Unless someone yells, I'll go ahead and push this into python-srpm-macros and epel-rpm-macros as well and then draft a bit of guidelines.

Edit, in case it isn't obvious, this allows the majority (about 360 out of maybe 580) of our packages to just have:

Source: %pypi_source

and not need to specify anything else.

%global __pypi_url https://files.pythonhosted.org/packages/source/
%global __pypi_default_extension tar.gz

%define pypi_source() %{lua:
    -- Expands to the pythonhosted URL for a package
    -- Accepts 0 to three arguments:
    -- 1:  The PYPI project name, defaulting to %srcname if it is defined, then
    --     %pypi_name if it is defined, then just %name.
    -- 2:  The PYPI version, defaulting to %version.
    -- 3:  The file extension, defaulting to "tar.gz".  (A period will be added
    --     automatically.)
    -- Requires %__pypi_url and %__pypi_default_extension to be defined.
\
    local src = rpm.expand('%1')
    local ver = rpm.expand('%2')
    local ext = rpm.expand('%3')
    local url = rpm.expand('%__pypi_url')
\
    -- If no first argument, try %srcname, then %pypi_name, then %name
    -- Note that rpm leaves macros unchanged if they are not defined.
    if src == '%1' then
        src = rpm.expand('%srcname')
    end
    if src == '%srcname' then
        src = rpm.expand('%pypi_name')
    end
    if src == '%pypi_name' then
        src = rpm.expand('%name')
    end
\
    -- If no second argument, use %version
    if ver == '%2' then
        ver = rpm.expand('%version')
    end
\
    -- If no third argument, use the preset default extension
    if ext == '%3' then
        ext = rpm.expand('%__pypi_default_extension')
    end
\
    local first = string.sub(src, 1, 1)
\
    print(url .. first .. '/' .. src .. '/' .. src .. '-' .. ver .. '.' .. ext)
}

Committed to python-rpm-macros (https://src.fedoraproject.org/rpms/python-rpm-macros/c/37a004eed88a71503a497bdfda6e830ffec2e353?branch=master) and built in rawhide (https://koji.fedoraproject.org/koji/taskinfo?taskID=27706275). This should be available for rawhide builds in max 30 minutes . Will let it soak a bit longer and then pull back to F28, F27 and will also push out to epel-rpm-macros.

Guidelines changes needed (once this is widely available):

Add a section "Source Files" to https://fedoraproject.org/wiki/Packaging:Python just above the Macros section:

Source Files from PyPI

When packaging software which is available from PyPI, you can make use of the %pypi_source macro. This macro accepts from zero to three arguments and evaluates to an appropriate URL for the source file on PyPI. The arguments are:

  1. The name of the PyPI project. Defaults to %srcname if defined, or to %pypi_name if defined, or to %name (the package name).
  2. The version of the PyPI project. Defaults to %version (the package version).
  3. The file extension to use. Defaults to tar.gz.

In most cases it is not necessary to specify any arguments.

Then add the %pypi_source macro to https://fedoraproject.org/wiki/Packaging:Python#Macros:

pypi_source   (Lua script)   Evaluates to the appropriate URL for the package.  See above for more information.

And change the Source line in the sample specfile to:

Source: %pypi_source

And I think that's it.

It occurs to me that we could have a matching %pypi_url macro, but... would that help? I see 599 packages with URLs on PyPI and just a few with URLs on pythonhosted. Seems like it could be a slight win, but then that obscures the URL even further (which means you can't click straight from the specfile, which always bugs me). So maybe not. Plus some PyPI URLs specify the version in the URL, and some don't.

  1. you say py_dist_name in the macros section, that's likely a copy paste error (edited, fixed)
  2. url: nope, keep the url clickable

This macro is now in all live branches (back to EL6). If anyone wants to give karma, the updates are:

Buildroot overrides are active for all of these, so they should work for koji builds now. According to the last specfile tarball, this is already being used in rawhide.

Also, I did fix that error in proposed guideline yesterday and so the proposal should be ready to go.

Metadata Update from @tibbs:
- Issue tagged with: hasdraft, meeting

10 months ago

This is obviously a +1 from me.

I guess the +1 from me should be obvious, but I'll state it for the record anyway.

From: https://meetbot-raw.fedoraproject.org/fedora-meeting-1/2018-07-19/fpc.2018-07-19-16.00.txt

  • 759 Don't require editing of Source0 in Python spec template
    (geppetto, 16:05:03)
  • ACTION: Don't require editing of Source0 in Python spec template
    (+1:7, 0:0, -1:0) (geppetto, 16:08:08)

Metadata Update from @tibbs:
- Issue untagged with: hasdraft, meeting
- Issue assigned to tibbs
- Issue tagged with: writeup

9 months ago

I wrote this one up during the meeting as well.

Announcement text:
The Python guidelines were modified to mention the new %pypi_source macro, which conveniently expands to the proper source URL for the package at PyPi.

Metadata Update from @tibbs:
- Issue untagged with: writeup
- Issue tagged with: announce

9 months ago

Metadata Update from @tibbs:
- Issue untagged with: announce
- Issue close_status updated to: accepted
- Issue status updated to: Closed (was: Open)

9 months ago

Login to comment on this ticket.

Metadata