[tor-commits] [gettor/master] Deduplicate utility methods

ilv at torproject.org ilv at torproject.org
Mon Feb 15 20:06:58 UTC 2016


commit 8a99835cfea2d3b594bf3951b8970c5e2117de77
Author: aagbsn <aagbsn at extc.org>
Date:   Fri Feb 5 18:53:36 2016 +0000

    Deduplicate utility methods
    
    move get_bundle_info, get_file_sha256, valid_format to gettor/utils.py
---
 gettor/utils.py           | 92 ++++++++++++++++++++++++++++++++++++++++++++++
 upload/bundles2drive.py   | 93 +----------------------------------------------
 upload/bundles2dropbox.py | 92 +---------------------------------------------
 upload/bundles2github.py  | 56 +---------------------------
 4 files changed, 95 insertions(+), 238 deletions(-)

diff --git a/gettor/utils.py b/gettor/utils.py
index 3f69e08..c684097 100644
--- a/gettor/utils.py
+++ b/gettor/utils.py
@@ -47,3 +47,95 @@ def get_sha256(string):
 
     """
     return str(hashlib.sha256(string).hexdigest())
+
+
+def get_bundle_info(file, osys):
+    """Get the os, arch and lc from a bundle string.
+
+    :param: file (string) the name of the file.
+    :param: osys (string) the OS.
+
+    :raise: ValueError if the bundle doesn't have a valid bundle format.
+
+    :return: (list) the os, arch and lc.
+
+    """
+    if(osys == 'windows'):
+        m = re.search(
+            'torbrowser-install-\d\.\d\.\d_(\w\w)(-\w\w)?\.exe',
+            file)
+        if m:
+            lc = m.group(1)
+            return 'windows', '32/64', lc
+        else:
+            raise ValueError("Invalid bundle format %s" % file)
+    elif(osys == 'linux'):
+        m = re.search(
+            'tor-browser-linux(\d\d)-\d\.\d\.\d_(\w\w)(-\w\w)?\.tar\.xz',
+            file)
+        if m:
+            arch = m.group(1)
+            lc = m.group(2)
+            return 'linux', arch, lc
+        else:
+            raise ValueError("Invalid bundle format %s" % file)
+    elif(osys == 'osx'):
+        m = re.search(
+            'TorBrowser-\d\.\d\.\d-osx(\d\d)_(\w\w)(-\w\w)?\.dmg',
+            file)
+        if m:
+            os = 'osx'
+            arch = m.group(1)
+            lc = m.group(2)
+            return 'osx', arch, lc
+        else:
+            raise ValueError("Invalid bundle format %s" % file)
+
+
+def valid_format(file, osys):
+    """Check for valid bundle format
+
+    Check if the given file has a valid bundle format
+    (e.g. tor-browser-linux32-3.6.2_es-ES.tar.xz)
+
+    :param: file (string) the name of the file.
+    :param: osys (string) the OS.
+
+    :return: (boolean) true if the bundle format is valid, false otherwise.
+
+    """
+    if(osys == 'windows'):
+        m = re.search(
+            'torbrowser-install-\d\.\d\.\d_\w\w(-\w\w)?\.exe',
+            file)
+    elif(osys == 'linux'):
+        m = re.search(
+            'tor-browser-linux\d\d-\d\.\d\.\d_(\w\w)(-\w\w)?\.tar\.xz',
+            file)
+    elif(osys == 'osx'):
+        m = re.search(
+            'TorBrowser-\d\.\d\.\d-osx\d\d_(\w\w)(-\w\w)?\.dmg',
+            file)
+    if m:
+        return True
+    else:
+        return False
+
+
+def get_file_sha256(file):
+    """Get the sha256 of a file.
+
+    :param: file (string) the path of the file.
+
+    :return: (string) the sha256 hash.
+
+    """
+    # as seen on the internetz
+    BLOCKSIZE = 65536
+    hasher = hashlib.sha256()
+    with open(file, 'rb') as afile:
+        buf = afile.read(BLOCKSIZE)
+        while len(buf) > 0:
+            hasher.update(buf)
+            buf = afile.read(BLOCKSIZE)
+    return hasher.hexdigest()
diff --git a/upload/bundles2drive.py b/upload/bundles2drive.py
index bac6fea..e6ad04b 100644
--- a/upload/bundles2drive.py
+++ b/upload/bundles2drive.py
@@ -20,6 +20,7 @@ import logging
 import argparse
 import ConfigParser
 import gettor.core
+from gettor.utils import get_bundle_info, get_file_sha256, valid_format
 
 # import google drive libs
 import httplib2
@@ -31,98 +32,6 @@ from oauth2client.client import OAuth2WebServerFlow
 from oauth2client.client import Credentials
 
 
-def valid_format(file, osys):
-    """Check for valid bundle format
-
-    Check if the given file has a valid bundle format
-    (e.g. tor-browser-linux32-3.6.2_es-ES.tar.xz)
-
-    :param: file (string) the name of the file.
-    :param: osys (string) the OS.
-
-    :return: (boolean) true if the bundle format is valid, false otherwise.
-
-    """
-    if(osys == 'windows'):
-        m = re.search(
-            'torbrowser-install-\d\.\d\.?\d?_\w\w(-\w\w)?\.exe',
-            file)
-    elif(osys == 'linux'):
-        m = re.search(
-            'tor-browser-linux\d\d-\d\.\d\.?\d?_(\w\w)(-\w\w)?\.tar\.xz',
-            file)
-    elif(osys == 'osx'):
-        m = re.search(
-            'TorBrowser-\d\.\d\.?\d?-osx\d\d_(\w\w)(-\w\w)?\.dmg',
-            file)
-    if m:
-        return True
-    else:
-        return False
-
-
-def get_bundle_info(file, osys):
-    """Get the os, arch and lc from a bundle string.
-
-    :param: file (string) the name of the file.
-    :param: osys (string) the OS.
-
-    :raise: ValueError if the bundle doesn't have a valid bundle format.
-
-    :return: (list) the os, arch and lc.
-
-    """
-    if(osys == 'windows'):
-        m = re.search(
-            'torbrowser-install-\d\.\d\.?\d?_(\w\w)(-\w\w)?\.exe',
-            file)
-        if m:
-            lc = m.group(1)
-            return 'windows', '32/64', lc
-        else:
-            raise ValueError("Invalid bundle format %s" % file)
-    elif(osys == 'linux'):
-        m = re.search(
-            'tor-browser-linux(\d\d)-\d\.\d\.?\d?_(\w\w)(-\w\w)?\.tar\.xz',
-            file)
-        if m:
-            arch = m.group(1)
-            lc = m.group(2)
-            return 'linux', arch, lc
-        else:
-            raise ValueError("Invalid bundle format %s" % file)
-    elif(osys == 'osx'):
-        m = re.search(
-            'TorBrowser-\d\.\d\.?\d?-osx(\d\d)_(\w\w)(-\w\w)?\.dmg',
-            file)
-        if m:
-            os = 'osx'
-            arch = m.group(1)
-            lc = m.group(2)
-            return 'osx', arch, lc
-        else:
-            raise ValueError("Invalid bundle format %s" % file)
-
-
-def get_file_sha256(file):
-    """Get the sha256 of a file.
-
-    :param: file (string) the path of the file.
-
-    :return: (string) the sha256 hash.
-
-    """
-    # as seen on the internetz
-    BLOCKSIZE = 65536
-    hasher = hashlib.sha256()
-    with open(file, 'rb') as afile:
-        buf = afile.read(BLOCKSIZE)
-        while len(buf) > 0:
-            hasher.update(buf)
-            buf = afile.read(BLOCKSIZE)
-    return hasher.hexdigest()
-
-
 def upload_files(client, basedir):
     """Upload files to Google Drive.
 
diff --git a/upload/bundles2dropbox.py b/upload/bundles2dropbox.py
index bedeec0..9007410 100644
--- a/upload/bundles2dropbox.py
+++ b/upload/bundles2dropbox.py
@@ -18,97 +18,7 @@ import ConfigParser
 
 import dropbox
 import gettor.core
-
-def valid_format(file, osys):
-    """Check for valid bundle format
-
-    Check if the given file has a valid bundle format
-    (e.g. tor-browser-linux32-3.6.2_es-ES.tar.xz)
-
-    :param: file (string) the name of the file.
-    :param: osys (string) the OS.
-
-    :return: (boolean) true if the bundle format is valid, false otherwise.
-
-    """
-    if(osys == 'windows'):
-        m = re.search(
-            'torbrowser-install-\d\.\d\.\d_\w\w(-\w\w)?\.exe',
-            file)
-    elif(osys == 'linux'):
-        m = re.search(
-            'tor-browser-linux\d\d-\d\.\d\.\d_(\w\w)(-\w\w)?\.tar\.xz',
-            file)
-    elif(osys == 'osx'):
-        m = re.search(
-            'TorBrowser-\d\.\d\.\d-osx\d\d_(\w\w)(-\w\w)?\.dmg',     
-            file)
-    if m:
-        return True
-    else:
-        return False
-
-
-def get_bundle_info(file, osys):
-    """Get the os, arch and lc from a bundle string.
-
-    :param: file (string) the name of the file.
-    :param: osys (string) the OS.
-
-    :raise: ValueError if the bundle doesn't have a valid bundle format.
-
-    :return: (list) the os, arch and lc.
-
-    """
-    if(osys == 'windows'):
-        m = re.search(
-            'torbrowser-install-\d\.\d\.\d_(\w\w)(-\w\w)?\.exe',
-            file)
-        if m:
-            lc = m.group(1)
-            return 'windows', '32/64', lc
-        else:
-            raise ValueError("Invalid bundle format %s" % file)
-    elif(osys == 'linux'):
-        m = re.search(
-            'tor-browser-linux(\d\d)-\d\.\d\.\d_(\w\w)(-\w\w)?\.tar\.xz',
-            file)
-        if m:
-            arch = m.group(1)
-            lc = m.group(2)
-            return 'linux', arch, lc
-        else:
-            raise ValueError("Invalid bundle format %s" % file)
-    elif(osys == 'osx'):
-        m = re.search(
-            'TorBrowser-\d\.\d\.\d-osx(\d\d)_(\w\w)(-\w\w)?\.dmg',
-            file)
-        if m:
-            os = 'osx'
-            arch = m.group(1)
-            lc = m.group(2)
-            return 'osx', arch, lc
-        else:
-            raise ValueError("Invalid bundle format %s" % file)
-
-
-def get_file_sha256(file):
-    """Get the sha256 of a file.
-
-    :param: file (string) the path of the file.
-
-    :return: (string) the sha256 hash.
-
-    """
-    # as seen on the internetz
-    BLOCKSIZE = 65536
-    hasher = hashlib.sha256()
-    with open(file, 'rb') as afile:
-        buf = afile.read(BLOCKSIZE)
-        while len(buf) > 0:
-            hasher.update(buf)
-            buf = afile.read(BLOCKSIZE)
-    return hasher.hexdigest()
+from gettor.utils import get_bundle_info, get_file_sha256, valid_format
 
 
 def upload_files(basedir, client):
diff --git a/upload/bundles2github.py b/upload/bundles2github.py
index ee24edb..a5beea5 100644
--- a/upload/bundles2github.py
+++ b/upload/bundles2github.py
@@ -23,63 +23,9 @@ import argparse
 from libsaas.services import github
 import gnupg
 import gettor.core
+from gettor.utils import get_bundle_info, get_file_sha256
 
 
-def get_file_sha256(file):
-    """Get the sha256 of a file.
-
-    :param: file (string) the path of the file.
-
-    :return: (string) the sha256 hash.
-
-    """
-    # as seen on the internetz
-    BLOCKSIZE = 65536
-    hasher = hashlib.sha256()
-    with open(file, 'rb') as afile:
-        buf = afile.read(BLOCKSIZE)
-        while len(buf) > 0:
-            hasher.update(buf)
-            buf = afile.read(BLOCKSIZE)
-    return hasher.hexdigest()
-
-
-def get_bundle_info(file, osys):
-    """Get the os, arch and lc from a bundle string.
-
-    :param: file (string) the name of the file.
-    :param: osys (string) the OS.
-
-    :raise: ValueError if the bundle doesn't have a valid bundle format.
-
-    :return: (list) the os, arch and lc.
-
-    """
-    if(osys == 'windows'):
-        m = re.search(
-            'torbrowser-install-\d\.\d\.\d_(\w\w)(-\w\w)?\.exe',
-            file)
-        if m:
-            lc = m.group(1)
-            return 'windows', '32/64', lc
-    elif(osys == 'linux'):
-        m = re.search(
-            'tor-browser-linux(\d\d)-\d\.\d\.\d_(\w\w)(-\w\w)?\.tar\.xz',
-            file)
-        if m:
-            arch = m.group(1)
-            lc = m.group(2)
-            return 'linux', arch, lc
-    elif(osys == 'osx'):
-        m = re.search(
-            'TorBrowser-\d\.\d\.\d-osx(\d\d)_(\w\w)(-\w\w)?\.dmg',
-            file)
-        if m:
-            os = 'osx'
-            arch = m.group(1)
-            lc = m.group(2)
-            return 'osx', arch, lc
-
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(
         description='Utility to upload Tor Browser to Github.'





More information about the tor-commits mailing list