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

new setupcfg writer #204

Merged
merged 2 commits into from
Jan 9, 2024
Merged

new setupcfg writer #204

merged 2 commits into from
Jan 9, 2024

Conversation

clavedeluna
Copy link
Contributor

@clavedeluna clavedeluna commented Jan 4, 2024

Overview

Adds a setup.cfg file writer for new dependencies

Description

  • This writer relies on configparser only to detect the existing dependencies in the setup.cfg file. We can't rely on it to update and write to file because configparser doesn't retain some formatting and comment lines.
  • I tried some other parsers to see if they could work, but none did. Instead, this is an attempt to manually write the new dependencies to file with some formatting heuristics and as much reasonable error catching as I could think.

Copy link

codecov bot commented Jan 4, 2024

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (993c151) 96.37% compared to head (720dc09) 96.26%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #204      +/-   ##
==========================================
- Coverage   96.37%   96.26%   -0.12%     
==========================================
  Files          87       88       +1     
  Lines        3996     4065      +69     
==========================================
+ Hits         3851     3913      +62     
- Misses        145      152       +7     
Files Coverage Δ
...modder/dependency_management/dependency_manager.py 92.30% <100.00%> (+1.00%) ⬆️
...ect_analysis/file_parsers/setup_cfg_file_parser.py 90.00% <87.50%> (-3.75%) ⬇️
...odemodder/dependency_management/setupcfg_writer.py 90.32% <90.32%> (ø)

@clavedeluna clavedeluna marked this pull request as ready for review January 4, 2024 19:17
@clavedeluna
Copy link
Contributor Author

I made this PR ready to review despite the codecov failure which is valid but due to my attempt to catch exceptions. After review I"ll go back and add some testing or skip coverage in certain lines.

Copy link
Member

@drdavella drdavella left a comment

Choose a reason for hiding this comment

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

Just curious but did you investigate whether this project would help simplify the implementation? https://github.com/Preocts/commented-configparser

@clavedeluna
Copy link
Contributor Author

Just curious but did you investigate whether this project would help simplify the implementation? https://github.com/Preocts/commented-configparser

I tested with commented-configparser and am very slightly disappointed that it's only a tiny bit buggy. It does retain the comments in most cases, but in some cases it moves comments to different lines. It also added some trailing spaces to the new setup.cfg file. So here's a diff:

 #comment
 [metadata] 
name = my_package
 version = attr: my_package.VERSION
 author = Josiah Carberry
 author_email = [email protected]
 description = My package description
 long_description = file: README.rst, CHANGELOG.rst, LICENSE.rst
 keywords = one, two
 # comment should stay in ln 10
 license = BSD-3-Clause
-classifiers =
+classifiers =
+# comment should stay in ln 16
        Framework :: Django
        Programming Language :: Python :: 3

-# comment should stay in ln 16

changes are:

  1. added a leading spaces after classifiers =
  2. moved comment in ln 16 to ln13, while not changing comment in ln1

@drdavella lmk if you want to accept these changes and I can switch to using commented config or we can keep code as is

@drdavella
Copy link
Member

@clavedeluna thanks for the info. I'm sorry it didn't work out of the box. Let's accept this as-is for now and we can revisit later since I don't think this is the most critical work for us right now. One other possibility down the road would be to vendor in that third-party code (assuming a permissive license) and maybe tweak it to get the behavior we need.

Copy link
Member

@drdavella drdavella left a comment

Choose a reason for hiding this comment

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

LGTM but it would be good to add the test case mentioned below.

configparser does not retain formatting or comment lines, so we have to build
the output newline manually.
"""
clean_lines = list(map(lambda s: s.strip(), original_lines))
Copy link
Member

Choose a reason for hiding this comment

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

I like map but genuinely curious whether there's any benefit to doing it this way rather than using a comprehension?

clean_lines = [s.strip for s in original_lines]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no other benefit to map, I think the moment I wrote it that's the first thing that came to me. I'll switch it out

assert setup_cfg.read() == dedent(updated_setupcfg)


def test_dont_add_existing_dependency(tmpdir):
Copy link
Member

Choose a reason for hiding this comment

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

We should have another test case where the version of security that already exists in the package is different from the one being added. There should be no new dependency added (or version changed) in this case either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a really good test case to think about. None of the other writers handle for this either so if it's ok with you it would be good to handle it in a separate PR for all. It would be worth thinking about it a bit more: do we really not want to update the version? What if the project has pkg==1.0 but our recommendation is pkg==2.0?

Worth thinking about it independently.

Copy link

sonarqubecloud bot commented Jan 9, 2024

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

4 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@clavedeluna clavedeluna enabled auto-merge January 9, 2024 11:23
@clavedeluna clavedeluna added this pull request to the merge queue Jan 9, 2024
Merged via the queue into main with commit 316f95a Jan 9, 2024
14 checks passed
@clavedeluna clavedeluna deleted the setup-cfg branch January 9, 2024 11:53
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.

3 participants