From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36774) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1et2C3-00006V-9J for guix-patches@gnu.org; Mon, 05 Mar 2018 21:19:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1et2By-0006J4-Vl for guix-patches@gnu.org; Mon, 05 Mar 2018 21:19:07 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:38636) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1et2By-0006Iw-S6 for guix-patches@gnu.org; Mon, 05 Mar 2018 21:19:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1et2By-0005o1-F3 for guix-patches@gnu.org; Mon, 05 Mar 2018 21:19:02 -0500 Subject: [bug#30708] [PATCH] utils: Add helper method to list subdirectories. Resent-Message-ID: From: Maxim Cournoyer References: <87a7vnma48.fsf@gmail.com> <87lgf68n07.fsf@gnu.org> Date: Mon, 05 Mar 2018 21:18:12 -0500 In-Reply-To: <87lgf68n07.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Mon, 05 Mar 2018 18:12:40 +0100") Message-ID: <87y3j6kkuz.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: Ludovic =?UTF-8?Q?Court=C3=A8s?= Cc: 30708@debbugs.gnu.org Hi Ludovic, ludo@gnu.org (Ludovic Court=C3=A8s) writes: > Hi Maxim, > > Maxim Cournoyer skribis: > >> From b4b607800d770c4cf77f92c247276c368357e94f Mon Sep 17 00:00:00 2001 >> From: Maxim Cournoyer >> Date: Sun, 25 Feb 2018 17:49:06 -0500 >> Subject: [PATCH] utils: Add helper method to list subdirectories. >> >> * guix/build/utils.scm (find-subdirectories): New procedure. >> * tests/build-utils.scm: Rename module so that it can be used with Geise= r. >> (%test-dir-hierarchy): New variable. >> (make-test-dir-hierarchy): New test procedure. >> ("find-subdirectories"): New test. > > [...] > >> +(define* (find-subdirectories dir #:key fail-on-error?) >> + "Return the list of the immediate subdirectories of DIR." >> + ;; Strip the trailing '/' DIR is '/'. >> + (let ((dir (if (and (> 1 (string-length dir)) >> + (eq? (string-take-right dir 1) #\/)) >> + (string-drop-right dir 1) >> + dir))) >> + (define (pred filename stat) >> + (and (eq? (stat:type stat) 'directory) >> + (string-match (string-append dir "/[^/]*$") filename))) >> + (find-files dir pred >> + #:directories? #t >> + #:fail-on-error? fail-on-error?))) > > =E2=80=98find-files=E2=80=99 recurses in subdirectories, so the above imp= lementation is > not as efficient as it could be. > > I would instead suggest using =E2=80=98scandir=E2=80=99 (or =E2=80=98file= -system-fold=E2=80=99) from > Guile=E2=80=99s (ice-9 ftw) module. Thanks! See the new patched attached. The test still passes. > That said=E2=80=A6 is this a common enough operation? I'm using it in a forthcoming new Guix package (SuperCollider) where it allows me to explicitly list the bundled dependencies that are to be *kept* rather than the ones to be removed, as is more commonly done. Withou= t a list of the subdirectories the contrib/vendor/whatever bundled libraries directory I would not be able to do the following: --8<---------------cut here---------------start------------->8--- + ;; The build system doesn't allow us to unbundle the + ;; following libraries. + (let* ((all-dirs (find-subdirectories "./external_libraries")) + (keep-dirs '("nova-simd" "nova-tt" "hidapi" "TLSF-2.4.= 6" + "oscpack_1_1_0")) + (remove-dirs + (remove (lambda (x) + (member (basename x) keep-dirs)) + all-dirs))) + (format #t "Removing bundled libraries: ~s\n" remove-dirs) + (for-each delete-file-recursively remove-dirs))))))) --8<---------------cut here---------------end--------------->8--- Although now that you've made me see the light (scandir), I could rewrite the whole thing using: --8<---------------cut here---------------start------------->8--- (lambda _ ;; The build system doesn't allow us to unbundle the following ;; libraries. (let ((keep-dirs '("nova-simd" "nova-tt" "hidapi" "TLSF-2.4.6" "oscpack_1_1_0" "." ".."))) (with-directory-excursion "./external_libraries" (for-each delete-file-recursively (scandir "." (lambda (x) (and (eq? (stat:type (stat x)) 'directory) (not (member (basename x) keep-dirs))))))= )) --8<---------------cut here---------------end--------------->8--- So, this patch can go to the recycle bin. Thanks! :) Maxim