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

CS-2323 _aliases: Do not replace non-top-level directories #34

Merged
merged 5 commits into from
Sep 7, 2021

Conversation

xli12
Copy link

@xli12 xli12 commented Aug 25, 2021

In OpenPackagingConvention, the _processAliases method simply does the replacement by re.sub(). But we only want the top-level directory to be replaced, for example:

👍: /materials/something.txt --> /files/materials/something.txt
👎: /something/materials/foobar.txt --> /something/files/materials/foobar.txt

This is the fix.

Note: pathlib is not used here because usually a Path object needs to be actually resolvable. A PurePath object doesn't need to be resolvable, but it doesn't have the method to rename/replace. Therefore re.sub() is still used for the fix.

@allardhoeve
Copy link

This is needed to solve: https://jira.ultimaker.com/browse/CS-2323

@allardhoeve
Copy link

I think we need a test that reads a little like this:

👍: /materials/something.txt --> /files/materials/something.txt
👎: /something/materials/foobar.txt --> /something/files/materials/foobar.txt

Maybe this?:

def write_package_with_file_ and_read_back(location):
     # The function you wrote
     return new_path

assert write_pwfarb("/materials/something.txt") == "/files/materials/something.txt"

This text you wrote is clearer to me than the test you added before. The code is just really confusing in its intent, sorry.

@allardhoeve
Copy link

Added @nallath to make sure the new code does what Cura expects it to do.

CoenSchalkwijk
CoenSchalkwijk previously approved these changes Sep 2, 2021
@@ -298,7 +298,11 @@ def _processAliases(self, virtual_path: str) -> str:

# Replace all aliases.
for regex, replacement in self._aliases.items():
virtual_path = re.sub(regex, replacement, virtual_path)
if regex.startswith("/"):
expression = r"^" + regex
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expression = r"^" + regex
expression = rf"^{regex}"

Double checked this. Order of rf doesn't seem to matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought; now that I have taken a closer look at the code, this is a nice fail-safe but isn't this 'just' a case of an incorrect alias definition?

If you look at libCharon/Charon/filetypes/UltimakerFormatPackage.py, you see that all aliases are neatly prefixed with "^/"...

Copy link
Author

Choose a reason for hiding this comment

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

Indeed in UltimakerFormatPackage there is a ^/ fixed. There expression = rf"^{regex}" doesn't fit here...
On the other hand for replacement of paths, usually / refers to a top-level directory. It seems possible that when using it, even developers may forget putting ^ at the beginning (and consider the method as merely a regex replacement, not a path handler).

Therefore I'll just keep

            if regex.startswith("/"):
                expression = r"^" + regex

Copy link
Contributor

Choose a reason for hiding this comment

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

My first suggestion still stands ;)

@CoenSchalkwijk
Copy link
Contributor

@xli12 Looks like you need to fix the mypy violations for libCharon first :-/
The gitlab checks fail on more than just your specific changes.

jellespijker
jellespijker previously approved these changes Sep 3, 2021
Copy link
Member

@jellespijker jellespijker left a comment

Choose a reason for hiding this comment

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

Code looks fine by me, maybe implement @CoenSchalkwijk his suggestion for using a context manager. Then again it used in the test, so I have no problem if you decide to let it be.

I don't have time to test it with the Spatial plugin today, maybe @rburema can try that but it could be that we need to wait on Arjen his credentials for testing it.

CoenSchalkwijk
CoenSchalkwijk previously approved these changes Sep 6, 2021
Charon/filetypes/OpenPackagingConvention.py Outdated Show resolved Hide resolved
@@ -298,7 +298,11 @@ def _processAliases(self, virtual_path: str) -> str:

# Replace all aliases.
for regex, replacement in self._aliases.items():
virtual_path = re.sub(regex, replacement, virtual_path)
if regex.startswith("/"):
expression = r"^" + regex
Copy link
Contributor

Choose a reason for hiding this comment

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

My first suggestion still stands ;)

Co-authored-by: Coen Schalkwijk <c.schalkwijk@ultimaker.com>
CoenSchalkwijk
CoenSchalkwijk previously approved these changes Sep 6, 2021
@@ -298,7 +298,11 @@ def _processAliases(self, virtual_path: str) -> str:

# Replace all aliases.
for regex, replacement in self._aliases.items():
virtual_path = re.sub(regex, replacement, virtual_path)
if regex.startswith("/"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome :P

@rburema
Copy link
Member

rburema commented Sep 7, 2021

Note that the reason we originally thought the top-level approach wasn't taken from the get go, is because of the way a zipped package can be made: Do submitters have to zip the containing folder, or zip the folders and files directly (zip-bomb)?

But maybe this is not an issue for anything that is a materials package?

(Or I haven't looked into libCharon too hard: maybe the top-level is also selected contextually?)

@xli12 xli12 merged commit 6d6f1c7 into master Sep 7, 2021
@xli12 xli12 deleted the CS-2323/do-not-replace-non-top-level-dirs branch September 7, 2021 07:25
@xli12
Copy link
Author

xli12 commented Sep 7, 2021

@rburema Yeah the issue we fixed in this PR is to avoid that non-top-level directories being accidentally selected and replaced by a specified alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants