Welcome to mirror list, hosted at ThFree Co, Russian Federation.

maintainers.py « calm - cygwin.com/git/cygwin-apps/calm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 473e65a6b6f80f877ac2ec4bc73a3222eeee32a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
#
# Copyright (c) 2015 Jon Turney
#
# 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.
#

#
# utilities for working with a maintainer list
#

#
# things we know about a maintainer:
#
# - their home directory
# - the list of packages they maintain (given by cygwin-pkg-list)
# - an email address (in HOME/!email (or !mail), as we don't want to publish
#   it, and want to allow the maintainer to change it)
# - the timestamp when 'ignoring' warnings were last emitted
#

import itertools
import logging
import os
import re

#
#
#


def touch(fn, times=None):
    with open(fn, 'a'):
        os.utime(fn, times)

#
#
#


class Maintainer(object):
    _homedirs = ''

    def __init__(self, name, email=None, pkgs=None):
        if email is None:
            email = []
        if pkgs is None:
            pkgs = []

        self.name = name
        self.email = email
        self.pkgs = pkgs

        # the mtime of this file records the timestamp
        reminder_file = os.path.join(self.homedir(), '!reminder-timestamp')
        if os.path.exists(reminder_file):
            self.reminder_time = os.path.getmtime(reminder_file)
        else:
            self.reminder_time = 0
        self.reminders_issued = False
        self.reminders_timestamp_checked = False

    def __repr__(self):
        return "maintainers.Maintainer('%s', %s, %s)" % (self.name, self.email, self.pkgs)

    def homedir(self):
        return os.path.join(Maintainer._homedirs, self.name)

    def _update_reminder_time(self):
        reminder_file = os.path.join(self.homedir(), '!reminder-timestamp')

        if self.reminders_issued:
            # if reminders were issued, update the timestamp
            logging.debug("updating reminder time for %s" % self.name)
            touch(reminder_file)
        elif (not self.reminders_timestamp_checked) and (self.reminder_time != 0):
            # if we didn't need to check the reminder timestamp, it can be
            # reset
            logging.debug("resetting reminder time for %s" % self.name)
            try:
                os.remove(reminder_file)
            except FileNotFoundError:
                pass

    @staticmethod
    def _find(mlist, name):
        mlist.setdefault(name, Maintainer(name))
        return mlist[name]

    # add maintainers which have existing directories
    @classmethod
    def add_directories(self, mlist, homedirs):
        self._homedirs = homedirs

        for n in os.listdir(homedirs):
            if not os.path.isdir(os.path.join(homedirs, n)):
                continue

            m = Maintainer._find(mlist, n)

            for e in ['!email', '!mail']:
                email = os.path.join(homedirs, m.name, e)
                if os.path.isfile(email):
                    with open(email) as f:
                        for l in f:
                            # one address per line, ignore blank and comment lines
                            if l.startswith('#'):
                                continue
                            l = l.strip()
                            if l:
                                m.email.append(l)

        return mlist

    # add maintainers from the package maintainers list, with the packages they
    # maintain
    @staticmethod
    def add_packages(mlist, pkglist, orphanMaint=None):
        with open(pkglist) as f:
            for (i, l) in enumerate(f):
                l = l.rstrip()

                # match lines of the form '<package> <maintainer(s)|status>'
                match = re.match(r'^(\S+)\s+(.+)$', l)
                if match:
                    pkg = match.group(1)
                    rest = match.group(2)

                    # does rest starts with a status in all caps?
                    status_match = re.match(r'^([A-Z]+)\b.*$', rest)
                    if status_match:
                        status = status_match.group(1)

                        # ignore packages marked as 'OBSOLETE'
                        if status == 'OBSOLETE':
                            continue

                        # orphaned packages get the default maintainer if we
                        # have one, otherwise they are assigned to 'ORPHANED'
                        elif status == 'ORPHANED':
                            if orphanMaint is not None:
                                m = orphanMaint
                            else:
                                m = status

                            # also add any previous maintainer(s) listed
                            prevm = re.match(r'^ORPHANED\s\((.*)\)', rest)
                            if prevm:
                                m = m + '/' + prevm.group(1)

                        else:
                            logging.error("unknown package status '%s' in line %s:%d: '%s'" % (status, pkglist, i, l))
                            continue
                    else:
                        m = rest

                    # joint maintainers are separated by '/'
                    for name in m.split('/'):

                        # is the maintainer name ascii?
                        #
                        # (despite containing spaces, think of these as an account
                        # name, rather than a display name)
                        try:
                            name.encode('ascii')
                        except UnicodeError:
                            logging.error("non-ascii maintainer name '%s' in line %s:%d, skipped" % (rest, pkglist, i))
                            continue

                        m = Maintainer._find(mlist, name)
                        m.pkgs.append(pkg)

                else:
                    logging.error("unrecognized line in %s:%d: '%s'" % (pkglist, i, l))

        return mlist

    # create maintainer list
    @staticmethod
    def read(args, orphanmaint=None):
        mlist = {}
        mlist = Maintainer.add_directories(mlist, args.homedir)
        mlist = Maintainer.add_packages(mlist, args.pkglist, orphanmaint)

        return mlist

    @staticmethod
    def update_reminder_times(mlist):
        for m in mlist.values():
            m._update_reminder_time()

    # a list of all packages
    @staticmethod
    def all_packages(mlist):
        return list(itertools.chain.from_iterable(mlist[m].pkgs for m in mlist))