Comparing text strings

Cameron Simpson cs at cskk.id.au
Mon Apr 12 19:53:24 EDT 2021


On 12Apr2021 16:11, Rich Shepard <rshepard at appl-ecosys.com> 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
>
>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.

I do not know if there are preexisting modules/tools for this, but I 
recommend looking at slackware's package management tool - they usually 
have some kind of 'clean" operation to purge "old" package install 
files. Sometimes that purges all the install files, not just the 
obsolete ones, so take care.

If you're writing a script, what you want to do is read the names and 
extract the version information from them, and also the "package name" - 
the bit which identifies the package and does not change with an 
upgrade.

I would then make a dict mapping package names to a list of versions 
and/or the full "...tgz" names above. (Actually, use a 
defaultdict(list), it will avoid a lot of tedious mucking about.)

Alternatively, and now that I think about it, more simply: _if_ the 
package files can be sorted by version, then all you need to do is read 
a sorted listing and note that latest fil for a particular package. If 
you need another one, it should be newer and you can remove the "known" 
package file, and update your record that to the new one.

Note that this depends on sorting by version. A lexical sort (eg 
"ls|sort") will look good intil a package version crosses a boundary 
like this:

    1.9.1
    1.10.0

A lexical sort will put those the other way around because "9" > "1".  
Wrongness will ensue.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list