jssimporter / jss_helper

jss_helper is deprecated.
GNU General Public License v3.0
66 stars 7 forks source link

KeyError on package with leading zero's #29

Open krispayne opened 4 years ago

krispayne commented 4 years ago
[autopkg@vnetboot AutoPkg]$ jss_helper promote

++ jss_helper: 2.2.0b2 ++ python-jss: 2.1 ++

JSS: https://server.com:8443
Preferences: ~/Library/Preferences/com.github.autopkg.plist

No policy specified in args: Building a list of policies which have newer packages available...
Retrieving 261 policies. Please wait...

 0: Adobe Acrobat DC

Enter a number to select from list.
Enter 'F' to expand the options list.
Please choose an object: 0
Traceback (most recent call last):
  File "/usr/local/bin/jss_helper", line 120, in <module>
    main()
  File "/usr/local/bin/jss_helper", line 113, in main
    func(args)
  File "/usr/local/bin/jss_helper_lib/actions.py", line 687, in promote
    new_pkg_name = tools.get_pkg_menu(all_packages, cur_pkg)
  File "/usr/local/bin/jss_helper_lib/tools.py", line 771, in get_pkg_menu
    default = get_newest_pkg(matching_options)
  File "/usr/local/bin/jss_helper_lib/tools.py", line 462, in get_newest_pkg
    result = versions[str(newest)]
KeyError: '20.6.20042'
[autopkg@vnetboot AutoPkg]$

The package is Adobe Acrobat DC-20.006.20034.pkg going to Adobe Acrobat DC-20.006.20042.pkg

This is the first time I've had issues with Acrobat in this regard. Maybe something is different about the way that version numbers are interpreted in the new version of jss_helper?

ghost commented 4 years ago

It looks like it's the call to LooseVersion (packaging.version import parse as LooseVersion) that's modifying the string. I tweaked get_newest_package() to print out version before and after calling LooseVersion:

`def get_newest_pkg(options): """Get the newest package from a list of a packages.

Args:
    options: List of package names.

Returns: Either the newest package name or None. Package names
    must be in some format that get_package_info() can extract a
    version number.
"""
versions = {get_package_info(package)[1]: package for package
            in options if get_package_info(package)[1]}
if versions:
    for version in versions:
        print(f"Before LooseVersion: {version}")
        print(f"After LooseVersion: {LooseVersion(version)}")

    # newest = max([LooseVersion(version) for version in versions])
    # result = versions[str(newest)]
    result = None
else:
    result = None

return result`

And I got the following:

me@namaste jss_helper % ./jss_helper promote "Microsoft Teams"

++ jss_helper: 2.2.0b3 ++ python-jss: 2.1 ++

JSS: https://blah.jamfcloud.com
Preferences: ~/Library/Preferences/com.github.autopkg.plist

Before LooseVersion: 1.00.234155
After LooseVersion: 1.0.234155
Before LooseVersion: 1.00.300362
After LooseVersion: 1.0.300362
Before LooseVersion: 1.00.304460
After LooseVersion: 1.0.304460
Before LooseVersion: 1.00.308663
After LooseVersion: 1.0.308663

 0: Microsoft_Teams_1.00.234155.pkg
 1: Microsoft_Teams_1.00.300362.pkg (CURRENT)
 2: Microsoft_Teams_1.00.304460.pkg
 3: Microsoft_Teams_1.00.308663.pkg

Because version has been changed it's then not a key in versions. From doing a bit of reading it seems that packaging.version.parse splits the string on "." and then converts to int. At that point 00 becomes 0 and version is now not a key in the versions dict.

Not sure how that can be fixed though.

grahampugh commented 4 years ago

I may be able to use APLooseVersion given that we already pretty much have a dependency on AutoPkg being installed to use Spruce anyway. APLooseVersion behaves closer to what LooseVersion did in python 2. I'll give that a go when I get a chance.

ghost commented 4 years ago

Thanks Graham, your work is much appreciated.

krispayne commented 4 years ago

This issue persists on the general 2.2.0 release:

[autopkg@server AutoPkg]$ jss_helper promote

++ jss_helper: 2.2.0 ++ python-jss: 2.1 ++

JSS: https://jss.net:8443
Preferences: ~/Library/Preferences/com.github.autopkg.plist

No policy specified in args: Building a list of policies which have newer packages available...
Retrieving 268 policies. Please wait...

 0: Zoom

Enter a number to select from list.
Enter 'F' to expand the options list.
Please choose an object: 0
Traceback (most recent call last):
  File "/usr/local/bin/jss_helper", line 120, in <module>
    main()
  File "/usr/local/bin/jss_helper", line 113, in main
    func(args)
  File "/usr/local/bin/jss_helper_lib/actions.py", line 687, in promote
    new_pkg_name = tools.get_pkg_menu(all_packages, cur_pkg)
  File "/usr/local/bin/jss_helper_lib/tools.py", line 771, in get_pkg_menu
    default = get_newest_pkg(matching_options)
  File "/usr/local/bin/jss_helper_lib/tools.py", line 462, in get_newest_pkg
    result = versions[str(newest)]
KeyError: '5.0.24030.508'
[autopkg@server AutoPkg]$

Is this the same problem as the adobe issue from my initial bug report, or a separate one?

krispayne commented 4 years ago

Is there anything I can do to help this issue progress?

grahampugh commented 4 years ago

Are you prepared to have AutoPkg as a dependency for jss_helper?

krispayne commented 4 years ago

@grahampugh for me, it wouldn't be a problem. Their use is not independent in my environment

grahampugh commented 4 years ago

@krispayne please try https://github.com/jssimporter/jss_helper/releases/tag/2.2.1b1 (no package yet, but the only file that changed is jss_helper_lib/tools.py.

krispayne commented 4 years ago

Looks like the new tools.py lib worked in the case I was having with Zoom above.

Let me demote the adobe policy and test that one, too

krispayne commented 4 years ago

👍 The original adobe policy also works.

Thanks!