Skip to content

Commit

Permalink
Add C linter script (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo authored Feb 18, 2020
1 parent 41fcba5 commit 5e47e0b
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .ci/travis/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if [ "$PYVER" == "2.7" ] || [ "$PYVER" == "3.6" ]; then
PSUTIL_TESTING=1 python -Wa psutil/tests/test_memory_leaks.py
# run linter (on Linux only)
if [[ "$(uname -s)" != 'Darwin' ]]; then
python -m flake8
make lint PYTHON=python
fi
fi

Expand Down
9 changes: 5 additions & 4 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
*Bug tracker at https://github.com/giampaolo/psutil/issues*

5.7.0 (unreleased)
==================
5.7.0
=====

XXXX-XX-XX
2020-12-18

**Enhancements**

- 1637_: [SunOS] Add partial support for old SunOS 5.10 Update 0 to 3.
- 1637_: [SunOS] add partial support for old SunOS 5.10 Update 0 to 3.
- 1648_: [Linux] sensors_temperatures() looks into an additional /sys/device/
directory for additional data. (patch by Javad Karabi)
- 1652_: [Windows] dropped support for Windows XP and Windows Server 2003.
Expand All @@ -16,6 +16,7 @@ XXXX-XX-XX
- 1677_: [Windows] process exe() will succeed for all process PIDs (instead of
raising AccessDenied).
- 1679_: [Windows] net_connections() and Process.connections() are 10% faster.
- 1682_: [PyPy] added CI / test integration for PyPy via Travis.
- 1686_: [Windows] added support for PyPy on Windows.
- 1693_: [Windows] boot_time(), Process.create_time() and users()'s login time
now have 1 micro second precision (before the precision was of 1 second).
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ include scripts/internal/README
include scripts/internal/bench_oneshot.py
include scripts/internal/bench_oneshot_2.py
include scripts/internal/check_broken_links.py
include scripts/internal/clinter.py
include scripts/internal/fix_flake8.py
include scripts/internal/generate_manifest.py
include scripts/internal/print_access_denied.py
Expand Down
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,17 @@ test-coverage: ## Run test coverage.
# Linters
# ===================================================================

flake8: ## flake8 linter.
@git ls-files | grep \\.py$ | xargs $(PYTHON) -m flake8
lint-py: ## Run Python (flake8) linter.
@git ls-files '*.py' | xargs $(PYTHON) -m flake8

fix-flake8: ## Attempt to automaticall fix some flake8 issues.
lint-c: ## Run C linter.
@git ls-files '*.c' '*.h' | xargs $(PYTHON) scripts/internal/clinter.py

lint: ## Run Python (flake8) and C linters.
${MAKE} lint-py
${MAKE} lint-c

fix-lint: ## Attempt to automatically fix some Python lint issues.
@git ls-files | grep \\.py$ | xargs $(PYTHON) -m flake8 --exit-zero | $(PYTHON) scripts/internal/fix_flake8.py

# ===================================================================
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Build: 2 (bump this up by 1 to force an appveyor run)
# Build: 3 (bump this up by 1 to force an appveyor run)

os: Visual Studio 2015

Expand Down
2 changes: 1 addition & 1 deletion docs/DEVGUIDE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Some useful make commands:
make test # run unit tests
make test-memleaks # run memory leak tests
make test-coverage # run test coverage
make flake8 # run PEP8 linter
make lint # run Python (PEP8) and C linters
There are some differences between ``make`` on UNIX and Windows.
For instance, to run a specific Python version. On UNIX:
Expand Down
4 changes: 4 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,10 @@ Supported Python versions are 2.6, 2.7, 3.4+ and PyPy3.
Timeline
========

- 2020-02-18:
`5.7.0 <https://pypi.org/project/psutil/5.7.0/#files>`__ -
`what's new <https://github.com/giampaolo/psutil/blob/master/HISTORY.rst#570>`__ -
`diff <https://github.com/giampaolo/psutil/compare/release-5.6.7...release-5.7.0#files_bucket>`__
- 2019-11-26:
`5.6.7 <https://pypi.org/project/psutil/5.6.7/#files>`__ -
`what's new <https://github.com/giampaolo/psutil/blob/master/HISTORY.rst#567>`__ -
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* - psutil.Process.io_counters read count is always 0
* - psutil.Process.io_counters may not be available on older AIX versions
* - psutil.Process.threads may not be available on older AIX versions
# - psutil.net_io_counters may not be available on older AIX versions
* - psutil.net_io_counters may not be available on older AIX versions
* - reading basic process info may fail or return incorrect values when
* process is starting (see IBM APAR IV58499 - fixed in newer AIX versions)
* - sockets and pipes may not be counted in num_fds (fixed in newer AIX
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psutil_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ psutil_convert_ipaddr(struct sockaddr *addr, int family) {
// XXX we get here on FreeBSD when processing 'lo' / AF_INET6
// broadcast. Not sure what to do other than returning None.
// ifconfig does not show anything BTW.
//PyErr_Format(PyExc_RuntimeError, gai_strerror(err));
//return NULL;
// PyErr_Format(PyExc_RuntimeError, gai_strerror(err));
// return NULL;
Py_INCREF(Py_None);
return Py_None;
}
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ proc_io_counters(PyObject* self, PyObject* args) {
info.pr_inblk,
info.pr_oublk);
}
*/
*/


/*
Expand Down
2 changes: 1 addition & 1 deletion psutil/arch/aix/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ psutil_read_process_table(int * num) {

*num = np;
return processes;
}
}
2 changes: 1 addition & 1 deletion psutil/arch/aix/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ int getifaddrs(struct ifaddrs **ifap)
close(sd);
freeifaddrs(*ifap);
return (-1);
}
}
3 changes: 1 addition & 2 deletions psutil/arch/aix/ifaddrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ struct ifaddrs {

extern int getifaddrs(struct ifaddrs **);
extern void freeifaddrs(struct ifaddrs *);

#endif
#endif
2 changes: 1 addition & 1 deletion psutil/arch/aix/net_connections.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

PyObject* psutil_net_connections(PyObject *self, PyObject *args);

#endif /* __NET_CONNECTIONS_H__ */
#endif /* __NET_CONNECTIONS_H__ */
2 changes: 1 addition & 1 deletion psutil/arch/aix/net_kernel_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ struct mbuf64

#define m_len m_hdr.mh_len

#endif /* __64BIT__ */
#endif /* __64BIT__ */
2 changes: 1 addition & 1 deletion psutil/arch/freebsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ psutil_disk_io_counters(PyObject *self, PyObject *args) {
PyObject *
psutil_proc_memory_maps(PyObject *self, PyObject *args) {
// Return a list of tuples for every process memory maps.
//'procstat' cmdline utility has been used as an example.
// 'procstat' cmdline utility has been used as an example.
pid_t pid;
int ptrwidth;
int i, cnt;
Expand Down
4 changes: 2 additions & 2 deletions psutil/arch/solaris/environ.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <Python.h>

#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
# undef _FILE_OFFSET_BITS
# undef _LARGEFILE64_SOURCE
#undef _FILE_OFFSET_BITS
#undef _LARGEFILE64_SOURCE
#endif

#include <sys/types.h>
Expand Down
2 changes: 1 addition & 1 deletion psutil/arch/windows/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ psutil_net_if_stats(PyObject *self, PyObject *args) {
}

// is up?
if((pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED ||
if ((pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED ||
pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL) &&
pIfRow->dwAdminStatus == 1 ) {
py_is_up = Py_True;
Expand Down
16 changes: 14 additions & 2 deletions scripts/internal/.git-pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def main():
out = sh("git diff --cached --name-only")
py_files = [x for x in out.split('\n') if x.endswith('.py') and
os.path.exists(x)]
c_files = [x for x in out.split('\n') if x.endswith(('.c', '.h')) and
os.path.exists(x)]

lineno = 0
kw = {'encoding': 'utf8'} if sys.version_info[0] == 3 else {}
Expand All @@ -100,20 +102,30 @@ def main():
print("%s:%s %s" % (path, lineno, line))
return exit("commit aborted: bare except clause")

# flake8
# Python linter
if py_files:
try:
import flake8 # NOQA
except ImportError:
return exit("commit aborted: flake8 is not installed; "
"run 'make setup-dev-env'")

# XXX: we should scape spaces and possibly other amenities here
# XXX: we should escape spaces and possibly other amenities here
ret = subprocess.call(
"%s -m flake8 %s" % (sys.executable, " ".join(py_files)),
shell=True)
if ret != 0:
return exit("commit aborted: python code is not flake8 compliant")

# C linter
if c_files:
# XXX: we should escape spaces and possibly other amenities here
cmd = "%s scripts/internal/clinter.py %s" % (
sys.executable, " ".join(c_files))
print(cmd)
ret = subprocess.call(cmd, shell=True)
if ret != 0:
return exit("commit aborted: C code didn't pass style check")


main()
76 changes: 76 additions & 0 deletions scripts/internal/clinter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python3

# Copyright (c) 2009 Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A super simple linter to check C syntax."""

from __future__ import print_function
import argparse
import sys


warned = False


def warn(path, line, lineno, msg):
global warned
warned = True
print("%s:%s: %s" % (path, lineno, msg), file=sys.stderr)


def check_line(path, line, idx, lines):
s = line
lineno = idx + 1
eof = lineno == len(lines)
if s.endswith(' \n'):
warn(path, line, lineno, "extra space at EOL")
elif '\t' in line:
warn(path, line, lineno, "line has a tab")
elif s.endswith('\r\n'):
warn(path, line, lineno, "Windows line ending")
# end of global block, e.g. "}newfunction...":
elif s == "}\n":
if not eof:
nextline = lines[idx + 1]
# "#" is a pre-processor line
if nextline != '\n' and \
nextline.strip()[0] != '#' and \
nextline.strip()[:2] != '*/':
warn(path, line, lineno, "expected 1 blank line")

sls = s.lstrip()
if sls.startswith('//') and sls[2] != ' ' and line.strip() != '//':
warn(path, line, lineno, "no space after // comment")

# e.g. "if(..." after keywords
keywords = ("if", "else", "while", "do", "enum", "for")
for kw in keywords:
if sls.startswith(kw + '('):
warn(path, line, lineno, "missing space between %r and '('" % kw)
# eof
if eof:
if not line.endswith('\n'):
warn(path, line, lineno, "no blank line at EOF")


def process(path):
with open(path, 'rt') as f:
lines = f.readlines()
for idx, line in enumerate(lines):
check_line(path, line, idx, lines)


def main():
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs='+', help='path(s) to a file(s)')
args = parser.parse_args()
for path in args.paths:
process(path)
if warned:
sys.exit(1)


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions scripts/internal/winmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def setup_dev_env():
sh("%s -m pip install -U %s" % (PYTHON, " ".join(DEPS)))


def flake8():
def lint():
"""Run flake8 against all py files"""
py_files = subprocess.check_output("git ls-files")
if PY3:
Expand Down Expand Up @@ -551,11 +551,11 @@ def main():
sp.add_parser('build', help="build")
sp.add_parser('clean', help="deletes dev files")
sp.add_parser('coverage', help="run coverage tests.")
sp.add_parser('flake8', help="run flake8 against all py files")
sp.add_parser('help', help="print this help")
sp.add_parser('install', help="build + install in develop/edit mode")
sp.add_parser('install-git-hooks', help="install GIT pre-commit hook")
sp.add_parser('install-pip', help="install pip")
sp.add_parser('lint', help="run flake8 against all py files")
sp.add_parser('print-access-denied', help="print AD exceptions")
sp.add_parser('print-api-speed', help="benchmark all API calls")
sp.add_parser('setup-dev-env', help="install deps")
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ def main():
'Topic :: System :: Networking :: Monitoring',
'Topic :: System :: Networking',
'Topic :: System :: Operating System',
'Topic :: System :: Power (UPS)'
'Topic :: System :: Systems Administration',
'Topic :: Utilities',
],
Expand Down

0 comments on commit 5e47e0b

Please sign in to comment.