Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new command: spack pkg grep to search package files #34388

Merged
merged 1 commit into from Dec 9, 2022
Merged

Conversation

tgamblin
Copy link
Member

@tgamblin tgamblin commented Dec 8, 2022

It's very common for us to tell users to grep through the existing Spack packages to find examples of what they want, and it's also very common for package developers to do it. Now, searching packages is even easier.

spack pkg grep runs grep on all package.py files in repos known to Spack. It has no special options other than the search string; all options passed to it are forwarded along to grep.

> spack pkg grep --help
usage: spack pkg grep [--help] ...

positional arguments:
  grep_args  arguments for grep

options:
  --help     show this help message and exit

Examples:

> spack pkg grep CMakePackage | head -3
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/3dtk/package.py:class _3dtk(CMakePackage):
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/abseil-cpp/package.py:class AbseilCpp(CMakePackage):
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/accfft/package.py:class Accfft(CMakePackage, CudaPackage):
> spack pkg grep -Eho '(\S*)\(PythonPackage\)' | head -3
AwsParallelcluster(PythonPackage)
Awscli(PythonPackage)
Bueno(PythonPackage)

Return value

This retains the return value semantics of grep:

  • 0 for found,
  • 1 for not found
  • 1 for error

Choosing a grep

You can set the SPACK_GREP environment variable to choose the grep executable this command should use.

@spackbot-app spackbot-app bot added commands core PR affects Spack core functionality tests General test capability(ies) labels Dec 8, 2022
@tgamblin tgamblin assigned alalazo and adamjstewart and unassigned alalazo and adamjstewart Dec 8, 2022
@tgamblin tgamblin changed the title new command: spack pkg grep to search package files for strings new command: spack pkg grep to search package files Dec 8, 2022
@adamjstewart
Copy link
Member

Interesting! This is obviously something I do daily. I currently have a csv alias that Cd's to Spack's Var directory, then I run grep from there. This is slightly quicker.

This is up there with spack edit and spack unit-test in the sense that it's a tiny shortcut on top of a normal shell command. I don't think it's too ridiculous to add, doesn't have a ton of overhead to maintain, and non-power users can just ignore it. So it's fine by me.

@tgamblin tgamblin requested a review from trws December 8, 2022 02:26
@tgamblin tgamblin force-pushed the spack-pkg-grep branch 2 times, most recently from b9023d7 to 2134799 Compare December 8, 2022 07:30
Copy link
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.

Agree completely with @adamjstewart review

Copy link
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.

...but tests must pass first! 😆

@trws
Copy link
Contributor

trws commented Dec 8, 2022

Looks good if tests pass. Only thing I'd request, if we're doing this maybe allow the user to specify the name of their grep command? Mainly to deal with things like ggpep as default name for gnu grep on mac, wanting egrep or pt or whatever for different flags etc.

@adamjstewart
Copy link
Member

For @trws's suggestion, maybe we could rely on a SPACK_GREP env var like we do for spack edit?

@trws
Copy link
Contributor

trws commented Dec 8, 2022

I like that idea, it's similar to how FZF and others normally handle it so it would be convenient to add to existing environment setups that pick a grep for other tools.

@tgamblin
Copy link
Member Author

tgamblin commented Dec 8, 2022

@trws added

adamjstewart
adamjstewart previously approved these changes Dec 9, 2022
Copy link
Member

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

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

Works for me!

It's very common for us to tell users to grep through the existing Spack packages to
find examples of what they want, and it's also very common for package developers to do
it. Now, searching packages is even easier.

`spack pkg grep` runs grep on all `package.py` files in repos known to Spack. It has no
special options other than the search string; all options passed to it are forwarded
along to `grep`.

```console
> spack pkg grep --help
usage: spack pkg grep [--help] ...

positional arguments:
  grep_args  arguments for grep

options:
  --help     show this help message and exit
```

```console
> spack pkg grep CMakePackage | head -3
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/3dtk/package.py:class _3dtk(CMakePackage):
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/abseil-cpp/package.py:class AbseilCpp(CMakePackage):
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/accfft/package.py:class Accfft(CMakePackage, CudaPackage):
```

```console
> spack pkg grep -Eho '(\S*)\(PythonPackage\)' | head -3
AwsParallelcluster(PythonPackage)
Awscli(PythonPackage)
Bueno(PythonPackage)
```

This retains the return value semantics of `grep`:
* 0  for found,
* 1  for not found
* >1 for error

You can set the ``SPACK_GREP`` environment variable to choose the ``grep``
executable this command should use.
Copy link
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.

@trws @adamjstewart If you want to have a final look this LGTM

@tgamblin tgamblin merged commit d991ec9 into develop Dec 9, 2022
@tgamblin tgamblin deleted the spack-pkg-grep branch December 9, 2022 18:07
@tgamblin tgamblin added this to the v0.20.0 milestone Dec 9, 2022
amd-toolchain-support pushed a commit to amd-toolchain-support/spack that referenced this pull request Feb 16, 2023
It's very common for us to tell users to grep through the existing Spack packages to
find examples of what they want, and it's also very common for package developers to do
it. Now, searching packages is even easier.

`spack pkg grep` runs grep on all `package.py` files in repos known to Spack. It has no
special options other than the search string; all options passed to it are forwarded
along to `grep`.

```console
> spack pkg grep --help
usage: spack pkg grep [--help] ...

positional arguments:
  grep_args  arguments for grep

options:
  --help     show this help message and exit
```

```console
> spack pkg grep CMakePackage | head -3
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/3dtk/package.py:class _3dtk(CMakePackage):
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/abseil-cpp/package.py:class AbseilCpp(CMakePackage):
/Users/gamblin2/src/spack/var/spack/repos/builtin/packages/accfft/package.py:class Accfft(CMakePackage, CudaPackage):
```

```console
> spack pkg grep -Eho '(\S*)\(PythonPackage\)' | head -3
AwsParallelcluster(PythonPackage)
Awscli(PythonPackage)
Bueno(PythonPackage)
```

## Return Value

This retains the return value semantics of `grep`:
* 0  for found,
* 1  for not found
* >1 for error

## Choosing a `grep`

You can set the ``SPACK_GREP`` environment variable to choose the ``grep``
executable this command should use.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commands core PR affects Spack core functionality shell-support tests General test capability(ies)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants