utils.py 3.96 KB
Newer Older
1
2
3
4
# -*- coding: utf-8 -*-
#
#   utils.py — Debexpo utility functions
#
Arno Töll's avatar
Arno Töll committed
5
#   This file is part of debexpo - https://alioth.debian.org/projects/debexpo/
6
#
Jonny Lamb's avatar
Jonny Lamb committed
7
#   Copyright © 2008 Jonny Lamb <jonny@debian.org>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

30
31
32
33
"""
Holds misc utility functions.
"""

34
35
36
37
__author__ = 'Jonny Lamb'
__copyright__ = 'Copyright © 2008 Jonny Lamb'
__license__ = 'MIT'

38
import logging
39
import hashlib
40
41
42
43
import os

from pylons import config

Clément Schreiner's avatar
Clément Schreiner committed
44
45
from debexpo.lib import gnupg

46
log = logging.getLogger(__name__)
47

48
def parse_section(section):
49
50
51
52
    """
    Works out the component and section from the "Section" field.
    Sections like `python` or `libdevel` are in main.
    Sections with a prefix, separated with a forward-slash also show the component.
53
    It returns a list of strings in the form [component, section].
54
55
56
57
58
59

    For example, `non-free/python` has component `non-free` and section `python`.

    ``section``
        Section name to parse.
    """
60
    if '/' in section:
61
        return section.split('/')
62
    else:
63
        return ['main', section]
64
65
66
67
68
69
70
71
72
73
74
75
76

def get_package_dir(source):
    """
    Returns the directory name where the package with name supplied as the first argument
    should be installed.

    ``source``
        Source package name to use to work out directory name.
    """
    if source.startswith('lib'):
        return os.path.join(source[:4], source)
    else:
        return os.path.join(source[0], source)
77
78
79
80
81
82
83
84
85
86
87

def md5sum(filename):
    """
    Returns the md5sum of a file specified.

    ``filename``
        File name of the file to md5sum.
    """
    try:
        f = file(filename, 'rb')
    except:
88
        raise AttributeError('Failed to open file %s.' % filename)
89

90
    sum = hashlib.md5()
91
92
93
94
95
96
97
98
99
    while True:
        chunk = f.read(10240)
        if not chunk:
            break
        sum.update(chunk)

    f.close()

    return sum.hexdigest()
100

101
102
103
104
def random_hash():
    s = os.urandom(20)
    return hash_it(s)

105
106
107
108
def hash_it(s):
    if type(s) == unicode:
        s = s.encode('utf-8')
    return hashlib.md5(s).hexdigest()
Clément Schreiner's avatar
Clément Schreiner committed
109
110

def get_gnupg():
Clément Schreiner's avatar
Typo.    
Clément Schreiner committed
111
112
    return gnupg.GnuPG(config['debexpo.gpg_path'],
                       config['debexpo.gpg_keyring'])
113

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def uncamel(s):
    """
    Remove uppercase characters in a string and add an underscore just before.
    e.g.: 'BuildsystemTest' -> 'buildsystem_test'
          'Plugin' -> 'plugin'
          'SomeClass_' -> some_class  -- yes, this
          '_SomeOther_' -> some_other -- sucks.
    """

    # FIXME: this is a ugly (was intended as a quick proof-of-concept)
    # FIXME: won't work when a string has several capitalized characters in a row
    # e.g.: 'DebianQA'-> debian_q_a -- yuck
    # a regexp might be better.
    # see this link for an example:
    # http://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-camel-case

    uncameled_string = ''.join(('_' if x.isupper() else '') + x.lower()
                               for x in s).strip('_')
    return uncameled_string