Comparing text strings

dn PythonList at DancesWithMice.info
Sun Apr 18 15:35:24 EDT 2021


On 14/04/2021 04.05, Mats Wichmann wrote:
> On 4/12/21 5:11 PM, Rich Shepard wrote:
>> I'm running Slackware64-14.2 and keep a list of installed packages.
>> When a
>> package is upgraded I want to remove the earlier version, and I've not
>> before written a script like this. Could there be a module or tool that
>> already exists to do this? If not, which string function would be best
>> suited to the task?
>>
>> Here's an example:
>> atftp-0.7.2-x86_64-2_SBo.tgz
>> atftp-0.7.4-x86_64-1_SBo.tgz

The 'trick' here is to understand how the distro handles versioning (and
multiple architectures, etc) and then to split the long name into
components before comparison, simplified example:

only relevant comparison if x86_64 == x86_64, then
atftp ? atftp == same, and thus
0.7.4 ? 0.7.4 => version update
(perhaps)


>> and there are others like this. I want the python3 script to remove the
>> first one. Tools like like 'find' or 'sort -u' won't work because
>> while the
>> file name is the same the version or build numbers differ.
> 
> Yes, you've identified why this is hard: package versioning takes many
> forms.  As suggested elsewhere, for Linux distribution packages, the
> only reliable approach is to lean on the distro's packaging
> infrastructure in some way, because those version strings (plus package
> metadata which may have "replaces" or "obsoletes" or some similar
> information) all have a defined meaning to *it* - it's the intended
> audience.
> 
> Don't know if Slack exposes this information in some way, it may be hard
> to make a reliable script if not. I know Debian actually does what
> you're looking for as a feature of the packaging system (apt-get
> autoclean), and the Fedora/RedHat universe does not, so I've also looked
> for what you're looking for :)


Not a sand-box I've played in. However, dnf is at least partly-written
in Python (it may still employs the older rpm, even yum, code).

Maybe the OP could learn from, or even piggy-back off, the existing code?
(which may be at https://github.com/rpm-software-management/dnf)
-- 
Regards,
=dn


More information about the Python-list mailing list