diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 378eac2..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -build diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 9045234..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,59 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(scl-utils C) -SET(scl_VERSION_MAJOR 2) -SET(scl_VERSION_MINOR 0) -SET(scl_VERSION_PATCH 3) - -IF(DEFINED scl_VERSION_PATCH) -SET(scl_VERSION - "${CMAKE_PROJECT_NAME}-${scl_VERSION_MAJOR}.${scl_VERSION_MINOR}.${scl_VERSION_PATCH}") -ELSE(DEFINED scl_VERSION_PATCH) -SET(scl_VERSION - "${CMAKE_PROJECT_NAME}-${scl_VERSION_MAJOR}.${scl_VERSION_MINOR}") -ENDIF(DEFINED scl_VERSION_PATCH) - -ADD_CUSTOM_TARGET(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source) -ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure) - -INCLUDE(InstallRequiredSystemLibraries) - -SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "scl - base package for software -collections") -SET(CPACK_PACKAGE_VENDOR "Jan Zeleny") -SET(CPACK_PACKAGE_VERSION_MAJOR ${scl_VERSION_MAJOR}) -SET(CPACK_PACKAGE_VERSION_MINOR ${scl_VERSION_MINOR}) -SET(CPACK_PACKAGE_VERSION_PATCH ${scl_VERSION_PATCH}) -SET(CPACK_STRIP_FILES "scl") -SET(CPACK_SOURCE_IGNORE_FILES "/build/" "\\\\.git" ".*tags") -SET(CPACK_SOURCE_GENERATOR "TBZ2") -SET(CPACK_SOURCE_PACKAGE_FILE_NAME ${scl_VERSION}) - -INCLUDE(CPack) - -INSTALL(FILES rpm/macros.scl DESTINATION /etc/rpm) -INSTALL(FILES rpm/scl.attr DESTINATION /usr/lib/rpm/fileattrs) -INSTALL(FILES rpm/sclbuild.attr DESTINATION /usr/lib/rpm/fileattrs) -FILE(GLOB rpmHelpers "rpm/brp-scl-*") -INSTALL(PROGRAMS ${rpmHelpers} rpm/scldeps.sh DESTINATION /usr/lib/rpm) - -INSTALL(FILES doc/scl.1 DESTINATION share/man/man1) -INSTALL(PROGRAMS shell/scl_enabled DESTINATION /usr/bin) -INSTALL(PROGRAMS shell/scl_source DESTINATION /usr/bin) - -INSTALL(FILES shell/scl-init.sh DESTINATION /etc/profile.d) -INSTALL(FILES shell/scl-init.csh DESTINATION /etc/profile.d) -INSTALL(FILES shell/func_scl.csh DESTINATION /etc/scl) - -FIND_PACKAGE(bash-completion) -IF(BASH_COMPLETION_FOUND) - MESSAGE(STATUS "Using bash completion dir ${BASH_COMPLETION_COMPLETIONSDIR}") -ELSE() - SET(BASH_COMPLETION_COMPLETIONSDIR "/etc/bash_completion.d") - MESSAGE(STATUS "Using fallback bash completion dir ${BASH_COMPLETION_COMPLETIONSDIR}") -ENDIF() -INSTALL(FILES shell/scl-completion.bash DESTINATION ${BASH_COMPLETION_COMPLETIONSDIR} RENAME scl) - -SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") -ENABLE_TESTING() -ADD_SUBDIRECTORY(src) -ADD_SUBDIRECTORY(tests) diff --git a/LICENSE b/LICENSE deleted file mode 100644 index d159169..0000000 --- a/LICENSE +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f050285 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +NAME=scl-utils +VERSION=`date +%Y%m%d` +WARNINGS?=-Wall -Wshadow -Wcast-align -Winline -Wextra -Wmissing-noreturn +CFLAGS?=-O2 +CFILES=scl.c +OTHERFILES=Makefile scl_enabled macros.scl scl.1 scldeps.sh scl.attr brp-scl-compress brp-scl-python-bytecompile scl.bash sclbuild +SOURCES=$(CFILES) $(OTHERFILES) + +BINDIR?=/usr/bin +MANDIR?=/usr/share/man +RPMCONFDIR?=/usr/lib/rpm +CNFDIR?=/etc + +all: scl + +scl: $(SOURCES) $(OTHERFILES) + $(CC) $(CFLAGS) $(LDFLAGS) $(WARNINGS) $(CFILES) -o $@ + +clean: + rm -f *.o scl + +distclean: clean + rm -f *~ + +dist: all + LANG=C + rm -rf $(NAME)-$(VERSION) + mkdir $(NAME)-$(VERSION) + cp $(SOURCES) $(NAME)-$(VERSION) + tar fcz $(NAME)-$(VERSION).tar.gz $(NAME)-$(VERSION) + rm -rf $(NAME)-$(VERSION) + +install: all + mkdir -p $(DESTDIR)/$(BINDIR) + mkdir -p $(DESTDIR)/$(CNFDIR)/rpm + mkdir -p $(DESTDIR)/$(CNFDIR)/bash_completion.d + mkdir -p $(DESTDIR)/$(RPMCONFDIR)/fileattrs + cp macros.scl $(DESTDIR)/$(CNFDIR)/rpm + cp scl $(DESTDIR)/$(BINDIR) + cp sclbuild $(DESTDIR)/$(BINDIR) + cp scl_enabled $(DESTDIR)/$(BINDIR) + cp scl.1 $(DESTDIR)/$(MANDIR)/man1 + cp scl.attr $(DESTDIR)/$(RPMCONFDIR)/fileattrs + cp scldeps.sh $(DESTDIR)/$(RPMCONFDIR) + cp brp-scl-compress $(DESTDIR)/$(RPMCONFDIR) + cp brp-scl-python-bytecompile $(DESTDIR)/$(RPMCONFDIR) + cp scl.bash $(DESTDIR)/$(CNFDIR)/bash_completion.d + +uninstall: + rm -f $(BINDIR)/scl $(BINDIR)/scl_enabled $(BINDIR)/sclbuild + rm -f $(CNFDIR)/rpm/macros.scl + rm -f $(MANDIR)/man1/scl.1 + rm -f $(RPMCONFDIR)/fileattrs/scl.attr + rm -f $(RPMCONFDIR)/scldeps.sh + rm -f $(RPMCONFDIR)/brp-scl-compress + rm -f $(RPMCONFDIR)/brp-scl-python-bytecompile + rm -f $(CNFDIR)/bash_completion.d/scl.bash diff --git a/rpm/brp-scl-compress b/brp-scl-compress old mode 100755 new mode 100644 similarity index 100% rename from rpm/brp-scl-compress rename to brp-scl-compress diff --git a/rpm/brp-scl-python-bytecompile b/brp-scl-python-bytecompile old mode 100755 new mode 100644 similarity index 91% rename from rpm/brp-scl-python-bytecompile rename to brp-scl-python-bytecompile index c91b952..58cd248 --- a/rpm/brp-scl-python-bytecompile +++ b/brp-scl-python-bytecompile @@ -10,7 +10,7 @@ fi [ "0$scl_path" = "0" ] && exit 0 # If we don't have a python interpreter, avoid changing anything. -default_python=${1:-/usr/bin/python3} +default_python=${1:-/usr/bin/python} if [ ! -x "$default_python" ]; then exit 0 fi @@ -26,15 +26,15 @@ fi # .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python # bytecode that they are for. # -# The files below RPM_BUILD_ROOT could be targeting multiple versions of +# The files below RPM_BUILD_ROOT could be targetting multiple versions of # python (e.g. a single build that emits several subpackages e.g. a # python26-foo subpackage, a python31-foo subpackage etc) # # Support this by assuming that below each /usr/lib/python$VERSION/, all # .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION. # -# For example, below /usr/lib/python2.6/, we're targeting /usr/bin/python2.6 -# and below /usr/lib/python3.1/, we're targeting /usr/bin/python3.1 +# For example, below /usr/lib/python2.6/, we're targetting /usr/bin/python2.6 +# and below /usr/lib/python3.1/, we're targetting /usr/bin/python3.1 shopt -s nullglob for python_libdir in "$RPM_BUILD_ROOT"/usr/lib{,64}/python[0-9].[0-9]/ "$RPM_BUILD_ROOT"/$scl_path/usr/lib{,64}/python[0-9].[0-9]/; diff --git a/doc/scl.1 b/doc/scl.1 deleted file mode 100644 index 41c777e..0000000 --- a/doc/scl.1 +++ /dev/null @@ -1,114 +0,0 @@ -.TH "scl" "1" -.SH "NAME" -scl \- Setup and run software from Software Collection environment -.SH "SYNOPSIS" -.PP -\fBscl enable|run\fP [\fB-x|--exec\fP] \fI\fR [\fI ...\fR] \fI\fR - -.PP -\fBscl load|unload\fP \fI\fR [\fI ...\fR] -.PP -\fBscl list-collections\fP -.PP -\fBscl list-enabled\fP -.PP -\fBscl list-packages\fP \fI\fR -.PP -\fBscl register\fP \fI\fR -.PP -\fBscl deregister\fP \fI\fR [\fB-f|--force\fP] -.PP -\fBscl man\fP \fI\fR -.PP -\fBscl -V|--version\fR -.SH "DESCRIPTION" -.PP -This manual page documents \fBscl\fP, a -program which is an utility for running software packaged as a Software Collection. -.PP -\fBscl\fP utility allows to execute an application which is not located in the filesystem -root hierarchy but is present in an alternative location. This application can still use -and benefit from software installed in the root filesystem. In order to let an application -be visible to the system one has to use \fBscl\fP utility as an interface. -.PP -.SH "OPTIONS" -.PP -.IP "\fBenable, run\fP \fI\fR [\fI ...\fR] \fI\fR [\fB-x|--exec\fP]" -Run \fI\fR within the environment of the given collections. Each collection contains a module file that defines how collection modifies the environment. First the environment is modified according to module files of the given collections and then \fI\fR is executed. Control is returned back to the caller with the original environment as soon as the \fI\fR finishes. If \fI\fR is '-' (dash) then it is read from the standard input. - -If \fB--exec\fP is specified then function exec() will be used for command execution otherwise function system() will be used. - -Note: if you use \fI\fR consisting of multiple arguments, you either need to -use quotes or the \fB--\fP command separator. Everything that follows the -separator will be considered a command or its argument. - -Note: some commands modify user environment in some way. Those commands may -potentially break SCLs, as their activation also usually depends on env. -modification as well. Typical examples of such commands are \fBsu\fP and -\fBsudo\fP. Every collection modifies the environment differently, thus more -details may be found in the documentation of the particular collection. -.IP "\fBload\fP \fI\fR [\fI ...\fR] -Load the given collections into current shell. The environment of current shell is modified according to module files of the collections. This command properly works in these shells: bash, zsh, tcsh. You need to source \fB/etc/profile.d/modules.sh\fR and \fB/etc/profile.d/scl-init.sh\fR for this command to work in shells: dash, mksh, ksh. This command is not supported in other shells. -.IP "\fBunload\fP \fI\fR [\fI ...\fR] -Unload the given collections from current shell. The environment of current shell is returned back to the state before loading the collections. This command properly works in these shells: bash, zsh, tcsh. You need to source \fB/etc/profile.d/modules.sh\fR and \fB/etc/profile.d/scl-init.sh\fR for this command to work in shells: dash, mksh, ksh. This command is not supported in other shells. -.IP "\fBlist-collections\fP" -Lists all installed Software Collections on the system. -.IP "\fBlist-enabled\fP" -Lists Software Collections enabled in current shell. -.IP "\fBlist-packages\fP \fI ...\fR" -If a collection name is specified then list of installed packages belonging to the collection is listed. -.IP "\fBregister\fP \fI\fR" -If \fI\fR leads to valid SCL file structure, \fBscl\fP will register that as a SCL. -\fI\fR directory needs to contain \fBmodule\fR file and \fBroot\fP directory, -to be considered valid SCL. - needs to be an absolute path to the collection location. -.IP "\fBderegister\fP \fI\fR [\fB-f|--force\fP]" -\fI\fR will no longer be considered SCL. -If the \fI\fR was installed locally, then the use of \fB--force\fP is needed.i -.IP "\fBman\fP \fI\fR -Show manual page for \fI\fR. -.IP "\fB-v, --version\fR" -Show version. -.SH "EXAMPLES" -.TP -scl enable example 'less --version' -runs command 'less --version' in the environment with collection 'example' enabled -.TP -scl load foo bar -Load foo and bar Software Collections into current shell -.TP -scl unload foo bar -Unload foo and bar Software Collections from current shell -.TP -cat my_command | scl enable baz - -run set of commands listed in my_command file in the environment with baz Software Collection -enabled -.TP -scl list-collections -list all installed collections -.TP -scl list-packages example -list all packages within example collection -.TP -scl register /foo/bar -registers new collection with a name bar -.TP -scl deregister bar --force -forces the deregistration of collection bar -.TP -A wrapper script must use proper quoting or command separator like in the -following examples -.PP -.nf -.RS -#!/bin/bash -COMMAND="ruby $(printf "%q " "$@")" -scl enable ruby193 "$COMMAND" -# or -scl enable ruby193 -- $COMMAND -.RE -.fi -.SH "AUTHOR" -.PP -\fBscl\fP was written by Jindrich Novy and Jan Zeleny -. diff --git a/rpm/macros.scl b/macros.scl similarity index 54% rename from rpm/macros.scl rename to macros.scl index 0d27a6b..8b6377e 100644 --- a/rpm/macros.scl +++ b/macros.scl @@ -3,49 +3,12 @@ # Copyright (C) 2012 Red Hat, Inc. # Written by Jindrich Novy . -%scl_debug() %{expand: -%define old_debug %{lua:print(rpm.expand("%{debug_package}"):len())} -%undefine _debugsource_packages -%global debug_package %{expand: -%if "%{?old_debug}" == "0" - %{expand: %{nil}} -%else -%if "%{?scl}%{!?scl:0}" == "%{pkg_name}" - %{expand: %{nil}} -%else -%ifnarch noarch -%package debuginfo -Summary: Debug information for package %{name} -Group: Development/Debug -AutoReqProv: 0 -Requires: %scl_runtime -Provides: scl-package(%scl) -%{lua: - debuginfo=tonumber(rpm.expand("%{old_debug}")) - if debuginfo > 0 then - rpm.define("__debug_package 1") - end -} -%description debuginfo -This package provides debug information for package %{name}. -Debug information is useful when developing applications that use this -package or when debugging this package. -%files debuginfo -f debugfiles.list -%defattr(-,root,root) -%endif -%endif -%endif -%{nil}}} - -%scl_prefix() %{?scl:%(if [ "%1" = "%%1" ]; then echo "%{scl}-"; else echo "%1-"; fi)}%{!?scl:%{nil}} - %scl_package() %{expand:%{!?_root_prefix: %global pkg_name %1 %global scl_name %{scl} +%global scl_prefix %{scl}- %global scl_runtime %{scl}-runtime -%global scl_basedir /opt -%{!?scl_vendor: %global scl_vendor rh} -%{!?_scl_prefix: %global _scl_prefix %{scl_basedir}/%{scl_vendor}} +%{!?_scl_prefix: %global _scl_prefix /opt/rh} %global _scl_scripts %{_scl_prefix}/%{scl} %global _scl_root %{_scl_prefix}/%{scl}/root %global _root_prefix %{_prefix} @@ -55,7 +18,7 @@ package or when debugging this package. %global _root_libexecdir %{_exec_prefix}/libexec %global _root_datadir %{_prefix}/share %global _root_sysconfdir %{_sysconfdir} -%global _root_sharedstatedir %{_sharedstatedir} +%global _root_sharedstatedir %{_prefix}/com %global _root_localstatedir %{_localstatedir} %global _root_libdir %{_exec_prefix}/%{_lib} %global _root_includedir %{_prefix}/include @@ -69,44 +32,46 @@ package or when debugging this package. %global _libexecdir %{_exec_prefix}/libexec %global _datadir %{_prefix}/share %global _sysconfdir %{_scl_root}/etc -%{?nfsmountable: %global _sysconfdir %{_root_sysconfdir}%{_scl_prefix}/scls/%{scl}} -%global _sharedstatedir %{_scl_root}/var/lib -%{?nfsmountable: %global _sharedstatedir %{_root_localstatedir}%{_scl_prefix}/scls/%{scl}/lib} +%global _sharedstatedir %{_prefix}/com %global _localstatedir %{_scl_root}/var -%{?nfsmountable: %global _localstatedir %{_root_localstatedir}%{_scl_prefix}/scls/%{scl}} %global _libdir %{_exec_prefix}/%{_lib} %global _includedir %{_prefix}/include %global _infodir %{_datadir}/info %global _mandir %{_datadir}/man %global _docdir %{_datadir}/doc %global _defaultdocdir %{_docdir} -} -%{?scl_dependency_generators:%scl_dependency_generators} %global scl_pkg_name %{scl}-%{pkg_name} -%scl_debug %global __os_install_post %{expand: /usr/lib/rpm/brp-scl-compress %{_scl_root} - %{!?__debug_package:/usr/lib/rpm/brp-strip %{__strip} - /usr/lib/rpm/brp-strip-comment-note %{__strip} %{__objdump} + %{!?__debug_package:/usr/lib/rpm/redhat/brp-strip %{__strip} + /usr/lib/rpm/redhat/brp-strip-comment-note %{__strip} %{__objdump} } - /usr/lib/rpm/brp-strip-static-archive %{__strip} - /usr/lib/rpm/brp-scl-python-bytecompile %{__python3} %{?_python_bytecompile_errors_terminate_build} %{_scl_root} - [ -f /usr/lib/rpm/redhat/brp-python-hardlink ] && /usr/lib/rpm/redhat/brp-python-hardlink || /usr/lib/rpm/brp-python-hardlink + /usr/lib/rpm/redhat/brp-strip-static-archive %{__strip} + /usr/lib/rpm/brp-scl-python-bytecompile %{__python} %{?_python_bytecompile_errors_terminate_build} %{_scl_root} + /usr/lib/rpm/redhat/brp-python-hardlink + %{!?__jar_repack:/usr/lib/rpm/redhat/brp-java-repack-jars} %{nil}} -BuildRequires: scl-utils-build -%if "%{?scl}%{!?scl:0}" == "%{pkg_name}" -Requires: %{scl_runtime} -Provides: scl-package(%{scl}) +%global debug_package %{expand: +%ifnarch noarch +%global __debug_package 1 +%package debuginfo +Summary: Debug information for package %{name} +Group: Development/Debug +AutoReqProv: 0 +Provides: scl-package(%scl) +%description debuginfo +This package provides debug information for package %{name}. +Debug information is useful when developing applications that use this +package or when debugging this package. +%files debuginfo -f debugfiles.list +%defattr(-,root,root) %endif +%{nil}} +BuildRequires: scl-utils-build +} %{?scl_package_override:%scl_package_override} } %scl_require() %{_scl_prefix}/%1/enable, %1 %scl_require_package() %1-%2 -%scl_enable_script() %{expand: -cat >> %{buildroot}%{_scl_scripts}/enable << 'EOF' -eval `/usr/bin/modulecmd bash load %{scl}` -EOF -} - diff --git a/rpm/scl.attr b/rpm/scl.attr deleted file mode 100644 index 6dc881b..0000000 --- a/rpm/scl.attr +++ /dev/null @@ -1,3 +0,0 @@ -%__scl_provides %{_rpmconfigdir}/scldeps.sh --provides %{scl} -%__scl_requires %{_rpmconfigdir}/scldeps.sh --requires %{scl_runtime} -%__scl_path %{?scl:^%{_scl_prefix}/.*|%{_root_sysconfdir}/rpm/macros.%{scl}-config$} diff --git a/rpm/sclbuild.attr b/rpm/sclbuild.attr deleted file mode 100644 index 6871a26..0000000 --- a/rpm/sclbuild.attr +++ /dev/null @@ -1,4 +0,0 @@ -%__sclbuild_provides %{_rpmconfigdir}/scldeps.sh --provides %{scl} -%__sclbuild_requires %{_rpmconfigdir}/scldeps.sh --requires scl-utils-build %{scl_runtime} -%__sclbuild_path %{?scl:^%{_root_sysconfdir}/rpm/macros.%{scl}-config$} - diff --git a/rpm/scldeps.sh b/rpm/scldeps.sh deleted file mode 100755 index 7233f6c..0000000 --- a/rpm/scldeps.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -# Read all the input! -cat > /dev/null - -[ $# -ge 2 ] || { - exit 0 -} - -option=$1 -shift 1 - -case $option in --P|--provides) - echo -n "scl-package($1)" - ;; --R|--requires) - for o in "$@"; do - echo "$o" - done - ;; -esac -exit 0 diff --git a/scl.1 b/scl.1 new file mode 100644 index 0000000..aa79773 --- /dev/null +++ b/scl.1 @@ -0,0 +1,55 @@ +.TH "scl" "1" +.SH "NAME" +scl \- Setup and run software from Software Collection environment +.SH "SYNOPSIS" +.PP +\fBscl\fP \fI\fR \fI\fR [\fI ...\fR] \fI\fR +.PP +\fBscl\fP {\fB-l|--list\fP} [\fI ...\fR] +.SH "DESCRIPTION" +.PP +This manual page documents \fBscl\fP, a +program which is an utility for running software packaged as a Software Collection. +.PP +\fBscl\fP utility allows to execute an application which is not located in the filesystem +root hierarchy but is present in an alternative location. This application can still use +and benefit from software installed in the root filesystem. In order to let an application +be visible to the system one has to use \fBscl\fP utility as an interface. +.PP +\fI\fR is a script name to execute in a bash environment before the application +itself takes in executed. Currently only \fBenable\fP scriptlet is mandatory which is needed +to update search paths, etc. +.PP +One can enable more Software Collections if needed and therefore one can use multiple +collections which are enabled by the left-right order as present on \fBscl\fP command-line. +.PP +\fI\fR is an arbitrary command or set of commands to execute within the Software +Collection environment enabled. Control is returned back to the caller with the original +environment as soon as the command finishes. It \fI\fR is '-' (dash) then it is +read from the standard input. +.SH "OPTIONS" +.PP +.IP "\fB-l, --list\fP" +Lists all installed Software Collections on the system. +.IP "\fB-l, --list\fP \fI ...\fR" +If a collection name is specified then list of installed packages belonging to the collection is listed. +.SH "EXAMPLES" +.TP +scl enable example 'less --version' +runs command 'less --version' in the environment with collection 'example' enabled +.TP +scl enable foo bar bash +runs bash instance with foo and bar Software Collections enabled +.TP +cat my_command | scl enable baz - +run set of commands listed in my_command file in the environment with baz Software Collection +enabled +.TP +scl -l +list all installed collections +.TP +scl -l example +list all packages within example collection +.SH "AUTHOR" +.PP +\fBscl\fP was written by Jindrich Novy . diff --git a/scl.attr b/scl.attr new file mode 100644 index 0000000..6521995 --- /dev/null +++ b/scl.attr @@ -0,0 +1,2 @@ +%__pkgconfig_provides %{_rpmconfigdir}/scldeps.sh --provides %{scl} +%__pkgconfig_path %{?scl:^%{_scl_prefix}/.*$} diff --git a/scl.bash b/scl.bash new file mode 100644 index 0000000..7f77233 --- /dev/null +++ b/scl.bash @@ -0,0 +1,44 @@ +# main function bound to scl command +_scl() +{ + local cur prev opts + COMPREPLY=() + + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + opts="-l --list" + + # handle options + if [[ ${cur} == -* ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + + # handle scriptlets; the first parameter must be a scriptlet if it is not an option + if ((COMP_CWORD == 1)); then + # get array of scriptlets found throughout collections + local collections=($(find /etc/scl/prefixes -maxdepth 1 -mindepth 1 -type f -exec basename {} \; | sort -u)) + local scriptlets=() + for col in ${collections[@]}; do + local prefix=`cat /etc/scl/prefixes/$col` + scriptlets+=($(find $prefix/$col/* -maxdepth 1 -type f -exec basename {} \; | sort -u)) + done + scriptlets_str=`echo ${scriptlets[@]} | sed 's/ /\n/g'| sort -u` + COMPREPLY=( $(compgen -W "$scriptlets_str" -- ${cur}) ) + return 0 + fi + + # handle commands; does not handle commands without single or double quotes + if [[ ${cur} == \'* || ${cur} == \"* ]] ; then + # it is a command do not reply with anything + return 0 + fi + + # handle collections; if it is not an option or a command, it must be a collection + local collections=($(find /etc/scl/prefixes -maxdepth 1 -mindepth 1 -type f -exec basename {} \; | sort -u)) + COMPREPLY=( $(compgen -W "${collections[*]}" -- ${cur}) ) + return 0 +} + +# bind the scl command to the _scl function for completion +complete -F _scl scl diff --git a/scl.c b/scl.c new file mode 100644 index 0000000..81fdf5d --- /dev/null +++ b/scl.c @@ -0,0 +1,381 @@ +/* Copyright (C) 2011 Red Hat, Inc. + + Written by Jindrich Novy . + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; see the file COPYING. If not, + write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SCL_CONF_DIR "/etc/scl/conf/" + +static void check_asprintf( char **strp, const char *fmt, ... ) { + va_list args; + + va_start(args, fmt); + + if (vasprintf(strp, fmt, args) == -1 || !*strp) { + fprintf(stderr, "Allocation failed.\n"); + exit(EXIT_FAILURE); + } + + va_end(args); +} + +static void write_script( int tfd, char *s ) { + if (write(tfd, s, strlen(s)) == -1) { + fprintf(stderr, "Error writing to temporary file\n"); + exit(EXIT_FAILURE); + } +} + +static void print_usage( const char *name ) { + fprintf(stderr, "usage: %s [...] \n", name); + fprintf(stderr, " or: %s -l|--list [...]\n", name); + + fprintf(stderr, "\nOptions:\n" + " -l, --list list installed Software Collections or packages\n" + " that belong to them\n" + " -h, --help display this help and exit\n" + "\nActions:\n" + " enable calls enable script from Software Collection\n" + " (enables a Software Collection)\n" + " calls arbitrary script from a Software Collection\n" + "\nUse '-' as to read the command from standard input.\n"); +} + +static void list_collections() { + struct stat sb; + struct dirent **nl; + int n, i; + + if (stat(SCL_CONF_DIR, &sb) == -1) { + fprintf(stderr, "%s does not exist\n", SCL_CONF_DIR); + exit(EXIT_FAILURE); + } + + if (!S_ISDIR(sb.st_mode)) { + fprintf(stderr, "%s is not a directory\n", SCL_CONF_DIR); + exit(EXIT_FAILURE); + } + + if ((n = scandir(SCL_CONF_DIR, &nl, 0, alphasort)) < 0) { + perror("scandir"); + exit(EXIT_FAILURE); + } + + for (i=0; id_name != '.') { + printf("%s\n", nl[i]->d_name); + } + } + + free(nl); +} + +static char **read_script_output( char *ori_cmd ) { + struct stat sb; + char tmp[] = "/var/tmp/sclXXXXXX"; + char *m, **lines, *mp, *cmd; + int lp = 0, ls, tfd, i; + FILE *f; + + tfd = mkstemp(tmp); + check_asprintf(&cmd, "%s > %s", ori_cmd, tmp); + i = system(cmd); + free(cmd); + + if (WEXITSTATUS(i) != 0) { + fprintf(stderr, "Command execution failed: %s\n", ori_cmd); + exit(EXIT_FAILURE); + } + free(ori_cmd); + + if (stat(tmp, &sb) == -1) { + fprintf(stderr, "%s does not exist\n", tmp); + exit(EXIT_FAILURE); + } + + if ((m = malloc(sb.st_size)) == NULL) { + fprintf(stderr, "Can't allocate memory.\n"); + exit(EXIT_FAILURE); + } + + if ((f=fopen(tmp, "r")) == NULL || fread(m, 1, sb.st_size, f) < 1) { + fprintf(stderr, "Unable to read contents of temporary file.\n"); + exit(EXIT_FAILURE); + } + + fclose(f); + close(tfd); + unlink(tmp); + + ls = 0x100; + lines = malloc(ls*sizeof(char*)); + *lines = NULL; + + for (mp=m; mp && mp < &m[sb.st_size];) { + lines[lp++] = mp; + if (lp == ls-1 ) { + ls += 0x100; + lines = realloc(lines, ls*sizeof(char*)); + } + mp = strchr(mp, '\n'); + *mp = '\0'; + mp++; + } + + lines[lp] = NULL; + return lines; +} + +static int list_packages_in_collection( const char *colname) { + struct stat sb; + struct dirent **nl; + int i, n, found, smax, ss; + char *cmd, **lines; + char **srpms = NULL; + size_t cns; + + if (stat(SCL_CONF_DIR, &sb) == -1) { + fprintf(stderr, "%s does not exist\n", SCL_CONF_DIR); + exit(EXIT_FAILURE); + } + + if (!S_ISDIR(sb.st_mode)) { + fprintf(stderr, "%s is not a directory\n", SCL_CONF_DIR); + exit(EXIT_FAILURE); + } + + if ((n = scandir(SCL_CONF_DIR, &nl, 0, alphasort)) < 0) { + perror("scandir"); + exit(EXIT_FAILURE); + } + + for (found=i=0; id_name != '.') { + if (!strcmp(nl[i]->d_name, colname)) { + found = 1; + } + } + free(nl[i]); + } + + free(nl); + + if (!found) { + fprintf(stderr, "warning: collection \"%s\" doesn't seem to be installed, checking anyway...\n", colname); + } + + check_asprintf(&cmd, "rpm -qa --qf=\"#%%{name}-%%{version}-%%{release}.%%{arch}\n%%{sourcerpm}\n[%%{provides}\n]\""); + lines = read_script_output(cmd); + if (!lines[0]) { + fprintf(stderr, "No package list from RPM received.\n"); + exit(EXIT_FAILURE); + } + + cns = strlen(colname); + + for (smax=ss=i=0; lines[i];) { + char *srpm = lines[i+1]; + i += 2; + for (;lines[i] && lines[i][0] != '#'; i++) { + if (!strncmp(lines[i], "scl-package(", 12) && !strncmp(&lines[i][12], colname, cns) && lines[i][12+cns] == ')') { + for (found=n=0; n= 2 && (!strcmp(argv[1],"--list") || !strcmp(argv[1],"-l"))) { + if (argc == 2) { + list_collections(); + } else { + for (i=2; i " +else + SCL=$1 + shift + rpmbuild $@ --define "scl $SCL" +fi diff --git a/scldeps.sh b/scldeps.sh new file mode 100644 index 0000000..268d6bb --- /dev/null +++ b/scldeps.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +[ $# -ge 2 ] || { + cat > /dev/null + exit 0 +} + +case $1 in +-P|--provides) + echo -n "scl-package($2)" + ;; +esac +exit 0 diff --git a/shell/func_scl.csh b/shell/func_scl.csh deleted file mode 100644 index 31f6750..0000000 --- a/shell/func_scl.csh +++ /dev/null @@ -1,7 +0,0 @@ -if ( "$1" == "load" || "$1" == "unload" ) then -# It is possible that function module is not declared in time of this -# declaration so eval is used instead of direct calling of function module - eval "module $*:q" -else - /usr/bin/scl $*:q -endif diff --git a/shell/scl-completion.bash b/shell/scl-completion.bash deleted file mode 100644 index 78aa574..0000000 --- a/shell/scl-completion.bash +++ /dev/null @@ -1,88 +0,0 @@ -# main function bound to scl command -_scl() -{ - local cur actions cur_action collections - COMPREPLY=() - - cur="${COMP_WORDS[COMP_CWORD]}" - actions="enable run load unload list-collections list-packages man register deregister --help" - - collections=`scl list-collections` - - # Complete action names - if ((COMP_CWORD == 1)); then - COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) ) - return 0; - fi - - # If there is command or separator in arguments then stop completition - if ((COMP_CWORD > 3)); then - for word in "${COMP_WORDS[@]}"; do - if [[ ${word} == \'* || ${word} == \"* || ${word} == "--" ]] ; then - return 0 - fi - done - fi - - # Complete one or none action argument - if ((COMP_CWORD >= 2)); then - cur_action="${COMP_WORDS[1]}" - - case "$cur_action" in - # No argument - list-collections|list-enabled|--help) - return 0 - ;; - - # Argument is collection name - list-packages|man) - if ((COMP_CWORD == 2)); then - COMPREPLY=( $(compgen -W "$collections" -- ${cur}) ) - fi - return 0 - ;; - - # Argument is collection name or "-f" or "--force" - deregister) - if ((COMP_CWORD == 2)); then - COMPREPLY=( $(compgen -W "$collections --force -f" -- ${cur})) - fi - if [ "$COMP_CWORD" -eq 3 -a "(" "${COMP_WORDS[2]}" == "--force" -o "${COMP_WORDS[2]}" == "-f" ")" ]; then - COMPREPLY=( $(compgen -W "$collections" -- ${cur})) - fi - return 0 - ;; - - # Argument is directory - register) - compopt -o plusdirs - if ((COMP_CWORD == 2)); then - COMPREPLY=( $(compgen -A directory -- ${cur}) ) - fi - return 0 - ;; - - # Arguments are collections or "-x" or "--exec" - run|enable) - if ((COMP_CWORD == 2)); then - COMPREPLY=( $(compgen -W "$collections -x --exec" -- ${cur}) ) - else - COMPREPLY=( $(compgen -W "$collections" -- ${cur}) ) - fi - return 0 - ;; - - # Arguments are collections - load|unload) - COMPREPLY=( $(compgen -W "$collections" -- ${cur}) ) - return 0 - ;; - *) - ;; - esac - fi - -} - -# bind the scl command to the _scl function for completion -complete -F _scl scl diff --git a/shell/scl-init.csh b/shell/scl-init.csh deleted file mode 100644 index 45b22be..0000000 --- a/shell/scl-init.csh +++ /dev/null @@ -1,9 +0,0 @@ -alias scl 'source /etc/scl/func_scl.csh' - -setenv MODULESHOME /usr/share/Modules - -if (! $?MODULEPATH ) then - setenv MODULEPATH `sed -n 's/[ #].*$//; /./H; $ { x; s/^\n//; s/\n/:/g; p; }' ${MODULESHOME}/init/.modulespath` -endif - -setenv MODULEPATH /etc/scl/modulefiles:$MODULEPATH diff --git a/shell/scl-init.sh b/shell/scl-init.sh deleted file mode 100644 index 6eeabea..0000000 --- a/shell/scl-init.sh +++ /dev/null @@ -1,26 +0,0 @@ -scl() -{ -if [ "$1" = "load" -o "$1" = "unload" ]; then -# It is possible that function module is not declared in time of this -# declaration so eval is used instead of direct calling of function module - eval "module $@" -else - /usr/bin/scl "$@" -fi -} - -shell=`/bin/basename \`/bin/ps -p $$ -ocomm=\`` -[ "$shell" = "bash" ] && export -f scl # export -f works only in bash - -MODULESHOME=/usr/share/Modules -export MODULESHOME - -if [ "${MODULEPATH:-}" = "" ]; then - MODULEPATH=`sed -n 's/[ #].*$//; /./H; $ { x; s/^\n//; s/\n/:/g; p; }' ${MODULESHOME}/init/.modulespath` -fi - -MODULEPATH=/etc/scl/modulefiles:$MODULEPATH - -export MODULEPATH - - diff --git a/shell/scl_source b/shell/scl_source deleted file mode 100755 index 27ad541..0000000 --- a/shell/scl_source +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash - -_scl_source_help="Usage: source scl_source [ ...] - -Don't use this script outside of SCL scriptlets! - -Options: - -h, --help display this help and exit" - -if [ $# -eq 0 -o "$1" = "-h" -o "$1" = "--help" ]; then - echo "$_scl_source_help" -else # main operation mode - - -if [ -z "$_recursion" ]; then - _recursion="false" -fi -if [ -z "$_scl_scriptlet_name" ]; then - # The only allowed action in the case of recursion is the same - # as was the original - _scl_scriptlet_name=$1 -fi -shift 1 - -if [ -z "$_scl_dir" ]; then - # No need to re-define the directory twice - _scl_dir=/etc/scl/conf - if [ ! -e $_scl_dir ]; then - _scl_dir=/etc/scl/prefixes - fi -fi - -for arg in "$@"; do - _scl_prefix_file=$_scl_dir/$arg - _scl_prefix=`cat "$_scl_prefix_file" 2> /dev/null` - if [ $? -ne 0 ]; then - echo >&2 "Can't read $_scl_prefix_file, $arg is probably not installed." - return 1 - fi - - # First check if the collection is already in the list - # of collections to be enabled - for scl in "${_scls[@]}"; do - if [ "$arg" == "$scl" ]; then - continue 2 - fi - done - - # Now check if the collection isn't already enabled - if ! /usr/bin/scl_enabled "$arg" > /dev/null 2> /dev/null; then - _scls+=( "$arg" ) - _scl_prefixes+=( "$_scl_prefix" ) - fi; -done - -if [ $_recursion == "false" ]; then - _i=0 - _recursion="true" - while [ $_i -lt ${#_scls[@]} ]; do - _scl_scriptlet_path="${_scl_prefixes[$_i]}/${_scls[$_i]}/${_scl_scriptlet_name}" - source "$_scl_scriptlet_path" - if [ $? -ne 0 ]; then - echo >&2 "Can't source $_scl_scriptlet_name, skipping." - else - export X_SCLS="${_scls[$_i]} $X_SCLS" - fi; - _i=$(( _i + 1 )) - done - _scls=() - _scl_prefixes=() - _scl_scriptlet_name="" - _recursion="false" -fi - -fi # main operation mode diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index d0f96fe..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -SET( PRG_NAME "scl" ) -SET(MODULES_PATH "/etc/scl/modulefiles" ) -SET(MODULE_CMD "/usr/bin/modulecmd" ) -SET(CONF_DIR "/etc/scl/conf/" ) -CONFIGURE_FILE( config.h.cmake config.h ) - -SET( CMAKE_C_FLAGS "-Wall -pedantic --std=gnu99 -D_GNU_SOURCE -g ${CMAKE_C_FLAGS}" ) -INCLUDE_DIRECTORIES ("${PROJECT_BINARY_DIR}/src") -list(APPEND SOURCES scl.c debug.c scllib.c lib_common.c args.c sclmalloc.c fallback.c) -ADD_EXECUTABLE (scl ${SOURCES}) -INSTALL(TARGETS scl RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE - DESTINATION lib) -FILE(GLOB helpers "helpers/*") -INSTALL(PROGRAMS ${helpers} DESTINATION bin) - -link_directories (/lib64/) -target_link_libraries (scl librpm.so) -target_link_libraries (scl librpmio.so) diff --git a/src/args.c b/src/args.c deleted file mode 100644 index dda57cd..0000000 --- a/src/args.c +++ /dev/null @@ -1,405 +0,0 @@ -#include -#include -#include -#include - -#include "errors.h" -#include "args.h" -#include "scllib.h" -#include "sclmalloc.h" -#include "lib_common.h" -#include "debug.h" - -/** - * Procedure to parse shebang line and transform it to standard command - * - * In general, the shebang line has n parts at the beginnning: - * argv[0] => the name of the utility stated in shebang - * argv[1] => all the arguments as one string - * argv[2] => the script which was executed - * argv[n] => argument for executed script - * - * Considering the following shebang line in script ./test.py: - * #!/usr/bin/scl enable collectionX - * argv[0] = "/usr/bin/scl" - * argv[1] = "enable collectionX" - * argv[2] = "./test.py" - * argv[n] = "argn" - */ -static int parse_shebang(int argc, char *argv[], - int *_shebang_argc, char ***_shebang_argv) { - int i = 0; - char *p; - int shebang_argc; - char **shebang_argv; - - shebang_argc = count_words(argv[1], ' ') + argc - 1; - shebang_argv = (char **)xmalloc(sizeof(char *) * shebang_argc); - - shebang_argv[i++] = argv[0]; - - p = strtok(argv[1], " "); - while (p != NULL) { - shebang_argv[i++] = p; - p = strtok(NULL, " "); - } - - while (i < shebang_argc) { - shebang_argv[i] = argv[i - (shebang_argc - argc)]; - i++; - } - - *_shebang_argc = shebang_argc; - *_shebang_argv = shebang_argv; - - return EOK; -} - -static int extract_colpaths(int argc, char **argv, struct scl_args *args) -{ - int i; - - if (argc < 3) { - return EINPUT; - } - - /* two skipped, but one terminator*/ - args->colpaths = xcalloc(argc - 2 + 1, sizeof(*args->colpaths)); - - /* argv[0] is program name, argv[1] is a command -> skip those */ - for (i = 0; i < argc - 2; i++) { - args->colpaths[i] = argv[i + 2]; - if (args->colpaths[i][0] != '/') { - debug("Collection must be specified with absolute path!\n\n"); - return EINPUT; - } - } - args->colpaths[i] = NULL; - - return EOK; -} - -static int extract_collections(int argc, char **argv, struct scl_args *args) -{ - int i; - - if (argc < 3) { - return EINPUT; - } - - /* two skipped, but one terminator*/ - args->collections = xcalloc(argc - 2 + 1, sizeof(*args->collections)); - - /* argv[0] is program name, argv[1] is a command -> skip those */ - for (i = 0; i < argc - 2; i++) { - args->collections[i] = argv[i + 2]; - } - args->collections[i] = NULL; - - return EOK; -} - -static int extract_command(int argc, char **argv, int command_pos, - struct scl_args *args) { - int i; - int chunk_len, len = 0; - char *command = NULL; - - for (i = command_pos; i < argc; i++) { - len += strlen(argv[i])+3; /* +1 for additional space, +2 for additional quotes */ - } - len -= 3; /* The first part doesn't need neither quotes, nor space before */ - - command = xmalloc((len+1)*sizeof(char)); - - /* The first one is without quotes */ - len = strlen(argv[command_pos]); - memcpy(command, argv[command_pos], len); - for (i = command_pos+1; i < argc; i++) { - command[len++] = ' '; - command[len++] = '"'; - chunk_len = strlen(argv[i]); - memcpy(command+len, argv[i], chunk_len); - len += chunk_len; - command[len++] = '"'; - } - command[len] = '\0'; - - args->command = command; - return EOK; -} - -static int extract_command_stdin(struct scl_args *args) -{ - size_t len; - size_t r; - char *command = NULL; - - command = xmalloc(BUFSIZ+1); - - len = 0; - while ((r = fread(command+len, 1, BUFSIZ, stdin)) == BUFSIZ) { - len += r; - command = xrealloc(command, len+BUFSIZ+1); - } - - if (feof(stdin)) { - if (r < BUFSIZ) { - len += r; - } - command[len] = '\0'; - } else { - free(command); - return EDISK; - } - - args->command = command; - return EOK; -} - -static int parse_run_args(int argc, char **argv, struct scl_args *args) -{ - int i, ret; - /* This initialization is important for the condition below */ - int separator_pos = argc-1; - - /* Find the separator (skip the argv[0] and [1]) */ - for (i = 2; i < argc; i++) { - if (strcmp(argv[i], "--") == 0) { - separator_pos = i; - break; - } - } - if (separator_pos == 2) { - /* No collections specified */ - return EINPUT; - } else { - ret = extract_collections(separator_pos, argv, args); - if (ret != EOK) { - return ret; - } - - if (separator_pos > 2 && separator_pos < argc-1) { - /* Collections and command separated */ - ret = extract_command(argc, argv, separator_pos+1, args); - if (ret != EOK) { - goto fail; - } - } else { - /* One of two situations could have happened: - * a) the separator is the last argument - * b) the separator was not found - * => re-test the last argument again - */ - if (strcmp(argv[separator_pos], "--") != 0) { - /* Separator not found - the last arg is command */ - args->command = xstrdup(argv[argc-1]); - } else { - /* Separator is the last arg -> command from stdin */ - args->command = xstrdup("-"); - } - } - - if (strcmp(args->command, "-") == 0) { - free(args->command); - args->command = NULL; /* In case the subsequent call fails */ - ret = extract_command_stdin(args); - if (ret != EOK) { - goto fail; - } - } - } - - return EOK; - -fail: - scl_args_free(args); - return ret; -} - -/** - * Frees struct scl_args - */ -void scl_args_free(struct scl_args *args) -{ - if (args->collections != NULL) { - free(args->collections); - } - if (args->colpaths != NULL) { - free(args->colpaths); - } - if (args->command != NULL) { - free(args->command); - args->command = NULL; - } -} - -int scl_args_get(int argc, char *argv[], struct scl_args **_args) -{ - struct scl_args *args = NULL; - char **shebang_argv = NULL; - int shebang_argc; - int ret; - - if (argc < 2) { - return EINPUT; - } - - if (argc >= 3 && strchr(argv[1], ' ') != NULL) { - /* Apparently a shebang line */ - - ret = parse_shebang(argc, argv, &shebang_argc, &shebang_argv); - if (ret != EOK) { - goto fail; - } - - argc = shebang_argc; - argv = shebang_argv; - } - - args = xcalloc(1, sizeof(*args)); - - if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) { - args->action = ACTION_NONE; - } else if ( argc == 2 && ( - !strcmp(argv[1], "list-collections") || - !strcmp(argv[1], "--list") || - !strcmp(argv[1], "-l"))) { - - if (strcmp(argv[1], "list-collections")) { - debug("You use deprecated syntax \"-l|--list\", use \"list-collections\" instead.\n"); - } - args->action = ACTION_LIST_COLLECTIONS; - - } else if (!strcmp(argv[1], "list-packages") || - !strcmp(argv[1], "--list") || - !strcmp(argv[1], "-l")) { - - if (strcmp(argv[1], "list-packages")) { - debug("You use deprecated syntax \"-l|--list\", use \"list-packages\" instead.\n"); - } - - if (argc < 3) { - ret = EINPUT; - goto fail; - } else { - args->action = ACTION_LIST_PACKAGES; - ret = extract_collections(argc, argv, args); - if (ret != EOK) { - goto fail; - } - } - } else if (!strcmp(argv[1], "register")) { - args->action = ACTION_REGISTER; - if (argc < 3) { - ret = EINPUT; - goto fail; - } - ret = extract_colpaths(argc, argv, args); - if (ret != EOK) { - goto fail; - } - } else if (!strcmp(argv[1], "deregister")) { - int i, i2; - - args->action = ACTION_DEREGISTER; - - for (i = 2; i < argc; i++) { - if (!strcmp(argv[i], "--force") || !strcmp(argv[i], "-f")) { - args->force_flag = true; - break; - } - } - - /* Remove -f / --force from argv */ - if (args->force_flag) { - for (i2 = i; i2 < argc - 1; i2++) { - argv[i2] = argv[i2 + 1]; - } - argc--; - } - - if (argc < 3) { - ret = EINPUT; - goto fail; - } - - ret = extract_collections(argc, argv, args); - if (ret != EOK) { - goto fail; - } - } else if (!strcmp(argv[1], "run") || !strcmp(argv[1], "enable")) { - int i, i2; - - args->action = ACTION_COMMAND; - - for (i = 2; i < argc; i++) { - if (!strcmp(argv[i], "--exec") || !strcmp(argv[i], "-x")) { - args->exec_flag = true; - break; - } - } - - /* Remove -x / --exec from argv */ - if (args->exec_flag) { - for (i2 = i; i2 < argc - 1; i2++) { - argv[i2] = argv[i2 + 1]; - } - argc--; - } - - if (argc < 4) { - ret = EINPUT; - goto fail; - } - - ret = parse_run_args(argc, argv, args); - if (ret != EOK) { - goto fail; - } - } else if (!strcmp(argv[1], "load")) { - /* Just a list of collections */ - args->action = ACTION_LOAD; - ret = extract_collections(argc, argv, args); - if (ret != EOK) { - goto fail; - } - } else if (!strcmp(argv[1], "unload")) { - /* Just a list of collections */ - args->action = ACTION_UNLOAD; - ret = extract_collections(argc, argv, args); - if (ret != EOK) { - goto fail; - } - } else if (!strcmp(argv[1], "man")) { - /* Just one collection */ - args->action = ACTION_MAN; - if (argc < 3) { - ret = EINPUT; - goto fail; - } else if (argc > 3) { - debug("Only the first collection will be taken into account!\n"); - } - ret = extract_collections(3, argv, args); - if (ret != EOK) { - goto fail; - } - } else if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")) { - args->action = ACTION_VERSION; - } else if (!strcmp(argv[1], "list-enabled")) { - args->action = ACTION_LIST_ENABLED; - } else { - ret = EINPUT; - goto fail; - } - - free(shebang_argv); - *_args = args; - - return EOK; - -fail: - free(args); - free(shebang_argv); - return ret; -} diff --git a/src/args.h b/src/args.h deleted file mode 100644 index 0879fc8..0000000 --- a/src/args.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __ARGS_H__ -#define __ARGS_H__ - -#include "scllib.h" - -#define ACTION_NONE 0 -#define ACTION_LIST_COLLECTIONS 1 -#define ACTION_LIST_PACKAGES 2 -#define ACTION_COMMAND 3 -#define ACTION_REGISTER 4 -#define ACTION_DEREGISTER 5 -#define ACTION_MAN 6 -#define ACTION_LOAD 7 -#define ACTION_UNLOAD 8 -#define ACTION_VERSION 9 -#define ACTION_LIST_ENABLED 10 - -struct scl_args { - int action; - char **collections; /* collection info array */ - char **colpaths; /* paths to collections */ - char *command; /* command to be run */ - - bool force_flag; /* if set, the collection deregistration was forced */ - bool exec_flag; /* if set, exec() is used insted of system() to run command */ -}; - -void scl_args_free(struct scl_args *args); -int scl_args_get(int argc, char *argv[], struct scl_args **_args); - -#endif diff --git a/src/config.h.cmake b/src/config.h.cmake deleted file mode 100644 index 3bce2de..0000000 --- a/src/config.h.cmake +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define SCL_PRG_NAME "@PRG_NAME@" -#define SCL_MODULES_PATH "@MODULES_PATH@" -#define MODULE_CMD "@MODULE_CMD@" -#define SCL_CONF_DIR "@CONF_DIR@" -#define SCL_VERSION "@scl_VERSION@" - -#endif diff --git a/src/debug.c b/src/debug.c deleted file mode 100644 index 0df06da..0000000 --- a/src/debug.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -#include "debug.h" - -FILE *debug_file; - -void debug(const char *format, ...) -{ - va_list ap; - - va_start(ap, format); - - vfprintf(debug_file ? debug_file : stderr, format, ap); - fflush(debug_file ? debug_file : stderr); - - va_end(ap); -} diff --git a/src/debug.h b/src/debug.h deleted file mode 100644 index 97b4340..0000000 --- a/src/debug.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __DEBUG_H__ -#define __DEBUG_H__ - -void debug(const char *format, ...); - -#endif diff --git a/src/errors.h b/src/errors.h deleted file mode 100644 index 3063e92..0000000 --- a/src/errors.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __SCL_H__ -#define __SCL_H__ - -typedef enum { - EOK, /* everything ok */ - EMEM, /* memory problems (like ENOMEM) */ - EINPUT, /* unexpected input value */ - EDISK, /* some disk operation failed */ - ECONFIG, /* some problem with scl configuration */ - ERUN, /* some problem with running external program */ - ERPMLIB, /* some occurred in rpm library */ - ESYS, /* unexpected system error, e. g. unexpected fail of function from stdlib */ -} scl_rc; - -#endif diff --git a/src/fallback.c b/src/fallback.c deleted file mode 100644 index e67654a..0000000 --- a/src/fallback.c +++ /dev/null @@ -1,252 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "scllib.h" -#include "sclmalloc.h" -#include "config.h" -#include "errors.h" -#include "debug.h" -#include "lib_common.h" - -bool has_old_collection(char * const colnames[]) -{ - char *module_file_link = NULL; - - while (*colnames != NULL) { - xasprintf(&module_file_link, SCL_MODULES_PATH "/%s", *colnames); - if (access(module_file_link, F_OK)) { - module_file_link = _free(module_file_link); - return true; - } - module_file_link = _free(module_file_link); - colnames++; - } - return false; -} - -scl_rc fallback_get_enabled_collections(char ***_enabled_collections) -{ - char **enabled_collections = NULL; - char *X_SCLS = getenv("X_SCLS"); - - if (X_SCLS != NULL) { - X_SCLS = xstrdup(X_SCLS); - enabled_collections = split(X_SCLS, ' '); - - for (int i = 0; enabled_collections[i] != NULL; i++) { - enabled_collections[i] = xstrdup(enabled_collections[i]); - } - } - X_SCLS = _free(X_SCLS); - *_enabled_collections = enabled_collections; - - return EOK; -} - -bool fallback_is_collection_enabled(const char *colname) -{ - bool ret = false; - char **enabled_collections = NULL; - - ret = fallback_get_enabled_collections(&enabled_collections); - - if (ret == EOK && enabled_collections != NULL) { - for (int i = 0; enabled_collections[i] != NULL; i++) { - if (!strcmp(enabled_collections[i], colname)) { - ret = true; - break; - } - } - - } - enabled_collections = free_string_array(enabled_collections); - - return ret; -} - -/* - * See function collection_exists() - */ -scl_rc fallback_collection_exists(const char *colname, bool *_exists) -{ - char *col_path = NULL; - char *conf_file = NULL; - scl_rc ret = EOK; - - xasprintf(&conf_file, SCL_CONF_DIR "/%s", colname); - *_exists = !access(conf_file, F_OK); - - if (*_exists) { - ret = get_collection_path(colname, &col_path); - if (ret != EOK) { - return ret; - } - - *_exists = !access(col_path, F_OK); - - } - - conf_file = _free(conf_file); - col_path = _free(col_path); - - return EOK; -} - -scl_rc fallback_get_installed_collections(char ***_colnames) -{ - struct dirent **nl; - int n, i, i2 = 0; - char **colnames; - bool col_exists; - scl_rc ret = EOK; - - n = scandir(SCL_CONF_DIR, &nl, 0, alphasort); - if (n < 0) { - debug("Cannot list directory %s: %s\n", SCL_CONF_DIR, strerror(errno)); - return EDISK; - } - - /* Add one item for terminator */ - colnames = xcalloc(n + 1, sizeof(*colnames)); - - for (i = 0; i < n; i++) { - if (nl[i]->d_name[0] != '.') { - ret = fallback_collection_exists(nl[i]->d_name, &col_exists); - if (ret != EOK) { - goto exit; - } - - if (col_exists) { - colnames[i2++] = nl[i]->d_name; - } - } - } - - for (i = 0; i < i2; i++) { - colnames[i] = xstrdup(colnames[i]); - } - - colnames[i2] = NULL; - *_colnames = colnames; - -exit: - if (ret != EOK) { - for (i = 0; i < i2; i++) { - colnames[i] = _free(colnames[i]); - } - colnames = _free(colnames); - } - - for (i = 0; i < n; i++) { - nl[i] = _free(nl[i]); - } - nl = _free(nl); - - return ret; -} - -scl_rc fallback_run_command(char * const colnames[], const char *cmd, bool exec) -{ - scl_rc ret = EOK; - char tmp[] = "/var/tmp/sclXXXXXX"; - int tfd; - int status; - FILE *tfp = NULL; - char *colname; - char *colpath = NULL; - char *enable_path = NULL; - char *bash_cmd = NULL;; - - const char *script_body = - "SCLS+=(%s)\n" - "export X_SCLS=$(printf '%%q ' \"${SCLS[@]}\")\n" - ". %s\n"; - - tfd = mkstemp(tmp); - if (tfd < 0) { - debug("Cannot create a temporary file %s: %s\n", tmp, strerror(errno)); - return EDISK; - } - tfp = fdopen(tfd, "w"); - if (tfp == NULL) { - debug("Cannot open a temporary file %s: %s\n", tmp, strerror(errno)); - close(tfd); - return EDISK; - } - - if (fprintf(tfp, "eval \"SCLS=( ${X_SCLS[*]} )\"\n") < 0) { - debug("Cannot write to a temporary file %s: %s\n", tmp, - strerror(errno)); - ret = EDISK; - goto exit; - } - - while (*colnames != NULL) { - colname = *colnames; - - if (fallback_is_collection_enabled(colname)) { - colnames++; - continue; - } - - ret = get_collection_path(colname, &colpath); - if (ret != EOK) { - goto exit; - } - xasprintf(&enable_path, "%s/enable", colpath); - - if (fprintf(tfp, script_body, colname, enable_path) < 0) { - debug("Cannot write to a temporary file %s: %s\n", tmp, - strerror(errno)); - ret = EDISK; - goto exit; - } - enable_path = _free(enable_path); - colpath = _free(colpath); - colnames++; - } - - if (exec) { - fprintf(tfp, "exec %s\n", cmd); - } else { - fprintf(tfp, "%s\n", cmd); - } - fclose(tfp); - tfp = NULL; - - if (exec) { - execl("/bin/bash", "/bin/bash", tmp, NULL); - debug("Problem with executing program %s: %s\n", "/bin/bash", strerror(errno)); - ret = ERUN; - - } else { - xasprintf(&bash_cmd, "/bin/bash %s", tmp); - status = system(bash_cmd); - if (status == -1 || !WIFEXITED(status)) { - if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) - goto exit; - debug("Problem with executing command \"%s\"\n", bash_cmd); - ret = ERUN; - goto exit; - } - ret = WEXITSTATUS(status); - } - -exit: - if (tfp != NULL) { - fclose(tfp); - } - enable_path = _free(enable_path); - colpath = _free(colpath); - bash_cmd = _free(bash_cmd); - unlink(tmp); - - return ret; -} diff --git a/src/fallback.h b/src/fallback.h deleted file mode 100644 index 6bb1157..0000000 --- a/src/fallback.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef __FALLBACK_H__ -#define __FALLBACK_H__ - -#include - -bool has_old_collection(char * const colnames[]); -bool fallback_is_collection_enabled(const char *colname); -scl_rc fallback_run_command(char * const colnames[], const char *cmd, bool exec); -scl_rc fallback_get_enabled_collections(char ***_colnames); -scl_rc fallback_get_installed_collections(char ***_colnames); -scl_rc fallback_collection_exists(const char *colname, bool *_exists); - -#endif diff --git a/src/lib_common.c b/src/lib_common.c deleted file mode 100644 index 2e7d116..0000000 --- a/src/lib_common.c +++ /dev/null @@ -1,309 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "errors.h" -#include "scllib.h" -#include "debug.h" -#include "sclmalloc.h" -#include "lib_common.h" - -char *get_command_output(const char *path, char *const argv[], int fileno) -{ - int pid = 0, status; - int outpipe[2] = {0, 0}; - FILE *fpipe = NULL; - int c; - char *buffer = NULL; - int count = 0, alloced = 5; - int error = 1; - - if (pipe(outpipe) == -1) { - goto exit; - } - - pid = fork(); - if (pid == -1) { - goto exit; - } else if (pid == 0) {/* Child */ - close(outpipe[0]); - if (dup2(outpipe[1], fileno) == -1) { - goto exit; - } - execv(path, argv); - exit(EXIT_FAILURE); - } - - close(outpipe[1]); - fpipe = fdopen(outpipe[0], "r"); - if (fpipe == NULL){ - goto exit; - } - outpipe[0] = 0; - buffer = xmalloc(alloced); - - while (1) { - c = fgetc(fpipe); - if (ferror(fpipe) != 0) { - goto exit; - } - count++; - if (count > alloced) { - alloced <<=1; - buffer = xrealloc(buffer, alloced); - } - if (c != EOF) { - buffer[count - 1] = c; - } else { - buffer[count - 1] = '\0'; - break; - } - } - error = 0; - -exit: - if (outpipe[0]) { - close(outpipe[0]); - } - - if (outpipe[1]) { - close(outpipe[1]); - } - - if (fpipe) { - fclose(fpipe); - } - - if (pid > 0) { - waitpid(pid, &status, 0); - if (!WIFEXITED(status)) { - debug("Program %s didn't terminate normally!\n", path); - error = 1; - }else if (WEXITSTATUS(status)) { - debug("Program %s returned nonzero return code!\n", path); - error = 1; - } - } - - if (error) { - buffer = _free(buffer); - } - - return buffer; -} - -scl_rc prepare_args(const char *cmd, char ***_argv) -{ - wordexp_t p; - char **argv; - int i; - - if (wordexp(cmd, &p, 0) != 0) { - debug("Problem occurred during parsing command!!!\n"); - return EINPUT; - } - argv = xmalloc((p.we_wordc + 1) * sizeof(*argv)); - - for (i = 0; i < p.we_wordc; i++) { - argv[i] = xstrdup(p.we_wordv[i]); - } - argv[i] = NULL; - wordfree(&p); - - *_argv = argv; - - return EOK; -} - -int count_words(const char *str, char ch) -{ - int count = 0; - bool inside_word = false; - - while (*str != '\0') { - if (*str != ch) { - if (!inside_word) { - count++; - } - inside_word = true; - } else { - inside_word = false; - } - str++; - } - - return count; -} - -void strip_trailing_chars(char *str, char char_to_strip) -{ - for (int i = strlen(str) - 1; i >=0; i--) { - if (str[i] == char_to_strip) { - str[i] = '\0'; - } else { - break; - } - } -} - -void unescape_string(char *str) -{ - int str_len = strlen(str); - int si, ti = 0; - - for (si = 0; si < str_len; si++) { - if (str[si] == '\\') { - si++; - if (si == str_len) { - break; - } - } - - str[ti++] = str[si]; - } - str[ti] = '\0'; -} - - -char **split(char *str, char delim) -{ - char **parts, *p; - int word_count, i = 0; - char delim_str[2]; - - delim_str[0] = delim; - delim_str[1] = '\0'; - - word_count = count_words(str, delim); - parts = xmalloc((word_count + 1) * sizeof(*parts)); - - p = strtok(str, delim_str); - while (p != NULL) { - parts[i++] = p; - p = strtok(NULL, delim_str); - } - parts[i] = NULL; - - return parts; -} - -/** - * Prints content of the array and frees the memory. - * @param array Array to print. - */ -void print_string_array(char *const *array) { - int i = 0; - if (array == NULL) { - return; - } - while (array[i] != NULL) { - printf("%s\n", array[i++]); - } -} - -int string_array_len(char *const *array) -{ - int i = 0; - - if (array == NULL) { - return 0; - } - while (array[i] != NULL) { - i++; - } - - return i; -} - -static int cmpstringp(const void *p1, const void *p2) -{ - return strcmp(* (char * const *) p1, * (char * const *) p2); -} - -char **merge_string_arrays(char *const *array1, char *const *array2) -{ - char **merged_array = NULL; - int len, ix = 0; - char *const *arrays[2] = {array1, array2}; - int array_len[2]; - int prev; - - array_len[0] = string_array_len(array1); - array_len[1] = string_array_len(array2); - len = array_len[0] + array_len[1]; - if (len == 0) { - return NULL; - } - - merged_array = xmalloc((len + 1) * sizeof(*merged_array)); - - /* Put content of two arrays into one array */ - for (int i = 0; i < 2; i++) { - /* If any of arrays is NULL then skip it */ - if (arrays[i] == NULL) { - continue; - } - - for (int i2 = 0; i2 < array_len[i]; i2++) { - merged_array[ix++] = arrays[i][i2]; - } - } - - /* Sort resulting array */ - if (len > 1) { - qsort(merged_array, len, sizeof(*merged_array), cmpstringp); - } - - /* Unique the array */ - prev = 0; - for (int i = 1; i < len; i++) { - if (strcmp(merged_array[prev], merged_array[i])) { - merged_array[++prev] = merged_array[i]; - } - } - merged_array[++prev] = NULL; - - for (int i = 0; i < prev; i++) { - merged_array[i] = xstrdup(merged_array[i]); - } - - return merged_array; -} - -/** - * Frees string array - * @param[in] count Number of items in array. - * @param[out] array String array, that will be freed. - */ -void *free_string_array(char **array) { - int i = 0; - - if (array != NULL) { - while (array[i] != NULL) { - free(array[i++]); - } - free(array); - } - return NULL; -} - -char *directory_name(const char *_path) -{ - char *path, *c; - - path = xstrdup(_path); - c = path + strlen(path) - 1; - while ( *c != '/' ) { - *c = '\0'; - c--; - } - - return path; -} diff --git a/src/lib_common.h b/src/lib_common.h deleted file mode 100644 index 068ffbc..0000000 --- a/src/lib_common.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __LIB_COMMON_H__ -#define __LIB_COMMON_H__ - -#include "errors.h" - -char *get_command_output(const char *path, char *const argv[], int fileno); -scl_rc prepare_args(const char *cmd, char ***_argv); -int count_words(const char *str, char ch); -void unescape_string(char *str); -void strip_trailing_chars(char *str, char char_to_strip); -char **split(char *str, char delim); -void print_string_array(char *const *array); -void *free_string_array(char **array); -char *strip_trailing_slashes(const char *path_to_strip); -char *directory_name(const char *_path); -char **merge_string_arrays(char *const *array1, char *const *array2); - -#endif diff --git a/src/scl.c b/src/scl.c deleted file mode 100644 index df8111a..0000000 --- a/src/scl.c +++ /dev/null @@ -1,137 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#include "errors.h" -#include "scllib.h" -#include "args.h" -#include "lib_common.h" -#include "debug.h" -#include "fallback.h" - -/** - * Prints help on stderr. - * @param name Name of the executed file. - */ -static void print_usage( const char *name ) { - fprintf(stderr, "usage: %s enable|run [...] \n", basename(name)); - fprintf(stderr, " %s load|unload [...]\n", basename(name)); - fprintf(stderr, " %s list-collections\n", basename(name)); - fprintf(stderr, " %s list-packages|man|register|deregister \n", basename(name)); - fprintf(stderr, " %s --help\n\n", basename(name)); - - fprintf(stderr, - "description:\n" - " enable|run run given command from Software Collection \n" - " load|unload enable/disable collection in current shell\n" - " list-collections list installed Software Collections\n" - " list-enabled list Software Collections enabled in current shell\n" - " list-packages list packages in Software Collection\n" - " man show manual page about Software Collection\n" - " register|deregister register/deregister Software Collection\n" - " --help show this help\n" - "\nUse '-' as to read the command from standard input.\n"); -} - - -int main(int argc, char *argv[]) { - int ret = EOK; - struct scl_args *args; - char **pkgs = NULL; - char **colls = NULL, **colls2 = NULL, **colls_merged = NULL; - - ret = scl_args_get(argc, argv, &args); - if (ret == EINPUT) { - fprintf(stderr, "Wrong arguments specified!\n\n"); - print_usage(argv[0]); - - return ret; - } else if (ret != EOK) { - fprintf(stderr, "Unspecified error when parsing arguments!\n\n"); - - return ret; - } - - - switch (args->action) { - case ACTION_NONE: - print_usage(argv[0]); - break; - - case ACTION_LIST_COLLECTIONS: - ret = fallback_get_installed_collections(&colls); - if (ret == EOK) { - print_string_array(colls); - } - break; - - case ACTION_LIST_PACKAGES: - ret = list_packages_in_collection(args->collections[0], &pkgs); - if (ret == EOK) { - print_string_array(pkgs); - } - break; - - case ACTION_COMMAND: - if (has_old_collection(args->collections)) { - ret = fallback_run_command(args->collections, args->command, args->exec_flag); - } else { - ret = run_command(args->collections, args->command, args->exec_flag); - } - break; - case ACTION_REGISTER: - ret = register_collection(args->colpaths[0]); - break; - case ACTION_DEREGISTER: - ret = deregister_collection(args->collections[0], args->force_flag); - break; - case ACTION_MAN: - ret = show_man(args->collections[0]); - break; - case ACTION_LOAD: - case ACTION_UNLOAD: - debug("Missing function scl in your environment!!!\n"); - ret = ECONFIG; - break; - case ACTION_VERSION: - printf("%s\n", get_version()); - break; - case ACTION_LIST_ENABLED: - /* Get enabled collections of new type */ - ret = get_enabled_collections(&colls); - if (ret != EOK) { - break; - } - /* Get enabled collections of old type */ - ret = fallback_get_enabled_collections(&colls2); - if (ret != EOK) { - break; - } - - /* - * Some new collections can behave like old collections by defining - * variable X_SCLS. There may be intersection between colls and - * colls2. So merge them. - */ - colls_merged = merge_string_arrays(colls, colls2); - print_string_array(colls_merged); - - break; - } - - scl_args_free(args); - free(args); - free_string_array(pkgs); - free_string_array(colls); - free_string_array(colls2); - free_string_array(colls_merged); - release_scllib_cache(); - return ret; -} diff --git a/src/scllib.c b/src/scllib.c deleted file mode 100644 index 2ba8df8..0000000 --- a/src/scllib.c +++ /dev/null @@ -1,706 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "errors.h" -#include "scllib.h" -#include "debug.h" -#include "lib_common.h" -#include "sclmalloc.h" -#include "fallback.h" -#include "ctype.h" - -char **installed_collections = NULL; - -static scl_rc initialize_env() -{ - char *module_path = getenv("MODULEPATH"); - char *new_module_env; - - if (!module_path || !strstr(module_path, SCL_MODULES_PATH)) { - - if (module_path) { - xasprintf(&new_module_env, "MODULEPATH=" SCL_MODULES_PATH ":%s", - module_path); - } else { - xasprintf(&new_module_env, "MODULEPATH=" SCL_MODULES_PATH); - } - - if(putenv(new_module_env) != 0) { - debug("Impossible to create environment variable %s: %s\n", - new_module_env, strerror(errno)); - return ESYS; - } - } - - return EOK; -} - -static scl_rc get_env_vars(const char *colname, char ***_vars) -{ - char *argv[] = {MODULE_CMD, MODULE_CMD, "sh", "add", "", NULL}; - char *output = NULL; - int i = 0; - char **parts, *part, **vars; - scl_rc ret = EOK; - - ret = initialize_env(); - if (ret != EOK) { - return ret; - } - - argv[4] = (char *) colname; - output = get_command_output(argv[0], argv + 1, STDOUT_FILENO); - if (output == NULL) { - debug("Problem with executing program %s: %s\n", argv[0], - strerror(errno)); - ret = ERUN; - goto exit; - } - - /* - * Expected format of string stored in variable output is following: - * var1=value1 ;export value1 ; var2=value2 ;export value2; - * var3=value\ with\ spaces - * NOTE: Newer (tcl-based) versions of MODULE_CMD put a newline after each - * export command so we need to take that into account. - */ - - vars = parts = split(output, ';'); - - /* Filter out strings without "=" i. e. strings with export. */ - while (*parts != NULL) { - part = *parts; - if (part[0] == '\n') - part++; - if (strchr(part, '=')) { - strip_trailing_chars(part, ' '); - unescape_string(part); - vars[i++] = xstrdup(part); - } - parts++; - } - vars[i] = NULL; - *_vars = vars; - -exit: - output = _free(output); - return ret; -} -scl_rc get_enabled_collections(char ***_enabled_collections) -{ - char **enabled_collections = NULL; - char *lm_files = getenv("_LMFILES_"); - - if (lm_files != NULL) { - lm_files = xstrdup(lm_files); - enabled_collections = split(lm_files, ':'); - - for (int i = 0; enabled_collections[i] != NULL; i++) { - if (!strncmp(SCL_MODULES_PATH, enabled_collections[i], - sizeof(SCL_MODULES_PATH - 1))){ - - enabled_collections[i] += sizeof(SCL_MODULES_PATH); - } - enabled_collections[i] = xstrdup(enabled_collections[i]); - } - - } - lm_files = _free(lm_files); - *_enabled_collections = enabled_collections; - return EOK; -} - -scl_rc get_installed_collections(char *const **_colnames) -{ - char *argv[] = {MODULE_CMD, "modulecmd", "sh", "-t", "avail", NULL}; - char *output = NULL, **lines = NULL; - int i2 = 0, take = 0; - scl_rc ret = EOK; - - if (installed_collections != NULL) { - *_colnames = installed_collections; - return EOK; - } - - ret = initialize_env(); - if (ret != EOK) { - return ret; - } - - output = get_command_output(argv[0], argv + 1, STDERR_FILENO); - if (output == NULL) { - debug("Problem with executing program %s: %s\n", argv[0], - strerror(errno)); - ret = ERUN; - goto exit; - } - /* - * Expected format of string stored in variable output is following: - * /usr/share/Modules/modulefiles: - * modulename1 - * modulename2 - * ... - * /etc/scl/modulefiles: - * sclmodulename1 - * sclmodulename2 - * ... - */ - - lines = split(output, '\n'); - - for (int i = 0; lines[i] != NULL; i++) { - /* Line with path to modules on following lines */ - if (strchr(lines[i], ':') != NULL) { - - /* We want only modules with path /etc/scl/modulefiles */ - if (!strcmp(SCL_MODULES_PATH ":", lines[i])) { - take = 1; - } else { - take = 0; - } - - /* Line with module name */ - } else { - if (take) { - lines[i2++] = xstrdup(lines[i]); - } - } - } - lines[i2] = NULL; - - installed_collections = lines; - *_colnames = installed_collections; - -exit: - output = _free(output); - - return ret; -} - -/* - * Return true in output parameter _exists if a collection given by parameter - * colname exists. This function works only for new type of collections i. e. - * collections containing module file. In other words it returns true only - * when a given collection exists and the collection is collection of new - * type otherwise it returns false. - * - * There is also function fallback_collection_exists() which - * returns true for all existing collections no matter of their types. - */ -static scl_rc collection_exists(const char *colname, bool *_exists) -{ - int ret = EOK; - char *const *collnames = NULL; - bool exists = false; - - ret = get_installed_collections(&collnames); - if (ret != EOK) { - return ret; - } - - for (int i = 0; collnames[i] != NULL; i++) { - if (!strcmp(collnames[i], colname)) { - exists = true; - break; - } - } - *_exists = exists; - - return ret; -} - -scl_rc get_collection_path(const char *colname, char **_colpath) -{ - FILE *fp = NULL; - char *file_path = NULL; - char *prefix = NULL; - char *colpath = NULL; - struct stat st; - scl_rc ret = EOK; - - xasprintf(&file_path, "%s%s", SCL_CONF_DIR, colname); - - if (stat(file_path, &st) != 0) { - debug("Unable to get file status %s: %s\n", file_path, strerror(errno)); - ret = EDISK; - goto exit; - } - - fp = fopen(file_path, "r"); - if (fp == NULL) { - debug("Unable to open file %s: %s\n", file_path, strerror(errno)); - ret = EDISK; - goto exit; - } - - prefix = xcalloc(st.st_size + 1, 1); - if (fread(prefix, st.st_size, 1, fp) != 1) { - debug("Unable to read file %s: %s\n", file_path, strerror(errno)); - ret = EDISK; - goto exit; - } - - for (int i = st.st_size - 1; i >= 0; i--) { - if (isspace(prefix[i]) || prefix[i] == '/') { - prefix[i] = '\0'; - } else { - break; - } - } - - xasprintf(&colpath, "%s/%s", prefix, colname); - - *_colpath = colpath; - -exit: - if (ret != EOK) { - colpath = _free(colpath); - } - - if (fp != NULL) { - fclose(fp); - } - file_path = _free(file_path); - prefix = _free(prefix); - - return ret; -} - -scl_rc run_command(char * const colnames[], const char *cmd, bool exec) -{ - char **argv = NULL, **envs = NULL, *env = NULL; - bool exists; - scl_rc ret = EOK; - int status; - - ret = initialize_env(); - if (ret != EOK) { - return ret; - } - - while (*colnames != NULL) { - if (fallback_is_collection_enabled(*colnames)) { - colnames++; - continue; - } - - ret = collection_exists(*colnames, &exists); - if (ret != EOK) { - goto exit; - } - if (!exists) { - debug("Collection %s doesn't exists!\n", *colnames); - ret = EINPUT; - goto exit; - } - - ret = get_env_vars(*colnames, &envs); - if (ret != EOK) { - goto exit; - } - - for (int i = 0; envs[i] != NULL; i++) { - env = xstrdup(envs[i]); - if(putenv(env) != 0) { - env = _free(env); - debug("Impossible to create environment variable %s: %s\n", - envs[i]); - ret = ESYS; - goto exit; - } - } - envs = free_string_array(envs); - colnames++; - } - - if (exec) { - /* Use function system */ - - ret = prepare_args(cmd, &argv); - if (ret != EOK) { - goto exit; - } - execvp(argv[0], argv); - debug("Problem with executing program %s: %s\n", argv[0], strerror(errno)); - ret = ERUN; - - } else { - /* Use function system */ - - status = system(cmd); - if (status == -1 || !WIFEXITED(status)) { - if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) - goto exit; - debug("Problem with executing program \"%s\"\n", cmd); - ret = ERUN; - goto exit; - } - ret = WEXITSTATUS(status); - } - - -exit: - argv = free_string_array(argv); - envs = free_string_array(envs); - - return ret; -} - -scl_rc list_packages_in_collection(const char *colname, char ***_pkgnames) -{ - char **srpms, **rpms; - int srpms_allocated = 10, rpms_allocated = 10; - int srpms_count = 0, rpms_count = 0; - - rpmts ts = NULL; - rpmdbMatchIterator mi = NULL; - Header h = NULL; - char *provide = NULL; - scl_rc ret = EOK; - bool exists; - const char *srpm; - - ret = fallback_collection_exists(colname, &exists); - if (ret != EOK) { - return ret; - } - - if (!exists) { - debug("Collection %s doesn't exists!\n", colname); - return EINPUT; - } - - if (rpmReadConfigFiles(NULL, NULL) == -1) { - debug("Error occurred in rpmlib!\n"); - return ERPMLIB; - } - - srpms = xmalloc(srpms_allocated * sizeof(*srpms)); - - xasprintf(&provide, "scl-package(%s)", colname); - - ts = rpmtsCreate(); - mi = rpmtsInitIterator(ts, RPMDBI_PROVIDENAME, provide, 0); - while ((h = rpmdbNextIterator(mi)) != NULL) { - - srpms[srpms_count++] = headerGetAsString(h, RPMTAG_SOURCERPM); - - if (srpms_count == srpms_allocated) { - srpms_allocated <<= 1; - srpms = xrealloc(srpms, srpms_allocated * sizeof(*srpms)); - } - } - srpms[srpms_count] = NULL; - mi = rpmdbFreeIterator(mi); - provide = _free(provide); - - rpms = xmalloc(rpms_allocated * sizeof(*rpms)); - mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0); - while ((h = rpmdbNextIterator(mi)) != NULL) { - - srpm = headerGetString(h, RPMTAG_SOURCERPM); - - for (int i = 0; i < srpms_count; i++) { - if (!strcmp(srpm, srpms[i])) { - rpms[rpms_count++] = headerGetAsString(h, RPMTAG_NEVRA); - break; - } - } - - if (rpms_count == rpms_allocated) { - rpms_allocated <<= 1; - rpms = xrealloc(rpms, rpms_allocated * sizeof(*rpms)); - } - } - rpms[rpms_count] = NULL; - mi = rpmdbFreeIterator(mi); - ts = rpmtsFree(ts); - srpms = free_string_array(srpms); - - *_pkgnames = rpms; - return ret; -} - -static scl_rc run_scriptlet(const char *program_dir, const char *program_name) -{ - char *program = NULL; - int status, pid = 0; - scl_rc ret = EOK; - - xasprintf(&program, "%s/%s", program_dir, program_name); - - if (!access(program, F_OK)) { - pid = fork(); - if (pid == -1) { - ret = ERUN; - debug("Fork failed: %s\n", strerror(errno)); - goto exit; - } else if (pid == 0) {/* Child */ - execl(program, program, NULL); - exit(EXIT_FAILURE); - } - - waitpid(pid, &status, 0); - if (!WIFEXITED(status)) { - debug("Program %s didn't terminate normally!\n", program); - ret = ERUN; - goto exit; - } - if (WEXITSTATUS(status)) { - debug("Program %s returned nonzero return code!\n", program); - ret = ERUN; - goto exit; - } - } - -exit: - program = _free(program); - return ret; -} - -scl_rc register_collection(const char *_colpath) -{ - scl_rc ret = EOK; - char *colname, *colpath = NULL, *colroot = NULL; - char *module_file = NULL, *module_file_link = NULL; - char *enable_script = NULL, *conf_file = NULL; - char *prefix = NULL; - FILE *f = NULL; - bool exists; - - colpath = xstrdup(_colpath); - strip_trailing_chars(colpath, '/'); - colname = basename(colpath); - prefix = directory_name(colpath); - - xasprintf(&module_file, "%s/%s", colpath, colname); - xasprintf(&enable_script, "%s/enable", colpath); - xasprintf(&module_file_link, SCL_MODULES_PATH "/%s", colname); - xasprintf(&conf_file, SCL_CONF_DIR "/%s", colname); - xasprintf(&colroot, "%s/root", colpath); - - if (access(enable_script, F_OK) == -1 || access(colroot, F_OK) == -1) { - debug("Collection %s is not valid! File %s or file %s doesn't exists: %s\n", - colname, enable_script, colroot, strerror(errno)); - ret = EINPUT; - goto exit; - } - - ret = fallback_collection_exists(colname, &exists); - if (ret != EOK) { - goto exit; - } - if (exists) { - debug("Collection %s has already been registered!\n", colname); - ret = EINPUT; - goto exit; - } - - f = fopen(conf_file, "w+"); - if (f == NULL) { - debug("Unable to open file %s: %s\n", conf_file, strerror(errno)); - ret = EDISK; - goto exit; - } - if (fprintf(f, "%s\n", prefix) < 0) { - debug("Unable to write to file %s: %s\n", conf_file, strerror(errno)); - ret = EDISK; - goto exit; - } - fclose(f); - f = NULL; - - if (access(module_file, F_OK) == 0) { - if (symlink(module_file, module_file_link) == -1) { - debug("Unable to create symlink %s to file %s: %s\n", - module_file_link, module_file, strerror(errno)); - ret = EDISK; - goto exit; - } - } - - ret = run_scriptlet(colpath, "register"); - if (ret != EOK) { - if (unlink(conf_file)) { - debug("Unable to remove file %s: %s\n", conf_file, - strerror(errno)); - debug("Remove this file manually before a new try to register collection\n"); - } - - if (access(module_file_link, F_OK) == 0) { - if (unlink(module_file_link)) { - debug("Unable to remove file %s: %s\n", module_file_link, - strerror(errno)); - debug("Remove this file manually before a new try to register collection\n"); - } - } - } - -exit: - if (f != NULL) { - fclose(f); - f = NULL; - } - colpath = _free(colpath); - module_file = _free(module_file); - module_file_link = _free(module_file_link); - enable_script = _free(enable_script); - prefix = _free(prefix); - - return ret; -} - -static scl_rc owned_by_package(const char *file_path, bool *_owned) -{ - rpmts ts; - rpmdbMatchIterator mi; - scl_rc ret = EOK; - - if (rpmReadConfigFiles(NULL, NULL) == -1) { - debug("Error occurred in rpmlib!\n"); - return ERPMLIB; - } - - ts = rpmtsCreate(); - mi = rpmtsInitIterator(ts, RPMDBI_INSTFILENAMES, file_path, 0); - - *_owned = rpmdbGetIteratorCount(mi) > 0; - - mi = rpmdbFreeIterator(mi); - ts = rpmtsFree(ts); - - return ret; -} - -scl_rc deregister_collection(const char *colname, bool force) -{ - bool exists; - scl_rc ret = EOK; - char *module_file_link = NULL, *conf_file = NULL; - bool conf_file_owned = false, module_file_owned = false; - char *colpath; - - ret = fallback_collection_exists(colname, &exists); - if (ret != EOK) { - return ret; - } - if (!exists) { - debug("Collection %s doesn't exists!\n", colname); - return EINPUT; - } - - ret = get_collection_path(colname, &colpath); - if (ret != EOK) { - return ret; - } - - xasprintf(&module_file_link, SCL_MODULES_PATH "/%s", colname); - xasprintf(&conf_file, SCL_CONF_DIR "/%s", colname); - - if (!force) { - ret = owned_by_package(conf_file, &conf_file_owned); - if (ret != EOK) { - goto exit; - } - - ret = owned_by_package(module_file_link, &module_file_owned); - if (ret != EOK) { - goto exit; - } - - if (module_file_owned || conf_file_owned) { - debug("Unable to deregister collection %s: Collection was " - "installed as a package, you can use '--force' to" - "deregister it.\n", colname); - ret = EINPUT; - goto exit; - } - - } - - ret = run_scriptlet(colpath, "deregister"); - if (ret != EOK) { - goto exit; - } - - if (unlink(conf_file) == -1) { - debug("Unable to remove file %s: %s\n", conf_file, - strerror(errno)); - ret = ESYS; - goto exit; - } - - if (access(module_file_link, F_OK) == 0) { - if (unlink(module_file_link) == -1) { - debug("Unable to remove file %s: %s\n", module_file_link, - strerror(errno)); - ret = ESYS; - goto exit; - } - } - -exit: - module_file_link = _free(module_file_link); - colpath = _free(colpath); - return ret; -} - -scl_rc show_man(const char *colname) -{ - scl_rc ret = EOK; - char *colnames[] = {(char *) colname, NULL}; - char *cmd = NULL; - bool exists; - bool need_fallback = false; - - ret = collection_exists(colname, &exists); - if (ret != EOK) { - return ret; - } - - if (!exists) { - ret = fallback_collection_exists(colname, &exists); - if (ret != EOK) { - return ret; - } - need_fallback = true; - } - - if (!exists) { - debug("Collection %s doesn't exists!\n", colname); - return EINPUT; - } - - xasprintf(&cmd, "man %s", colname); - - if (need_fallback) { - ret = fallback_run_command(colnames, cmd, true); - } else { - ret = run_command(colnames, cmd, true); - } - - cmd = _free(cmd); - return ret; -} - -const char* get_version() { - return SCL_VERSION; -} - -void release_scllib_cache() -{ - installed_collections = free_string_array(installed_collections); -} diff --git a/src/scllib.h b/src/scllib.h deleted file mode 100644 index d7bd5cd..0000000 --- a/src/scllib.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef __SCLLIB_H__ -#define __SCLLIB_H__ - -#include -#include "errors.h" - -/* - * Runs specified command. - * @param[in] colnames Collections in which will be the command run. - * @param[in] cmd Command to run. - * @return EOK on succes otherwise err code - */ -scl_rc run_command(char *const colname[], const char *cmd, bool exec); - -/* - * Get collections enabled in current environment. - * @param[out] enabled_collections NULL-terminated array of char* - * @return EOK on succes otherwise err code - */ -scl_rc get_enabled_collections(char ***enabled_collections); - -/* - * Created array of installed collections. - * @param[out] _collnames NULL-terminated array of char* - * @return EOK on succes otherwise err code - */ -scl_rc get_installed_collections(char *const **colnames); - -/* - * Creates array of package names. - * @param[in] colname Name of inspected collection. - * @param[out] pkgnames NULL-terminated array of char* - * @return EOK on succes otherwise err code - */ -scl_rc list_packages_in_collection(const char *colname, char ***pkgnames); - -/* - * Register a new collection. - * @param[in] colpath Path to a valid collection directory structure. - * @return EOK on succes otherwise err code - */ -scl_rc register_collection(const char *colpath); - -/* - * Deregister a collection. - * @param[in] colname Name of collection to deregister. - * @param[in] force Force deregistration in case that collection - * was installed as a package - * @return EOK on succes otherwise err code - */ -scl_rc deregister_collection(const char *colname, bool force); - -/* - * Show manual page about collection. - * @param[in] colname Name of collection. - * @return EOK on succes otherwise err code - */ -scl_rc show_man(const char *colname); - -/* - * Get path where collection is located. - * @param[in] colname Name of collection. - * @param[out] _colpath Path where collection is located. - * @return EOK on succes otherwise err code - */ -scl_rc get_collection_path(const char *colname, char **_colpath); - -/* - * Return version of scl-utils as a string. - */ -const char* get_version(); - -/* - * Release scllib cache. It has to be called after work with scllib is done. - */ -void release_scllib_cache(); - -#endif diff --git a/src/sclmalloc.c b/src/sclmalloc.c deleted file mode 100644 index 10b14d1..0000000 --- a/src/sclmalloc.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include - -#include "debug.h" - -inline static void vmefail() -{ - debug("Memory allocation failed.\n"); - exit(EXIT_FAILURE); -} - -void *xmalloc(size_t size) -{ - register void *value; - value = malloc(size); - if (value == NULL) - vmefail(); - return value; -} - -void *xcalloc(size_t nmemb, size_t size) -{ - register void *value; - value = calloc(nmemb, size); - if (value == NULL) - vmefail(); - return value; -} - -void *xrealloc(void *ptr, size_t size) -{ - register void *value; - value = realloc(ptr, size); - if (value == NULL) - vmefail(); - return value; -} - -char *xstrdup(const char *str) -{ - size_t size = strlen(str) + 1; - char *newstr = (char *) malloc(size); - if (newstr == NULL) - vmefail(); - strcpy(newstr, str); - return newstr; -} - -int xasprintf(char **strp, const char *fmt, ...) -{ - va_list args; - int ret; - - va_start(args, fmt); - ret = vasprintf(strp, fmt, args); - if (ret == -1) - vmefail(); - va_end(args); - - return ret; -} - -void *_free(void *ptr) -{ - free(ptr); - return NULL; -} diff --git a/src/sclmalloc.h b/src/sclmalloc.h deleted file mode 100644 index 492ba5f..0000000 --- a/src/sclmalloc.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __SCLMALLOC_H__ -#define __SCLMALLOC_H__ - -void *xmalloc(size_t size); -void *xcalloc(size_t nmemb, size_t size); -void *xrealloc(void *ptr, size_t size); -void *_free(void *ptr); -char *xstrdup(const char *str); -int xasprintf(char **strp, const char *fmt, ...); - -#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index 73a392d..0000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -ENABLE_TESTING() -INCLUDE_DIRECTORIES(. ../src ${PROJECT_BINARY_DIR}/src ) -SET( CMAKE_C_FLAGS "-Wall -pedantic --std=gnu99 -D_GNU_SOURCE -g -fPIE -Wl,--wrap=get_command_output -Wl,--wrap=system -Wl,--wrap=putenv -Wl,--wrap=getenv" ) - - -SET(tested_sources ../src/scllib.c ../src/sclmalloc.c ../src/lib_common.c ../src/debug.c ../src/fallback.c) -SET(testing_sources test_scllib.c test_common.c dict.c) -ADD_EXECUTABLE(test_scllib ${testing_sources} ${tested_sources}) -TARGET_LINK_LIBRARIES(test_scllib libcmocka.so) -TARGET_LINK_LIBRARIES(test_scllib librpm.so librpmio.so) -ADD_TEST(test_scllib ${CMAKE_CURRENT_BINARY_DIR}/test_scllib) - -SET(tested_sources ../src/args.c ../src/sclmalloc.c ../src/lib_common.c ../src/debug.c) -SET(testing_sources test_args.c test_common.c) -ADD_EXECUTABLE(test_args ${testing_sources} ${tested_sources}) -TARGET_LINK_LIBRARIES(test_args libcmocka.so) -ADD_TEST(test_args ${CMAKE_CURRENT_BINARY_DIR}/test_args) - -# FILE(INSTALL test_build.sh DESTINATION . FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE) -# FILE(INSTALL SRPMS RPMS DESTINATION .) -# ADD_TEST(test_build ${CMAKE_CURRENT_BINARY_DIR}/test_build.sh) diff --git a/tests/dict.c b/tests/dict.c deleted file mode 100644 index c341b3c..0000000 --- a/tests/dict.c +++ /dev/null @@ -1,83 +0,0 @@ -/* A simple key-value store */ - -#include -#include -#include -#include - -#include "dict.h" -#include "../src/sclmalloc.h" -#include "../src/lib_common.h" - -dict dict_init() -{ - return calloc(1, sizeof(entry*)); -} - -void dict_free(dict d) -{ - int i = 0; - entry *e; - while ((e = d[i++])) { - free(e->str); - free(e); - } - free(d); -} - -int dict_put(dict *d, char *string) -{ - int i = 0; - int size = 0; - char **parts; - char *key; - char *val; - bool update = false; - entry *e; - - parts = split(string, '='); - key = parts[0]; - val = parts[1]; - free(parts); - - while ((e = (*d)[i++])) { - if (strcmp(e->key, key)) - continue; - update = true; - break; - } - - if (update) { - free(e->str); - } else { - size = i + 1; - *d = realloc(*d, size * sizeof(entry*)); - e = malloc(sizeof(entry)); - (*d)[size - 1] = NULL; - (*d)[size - 2] = e; - } - - e->str = string; - e->key = key; - e->val = val; - - return 0; -} - -char *dict_get(dict d, const char *key) -{ - int i = 0; - entry *e; - while ((e = d[i++])) - if (strcmp(e->key, key) == 0) - return e->val; - return NULL; -} - -void dict_dump(dict d) -{ - int i = 0; - entry *e; - while ((e = d[i++])) - printf("%s=%s\n", e->key, e->val); -} diff --git a/tests/dict.h b/tests/dict.h deleted file mode 100644 index 0b5dc21..0000000 --- a/tests/dict.h +++ /dev/null @@ -1,13 +0,0 @@ -typedef struct { - char *str; - char *key; - char *val; -} entry; - -typedef entry** dict; - -dict dict_init(); -void dict_free(dict d); -int dict_put(dict *d, char *string); -char *dict_get(dict d, const char *key); -void dict_dump(dict d); diff --git a/tests/test_args.c b/tests/test_args.c deleted file mode 100644 index 696f9dc..0000000 --- a/tests/test_args.c +++ /dev/null @@ -1,251 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "test_common.h" -#include "../src/args.h" -#include "../src/errors.h" - - -static void test_scl_args_get_basic_args(void **state) -{ - (void) state; /* unused */ - int argc; - char **argv; - struct scl_args *args; - scl_rc ret; - - /* test list-collections argument */ - argv = (char *[]) {"scl", "list-collections"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_LIST_COLLECTIONS); - scl_args_free(args); - - /* test --help argument */ - argv = (char *[]) {"scl", "--help"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_NONE); - scl_args_free(args); - - /* test list-packages without defining collection, it should return EINPUT */ - argv = (char *[]) {"scl", "list-packages"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* test list-packages argument */ - argv = (char *[]) {"scl", "list-packages", "collection"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_LIST_PACKAGES); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection", NULL})); - scl_args_free(args); - - /* test man without defining collection, it should return EINPUT */ - argv = (char *[]) {"scl", "man"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* test man argument */ - argv = (char *[]) {"scl", "man", "collection"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_MAN); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection", NULL})); - scl_args_free(args); -} - -static void test_scl_args_get_register_deregister(void **state) -{ - (void) state; /* unused */ - int argc; - char **argv; - struct scl_args *args; - scl_rc ret; - - /* test deregister without defining collection. It should return EINPUT */ - argv = (char *[]) {"scl", "deregister"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* test deregister argument */ - argv = (char *[]) {"scl", "deregister", "collection"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_DEREGISTER); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection", NULL})); - scl_args_free(args); - - /* test "deregister -f" without defining collection. It should return EINPUT */ - argv = (char *[]) {"scl", "deregister", "-f"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* test "deregister -f" arguments */ - argv = (char *[]) {"scl", "deregister", "-f", "collection"}; - argc = 4; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_DEREGISTER); - assert_true(args->force_flag); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection", NULL})); - scl_args_free(args); - - /* test deregister without defining path to collection. It should return EINPUT */ - argv = (char *[]) {"scl", "register"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* test deregister with relative path. It should return EINPUT */ - argv = (char *[]) {"scl", "register", "colection_dir/collection"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* test deregister argument */ - argv = (char *[]) {"scl", "register", "/path/to/collection"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_REGISTER); - assert_true(compare_string_arrays(args->colpaths, - (char *[]) {"/path/to/collection", NULL})); - scl_args_free(args); -} - -static void test_scl_args_get_run_command(void **state) -{ - (void) state; /* unused */ - int argc; - char **argv; - struct scl_args *args; - scl_rc ret; - - /* Test wrong argument for scl run */ - argv = (char *[]) {"scl", "run"}; - argc = 2; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - argv = (char *[]) {"scl", "run", "collection"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - argv = (char *[]) {"scl", "run", "-x", "collection"}; - argc = 4; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EINPUT); - - /* Test run with one collection */ - argv = (char *[]) {"scl", "run", "collection", "cmd"}; - argc = 4; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection", NULL})); - assert_string_equal(args->command, "cmd"); - scl_args_free(args); - - /* Test run with multiple collections */ - argv = (char *[]) {"scl", "run", "collection1", "collection2", - "collection3", "cmd"}; - argc = 6; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection1", "collection2", "collection3", NULL})); - assert_string_equal(args->command, "cmd"); - scl_args_free(args); - - /* Test run with compound command */ - argv = (char *[]) {"scl", "run", "collection1", "collection2", - "cmd arg1 arg2"}; - argc = 5; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection1", "collection2", NULL})); - assert_string_equal(args->command, "cmd arg1 arg2"); - scl_args_free(args); - - /* Test run with separator */ - argv = (char *[]) {"scl", "run", "collection1", "collection2", - "--", "cmd", "arg1 with spaces", "arg2"}; - argc = 8; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection1", "collection2", NULL})); - assert_string_equal(args->command, "cmd \"arg1 with spaces\" \"arg2\""); - scl_args_free(args); - - /* Test run in format of shebang */ - argv = (char *[]) {"scl", strdup("run collection1 collection2 -- python"), "test.py"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection1", "collection2", NULL})); - assert_string_equal(args->command, "python \"test.py\""); - free(argv[1]); - scl_args_free(args); - - argv = (char *[]) {"scl", strdup("run collection1 collection2 -- python -O"), "test.py"}; - argc = 3; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection1", "collection2", NULL})); - assert_string_equal(args->command, "python \"-O\" \"test.py\""); - free(argv[1]); - scl_args_free(args); - - argv = (char *[]) {"scl", strdup("run collection1 collection2 -- python"), - "test.py", "argv1", "argv2"}; - argc = 5; - ret = scl_args_get(argc, argv, &args); - assert_int_equal(ret, EOK); - assert_int_equal(args->action, ACTION_COMMAND); - assert_true(compare_string_arrays(args->collections, - (char *[]) {"collection1", "collection2", NULL})); - assert_string_equal(args->command, "python \"test.py\" \"argv1\" \"argv2\""); - free(argv[1]); - scl_args_free(args); -} - - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_scl_args_get_basic_args), - cmocka_unit_test(test_scl_args_get_register_deregister), - cmocka_unit_test(test_scl_args_get_run_command), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/tests/test_build.sh b/tests/test_build.sh deleted file mode 100755 index 475d30d..0000000 --- a/tests/test_build.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/bash -. /usr/share/beakerlib/beakerlib.sh - -TEST="Collections test" - - -rpmdir=`pwd`/rpmdir - -rlJournalStart - rlPhaseStartTest "building collections" - for col_srpm_dir in SRPMS/*; do - rm -rf $rpmdir - mkdir $rpmdir - - colname=`basename $col_srpm_dir` - srpms=$col_srpm_dir/* - - rlLog "Collection: $colname" - - rpmbuild --define "_topdir $rpmdir" --define "scl $colname" --rebuild $srpms - rlAssert0 "Check if it is possible to build collection" $? - - runtime_package=`find $rpmdir/RPMS | grep "/${colname}-runtime"` - rpm -qp --provides $runtime_package | grep "scl-package($colname)" - rlAssert0 "Check if runtime packages provides scl-package($colname)" $? - - non_runtime_packages=`find $rpmdir/RPMS -type f | grep -v "/${colname}-runtime"` - echo $non_runtime_packages; - for package in $non_runtime_packages; do - rpm -qpR $package | grep ${colname}-runtime - rlAssert0 "Check if `basename $package` requires ${colname}-runtime" $? - done - done - rlPhaseEnd - - rlPhaseStartTest "collection runtime" - rpm -q test555-runtime && yum erase -y test555-runtime - yum install -y RPMS/test555-runtime-1-1.x86_64.rpm - rlAssert0 "Check if it is possible to install collection test555" $? - - scl list-collections | grep test555 - rlAssert0 "Check if collection test555 is in list of collections" $? - - rm -rf /tmp/scl - mkdir /tmp/scl - scl deregister -f test555 - rlAssert0 "Check if it is possilbe to deregister collection test555" $? - - test -f /tmp/scl/deregister - rlAssert0 "Check if deregister script was successfully executed" $? - - scl register /opt/rh/test555 - rlAssert0 "Check if it is possilbe to register collection test555" $? - - test -f /tmp/scl/register - rlAssert0 "Check if register script was successfully executed" $? - - rlPhaseEnd - -rlJournalEnd - -rlJournalPrintText - -rlGetTestState - - diff --git a/tests/test_common.c b/tests/test_common.c deleted file mode 100644 index d0db8d0..0000000 --- a/tests/test_common.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -bool compare_string_arrays(char *const *array1, char *const *array2) -{ - int i; - - for (i = 0; array1[i] != NULL; i++) { - /* the second array is shorter or items are not equal */ - if (array2[i] == NULL || strcmp(array1[i], array2[i])) { - return false; - } - } - - /* the first array is shorter */ - if (array2[i] != NULL) { - return false; - } - - return true; -} diff --git a/tests/test_common.h b/tests/test_common.h deleted file mode 100644 index fda0e5a..0000000 --- a/tests/test_common.h +++ /dev/null @@ -1,3 +0,0 @@ -#include - -bool compare_string_arrays(char *const *array1, char *const *array2); diff --git a/tests/test_scllib.c b/tests/test_scllib.c deleted file mode 100644 index 0f8f34b..0000000 --- a/tests/test_scllib.c +++ /dev/null @@ -1,284 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "test_common.h" -#include "dict.h" -#include "../src/scllib.h" -#include "../src/errors.h" - -extern int __real_putenv(); -extern char *__real_getenv(); -dict env; - -int __wrap_putenv(char *string) -{ - if (env) - return dict_put(&env, string); - return __real_putenv(string); -} - -char *__wrap_getenv(const char *name) -{ - char *value; - if (env && (value = dict_get(env, name))) - return value; - return __real_getenv(name); -} - -char *__wrap_get_command_output(const char *path, char *const argv[], int fileno) -{ - return mock_ptr_type(char *); -} - -typedef struct { - char *cmd_output; /* value that will be set as return value of get_command_output() */ - char **expected_colnames; /* expected output value from get_installed_collections() */ - scl_rc ret; /* expected return value from get_installed_collections() */ -} inst_col_test_case; - -static void test_get_installed_collections(void **state) -{ - (void) state; /* unused */ - char *const *colnames; - char *output_allocated; - scl_rc ret; - - - inst_col_test_case testcases[] = { - /* Collections are in the end of output */ - { - .cmd_output = - "/usr/share/Modules/modulefiles:\n" - "modulename1\n" - "modulename2\n" - "modulename3\n" - "/etc/scl/modulefiles:\n" - "scl1\n" - "scl2\n" - "scl3\n" - "scl4\n", - - .expected_colnames = (char *[]){ - "scl1", - "scl2", - "scl3", - "scl4", - NULL, - }, - - .ret = EOK, - }, - - /* Collections are in the middle of output */ - { - .cmd_output = - "/usr/share/Modules/modulefiles:\n" - "modulename1\n" - "modulename2\n" - "modulename3\n" - "/etc/scl/modulefiles:\n" - "sclA\n" - "sclB\n" - "sclC\n" - "/etc/modulefiles:\n" - "modulename4\n" - "modulename5\n", - - .expected_colnames = (char *[]){ - "sclA", - "sclB", - "sclC", - NULL, - }, - - .ret = EOK, - }, - - /* Collections are in the beginning of output */ - { - .cmd_output = - "/etc/scl/modulefiles:\n" - "sclX\n" - "sclY\n" - "/etc/modulefiles:\n" - "modulename4\n" - "modulename5\n", - - .expected_colnames = (char *[]){ - "sclX", - "sclY", - NULL, - }, - - .ret = EOK, - }, - - /* No collection is installed */ - { - .cmd_output = - "/usr/share/Modules/modulefiles:\n" - "modulename1\n" - "modulename2\n" - "modulename3\n" - "/etc/scl/modulefiles:\n", - - .expected_colnames = (char *[]){ - NULL, - }, - - .ret = EOK, - }, - - /* get_command_output returns NULL (indicate error) */ - { - .cmd_output = NULL, - .expected_colnames = NULL, - .ret = ERUN, - }, - - }; - - int tc_count = sizeof(testcases) / sizeof(testcases[0]); - - - for (int i = 0; i < tc_count; i++) { - - release_scllib_cache(); - - if (testcases[i].cmd_output != NULL) { - output_allocated = malloc(strlen(testcases[i].cmd_output) + 1); - strcpy(output_allocated, testcases[i].cmd_output); - } else { - output_allocated = NULL; - } - - will_return(__wrap_get_command_output, output_allocated); - ret = get_installed_collections(&colnames); - - assert_int_equal(ret, testcases[i].ret); - if (ret == EOK) { - assert_true(compare_string_arrays(colnames, - testcases[i].expected_colnames)); - } - - } -} - -int __wrap_system(const char *command) -{ - char *env_path; - - env_path = getenv("PATH"); - assert_true(strstr(env_path, mock_ptr_type(char *)) != NULL); - - - return 0; -} - -typedef struct { - char *col_list; - char *env_vars; - char **collections; - char *expected_env_path; - scl_rc ret; -} run_command_testcase; - -static void test_run_command(void **state) -{ - (void) state; /* unused */ - scl_rc ret; - - run_command_testcase testcases[] = { - /* One collection enabled to run program */ - { - .col_list = - "/usr/share/Modules/modulefiles:\n" - "modulename1\n" - "modulename2\n" - "modulename3\n" - "/etc/scl/modulefiles:\n" - "scl1\n" - "scl2\n" - "scl3\n" - "scl4\n", - - .env_vars = "PATH=/opt/rh/scl1/root/usr/bin ;export PATH", - .expected_env_path = "/opt/rh/scl1/root/usr/bin", - .collections = (char *[]) {"scl1", NULL}, - .ret = EOK, - }, - - /* Newlines in module(1) output are stripped */ - { - .col_list = - "/etc/scl/modulefiles:\n" - "scl1\n", - - .env_vars = - "LD_LIBRARY_PATH=/opt/rh/scl1/root/usr/lib64; export LD_LIBRARY_PATH;\n" - "PATH=/opt/rh/scl1/root/usr/bin; export PATH;", - - .expected_env_path = "/opt/rh/scl1/root/usr/bin", - .collections = (char *[]) {"scl1", NULL}, - .ret = EOK, - }, - - /* Try to enable non-existing collection */ - { - .col_list = - "/usr/share/Modules/modulefiles:\n" - "modulename1\n" - "modulename2\n" - "modulename3\n" - "/etc/scl/modulefiles:\n" - "scl1\n" - "scl2\n" - "scl3\n" - "scl4\n", - - .env_vars = NULL, - .expected_env_path = NULL, - .collections = (char *[]) {"scl5", NULL}, - .ret = EINPUT, - }, - }; - - int tc_count = sizeof(testcases) / sizeof(testcases[0]); - - for (int i = 0; i < tc_count; i++) { - env = dict_init(); - release_scllib_cache(); - - if (testcases[i].col_list) - will_return(__wrap_get_command_output, strdup(testcases[i].col_list)); - - if (testcases[i].env_vars) - will_return(__wrap_get_command_output, strdup(testcases[i].env_vars)); - - if (testcases[i].expected_env_path) - will_return(__wrap_system, testcases[i].expected_env_path); - - - ret = run_command(testcases[i].collections, "test_cmd", false); - assert_int_equal(ret, testcases[i].ret); - dict_free(env); - } - -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_get_installed_collections), - cmocka_unit_test(test_run_command), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -}