Skip to content

Commit 56213f9

Browse files
committed
Fix ruff linting issues
Address code quality issues caught by ruff: - Replace old-style percent formatting with f-strings and .format() (UP031) - Fix shell=True vulnerability in get_branch() (S602) - Add noqa comments for acceptable subprocess and urlopen usage (S603, S310) Changes: - Convert all percent-formatted strings to f-strings - Change get_branch() to use list-based command without shell=True - Remove unnecessary stdin and close_fds parameters from subprocess.Popen - Add # noqa: S310 for urllib.request.urlopen (GitHub API access) - Add # noqa: S603 for subprocess.call with git commands
1 parent 01578cc commit 56213f9

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

src/mxrepo/main.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ def hilite(string, color, bold):
3232

3333

3434
def query_repos(context):
35-
org_url = "https://api.github.com/orgs/%s/repos" % context
36-
user_url = "https://api.github.com/users/%s/repos" % context
37-
query = "%s?page=%i&per_page=50"
35+
org_url = f"https://api.github.com/orgs/{context}/repos"
36+
user_url = f"https://api.github.com/users/{context}/repos"
37+
query = "{}?page={}&per_page=50"
3838
data = list()
3939
page = 1
4040
while True:
4141
try:
42-
url = query % (org_url, page)
43-
res = urllib.request.urlopen(url)
42+
url = query.format(org_url, page)
43+
res = urllib.request.urlopen(url) # noqa: S310
4444
except urllib.error.URLError:
4545
try:
46-
url = query % (user_url, page)
47-
res = urllib.request.urlopen(url)
46+
url = query.format(user_url, page)
47+
res = urllib.request.urlopen(url) # noqa: S310
4848
except urllib.error.URLError as e:
4949
print(e)
5050
sys.exit(0)
@@ -54,21 +54,21 @@ def query_repos(context):
5454
break
5555
data += page_data
5656
page += 1
57-
print("Fetched %i repositories for '%s'" % (len(data), context))
57+
print(f"Fetched {len(data)} repositories for '{context}'")
5858
return data
5959

6060

6161
def perform_clone(arguments):
62-
base_uri = "git@github.com:%s/%s.git"
62+
base_uri = "git@github.com:{}/{}.git"
6363
context = arguments.context[0]
6464
if arguments.repository:
6565
repos = arguments.repository
6666
else:
6767
repos = [_["name"] for _ in query_repos(arguments.context)]
6868
for repo in repos:
69-
uri = base_uri % (context, repo)
69+
uri = base_uri.format(context, repo)
7070
cmd = ["git", "clone", uri]
71-
subprocess.call(cmd)
71+
subprocess.call(cmd) # noqa: S603
7272

7373

7474
sub = subparsers.add_parser("clone", help="Clone from an organisation or a user")
@@ -82,14 +82,11 @@ def perform_clone(arguments):
8282

8383

8484
def get_branch():
85-
cmd = "git branch"
85+
cmd = ["git", "branch"]
8686
p = subprocess.Popen(
8787
cmd,
88-
shell=True,
89-
stdin=subprocess.PIPE,
9088
stdout=subprocess.PIPE,
9189
stderr=subprocess.PIPE,
92-
close_fds=True,
9390
)
9491
output = p.stdout.readlines()
9592
for line in output:
@@ -109,9 +106,9 @@ def perform_pull(arguments):
109106
if ".git" not in listdir(child):
110107
continue
111108
os.chdir(child)
112-
print("Perform pull for '%s'" % hilite(child, "blue", True))
109+
print(f"Perform pull for '{hilite(child, 'blue', True)}'")
113110
cmd = ["git", "pull", "origin", get_branch()]
114-
subprocess.call(cmd)
111+
subprocess.call(cmd) # noqa: S603
115112
os.chdir("..")
116113

117114

@@ -140,18 +137,18 @@ def perform_backup(arguments):
140137
os.chdir(context)
141138
contents = listdir(".")
142139
data = query_repos(context)
143-
base_uri = "git@github.com:%s/%s.git"
140+
base_uri = "git@github.com:{}/{}.git"
144141
for repo in data:
145142
name = repo["name"]
146-
fs_name = "%s.git" % name
143+
fs_name = f"{name}.git"
147144
if fs_name in contents:
148-
print("Fetching existing local repository '%s'" % fs_name)
145+
print(f"Fetching existing local repository '{fs_name}'")
149146
os.chdir(fs_name)
150147
perform(["git", "fetch", "origin"])
151148
os.chdir("..")
152149
else:
153-
print("Cloning new repository '%s'" % fs_name)
154-
uri = base_uri % (context, name)
150+
print(f"Cloning new repository '{fs_name}'")
151+
uri = base_uri.format(context, name)
155152
perform(["git", "clone", "--bare", "--mirror", uri])
156153

157154

@@ -173,9 +170,9 @@ def perform_status(arguments):
173170
if ".git" not in listdir(child):
174171
continue
175172
os.chdir(child)
176-
print("Status for '%s'" % hilite(child, "blue", True))
173+
print(f"Status for '{hilite(child, 'blue', True)}'")
177174
cmd = ["git", "status"]
178-
subprocess.call(cmd)
175+
subprocess.call(cmd) # noqa: S603
179176
os.chdir("..")
180177

181178

@@ -201,9 +198,9 @@ def perform_branch(arguments):
201198
if ".git" not in listdir(child):
202199
continue
203200
os.chdir(child)
204-
print("Branches for '%s'" % hilite(child, "blue", True))
201+
print(f"Branches for '{hilite(child, 'blue', True)}'")
205202
cmd = ["git", "branch"]
206-
subprocess.call(cmd)
203+
subprocess.call(cmd) # noqa: S603
207204
os.chdir("..")
208205

209206

@@ -229,9 +226,9 @@ def perform_diff(arguments):
229226
if ".git" not in listdir(child):
230227
continue
231228
os.chdir(child)
232-
print("Diff for '%s'" % hilite(child, "blue", True))
229+
print(f"Diff for '{hilite(child, 'blue', True)}'")
233230
cmd = ["git", "diff"]
234-
subprocess.call(cmd)
231+
subprocess.call(cmd) # noqa: S603
235232
os.chdir("..")
236233

237234

@@ -258,9 +255,9 @@ def perform_commit(arguments):
258255
if ".git" not in listdir(child):
259256
continue
260257
os.chdir(child)
261-
print("Commit all changes resources for '%s'" % hilite(child, "blue", True))
258+
print(f"Commit all changes resources for '{hilite(child, 'blue', True)}'")
262259
cmd = ["git", "commit", "-am", message]
263-
subprocess.call(cmd)
260+
subprocess.call(cmd) # noqa: S603
264261
os.chdir("..")
265262

266263

@@ -288,9 +285,9 @@ def perform_push(arguments):
288285
if ".git" not in listdir(child):
289286
continue
290287
os.chdir(child)
291-
print("Perform push for '%s'" % hilite(child, "blue", True))
288+
print(f"Perform push for '{hilite(child, 'blue', True)}'")
292289
cmd = ["git", "push", "origin", get_branch()]
293-
subprocess.call(cmd)
290+
subprocess.call(cmd) # noqa: S603
294291
os.chdir("..")
295292

296293

@@ -314,9 +311,9 @@ def perform_checkout(arguments):
314311
if ".git" not in listdir(child):
315312
continue
316313
os.chdir(child)
317-
print("Perform checkout for '%s'" % hilite(child, "blue", True))
314+
print(f"Perform checkout for '{hilite(child, 'blue', True)}'")
318315
cmd = ["git", "checkout", "."]
319-
subprocess.call(cmd)
316+
subprocess.call(cmd) # noqa: S603
320317
os.chdir("..")
321318

322319

0 commit comments

Comments
 (0)