From patchwork Tue Apr 7 18:43:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov via arch-projects X-Patchwork-Id: 1577 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 9638318119486 for ; Tue, 7 Apr 2020 18:45:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on apollo.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=5.0 tests=DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1, MAILING_LIST_MULTI=-1,RCVD_IN_DNSWL_MED=-2.3,SPF_HELO_NONE=0.001, T_DMARC_POLICY_NONE=0.01 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-BL-Results: [127.0.9.2] Received: from orion.archlinux.org (orion.archlinux.org [88.198.91.70]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Tue, 7 Apr 2020 18:45:08 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 106061ACEC00FF; Tue, 7 Apr 2020 18:45:06 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: luna) by orion.archlinux.org (Postfix) with ESMTPSA id DC7891ACEC00FB; Tue, 7 Apr 2020 18:45:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=orion; t=1586285105; bh=Psjf/m2NIL0QaOT6BL4UR7cM8Rcb4b8wUSAsItPjE4M=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc; b=KUHNfbVVzezeK10eS63PKw73UbVpSU6aQQ8G/oIyRQgwWz2W7qwuYYwLjrxOCCv42 wTYTPpZQbqp3IXGIhwVrfKwl5IYyx2f86G5Duerj0JqDkMShRJAHeOwY/npahGh55C VyCci3EMM2aoJS6VWRLWR6QFBNxdADU3SD1Cvz08BaH76Z8KV6ISyUdfzhoFOrbwSD hXPYTlUcWmavbFjPgaPDqFpeZ4+Lh16ZbvcLiWI4FIyzTV/UXn7RVEgC6SA2EEFYZf Xw5mQynYeR9gHQlaplsK1z8PGzpkqg9Plg8wUyIK/zbm6ndFT/NDRA1g41K/M/4GLl OR5f2mmlj5kHGZrgsrrAUga8/rODXIsD1mRZiRg041TPZKwHycB22Y0KqzZlRdZ7EN jNl8a9u19dIdWGQhZ5MEqmUW4sn7SVlrL4ZpnoNuTjWbluG+81jf8t5xckzpLWqV0z uGUiZHy+x4yclnxqAt4dymzR8WNnOVXvtNz+VkT+eU2NEQKQGdB8HdW2NR1PGLlFXQ fNQq+QQgltoeOzGcrgUP3jxL9/3pUkZur/biiuTyW1EjsPl4LTraADWk911QrDSsQO f2uwgY8ZusnUOea9vQ9WjM8/QojLlkSHS0LU5dTXpDxm8mnybrzmVFECySPX1PlxlP 0dRWeOpiHpjuaSW9MNwnVflQ= Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id C53442B2B3; Tue, 7 Apr 2020 18:45:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=luna2; t=1586285105; bh=Psjf/m2NIL0QaOT6BL4UR7cM8Rcb4b8wUSAsItPjE4M=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc; b=m/PCw4dl1VA/v26xrbUw/TtT7/jU6PANBLJufEg75tGuGCVpUcq8q1qBUykTB0Rc5 r3J87efSM6jYnl+++ibVmFp07WQ7zJuIG3Fs2ZwbyOvBRx2fVoZ8u6xkG0iZsqGWgN nRQ18m/zVO+AmcWfq/GTm+KR3xgdszqnqCMGsRKz4rhKr1ZX7iE5tJbha5s11xL3uj CHUfPUNxGTrCAPoWFKE4OEmhIWbKgEv7VZ//evpQjNZm7vN0d3hDyJBUCdUD2dbyH8 IJLxK9JluCajZZ+QuXifl/JV0U6Knl+7pe0dK7qRgH/oNSxpRauzXStfXU+gJQUSB+ bT/l+ltorPLMLcOE9jJbMoFGJdXgfPe1xQ4R+hEBpv74WW31zgB7aEyvjwfOnmV+OS wDnrWFkaoaAg5DYLpc4KVhDZ7XHQIpVA2kJpnzWd72cB6HuWKfSDXuhna7SthwvxD/ 1D5c8TutbgiWdUrqT3SLLByjpYsu+n1HLvzCuNtqaFpAwxCMI56MPN0Jm/ys0ubtsG NvSwAN/ObJ01LcYwCNvD8LMXnywiLgcf5pTt2hjnM3LgSRymUy4CpVOxQUTbg1Ig/G 9A2RB+/g8y49+wPMyZYEOrMoGCsng5TrvDF0VN7/R+0tRP55/jVPiG2lgypEjrdzZw 7Q5VjdO1aI+JSpGvGlS33KJA= Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 857AD2B2B0 for ; Tue, 7 Apr 2020 18:45:02 +0000 (UTC) Received: from orion.archlinux.org (orion.archlinux.org [88.198.91.70]) by luna.archlinux.org (Postfix) with ESMTPS for ; Tue, 7 Apr 2020 18:45:02 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id D7F371ACEC00F7 for ; Tue, 7 Apr 2020 18:45:00 +0000 (UTC) Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by orion.archlinux.org (Postfix) with ESMTPS for ; Tue, 7 Apr 2020 18:45:00 +0000 (UTC) Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 1BAFA240107 for ; Tue, 7 Apr 2020 20:45:00 +0200 (CEST) Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 48xbtg0xKkz9rxc; Tue, 7 Apr 2020 20:44:58 +0200 (CEST) To: arch-projects@archlinux.org Date: Tue, 7 Apr 2020 20:43:18 +0200 Message-Id: <20200407184318.18010-1-michael.straube@posteo.de> X-Mailer: git-send-email 2.26.0 MIME-Version: 1.0 Subject: [arch-projects] [namcap] [PATCH] Warn on unquoted pkgdir and srcdir X-BeenThere: arch-projects@archlinux.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Arch Linux projects development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Michael Straube via arch-projects Reply-To: Arch Linux projects development discussion Cc: Michael Straube Errors-To: arch-projects-bounces@archlinux.org Sender: "arch-projects" Unqouted $pkgdir and $srcdir can lead to errors if the directory name contains spaces. Not quoting these variables is a common mistake. For example, it is often seen in PKGBUILDs that are submitted for review. Add a rule that warns on unquoted $pkgdir and $srcdir. Signed-off-by: Michael Straube --- Namcap/rules/__init__.py | 3 +- Namcap/rules/unquoteddirvars.py | 39 ++++++++++++ Namcap/tests/pkgbuild/test_unquoteddirvars.py | 63 +++++++++++++++++++ namcap-tags | 1 + 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 Namcap/rules/unquoteddirvars.py create mode 100644 Namcap/tests/pkgbuild/test_unquoteddirvars.py diff --git a/Namcap/rules/__init__.py b/Namcap/rules/__init__.py index 5ca6551..bd348b4 100644 --- a/Namcap/rules/__init__.py +++ b/Namcap/rules/__init__.py @@ -67,7 +67,8 @@ from . import ( pkginfo, pkgnameindesc, sfurl, - splitpkgbuild + splitpkgbuild, + unquoteddirvars ) all_rules = {} diff --git a/Namcap/rules/unquoteddirvars.py b/Namcap/rules/unquoteddirvars.py new file mode 100644 index 0000000..63de0c1 --- /dev/null +++ b/Namcap/rules/unquoteddirvars.py @@ -0,0 +1,39 @@ +# +# namcap rules - unquoteddirvars +# Copyright (C) 2020 Michael Straube +# +# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +import re +from Namcap.ruleclass import * + +class package(PkgbuildRule): + name = "unquoteddirvars" + description = "Looks for unquoted $pkgdir and $srcdir" + def analyze(self, pkginfo, pkgbuild): + needles = ['$pkgdir', '${pkgdir}', '$srcdir', '${srcdir}'] + hits = set() + for line in pkginfo.pkgbuild: + if not any(n in line for n in needles): + continue + double_quoted_strings = re.findall('"([^"]*)"', line) + for n in needles: + if line.count(n) != sum(n in s for s in double_quoted_strings): + hits.add(n) + for i in hits: + self.warnings.append(("unquoted-dirvar %s", i)) + +# vim: set ts=4 sw=4 noet: diff --git a/Namcap/tests/pkgbuild/test_unquoteddirvars.py b/Namcap/tests/pkgbuild/test_unquoteddirvars.py new file mode 100644 index 0000000..7a420bd --- /dev/null +++ b/Namcap/tests/pkgbuild/test_unquoteddirvars.py @@ -0,0 +1,63 @@ +# +# namcap tests - unquoteddirvars +# Copyright (C) 2020 Michael Straube +# +# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +from Namcap.tests.pkgbuild_test import PkgbuildTest +import Namcap.rules + +class NamcapUnqoutedDirVarsTest(PkgbuildTest): + pkgbuild = """ +# Maintainer: Arch Linux +# Contributor: Arch Linux + +pkgname=mypackage +pkgver=1.0 +pkgrel=1 +pkgdesc="A package" +url="http://www.example.com/" +arch=('x86_64') +depends=('glibc') +license=('GPL') +options=('!libtool') +source=(ftp://ftp.example.com/pub/mypackage-0.1.tar.gz) +md5sums=('abcdefabcdef12345678901234567890') + +build() { + cd $srcdir/$pkgname-$pkgver +} + +package() { + make install DESTDIR=$pkgdir/ + install -Dm644 ${srcdir}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname} + install -Dm644 "${srcdir}/example.desktop" "$pkgdir"/usr/share/applications +} +""" + test_valid = PkgbuildTest.valid_tests + + def preSetUp(self): + self.rule = Namcap.rules.unquoteddirvars.package + + def test_example(self): + needles = ['$pkgdir', '${pkgdir}', '$srcdir', '${srcdir}'] + r = self.run_on_pkg(self.pkgbuild) + self.assertEqual(r.errors, []) + self.assertEqual(set(r.warnings), + set(("unquoted-dirvar %s", i) for i in needles)) + self.assertEqual(r.infos, []) + +# vim: set ts=4 sw=4 noet: diff --git a/namcap-tags b/namcap-tags index 1f7bc69..e48f8fe 100644 --- a/namcap-tags +++ b/namcap-tags @@ -85,6 +85,7 @@ specific-sourceforge-mirror :: Attempting to use specific sourceforge mirror, us symlink-found %s points to %s :: Symlink (%s) found that points to %s systemd-location %s :: File %s should be in /usr/lib/systemd/system/ too-many-checksums %s %i needed :: Too many %s: %i needed +unquoted-dirvar %s :: Variable %s is not quoted. unused-sodepend %s %s :: Unused shared library '%s' by file ('%s') use-pkgdir :: Use $pkgdir instead of $startdir/pkg use-srcdir :: Use $srcdir instead of $startdir/src