Skip to content

Commit 426f656

Browse files
committed
Add support for --sleep in importImages
1 parent a33eae3 commit 426f656

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

miraheze/mediawiki/mwimport.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
import sys
77

88

9+
# The threshold for --images-sleep's automatic calculation, where it'll decide
10+
# whether or not to sleep for 0s or 1s.
11+
# https://wm-bot.wmcloud.org/logs/%23miraheze-tech-ops/20250714.txt#:~:text=[05:37:19],That's%20sensible
12+
_IMAGES_SLEEP_AUTO_THRESHOLD = 1000
13+
14+
915
def parse_args(input_args: list | None = None, check_paths: bool = True) -> argparse.Namespace:
1016
parser = argparse.ArgumentParser(description='A script to automate manual wiki imports')
1117
parser.add_argument(
@@ -28,6 +34,10 @@ def parse_args(input_args: list | None = None, check_paths: bool = True) -> argp
2834
'--search-recursively', action='store_true',
2935
help='Whether or not to pass --search-recursively (check files in subdirectories) to importImages.php',
3036
)
37+
parser.add_argument(
38+
'--images-sleep', type=int, default=-1,
39+
help='The time to sleep between importing images for importImages.php (negative for auto-calculation)',
40+
)
3141
parser.add_argument('wiki', help='Database name of the wiki to import to')
3242

3343
args = parser.parse_args(input_args)
@@ -46,9 +56,28 @@ def parse_args(input_args: list | None = None, check_paths: bool = True) -> argp
4656
if args.images and not os.path.exists(args.images):
4757
raise ValueError(f'Cannot find images to import: {repr(args.images)}')
4858

59+
if args.images and args.images_sleep < 0:
60+
args.images_sleep = calculate_images_sleep(args.images) if check_paths else 0
61+
4962
return args
5063

5164

65+
def calculate_images_sleep(images: str) -> int:
66+
# In the interest of code simplicity, all calculations are done assuming that
67+
# --search-recursively is passed. It is unlikely where one wants to only upload
68+
# files from a directory but not its subdirectories anyway, and this is meant
69+
# to be a "eh, good enough" heuristic, so an "eh, good enough" algorithm for
70+
# edge cases seems acceptable.
71+
total = 0
72+
73+
for _, _, files in os.walk(images):
74+
total += len(files)
75+
if total >= _IMAGES_SLEEP_AUTO_THRESHOLD:
76+
return 1
77+
78+
return 0
79+
80+
5281
def log(message: str): # pragma: no cover
5382
subprocess.run(
5483
['/usr/local/bin/logsalmsg', message],
@@ -76,7 +105,7 @@ def get_scripts(args: argparse.Namespace) -> list[list[str]]:
76105
scripts.append(script)
77106

78107
if args.images:
79-
script = ['importImages', f'--comment={args.images_comment}']
108+
script = ['importImages', f'--sleep={args.images_sleep}', f'--comment={args.images_comment}']
80109
if args.search_recursively:
81110
script.append('--search-recursively')
82111
script.extend(['--', args.images])

tests/test_mwimport.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,54 @@ def test_parse_args_both_xml_images_exists():
105105
assert args.images == images
106106

107107

108+
def test_parse_args_images_sleep_manual():
109+
args = mwimport.parse_args([
110+
'--images=images',
111+
'--images-comment=Importing from https://example.com',
112+
'--images-sleep=5',
113+
'examplewiki',
114+
], False)
115+
116+
assert args.images_sleep == 5
117+
118+
119+
def test_parse_args_images_sleep_auto_below_threshold():
120+
with tempfile.TemporaryDirectory() as tempdir:
121+
# Populate five test files
122+
for i in range(5):
123+
with open(os.path.join(tempdir, f'{i}.txt'), 'w'):
124+
pass
125+
126+
args = mwimport.parse_args([
127+
f'--images={tempdir}',
128+
'--images-comment=Importing from https://example.com',
129+
'examplewiki',
130+
])
131+
132+
assert args.images_sleep == 0
133+
134+
135+
def test_parse_args_images_sleep_auto_above_threshold():
136+
with tempfile.TemporaryDirectory() as tempdir:
137+
# Populate 1000 test files, bucketed in 10 directories
138+
# (to test file counts with subdirectories)
139+
for folder in range(10):
140+
folder = os.path.join(tempdir, str(folder))
141+
os.mkdir(folder)
142+
143+
for file in range(100):
144+
with open(os.path.join(folder, f'{file}.txt'), 'w'):
145+
pass
146+
147+
args = mwimport.parse_args([
148+
f'--images={tempdir}',
149+
'--images-comment=Importing from https://example.com',
150+
'examplewiki',
151+
])
152+
153+
assert args.images_sleep == 1
154+
155+
108156
def test_get_scripts_xml_images():
109157
args = mwimport.parse_args([
110158
'--version=0.42',
@@ -116,7 +164,7 @@ def test_get_scripts_xml_images():
116164
scripts = mwimport.get_scripts(args)
117165
expected = [
118166
['importDump', '--no-updates', '--', 'dump.xml'],
119-
['importImages', '--comment=Importing from https://example.com', '--', 'images'],
167+
['importImages', '--sleep=0', '--comment=Importing from https://example.com', '--', 'images'],
120168
['rebuildall'],
121169
['initEditCount'],
122170
['initSiteStats', '--update'],
@@ -157,7 +205,7 @@ def test_get_scripts_search_recursively():
157205
], False)
158206
scripts = mwimport.get_scripts(args)
159207
expected = [
160-
['importImages', '--comment=Importing from https://example.com', '--search-recursively', '--', 'images'],
208+
['importImages', '--sleep=0', '--comment=Importing from https://example.com', '--search-recursively', '--', 'images'],
161209
['initSiteStats', '--update'],
162210
]
163211
expected = [

0 commit comments

Comments
 (0)