Files
librenms-librenms/LibreNMS/Util/FileCategorizer.php
Tony Murray ce21011aff Rewrite development helper to lnms dev:check (#11650)
* Refactor pre-commit to class

* docs build

* dusk check

* ci mode for checks

* full checks

* other mysql

* make other lint checks actually work
fix pylint finding

* ci is a long opt

* fix undefined index

* dusk fully working

* ask for forgiveness, not permission

* fix whitespace

* skip dusk sometimes

* Handle 3com and other os with digits

* flags instead of if else spaghetti

* convert to command

* cleanup

* missed check

* fixes

* case

* self-check :D

* argument now

* fix bugs from refactors

* another fix

* adjust file change parsing

* refactor execut a bit

* fallback to global quiet when unknown type.

* allow quiet override for specific commands

* output cleanup

* check flow

* start of tests

* file categorizer tests and fixes

* fixes and cleanup

* skipable not implemented...

* more tests, fix bugs

* more tests and cleanup

* wrong command

* fix canCheck and set env properly

* full env fix

* don't allow dusk on user's run as it will erase their db.

* fix os option

* fix whitespace

* don't need to start server

* ci doesn't like that
2020-05-22 20:27:48 -05:00

138 lines
4.7 KiB
PHP

<?php
/**
* FileCategorizer.php
*
* Categorizes files in LibreNMS
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use Illuminate\Support\Str;
class FileCategorizer extends Categorizer
{
private const TESTS_REGEX = '#^tests/(snmpsim|data)/(([0-9a-z\-]+)(_[0-9a-z\-]+)?)(_[0-9a-z\-]+)?\.(json|snmprec)$#';
public function __construct($items = [])
{
parent::__construct($items);
if (getenv('CIHELPER_DEBUG')) {
$this->setSkippable(function ($item) {
return in_array($item, ['.travis.yml', 'LibreNMS/Util/CiHelper.php', 'LibreNMS/Util/FileCategorizer.php']);
});
}
$this->addCategory('php', function ($item) {
return Str::endsWith($item, '.php') ? $item : false;
});
$this->addCategory('docs', function ($item) {
return (Str::startsWith($item, 'doc/') || $item == 'mkdocs.yml') ? $item : false;
});
$this->addCategory('python', function ($item) {
return Str::endsWith($item, '.py') ? $item : false;
});
$this->addCategory('bash', function ($item) {
return Str::endsWith($item, '.sh') ? $item : false;
});
$this->addCategory('svg', function ($item) {
return Str::endsWith($item, '.svg') ? $item : false;
});
$this->addCategory('resources', function ($item) {
return Str::startsWith($item, 'resources/') ? $item : false;
});
$this->addCategory('full-checks', function ($item) {
return in_array($item, ['composer.lock', '.travis.yml']) ? $item : false;
});
$this->addCategory('os-files', function ($item) {
if (($os_name = $this->osFromFile($item)) !== null) {
return ['os' => $os_name, 'file' => $item];
}
return false;
});
}
public function categorize()
{
parent::categorize();
// split out os
$this->categorized['os'] = array_unique(array_column($this->categorized['os-files'], 'os'));
$this->categorized['os-files'] = array_column($this->categorized['os-files'], 'file');
// If we have more than 4 (arbitrary number) of OS' then blank them out
// Unit tests may take longer to run in a loop so fall back to all.
if (count($this->categorized['os']) > 4) {
$this->categorized['full-checks'] = [true];
}
return $this->categorized;
}
private function validateOs($os)
{
return file_exists("includes/definitions/$os.yaml") ? $os : null;
}
private function osFromFile($file)
{
if (Str::startsWith($file, 'includes/definitions/')) {
return basename($file, '.yaml');
} elseif (Str::startsWith($file, ['includes/polling', 'includes/discovery'])) {
return $this->validateOs(basename($file, '.inc.php'));
} elseif (preg_match('#LibreNMS/OS/[^/]+.php#', $file)) {
return $this->osFromClass(basename($file, '.php'));
} elseif (preg_match(self::TESTS_REGEX, $file, $matches)) {
if ($this->validateOs($matches[3])) {
return $matches[3];
}
if ($this->validateOs($matches[2])) {
return $matches[2];
}
}
return null;
}
/**
* convert class name to os name
*
* @param string $class
* @return string|null
*/
private function osFromClass($class)
{
preg_match_all("/[A-Z][a-z0-9]*/", $class, $segments);
$osname = implode('-', array_map('strtolower', $segments[0]));
$osname = preg_replace(
['/^zero-/', '/^one-/', '/^two-/', '/^three-/', '/^four-/', '/^five-/', '/^six-/', '/^seven-/', '/^eight-/', '/^nine-/',],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
$osname
);
if ($os = $this->validateOs($osname)) {
return $os;
}
return $this->validateOs(str_replace('-', '_', $osname));
}
}