diff mbox series

[OpenWrt-Devel,2/6] scripts/dl_github_archive.py: convert to Python 3 with 2-to-3

Message ID 1560802136-4157-3-git-send-email-ynezz@true.cz
State Changes Requested
Delegated to: Petr Štetiar
Headers show
Series build: switch to Python 3 | expand

Commit Message

Petr Štetiar June 17, 2019, 8:08 p.m. UTC
Let's convert the script to Python 3.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
---
 scripts/dl_github_archive.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

Yousong Zhou June 19, 2019, 11:31 a.m. UTC | #1
On Tue, 18 Jun 2019 at 04:09, Petr Štetiar <ynezz@true.cz> wrote:
>
> Let's convert the script to Python 3.
>
> Signed-off-by: Petr Štetiar <ynezz@true.cz>
> ---
>  scripts/dl_github_archive.py | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/scripts/dl_github_archive.py b/scripts/dl_github_archive.py
> index 4bb7d131bb83..3d1e07e5c551 100755
> --- a/scripts/dl_github_archive.py
> +++ b/scripts/dl_github_archive.py
> @@ -1,4 +1,4 @@
> -#!/usr/bin/env python
> +#!/usr/bin/env python3
>  #
>  # Copyright (c) 2018 Yousong Zhou <yszhou4tech@gmail.com>
>  #
> @@ -20,7 +20,7 @@ import ssl
>  import subprocess
>  import sys
>  import time
> -import urllib2
> +import urllib.request, urllib.error, urllib.parse

Only urllib.request needs to be imported.

>
>  TMPDIR = os.environ.get('TMP_DIR') or '/tmp'
>  TMPDIR_DL = os.path.join(TMPDIR, 'dl')
> @@ -194,7 +194,7 @@ class GitHubCommitTsCache(object):
>              self.cache[k] = (ts, updated)
>
>      def _cache_flush(self, fout):
> -        cache = sorted(self.cache.iteritems(), cmp=lambda a, b: b[1][1] - a[1][1])
> +        cache = sorted(iter(self.cache.items()), cmp=lambda a, b: b[1][1] - a[1][1])

Python3 sorted function changed its prototype.  This needs to be

cache = sorted(self.cache.items(), key=lambda a: a[1][1])

Regards,
        yousong

>          cache = cache[:self.__cachen]
>          self.cache = {}
>          os.ftruncate(fout.fileno(), 0)
> @@ -397,9 +397,9 @@ class DownloadGitHubTarball(object):
>              'Accept': 'application/vnd.github.v3+json',
>              'User-Agent': 'OpenWrt',
>          }
> -        req = urllib2.Request(url, headers=headers)
> +        req = urllib.request.Request(url, headers=headers)
>          sslcontext = ssl._create_unverified_context()
> -        fileobj = urllib2.urlopen(req, context=sslcontext)
> +        fileobj = urllib.request.urlopen(req, context=sslcontext)
>          return fileobj
>
>      def _error(self, msg):
> --
> 1.9.1
>
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
diff mbox series

Patch

diff --git a/scripts/dl_github_archive.py b/scripts/dl_github_archive.py
index 4bb7d131bb83..3d1e07e5c551 100755
--- a/scripts/dl_github_archive.py
+++ b/scripts/dl_github_archive.py
@@ -1,4 +1,4 @@ 
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #
 # Copyright (c) 2018 Yousong Zhou <yszhou4tech@gmail.com>
 #
@@ -20,7 +20,7 @@  import ssl
 import subprocess
 import sys
 import time
-import urllib2
+import urllib.request, urllib.error, urllib.parse
 
 TMPDIR = os.environ.get('TMP_DIR') or '/tmp'
 TMPDIR_DL = os.path.join(TMPDIR, 'dl')
@@ -194,7 +194,7 @@  class GitHubCommitTsCache(object):
             self.cache[k] = (ts, updated)
 
     def _cache_flush(self, fout):
-        cache = sorted(self.cache.iteritems(), cmp=lambda a, b: b[1][1] - a[1][1])
+        cache = sorted(iter(self.cache.items()), cmp=lambda a, b: b[1][1] - a[1][1])
         cache = cache[:self.__cachen]
         self.cache = {}
         os.ftruncate(fout.fileno(), 0)
@@ -397,9 +397,9 @@  class DownloadGitHubTarball(object):
             'Accept': 'application/vnd.github.v3+json',
             'User-Agent': 'OpenWrt',
         }
-        req = urllib2.Request(url, headers=headers)
+        req = urllib.request.Request(url, headers=headers)
         sslcontext = ssl._create_unverified_context()
-        fileobj = urllib2.urlopen(req, context=sslcontext)
+        fileobj = urllib.request.urlopen(req, context=sslcontext)
         return fileobj
 
     def _error(self, msg):