Comparing text strings

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Mon Apr 12 19:41:45 EDT 2021


On 2021-04-12 at 16:11:21 -0700,
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.
> 
> All suggestions welcome.

The trick to this is to define "one" in the phrase "first one."  What
makes a package name a first one, or a second one?  Is it the prefix
before the first hyphen?  Always?  Does Slackware define all the
possible bits, pieces, and permutations of package names, versions,
builds, etc.?

I don't know whether or how Slackware handles "compound" package names
(e.g., python-flask), but at some point, you're going to have to pull
apart (aka---gasp--parse), the package names to come up with the name
itself and the version (and the architecture, etc.), compare the name
parts to find the "duplicates," and then compare the versions (and maybe
the build number) to eliminate the lesser ones.

You'll also have to watch for the transitions from, say, 0.9 to 0.10, or
from 1.0rc2 to 1.0, which may not sort simply.

So to answer your question, a string function like split is a good
start.  IMO, if you think about the edge cases early, you'll have a
better chance at not being bitten by them.


More information about the Python-list mailing list