Skip to content

Commit e375c81

Browse files
committed
[utils] Add optional --directory flag to update_test_body.py
1 parent 62ec7b8 commit e375c81

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

llvm/utils/update_test_body.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@ def process(args, path):
4949
if not seen_gen:
5050
print("'gen' does not exist", file=sys.stderr)
5151
return 1
52-
with tempfile.TemporaryDirectory(prefix="update_test_body_") as dir:
52+
53+
# Use the provided directory if specified, otherwise create a temporary one
54+
if args.directory:
55+
dir = args.directory
56+
# Create the directory if it doesn't exist
57+
os.makedirs(dir, exist_ok=True)
58+
cleanup_dir = False
59+
else:
60+
temp_dir = tempfile.TemporaryDirectory(prefix="update_test_body_")
61+
dir = temp_dir.name
62+
cleanup_dir = True
63+
64+
try:
5365
try:
5466
# If the last line starts with ".endif", remove it.
5567
sub = subprocess.run(
@@ -65,7 +77,7 @@ def process(args, path):
6577
return 1
6678
with cd(dir):
6779
if args.shell:
68-
print(f"invoke shell in the temporary directory '{dir}'")
80+
print(f"invoke shell in the directory '{dir}'")
6981
subprocess.run([os.environ.get("SHELL", "sh")])
7082
return 0
7183

@@ -88,6 +100,10 @@ def process(args, path):
88100
print("stdout is empty; forgot -o - ?", file=sys.stderr)
89101
return 1
90102
content = sub.stdout.decode()
103+
finally:
104+
# Clean up the temporary directory if we created one
105+
if cleanup_dir:
106+
temp_dir.cleanup()
91107

92108
with open(path, "w") as f:
93109
# Print lines up to '.endif'.
@@ -103,6 +119,9 @@ def process(args, path):
103119
parser.add_argument(
104120
"--shell", action="store_true", help="invoke shell instead of 'gen'"
105121
)
122+
parser.add_argument(
123+
"--directory", "-d", help="use specified directory instead of a temporary one"
124+
)
106125
args = parser.parse_args()
107126
for path in args.files:
108127
retcode = process(args, path)

0 commit comments

Comments
 (0)