Skip to content

Allow arbitrary Prefix attributes#4591

Merged
tgamblin merged 5 commits intospack:developfrom
adamjstewart:featues/prefix
Jun 25, 2017
Merged

Allow arbitrary Prefix attributes#4591
tgamblin merged 5 commits intospack:developfrom
adamjstewart:featues/prefix

Conversation

@adamjstewart
Copy link
Copy Markdown
Member

@adamjstewart adamjstewart commented Jun 23, 2017

Previously, the following prefix attributes worked great:

  • prefix.bin
  • prefix.lib

but as soon as you needed something new, you were S.O.L.:

  • prefix.examples
  • prefix.bin.perl

This led to several PRs adding new attributes to the Prefix class:

This is cumbersome, and more often than not leads to people using join_path(prefix, 'examples'). I've never been a big fan of join_path. It's just plain ugly...

With this PR, these limitations have been removed. Now, any prefix attribute you can think of works:

  • prefix.foo.bar.baz == $prefix/foo/bar/baz

All prefix attributes are defined on the fly, so they don't need to be declared beforehand. This means that most of the uses of join_path are now obsolete. I won't remove all uses of join_path in this PR, but we should encourage users to do this from now on.

@tgamblin
Copy link
Copy Markdown
Member

This is pretty awesome. I also thought a while ago of just adding a / operator to Prefix so you could write, e.g.:

prefix / 'bin' / 'man'

But you have to quote everything, so it's slightly uglier. And it has associativity issues if you want to do something to the result (e.g. you'd likely have to add parens).

What if the subdirectory contains - or other special characters? Should the advice be to just use os.path.join?

@adamjstewart
Copy link
Copy Markdown
Member Author

I didn't think about special characters. I guess the only way would be os.path.join.

Copy link
Copy Markdown
Member

@davydden davydden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice.

Copy link
Copy Markdown
Member

@alalazo alalazo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes the Prefix class much more flexible, and covers 99% of the cases.

:py:func:`join_path(prefix, *args) <spack.join_path>`
Like ``os.path.join``, this joins paths using the OS path separator.
However, this version allows an arbitrary number of arguments, so
you can string together many path components.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some confusion here. Whoever wrote join_path was under the assumption that os.path.join only accepted 2 arguments, when in reality it accepts as many as you want.

I tried replacing join_path with os.path.join and discovered that the only difference between the two is that join_path converts every arg to a string first. At some point, we should start phasing out join_path, especially in Spack's core libraries. I'm fine with it in package.py files, but we shouldn't rely on it elsewhere.

@tgamblin tgamblin merged commit e5ce7b1 into spack:develop Jun 25, 2017
@adamjstewart adamjstewart deleted the featues/prefix branch June 25, 2017 12:12
@adamjstewart adamjstewart mentioned this pull request Jun 27, 2017
EmreAtes pushed a commit to EmreAtes/spack that referenced this pull request Jul 10, 2017
* Allow arbitrary Prefix attributes
* Test attribute type as well

* Flake8 fixes

* Remove __new__ method

* Fewer uses of join_path in the docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants