You are not logged in.

#1 2018-10-03 10:55:01

drrossum
Member
From: Chicago
Registered: 2009-02-24
Posts: 77

[PKGBUILD] howto determine source directory created during extraction?

In a PKGBUILD's build() function, how do I robustly determine the directory under $srcdir to which the source got extracted?  This is relevant in cases where the directory structure inside the tarball, as released by upstream, is not constant.

In many PKGBUILDs the build() function starts with

cd "$srcdir/somedir"

Often, somedir is unpredictable, e.g. contains a hash. In such a case I use something like this:

prepare() {
  _dirname=$(tar -tf "${source[0]##*/}" | head -n 1 | cut -d/ -f1)
  mv $_dirname $pkgname-$pkgver
}

Any ideas how to handle this more elegantly?

Offline

#2 2018-10-03 11:51:22

Vitsliputsli
Member
Registered: 2018-10-02
Posts: 5

Re: [PKGBUILD] howto determine source directory created during extraction?

Unpack tar into empty directory. And if it has no another directories do this:
cd **/
If directory have more than one directory, command will return error.

Offline

#3 2018-10-03 13:08:08

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,543

Re: [PKGBUILD] howto determine source directory created during extraction?

drrossum wrote:

In a PKGBUILD's build() function, how do I robustly determine the directory under $srcdir to which the source got extracted?  This is relevant in cases where the directory structure inside the tarball, as released by upstream, is not constant.

This is a major problem and should be dealt with by discussing it with upstream. In the worst-case scenario, update this by hand every single time I guess,


Often, somedir is unpredictable, e.g. contains a hash. In such a case I use something like this:

prepare() {
  _dirname=$(tar -tf "${source[0]##*/}" | head -n 1 | cut -d/ -f1)
  mv $_dirname $pkgname-$pkgver
}

Any ideas how to handle this more elegantly?

What happens when a user runs makepkg again, without using -C?

Ordinarily that should result in the sources getting re-extracted on top of the existing sources, essentially amounting to `touch`ing all files and forcing a recompilation.

In this case, it would result in a copy of all files getting moved to "$srcdir"/$pkgname-$pkgver/$pkgname-$pkgver which is probably not what you wanted.

cd */ is just rank foolishness, so don't be pressured into doing this. To use the aforementioned makepkg without -C case again, you'd end up trying to cd into multiple directories at the same time, one for each previous version you didn't clean up. Of course, cd does not accept multiple directories to change into at once...

...

If these directories are unpredictable because of git hashes, what is this tarball that is named with a pkgver but contains a commit sha1 inside?


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#4 2018-10-03 13:46:37

drrossum
Member
From: Chicago
Registered: 2009-02-24
Posts: 77

Re: [PKGBUILD] howto determine source directory created during extraction?

Eschwartz wrote:

In this case, it would result in a copy of all files getting moved to "$srcdir"/$pkgname-$pkgver/$pkgname-$pkgver which is probably not what you wanted.

Good point.  The '--no-target-directory' option of 'mv' should address that issue, I think, overwriting any pre-existing directory with that name.

Eschwartz wrote:

If these directories are unpredictable because of git hashes, what is this tarball that is named with a pkgver but contains a commit sha1 inside?

The hgsubversion package on the AUR triggered my question now.  I had encountered this issue with other packages in the past (phc-intel, llpp; admittedly a long time ago).

Last edited by drrossum (2018-10-03 13:47:06)

Offline

#5 2018-10-03 13:55:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,451
Website

Re: [PKGBUILD] howto determine source directory created during extraction?

Rather than moving, could the prepare function create a symlink?  You could then use the symlink as the directory name in the other functions.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2018-10-03 14:14:52

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,543

Re: [PKGBUILD] howto determine source directory created during extraction?

Oof, yeah, bitbucket is just a horrible site: https://bitbucket.org/site/master/issue … ding-a-tag

I guess, since it is a mecurial repository, there's no chance of convincing the developer to move to a sane hosting site, since those invariably only provide git...

I'm thinking your workaround is near to the best you'll get. But I do have one suggestion for you, regarding a better way to parse the file.

Use bsdtar instead of tar, as bsdtar is just awesomer but also because makepkg internally depends on bsdtar whereas GNU tar is only incidentally guaranteed to exist due to being in the base group of an Arch Linux install.
Also, use awk instead of piping to both head and cut.

So,

bsdtar -tf - "${source[0]##*/}" | awk -F / '{print $1; exit}'

Also while I'm at it, $pkgver.tar.bz2 is not a unique filename, please use:

source=("$pkgname-$pkgver.tar.bz2::https://bitbucket.org/durin42/$pkgname/get/$pkgver.tar.bz2")

prepare() {
  _dirname=$(bsdtar -tf "${source[0]%%::*}" | awk -F / '{print $1; exit}')
  mv "$_dirname" $pkgname-$pkgver
}

EDIT: or Trilby's suggestion of a symlink works too.

Last edited by eschwartz (2018-10-03 14:15:37)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#7 2018-10-04 14:33:46

drrossum
Member
From: Chicago
Registered: 2009-02-24
Posts: 77

Re: [PKGBUILD] howto determine source directory created during extraction?

Thanks for your suggestions, Trilby and Eschwartz!  That was exactly the kind of feedback I was hoping for.

I see the argument that libarchive already is a dependency of pacman and mkinitcpio.  But I'm still curious what makes bsdtar so much awesomer and why gnu tar still lives in the core repo and in the base group.

Offline

Board footer

Powered by FluxBB