Commit d5de689d authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

minor tweaks to utils.py

Summary:
Update `make_temp_dir()` to use `shutil.rmtree()` to clean up rather than just
`os.rmdir()`.  If the function using this context manager throws it may leave
behind files inside the temporary directory, and it is useful to always clean
those up.

Also update `read_fbcode_builder_config()` to work regardless of the current
working directory.  Previously this function only worked if the current working
directory was the directory containing the configuration file.  This makes sure
that `read_fbcode_builder_config()` calls are always treated as relative to the
directory containing the configuration file being read.

Reviewed By: yfeldblum

Differential Revision: D7597743

fbshipit-source-id: 817063104081336fcc0a9f825beb74d7d4377499
parent 59791afd
...@@ -8,6 +8,7 @@ from __future__ import unicode_literals ...@@ -8,6 +8,7 @@ from __future__ import unicode_literals
import itertools import itertools
import logging import logging
import os import os
import shutil
import subprocess import subprocess
import sys import sys
...@@ -34,8 +35,7 @@ def make_temp_dir(d): ...@@ -34,8 +35,7 @@ def make_temp_dir(d):
try: try:
yield d yield d
finally: finally:
if os.path.exists(d): shutil.rmtree(d, ignore_errors=True)
os.rmdir(d)
@contextmanager @contextmanager
...@@ -50,9 +50,19 @@ def push_dir(d): ...@@ -50,9 +50,19 @@ def push_dir(d):
def read_fbcode_builder_config(filename): def read_fbcode_builder_config(filename):
# Allow one spec to read another # Allow one spec to read another
scope = {'read_fbcode_builder_config': read_fbcode_builder_config} # When doing so, treat paths as relative to the config's project directory.
project_dir = os.path.dirname(filename)
def inner_read_config(path):
full_path = os.path.join(project_dir, path)
return read_fbcode_builder_config(full_path)
scope = {'read_fbcode_builder_config': inner_read_config}
with open(filename) as config_file: with open(filename) as config_file:
exec(config_file.read(), scope) # Note that this will need to be changed to an exec() function call for
# python 3 compatibility. Unfortunately python 2.7 does not seem to
# treat the scope correctly when using exec() function syntax here.
exec config_file.read() in scope
return scope['config'] return scope['config']
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment