Skip to content

Commit 70af03e

Browse files
committed
Fix all pylint warnings
1 parent 03245dd commit 70af03e

File tree

8 files changed

+96
-87
lines changed

8 files changed

+96
-87
lines changed

pylintrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[MESSAGES CONTROL]
22

33
disable =
4-
consider-using-with,
54
duplicate-code,
65
missing-docstring,
7-
redefined-outer-name,

userland/core/io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def perror(*errors: Any) -> None:
1414
@contextlib.contextmanager
1515
def safe_open(*args, **kwargs) -> Generator[IO | None]:
1616
try:
17+
# pylint: disable=unspecified-encoding
1718
with open(*args, **kwargs) as io:
1819
yield io
1920
except OSError as e:

userland/core/users.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def parse_owner_spec(self, owner_spec: str) -> tuple[int | None, int | None]:
2525

2626
return uid, gid
2727

28+
# pylint: disable=inconsistent-return-statements
2829
def parse_user(self, user: str) -> int:
2930
"""
3031
Accept a string representing a username or UID and return the UID.
@@ -38,6 +39,7 @@ def parse_user(self, user: str) -> int:
3839
except KeyError:
3940
self.error(f"invalid user: {user}")
4041

42+
# pylint: disable=inconsistent-return-statements
4143
def parse_group(self, group: str) -> int:
4244
"""
4345
Accept a string representing a group name or GID and return the GID.

userland/utilities/cat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def python_userland_cat(opts, args: list[str]):
154154
streams.append(core.readlines_stdin_raw())
155155
else:
156156
try:
157+
# pylint: disable=consider-using-with
157158
streams.append(open(name, "rb"))
158159
except OSError as e:
159160
failed = True

userland/utilities/chgrp.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@
124124
)
125125

126126

127+
def get_new_group(opts, args: list[str]) -> tuple[int, str]:
128+
if opts.reference:
129+
gid = Path(opts.reference).stat(follow_symlinks=True).st_gid
130+
131+
return gid, core.group_display_name_from_id(gid)
132+
133+
parser.expect_nargs(args, (2,))
134+
gname = args.pop(0)
135+
136+
return parser.parse_group(gname), gname
137+
138+
127139
@core.command(parser)
128140
def python_userland_chgrp(opts, args: list[str]):
129141
parser.expect_nargs(args, (1,))
@@ -134,22 +146,22 @@ def python_userland_chgrp(opts, args: list[str]):
134146
if opts.from_spec:
135147
from_uid, from_gid = parser.parse_owner_spec(opts.from_spec)
136148

137-
gid: int
138-
gname: str | None = None
149+
try:
150+
gid, gname = get_new_group(opts, args)
151+
except OSError as e:
152+
core.perror(e)
153+
return 1
139154

140-
if opts.reference:
141-
try:
142-
gid = Path(opts.reference).stat(follow_symlinks=True).st_gid
143-
except OSError as e:
144-
core.perror(e)
145-
return 1
146-
else:
147-
parser.expect_nargs(args, (2,))
148-
gname = args.pop(0)
155+
failed = False
149156

150-
gid = parser.parse_group(gname)
157+
def handle_error(err: Exception, level: int, msg: str) -> None:
158+
nonlocal failed
159+
failed = True
151160

152-
failed = False
161+
if opts.verbosity:
162+
core.perror(err)
163+
if opts.verbosity > level:
164+
print(msg, file=sys.stderr)
153165

154166
for file in core.traverse_files(
155167
(
@@ -169,14 +181,7 @@ def python_userland_chgrp(opts, args: list[str]):
169181
prev_uid = stat.st_uid
170182
prev_gid = stat.st_gid
171183
except OSError as e:
172-
failed = True
173-
if opts.verbosity:
174-
core.perror(e)
175-
if opts.verbosity > 2:
176-
print(
177-
f"failed to change group of '{file}' to {gname or gid}",
178-
file=sys.stderr,
179-
)
184+
handle_error(e, 2, f"failed to change group of '{file}' to {gname or gid}")
180185
continue
181186

182187
prev_gname = core.group_display_name_from_id(prev_gid)
@@ -195,14 +200,7 @@ def python_userland_chgrp(opts, args: list[str]):
195200
try:
196201
shutil.chown(file, group=gid, follow_symlinks=opts.dereference)
197202
except OSError as e:
198-
failed = True
199-
if opts.verbosity:
200-
core.perror(e)
201-
if opts.verbosity > 2:
202-
print(
203-
f"failed to change group of '{file}' to {gname or gid}",
204-
file=sys.stderr,
205-
)
203+
handle_error(e, 2, f"failed to change group of '{file}' to {gname or gid}")
206204
continue
207205

208206
if prev_gid == gid:

userland/utilities/chown.py

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -125,51 +125,65 @@
125125
)
126126

127127

128-
@core.command(parser)
129-
def python_userland_chown(opts, args: list[str]):
130-
parser.expect_nargs(args, (1,))
131-
132-
from_uid: int | None = None
133-
from_gid: int | None = None
134-
135-
if opts.from_spec:
136-
from_uid, from_gid = parser.parse_owner_spec(opts.from_spec)
137-
138-
chown_args = {"follow_symlinks": opts.dereference}
139-
140-
owner_spec: str
141-
128+
def get_new_owner(opts, args: list[str], chown_args: dict) -> str | None:
142129
if opts.reference:
143130
try:
144131
ref_stat = Path(opts.reference).stat(follow_symlinks=True)
145132
except OSError as e:
146133
core.perror(e)
147-
return 1
134+
return None
148135

149136
chown_args["user"] = ref_stat.st_uid
150137
chown_args["group"] = ref_stat.st_gid
151138

152-
owner_spec = (
139+
return (
153140
core.user_display_name_from_id(ref_stat.st_uid)
154141
+ ":"
155142
+ core.group_display_name_from_id(ref_stat.st_gid)
156143
)
157-
else:
158-
parser.expect_nargs(args, (2,))
159-
owner_spec = args.pop(0)
160144

161-
if not (owner_match := CHOWN_PATTERN.match(owner_spec)):
162-
parser.error(f"invalid owner spec: {owner_spec}")
145+
parser.expect_nargs(args, (2,))
146+
owner_spec = args.pop(0)
163147

164-
chown_args["user"] = (
165-
parser.parse_user(owner_match.group(1)) if owner_match.group(1) else None
166-
)
167-
chown_args["group"] = (
168-
parser.parse_group(owner_match.group(3)) if owner_match.group(3) else None
169-
)
148+
if not (owner_match := CHOWN_PATTERN.match(owner_spec)):
149+
parser.error(f"invalid owner spec: {owner_spec}")
150+
151+
chown_args["user"] = (
152+
parser.parse_user(owner_match.group(1)) if owner_match.group(1) else None
153+
)
154+
chown_args["group"] = (
155+
parser.parse_group(owner_match.group(3)) if owner_match.group(3) else None
156+
)
157+
158+
return owner_spec
159+
160+
161+
@core.command(parser)
162+
def python_userland_chown(opts, args: list[str]):
163+
parser.expect_nargs(args, (1,))
164+
165+
from_uid: int | None = None
166+
from_gid: int | None = None
167+
168+
if opts.from_spec:
169+
from_uid, from_gid = parser.parse_owner_spec(opts.from_spec)
170+
171+
chown_args = {"follow_symlinks": opts.dereference}
172+
173+
if not (owner_spec := get_new_owner(opts, args, chown_args)):
174+
return 1
170175

171176
failed = False
172177

178+
def handle_error(err: Exception, level: int, msg: str) -> None:
179+
nonlocal failed
180+
failed = True
181+
182+
if opts.verbosity:
183+
core.perror(err)
184+
if opts.verbosity > level:
185+
print(msg, file=sys.stderr)
186+
173187
for file in core.traverse_files(
174188
(tqdm(args, ascii=True, desc="Changing ownership") if opts.progress else args),
175189
recurse_mode=opts.recurse_mode if opts.recursive else None,
@@ -184,14 +198,9 @@ def python_userland_chown(opts, args: list[str]):
184198
prev_uid = stat.st_uid
185199
prev_gid = stat.st_gid
186200
except OSError as e:
187-
failed = True
188-
if opts.verbosity:
189-
core.perror(e)
190-
if opts.verbosity > 2:
191-
print(
192-
f"failed to change ownership of '{file}' to {owner_spec}",
193-
file=sys.stderr,
194-
)
201+
handle_error(
202+
e, 2, f"failed to change ownership of '{file}' to {owner_spec}"
203+
)
195204
continue
196205

197206
prev_uname = core.user_display_name_from_id(prev_uid)
@@ -207,14 +216,9 @@ def python_userland_chown(opts, args: list[str]):
207216
try:
208217
shutil.chown(file, **chown_args)
209218
except OSError as e:
210-
failed = True
211-
if opts.verbosity:
212-
core.perror(e)
213-
if opts.verbosity > 2:
214-
print(
215-
f"failed to change ownership of '{file}' to {owner_spec}",
216-
file=sys.stderr,
217-
)
219+
handle_error(
220+
e, 2, f"failed to change ownership of '{file}' to {owner_spec}"
221+
)
218222
continue
219223

220224
if prev_uid == chown_args["user"] or prev_gid == chown_args["group"]:

userland/utilities/env.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,26 @@
4949
)
5050

5151

52+
def parse_env_args(args: list[str], env: dict[str, str], prog_args: list[str]) -> None:
53+
parsing_decls = True
54+
55+
for arg in args:
56+
if parsing_decls and (eq_pos := arg.find("=")) >= 0:
57+
env[arg[:eq_pos]] = arg[eq_pos + 1 :]
58+
else:
59+
prog_args.append(arg)
60+
parsing_decls = False
61+
62+
5263
@core.command(parser)
64+
# pylint: disable=inconsistent-return-statements
5365
def python_userland_env(opts, args: list[str]):
5466
if args and args[0] == "-":
5567
opts.ignore_environment = True
5668
del args[0]
5769

70+
env: dict[str, str]
71+
5872
if opts.ignore_environment:
5973
env = {}
6074
elif opts.unset:
@@ -65,14 +79,7 @@ def python_userland_env(opts, args: list[str]):
6579
env = os.environ.copy()
6680

6781
prog_args = []
68-
69-
parsing_decls = True
70-
for arg in args:
71-
if parsing_decls and (eq_pos := arg.find("=")) >= 0:
72-
env[arg[:eq_pos]] = arg[eq_pos + 1 :]
73-
else:
74-
prog_args.append(arg)
75-
parsing_decls = False
82+
parse_env_args(args, env, prog_args)
7683

7784
if opts.split_string:
7885
prog_args = shlex.split(opts.split_string) + prog_args
@@ -96,5 +103,3 @@ def python_userland_env(opts, args: list[str]):
96103
except OSError as e:
97104
core.perror(e)
98105
return 126 if isinstance(e, FileNotFoundError) else 127
99-
100-
return 0

userland/utilities/id.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def python_userland_id(opts, args: list[str]):
4848
if opts.context:
4949
parser.error("--context (-Z) is not supported")
5050

51-
if (ugG := (opts.user, opts.group, opts.groups)).count(True) > 1:
51+
if (ugg := (opts.user, opts.group, opts.groups)).count(True) > 1:
5252
parser.error("cannot print more than one of -ugG")
5353

54-
if opts.name or opts.real and not any(ugG):
54+
if opts.name or opts.real and not any(ugg):
5555
parser.error("cannot print only names or real IDs in default format")
5656

57-
if opts.zero and not any(ugG):
57+
if opts.zero and not any(ugg):
5858
parser.error("option --zero not permitted in default format")
5959

6060
process_uid = os.getuid() if opts.real else os.geteuid()

0 commit comments

Comments
 (0)