~dobey/ubuntuone-storage-protocol/no-cpp-tests

« back to all changes in this revision

Viewing changes to ubuntuone/storageprotocol/context.py

  • Committer: Tarmac
  • Author(s): mike.mccracken at canonical
  • Date: 2012-08-16 19:21:06 UTC
  • mfrom: (151.4.3 fix-darwin-cert-loc)
  • Revision ID: tarmac-20120816192106-a5tyvxj3dldktvcx
- Add support for finding SSL certs in packaged mac app. (LP: #1025950)

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
from twisted.internet import error, ssl
37
37
from twisted.python import log
38
38
 
39
 
if sys.platform == "win32":
40
 
    # diable pylint warning, as it may be the wrong platform
41
 
    # pylint: disable=F0401
42
 
    import _winreg
43
 
 
44
 
    # First open the registry hive
45
 
    hive = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
46
 
    # Open the registry key where Windows stores the Shell Folder locations
47
 
    key = _winreg.OpenKey(hive, str(
48
 
            "Software\\Microsoft\\Windows\\CurrentVersion"
49
 
            "\\Explorer\\Shell Folders"))
50
 
    for i in range(0, _winreg.QueryInfoKey(key)[1]):
51
 
        name, value, val_type = _winreg.EnumValue(key, i)
52
 
        # Common AppData will always be present unless the user
53
 
        # played with regedit
54
 
        if name == "Common AppData":
55
 
            ssl_cert_location = os.path.join(value,
56
 
                                             "ubuntuone-storageprotocol")
57
 
            break
58
 
else:
59
 
    ssl_cert_location = '/etc/ssl/certs'
 
39
from dirspec.basedir import load_config_paths
 
40
 
 
41
 
 
42
def get_cert_location():
 
43
    """Return path to certificate files."""
 
44
 
 
45
    if sys.platform == "win32":
 
46
        ssl_cert_location = list(load_config_paths(
 
47
                "ubuntuone-storageprotocol"))[0]
 
48
 
 
49
    elif sys.platform == "darwin":
 
50
        if getattr(sys, "frozen", None) is not None:
 
51
            main_app_dir = "".join(__file__.partition(".app")[:-1])
 
52
            main_app_resources_dir = os.path.join(main_app_dir,
 
53
                                                  "Contents",
 
54
                                                  "Resources")
 
55
            ssl_cert_location = main_app_resources_dir
 
56
        else:
 
57
            pkg_dir = os.path.dirname(__file__)
 
58
            src_tree_path = os.path.dirname(os.path.dirname(pkg_dir))
 
59
            ssl_cert_location = os.path.join(src_tree_path,
 
60
                                             "data")
 
61
    else:
 
62
        ssl_cert_location = '/etc/ssl/certs'
 
63
 
 
64
    return ssl_cert_location
60
65
 
61
66
 
62
67
class HostnameVerifyContextFactory(ssl.CertificateOptions):
88
93
 
89
94
def get_certificates():
90
95
    """Get a list of certificate paths."""
 
96
    ssl_cert_location = get_cert_location()
91
97
    ca_file = ssl.Certificate.loadPEM(file(os.path.join(ssl_cert_location,
92
98
                     'UbuntuOne-Go_Daddy_Class_2_CA.pem'), 'r').read())
93
99
    ca_file_2 = ssl.Certificate.loadPEM(file(os.path.join(ssl_cert_location,