-
Notifications
You must be signed in to change notification settings - Fork 112
Improve source code readability #443
Comments
Hi, thanks for your interest! You are right, there are several readability issues with the current code. It lacks comments, some parts need to be refactored or put in separate functions, and in the current state pacaur can only be maintained efficiently by myself. I would however not adhere to the 80 char per line rule, although I do agree that long lines are probably an indication that the code could be better decoupled in sub-functions. There are also others issues I'd like to solve in a neat manner:
I've done some initial cleaning in the Feel free to fork and improve upon the actual code, or ask any questions about the code. An external pair of eyes is always welcome. |
Thanks for you fast answer! First, I'm going to start reading your code to add some commentaries. I'd rather understand well the code before refactor any piece of it. I'll do small pull request if you don't mind in order to keep everything tidy and manageable. I'm messing around with Python these days in my job (matplotlib, numpy, pyqt5, ...) but I am not sure if it will bring any advantage to this application. I think it is really fast and reliable right now. Additionally, you could break script into some subscripts, then you could source them from the main script (pacaur executable). Edit: About 80 char lines it is only a suggestion, you can always use 120 char or another random number of characters. I think it is a good practise to limit lines, it could help you to notice when to create a new function or think in another solution. |
I do very much Python at work and I am also not sure it is worth the effort. pacaur is "just" a nice wrapper for pacman which creates a unified and clean CLI for pacman, makepkg, cower and friends. I'm afraid rewriting pacaur in Python gains us nothing. :/ |
Thanks to both of you for your feedback. Yes, python might not substantially add gains, but there would be several issues it could tremendously help with:
For the record, I initially hesitated between bash and python when starting pacaur a few years ago, and went with bash because you couldn't be reliable without a bash parser at the time. Since .SRCINFO have now been implemented, the AUR RPC allows to directly query dependencies instead of parsing PKGBUILD so python wouldn't be an issue anymore. But right, moving to python would probably not be worth the time. It would still be a preferred choice if someone would start a similar wrapper today. Lastly, keeping an eye on aurutils by @AladW is worth it. It's basically a modular helper that use a different approach (it uses a local repository instead of doing the build as you would do it manually, like pacaur does) with a bunch of nice ideas. Still experimental but overall much cleaner. |
The more I look at the current code, the more I want to ditch it completely. Reading the bash style links above also give a few reasons why bash isn't the proper language for a robust and readable code. Data structure would be very welcome here, as pacaur currently relies on a different arrays to mimic it. Debugging would also be easier, and long term maintenance by someone else than myself would be less of an issue. If the code should be reworked to make it easier to read and maintain, why not doing it in python? Looking at the existing python code, I see that there are 3 different AUR rpc interface library available:
However, before any possible rewrite the question of the real need of a new pacaur version arises. I might as well ditch it for one of the competitors on the longer term - in some way the cost of maintaining a popular helper is really not worth the hassle. Among the existing helpers, only 3 or 4 are actually reliable:
In a nutshell, I'm still on the fence here. At the moment I'll still maintain and fix bugs in pacaur, but I'm not sure I really want to continue to work on it in the current state - especially if a better, cleaner design emerges in the near future (aurutils? a new pkgbuilder version?). |
It really depends on how you use the language - bash isn't very good with arrays, so focusing on that isn't good for effectiveness. Bash is however good at the stdin/stdout idea: piping programs and functions together, and using the filesystem to process data.
I agree, though my gripe with it is that it has no interactive inspection of packages.
Sometimes I get a bit too excited, but I've cut out experimental code and split up/refactored things further. Thanks for the reminder on keeping things simple. ;) |
Yes, you are right. Pacaur has never been designed with the pipe idea in mind. This is another reason why aurutils is interesting. Also, keeping things simple on the long term is always difficult... so keep up the good work here! The lack of interactive inspection of pkgbuilder is a design decision from the maintainer. I don't understand the rational behind that missing feature, but pkgbuilder is still one of the most interesting design out there. I guess I prefer it to the more "brute force" design of bauerbill that writes bash scripts each and every time. |
Since a few days ago I am wondering if it would make sense to redesign pacaur as some wrapper to aur-utils? That would create the script more modular and readable and keep the well known CLI of pacman/pacaur. |
Yes, this might be a possibility. The drawbacks I see are that
|
I hope the maturity is somewhat accelerated by the absurd test-cases people have since thrown at aurutils, including packages which violate Regarding the fast workflow, pacaur precalculates provides and presents the various choices at the beginning of the build. I don't know how difficult this would be to implement with a modular helper. Or you could keep using pacaur in parallel by setting the PKGDEST and PACMAN variables. On the flip-side, you get all build files in one window (using PAGER, or vifm when installed), rather than prompting for each PKGBUILD/.install file as with pacaur. Either way, if someone wanted to make an aurutils wrapper, perhaps it would be best to create a separate project for it and discuss it there. |
What about splitting the script into subscripts in order to improve readability? Nothing complicated, just divide the script into several files and source them from the main script. For example:
|
Indeed, part of the file could be split in @AladW To be honest, I consider the precalculation of provides and conflicts the ugliest part of pacaur. It's slow, terribly inefficient, hard to maintain. It's the part that required the most work to mature too. I wish I could reuse some pacman code easily instead of duplicating my own soup here. |
I think that dividing |
I think Here a nice code fragment of get_pkgbuild_attribute() {
# $1: package name
# $2: attribute name
# $3: multivalued
# $4: name of output var
local pkgname=$1 attrname=$2 isarray=$3 outputvar=$4
printf -v "$outputvar" %s ''
if [[ $pkgname ]]; then
extract_global_variable "$attrname" "$isarray" "$outputvar"
extract_function_variable "package_$pkgname" "$attrname" "$isarray" "$outputvar"
else
extract_global_variable "$attrname" "$isarray" "$outputvar"
fi
}
##
# usage : get_full_version()
# return : full version spec, including epoch (if necessary), pkgver, pkgrel
##
get_full_version() {
if (( epoch > 0 )); then
printf "%s\n" "$epoch:$pkgver-$pkgrel"
else
printf "%s\n" "$pkgver-$pkgrel"
fi
} Check commenting here. I think that specifying input parameters and the output of each function is a really nice idea. |
Definitely. And you're right, better start to split it the code right away. Looking at makepkg code, I'd suggest to start the following way:
This would occur in a new branch with the sole purpose of cleaning/splitting the code, while the current master branch is used as usual for maintenance. Adjusting code style to makepkg style could be a good idea too. |
Note: I have no time for this. If someone thinks he can kickstart this rewrite/code cleaning in a branch or fork, please do. |
Just to note it, I had some difficulties to properly rebase my branches when the master code changes. I think that the code clean up and division into different files would be beneficial in this aspect to people who wants to contribute. I want to start working on this issue soon. |
Those .in files are files preprocessed by make, which makes little sense here considering pacaur has no Makefile or generally macros to expand. Otherwise you should probably first ensure pacaur functions themselves are modular, i.e. work as filters, rather than simply split them to separate files to be all sourced by a main script which is a mostly superficial change (compare yaourt which has a similar layout, yet is the most unreadable thing since the invention of scripture). Another good way to increase readability is remove "home brew" code such as the color functions or more controversially the json parser; much of those are available in libmakepkg or other general-purpose tools. That might in fact be the best starting point for this whole operation. |
@AladW I see this step as the most reasonable before really changing the actual code workflow. Making aggressive change will surely introduce bugs. This will allow us to work in the code with notably less conflicting commits since the files are separated. I agree with you that the next step is ensuring modularity of the functions. |
Sure it will break things. Isn't that why it's put on a separate branch? ;) |
I'm more comfortable working with small changes and testing for bugs than making very big changes try to figure out where the bugs are introduced. Anyways, you are right this is a testing branch, let's have some fun. 😄 |
There is a ticket for the color functions and such (#422), so that's definitely something to go for. If the json parser get replaced by jshon, we might as well use others external tools to replace internal working (this includes pacutils, aurutils and maybe others). I've been skeptic about adding new dependencies (jshon), but if we go all-in for it and leverage tools that have been created the past few months/few years... why not. |
I'd like to start documenting the functions using |
Sure. You'll probably need (lot of) my help here, so just ask when something is badly undocumented. |
Should I wait for a code freeze before keep working on this? I'd have to manually merge every commit from master since files are different. |
It is unlikely code will be completely frozen. All I can say is that there will not be major changes in the 4.7.x branch, only bug fixes. |
@ChuckDaniels87 If this is too inconvenient, you might consider not backporting any changes and just focusing on getting the structure right and documenting/clarifying the current code. When the time is right, we could duplicate your work on master again. Not ideal, but changing the whole structure of an on-going project isn't easy :/ |
@rmarquis If there will be only bug fixes I think I can handle backporting the changes. I'm busy these days but I'll try to keep new branch up to date. |
@ChuckDaniels87 If that is of any help, I hope the current regressions introduced with 4.7.0 will be sorted out quickly, so we'll go back to the usual bi-weekly/monthly minor bugfixes only. |
I've taken a look to the last commits and I think half of them can be cherry-picked. I'd like to update the branch this week if I find some time. |
Since you are planning a major overhaul of the code for pacaur 5 and later, I think I should focus on documenting new code instead of old code on the I will port new commits to |
Yes, definitely. The unexpected arrival of auracle kinda make me change plans here, sorry about that. To give you a rough idea of what's coming, here is how I think we'll handle it:
Auracle still needs to mature before step 1 is possible, but I don't expect it to reach stable release before 2 or 3 months as falconindy is quite busy - though it might arrives sooner. Step 2 and 3 might be merged in a single step, or done gradually through the 5.x. series. I agree about documenting the revisited code, but if you'd like to have an early start, here are the functions that are the least susceptible to change (though I'd like to make them functional rather than linear):
|
I fully agree with the schedule you proposed. If
I prefer the idea of merging these steps. Since step 2 will introduce many code changes, we could start working directly on the restructured code (
So, do you want to split/extract some functions into small and more generic functions? If that is the idea, we can create a little toolbox in a separate file or in Besides, I will take a look on these functions and try to comment what it is not clear. That could also help on the future code overhaul. |
Yes, but auracle isn't ready yet. It is currently missing custom output formatting that pacaur uses for its
I see the above 4.9.x as very low risk too. Replacing the solver is a matter of removing the existing solver code and adjusting the old code pacaur used when it was relaying on cower internal solver. I'm more concerned about the deprecation of short commands transition, but that should be done before the 5.x release in any case. Actually, it would even be possible to merge steps one and two in a single step. I've done the distinction because I'm concerned that I might not have the required time. I'm quite busy at the moment, and I might be offline soon (travelling abroad for some time), so step 1 is more an approach to get rid of cower quickly with the minimum amount of work.
In the past, I've used the I'm also fine with fixing discovered bugs in the develoment code only. Backporting critical fixes in the stable version is also always possible.
The idea would make them rely on a passed parameter and adding checks, instead of working with global variables that are assummed to be always available - that would help for easing debugging. I haven't really looked at it yet, but there are quite some duplicate code here and there that could be refactored in more generic subfunctions.
Yes. I really need an external pair of eyeballs here, since what is obvious to me isn't necessary obvious to contributors. |
Another aspect in favor of releasing 4.8/4.9 early: I'd like to reuse the This also means having a separate |
Porting changes to I will try to keep that branch updated and port the changes as soon as you make them. It would be nice if you can take a look to these commits since the process could be a little error-prone. |
Once 4.8/4.9 are released, I think it will be easier to split the code on master in a similar way you did on the On a sidenote, I'd move |
I will try to keep pacaur5 up to date if the changes are not so drastic. I think is better to port them to the current branch instead of splitting the code again. Cleaning up removed code is the easiest part.
Done. |
First of all, I really appretiate your work in the development of this great piece of software. I've moved from
yaourt
topacaur
recently.I've been digging in the script source code moved by curiosity. I got some experience in bash scripting but I found difficult to follow the application workflow. I'd like to contribute improving readability of the code if you are interested, this could ease maintenance and future contributions to the script functionality.
Basically, I'd focus on these points:
I've found a short and interesting bash style guide if you want to take a look.
More references:
The text was updated successfully, but these errors were encountered: