Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new argument to generate_dynamic: tmp_dir_path and tmp_file_name #125

Open
wants to merge 4 commits into
base: kinetic-devel
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/genpy/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ def _gen_dyn_modify_references(py_text, current_type, types):
return py_text


def generate_dynamic(core_type, msg_cat):
def generate_dynamic(core_type, msg_cat, tmp_dir_path=None, tmp_file_name=None):
"""
Dymamically generate message classes from msg_cat .msg text gendeps dump.

This method modifies sys.path to include a temp file directory.
:param core_type str: top-level ROS message type of concatenated .msg text
:param msg_cat str: concatenation of full message text (output of gendeps --cat)
:param tmp_dir str: custom dir path to store tmp_file. default will
:param tmp_file str: custom tmp_file name
:raises: MsgGenerationException If dep_msg is improperly formatted
"""
msg_context = MsgContext.create_default()
Expand Down Expand Up @@ -161,16 +163,26 @@ def generate_dynamic(core_type, msg_cat):
buff.write(line + '\n')
full_text = buff.getvalue()

# Create a temporary directory
tmp_dir = tempfile.mkdtemp(prefix='genpy_')

# Afterwards, we are going to remove the directory so that the .pyc file gets cleaned up if it's still around
atexit.register(shutil.rmtree, tmp_dir)

# write the entire text to a file and import it (it will get deleted when tmp_dir goes - above)
tmp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.py', dir=tmp_dir, delete=False)
tmp_file.file.write(full_text)
tmp_file.file.close()
if tmp_dir_path:
# Create a temporary directory with specified path
tmp_dir = os.mkdir(tmp_dir_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably handle the case that the directory already exists.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought process was that if an output_file was given, it would overwrite the file if it exists. However, if the file does not exist, it would create a file at the given output_file path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By just removing this line it doesn't handle the case anymore where the directory doesn't exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By just removing this line it doesn't handle the case anymore where the directory doesn't exist.

@jimwelagu This comment is still pending.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimwelagu Friendly ping.

else:
# Create a temporary directory
tmp_dir = tempfile.mkdtemp(prefix='genpy_')

# Afterwards, we are going to remove the directory so that the .pyc file gets cleaned up if it's still around
atexit.register(shutil.rmtree, tmp_dir)

if tmp_file_name:
# create file with specified temp file name, overwrite file if already exist
tmp_file = open(tmp_file_name, 'w')
else:
# write the entire text to a file and import it (it will get deleted when tmp_dir goes - above)
tmp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.py', dir=tmp_dir, delete=False)

#write the entire text to a file and import it
tmp_file.write(full_text)
tmp_file.close()

# import our temporary file as a python module, which requires modifying sys.path
sys.path.append(os.path.dirname(tmp_file.name))
Expand Down