summaryrefslogtreecommitdiffstats
path: root/share/extensions/other/inkman/inkman/package.py
blob: a19127e5337f0f033c82cba4dc82e661e0a8e822 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#
# Copyright (C) 2019 Martin Owens
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Support for constructing and understanding installed extensions
as python packages.

Start by using 'PackageLists' and iterate over to get 'PackageList' objects
iterate over those to get 'Package' objects and use those to know more
information about the installed packages.
"""

import os
import sys
import json
import logging
from fnmatch import fnmatch

from .utils import (
    DATA_DIR,
    ICON_SEP,
    parse_metadata,
    clean_author,
    get_user_directory,
    get_inkscape_directory,
    ExtensionInx,
)

# The ORDER of these places is important, when installing packages it will
# choose the FIRST writable directory in the lsit, so make sure that the
# more preferable locations appear at the top.
EXTENSION_PLACES = []

for path in sys.path:
    if "extensions" in path:
        if "/bin" in path:
            path = path.replace("/bin", "")
        EXTENSION_PLACES.append(path)

CORE_ICON = os.path.join(DATA_DIR, "pixmaps", "core_icon.svg")
ORPHAN_ICON = os.path.join(DATA_DIR, "pixmaps", "orphan_icon.svg")
DEFAULT_ICON = os.path.join(DATA_DIR, "pixmaps", "default_icon.svg")
MODULE_ICON = os.path.join(DATA_DIR, "pixmaps", "module_icon.svg")


class PackageItem(object):
    """
    Gets information about the package using requests.

    Can be remote, or local json file.
    """

    default_icon = DEFAULT_ICON

    @classmethod
    def is_pkg(cls, info):
        """Returns true if this info package is considered a managed package"""
        for field in ("name", "author", "verified", "links", "summary"):
            if field not in info:
                return False
        return True

    def __init__(self, info, remote=None):
        if not self.is_pkg(info):
            raise ValueError("Not a valid package, refusing to create it.")

        self.info = info
        self.remote = remote
        self.installed = None
        self._installer = None
        self._uninstaller = None
        self._missing = []
        self._icon = None

    ident = property(lambda self: self.info.get("id"))
    url = property(lambda self: self.info["links"]["file"])
    link = property(lambda self: self.info["links"]["html"])
    name = property(lambda self: self.info["name"] or "Unnamed")
    license = property(lambda self: self.info.get("license") or "")
    author = property(lambda self: self.info["author"])
    tags = property(lambda self: self.info.get("tags", []))

    stars = property(lambda self: self.info["stats"]["liked"])
    downloads = property(lambda self: self.info["stats"]["downloaded"])
    verified = property(lambda self: self.info["verified"])
    recommended = property(lambda self: self.info["stats"].get("extra", 0) == 7)

    targets = property(lambda self: self.info.get("Inkscape Version", []))
    target = property(lambda self: ", ".join(self.targets))

    @property
    def summary(self):
        ret = self.info["summary"] or "No summary"
        if len(ret) > 110:
            ret = ret.split(". ", 1)[0]
        if len(ret) > 110:
            ret = ret[:110].rsplit(" ", 1)[0] + "..."
        return ret

    @property
    def version(self):
        if "version" in self.info:
            return str(self.info["version"])
        if "stats" in self.info and "revisions" in self.info["stats"]:
            return str(int(self.info["stats"]["revisions"]) + 1)
        return "?"

    def set_installer(self, fn):
        self._installer = fn

    def set_uninstaller(self, fn, *args):
        def _inner(info):
            return fn(info, *args)

        self._uninstaller = _inner

    def set_installed(self, installed=True):
        self.installed = installed

    def is_installable(self, version):
        if version not in self.targets or not self.verified:
            return False
        return bool(not self.installed and self._installer)

    def is_uninstallable(self):
        return bool(self._uninstaller)

    def install(self):
        """Install the remote package"""
        if not self.remote:
            raise IOError("Can not install without a defined remote")
        url = self.remote(self.info["links"]["file"])
        msg = self._installer(url.as_local_file(), self.info)
        self.set_installed(True)
        return msg

    def uninstall(self):
        """Remove the pakage if possible"""
        if self._uninstaller(self.info):
            self.set_installed(False)
            return True
        return False

    def get_icon(self):
        if self._icon:
            return self._icon
        if self.info.get("icon"):
            if not self.remote:
                raise IOError("Can get icon without a defined remote")
            icon = self.remote(self.info["icon"])
            self._icon = icon.as_local_file() or self.default_icon
            return self._icon
        for filename in self.get_files():
            name = os.path.basename(filename)
            if name in ("icon.svg", "icon.png"):
                self._icon = os.path.abspath(filename)
                return filename
        return self.default_icon

    def get_files(self, missing=False):
        """List files"""
        return [
            name
            for name in self.info.get("files", [])
            if missing or name not in self._missing
        ]


class OrphanedItem(PackageItem):
    """
    A special kind of item to collect all orphaned files
    """

    default_icon = ORPHAN_ICON

    def __init__(self, parent):
        super().__init__(
            {
                "name": "Orphan Extensions",
                "author": "Various",
                "verified": False,
                "summary": "Old and manually installed extensions",
                "license": "Unknown",
                "links": {},
                "version": "-",
                "stats": {"liked": -1, "downloaded": -1},
            }
        )

        self._icon = self.default_icon
        self._parent = os.path.abspath(parent)
        self._files = set()
        self._removed = set()
        self._items = {}

    def _get_target_file(self, filename):
        if not os.path.isabs(filename):
            filename = os.path.join(self._parent, filename)
        path = os.path.abspath(filename)
        return path.replace(self._parent, "").lstrip("/")

    def add_file(self, filename):
        """Add a file to the 'this file exists' list"""
        self._files.add(self._get_target_file(filename))

    def remove_file(self, filename, item=None):
        """Add a file to the 'this file is said to exist' list"""
        fn = self._get_target_file(filename)
        self._removed.add(fn)
        if item is not None:
            self._items[fn] = item

    def get_files(self, filters=()):
        """Returns a filtered set of files which exist, minus ones said to exist"""
        if not filters:
            filters = ("*",)
        items = []
        for item in self._files - self._removed:
            if any([fnmatch(item, filt) for filt in filters]):
                items.append(item)
        return items

    def get_missing(self):
        """Returns a set of files which don't exist, even though they were said to"""
        return [(fn, self._items.get(fn, None)) for fn in self._removed - self._files]


class PythonItem(PackageItem):
    """
    A python package that has an inx file, but was never installed properly.
    """

    def __init__(self, pip):
        self.pip = pip
        info = self.pip.get_metadata()
        super().__init__(
            {
                "pip": True,
                "name": info["name"],
                "summary": info["summary"],
                "author": info["author"] or "Unknown",
                "version": info["version"],
                "license": info["license"],
                "verified": False,
                "links": {},
                "stats": {"liked": -1, "downloaded": -1},
            }
        )

    def get_files(self):
        return self.pip.package_files()


class PythonPackage(object):
    """
    A reprisentation of the python package, NOT the json based packages.
    """

    name = property(lambda self: self.get_metadata()["name"])
    version = property(lambda self: self.get_metadata()["version"])

    def __init__(self, path, parent):
        self._metadata = None
        self.path = path
        self.parent = parent

    def get_inx(self):
        """Yields all of the inx files in this package"""
        for filename in self.package_files():
            if filename.endswith(".inx"):
                yield filename

    def package_files(self):
        """Return a generator of all files in this installed package"""
        parent = os.path.abspath(os.path.dirname(self.path))
        record = self.get_file("RECORD")
        if not record:
            return
        for line in record.split("\n"):
            if line and "," in line:
                (filename, checksum, size) = line.rsplit(",", 2)
                # XXX Check filesize or checksum?
                if not os.path.isabs(filename):
                    filename = os.path.join(parent, filename)
                yield filename

    def get_metadata(self):
        """Returns the metadata from an array of known types"""
        if self._metadata is None:
            for name in ("METADATA", "PKG-INFO"):
                md_mail = self.get_file(name)
                if md_mail:
                    self._metadata = parse_metadata(md_mail)

        if self._metadata is None:
            md_json = self.get_file("metadata.json")
            if md_json:
                self._metadata = clean_author(json.loads(md_json))

        if self._metadata is None:
            raise KeyError("Can't find package meta data: {}".format(self.path))
        return self._metadata

    def get_file(self, name):
        """Get filename if it exists"""
        if not self.path or not os.path.isdir(self.path):
            return None
        path = os.path.join(self.path, name)
        if os.path.isfile(path):
            with open(path, "r") as fhl:
                return fhl.read()
        return None

    def get_depedencies(self):
        """Return a list of other pip packages this packages needs"""
        return self.get_metadata()["requires"]