Repository: peterbe/hashin

Issue:
hashin cannot handle leading zeros in package version numbers
Pip can install packages with a missing leading zero in the version number e.g. `Unidecode==0.04.20`:

I was passing the output of `pip freeze` into `hashin` and encountered this problem.

```
/tmp > bin/pip install Unidecode==0.4.20  # or Unidecode==0.04.20
Collecting Unidecode==0.4.20
  Using cached Unidecode-0.04.20-py2.py3-none-any.whl
Installing collected packages: Unidecode
Successfully installed Unidecode-0.4.20
/tmp > bin/pip freeze
Unidecode==0.4.20
```
But hashin cannot handle this.
```
/tmp > bin/hashin Unidecode==0.4.20
Traceback (most recent call last):
  File "bin/hashin", line 11, in <module>
    sys.exit(main())
  File "/tmp/local/lib/python2.7/site-packages/hashin.py", line 400, in main
    verbose=args.verbose,
  File "/tmp/local/lib/python2.7/site-packages/hashin.py", line 96, in run
    run_single_package(spec, *args, **kwargs)
  File "/tmp/local/lib/python2.7/site-packages/hashin.py", line 119, in run_single_package
    algorithm=algorithm
  File "/tmp/local/lib/python2.7/site-packages/hashin.py", line 363, in get_package_hashes
    raise PackageError('No data found for version {0}'.format(version))
hashin.PackageError: No data found for version 0.4.20
```


Discussion:

Patch:
diff --git a/README.rst b/README.rst
index 965e63c..984d968 100644
--- a/README.rst
+++ b/README.rst
@@ -185,6 +185,11 @@ put it directly into ``pip``.
 Version History
 ===============
 
+0.11.0
+  * Cope with leading zeros in version numbers when figuring out what
+    the latest version is.
+    See https://github.com/peterbe/hashin/issues/39
+
 0.10.0
   * Latest version is now figured out by looking at all version numbers
     in the list of releases from the JSON payload. The pre releases are
diff --git a/hashin.py b/hashin.py
index c6c778e..6d05a41 100755
--- a/hashin.py
+++ b/hashin.py
@@ -186,10 +186,10 @@ def get_latest_version(data):
     for version in data['releases']:
         v = parse(version)
         if not v.is_prerelease:
-            all_versions.append(v)
+            all_versions.append((v, version))
     all_versions.sort(reverse=True)
     # return the highest non-pre-release version
-    return str(all_versions[0])
+    return str(all_versions[0][1])
 
 
 def expand_python_version(version):
diff --git a/setup.py b/setup.py
index c90df58..d8ed11b 100644
--- a/setup.py
+++ b/setup.py
@@ -13,7 +13,7 @@ except ImportError:
 
 setup(
     name='hashin',
-    version='0.10.0',
+    version='0.11.0',
     description='Edits your requirements.txt by hashing them in',
     long_description=open(path.join(_here, 'README.rst')).read(),
     author='Peter Bengtsson',


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 831697d..a9e2757 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -91,6 +91,20 @@ class Tests(TestCase):
         })
         self.assertEqual(version, '0.999')
 
+    @mock.patch('hashin.urlopen')
+    def test_get_latest_version_non_pre_release_leading_zeros(self, murlopen):
+        version = hashin.get_latest_version({
+            'info': {
+                'version': '0.3',
+            },
+            'releases': {
+                '0.04.13': {},
+                '0.04.21': {},
+                '0.04.09': {},
+            }
+        })
+        self.assertEqual(version, '0.04.21')
+
     @mock.patch('hashin.urlopen')
     def test_get_hashes_error(self, murlopen):