-
Notifications
You must be signed in to change notification settings - Fork 238
Improve code style in raspijamulus.sh #2489
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
Improve code style in raspijamulus.sh #2489
Conversation
distributions/raspijamulus.sh
Outdated
| rm ${OPUS}.tar.gz | ||
| cd ${OPUS} | ||
| wget https://archive.mozilla.org/pub/opus/"${OPUS}".tar.gz | ||
| tar -xzf "${OPUS}".tar.gz |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style note... I'd prefer to see the quoting around the entire parameter, so here "${OPUS}.tar.gz" and later "-j${NCORES}" (alternatively, in this latter case, put a space between -j and its argument if that's valid).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this ("-j${NCORES}") is valid (I can't really test it either since my laptop is intel based). At least the script fails on my machine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If -j3 is valid and NCORES=3 then "-j${NCORES}" better be identical to -j3 or there's a bug in the shell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think many of these items are needed. I don't know this Codacy, but it seems to be applying some rules without any knowledge of whether they should be applicable in this particular context.
For variables that have been set literally, one can know they don't contain spaces that are part of a pathname, and extra quoting is either unnecessary or even incorrect. It is only when a variable has been set by user input or globbing for filenames that extra quoting and possibly array syntax are necessary.
distributions/raspijamulus.sh
Outdated
| # install required packages | ||
| pkgs='alsamixergui build-essential qtbase5-dev qttools5-dev-tools libasound2-dev cmake libglib2.0-dev' | ||
| if ! dpkg -s $pkgs >/dev/null 2>&1; then | ||
| if ! dpkg -s "${pkgs}" >/dev/null 2>&1; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work either. Same comment as on line 13.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we even ask?
distributions/raspijamulus.sh
Outdated
| wget https://archive.mozilla.org/pub/opus/"${OPUS}.tar.gz" | ||
| tar -xzf "${OPUS}.tar.gz" | ||
| rm "${OPUS}.tar.gz" | ||
| cd "${OPUS}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, no need to quote ${OPUS}, because we know what is in it, from line 4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the point of populating a variable that you don't have to know its value to use it? In which case you should make sure you're using it safely. (And again, if this is a constant, use readonly to declare it.)
distributions/raspijamulus.sh
Outdated
| fi | ||
| ./configure --enable-custom-modes --enable-fixed-point | ||
| make -j${NCORES} | ||
| make -j"${NCORES}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And ${NCORES} is just a number. Again, quoting is unnecessary and cumbersome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if Codacy understands readonly and then would work out that the expression is safe? Somehow I doubt it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using declare -ri NCORES=(whatever) might allow Codacy to realise it's an integer constant, too.
In general, I agree that the quoting is optional in many places and quotes may even make it a bit harder to read. At the same time, depending on the script size it becomes harder and harder to keep in mind which things are safe to use without quotes and which are not. I think shellcheck (#2474) suggests doing the same -- quote or write it in a way which makes it obvious that expansion is wanted (i.e. use arrays). Maybe Codacy even uses Shellcheck, I don't know. So, if we want some kind of static analysis, we either have to accept that everything has to be quoted or we have to sprinkle ignore comments into the scripts. https://tldp.org/LDP/abs/html/quotingvar.html
https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary
|
|
I think codacy uses spellcheck to detect quoting, so we'd be notified either way. |
8c11975 to
b88d9f3
Compare
|
Please see my new try. I've now been able to test it in a Debian docker container and it seems to run (except for jack obviously). Couldn't check on real Pi yet - but I'll try in a VM. |
softins
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment so far. I'll try it on my Pi in the next day or two before approving.
distributions/raspijamulus.sh
Outdated
| wget https://archive.mozilla.org/pub/opus/"${OPUS}.tar.gz" | ||
| tar -xzf "${OPUS}.tar.gz" | ||
| rm "${OPUS}.tar.gz" | ||
| cd "${OPUS}" || exit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If exiting due to an error, use a non-zero exit status, e.g. exit 1. Similarly below on line 66. Then the caller can tell the script failed, if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
de27a24 to
6c37b8e
Compare
hoffie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me (haven't tested on a Pi).
| # Please run this script from the distributions/ folder | ||
|
|
||
| readonly OPUS="opus-1.3.1" | ||
| NCORES=$(nproc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can say declare -ri NCORES=$(nproc) to declare NCORES both read-only and containing an integer value.
(Assuming a version of bash from this millenium.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that but spellcheck complained: https://www.shellcheck.net/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh dear... What did it complain about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declare -ri NCORES=$(nproc)
^-- [SC2155](https://github.com/koalaman/shellcheck/wiki/SC2155) (warning): Declare and assign separately to avoid masking return values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh dear, then it is pretty broken. You cannot declare something readonly and then assign to it... It's right about assignment masking the exit code:
~$ ( declare -ri NCORES=$(nproc) || { echo "foo"; exit 1; }; echo $NCORES )
6
~$ ( declare -ri NCORES=$(barbaz) || { echo "foo"; exit 1; }; echo $NCORES )
barbaz: command not found
0
Where the second go should have said "foo", not "0". So it should equally not like
readonly NCORES=$(nproc)
on that basis:
$ ( readonly NCORES=$(barbaz) || { echo "foo"; exit 1; }; echo $NCORES )
barbaz: command not found
$
(no "foo" echoed). You end up with
tempvarnamehere=$(ncores) || exit 1
declare -ri NCORES="$tempvarnamehere"
which should keep it happy. Otherwise you end up having "...${NCORES}..." everywhere. Which is probably preferable.
|
The Raspberry Pi VM is probably as slow as a real Raspberry Pi 1st edition, so I'm going to abort it soon. 32 Bit Intel worked (although I had the feeling that the script did "too much"). |
distributions/raspijamulus.sh
Outdated
| tar -xzf "${OPUS}.tar.gz" | ||
| rm "${OPUS}.tar.gz" | ||
| cd "${OPUS}" || exit 1 | ||
| if [ ${OPUS} == "opus-1.3.1" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"${OPUS}"
distributions/raspijamulus.sh
Outdated
| git checkout v1.9.20 | ||
| ./waf configure --alsa --prefix=/usr/local --libdir=$(pwd)/build | ||
| ./waf -j${NCORES} | ||
| ./waf configure --alsa --prefix=/usr/local --libdir="$(pwd)"/build |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"--libdir=$(pwd)/build" is nicer (or --libdir="$(pwd)/build" - but splitting the /build off doesn't look as nice.)
distributions/raspijamulus.sh
Outdated
| # start Jack2 and Jamulus in headless mode | ||
| export LD_LIBRARY_PATH="distributions/${OPUS}/.libs:distributions/jack2/build:distributions/jack2/build/common" | ||
| distributions/jack2/build/jackd -R -T --silent -P70 -p16 -t2000 -d alsa -dhw:${ADEVICE} -p 128 -n 3 -r 48000 -s & | ||
| distributions/jack2/build/jackd -R -T --silent -P70 -p16 -t2000 -d alsa -dhw:"${ADEVICE}" -p 128 -n 3 -r 48000 -s & |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I'd do "-dhw:${ADEVICE}" if it's a single argument.
6c37b8e to
f86d21e
Compare
f86d21e to
8795988
Compare
|
Messed a bit with the rebase while trying to move the commits around. It's now all squashed and I hope I didn't loose anything. |
The diff between your force push and the state before looks ok to me: Diff$ git diff f86d21..8795988
diff --git a/distributions/raspijamulus.sh b/distributions/raspijamulus.sh
index 5dabcfc3..71f777db 100755
--- a/distributions/raspijamulus.sh
+++ b/distributions/raspijamulus.sh
@@ -1,6 +1,8 @@
#!/bin/bash
# This script is intended to setup a clean Raspberry Pi system for running Jamulus
+# This needs to be run from the distributions/ folder
+
readonly OPUS="opus-1.3.1"
NCORES=$(nproc)
readonly NCORES
@@ -66,7 +68,7 @@ else
cd jack2 || exit 1
git checkout v1.9.20
./waf configure --alsa --prefix=/usr/local "--libdir=$(pwd)/build"
- ./waf -j${NCORES}
+ ./waf "-j${NCORES}"
mkdir build/jack
cp build/*.so build/jack
cp build/common/*.so build/jack |
softins
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it on my Pi4 with a fresh image of Raspbian 11, and it works ok.
I'm not sure anyone other than Volker actually uses the script. I see that after building (if necessary), it automatically starts a headless client, connecting to anygenre3.jamulus.io (which actually ends up on the directory server for Any Genre 1, because no custom port is given), with the username Raspi raspberryp (where the raspberryp part comes from the default hostname of the Pi, truncated).
|
Yes, I think as soon as we have ARM builds, this script should be (re)-moved |
Short description of changes
Codacy complained that quoting is not right in raspijamulus.sh. This PR tries to fix it. I did not test it on a Raspberry Pi, but on my machine it doesn't seem to run.
This shouldn't touch the refactoring by @hoffie
CHANGELOG: Refactor raspijamulus.sh file
Context: Fixes an issue?
Not known.
Does this change need documentation? What needs to be documented and how?
Status of this Pull Request
Waiting for test.
What is missing until this pull request can be merged?
Needs testing on a raspberry pi.
Checklist