File: plugins.py

package info (click to toggle)
roundcube-plugins-extra 1.2.1-20160803
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,660 kB
  • ctags: 1,574
  • sloc: php: 11,361; sh: 91; xml: 90; makefile: 56; python: 54; awk: 51
file content (80 lines) | stat: -rwxr-xr-x 2,490 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Searches and extract updates for plugins

It uses uscan to search for updates currently you need to add
--download-current to the uscan command to intially get all plugins.
"""

import glob
import sh
import os, re, shutil
from debian import changelog

c = changelog.Changelog(open("debian/changelog").read())
UPSTREAM_VERSION = c.upstream_version

PLUGINS = []

def read():
    global PLUGINS
    with open("debian/plugins.overview") as f:
        c = f.read()

    p = c.split("\n\n")
    PLUGINS = []
    for i in p:
        l = re.finditer(r"(^|\n)(?P<key>[^:]+):(?P<value>([^\n]+|(\s*\n\s+[^\n]+)*))",i)
        PLUGINS.append(dict([(j.group("key").strip(),j.group("value").strip()) for j in l]))


def write():
    global PLUGINS
    with open("debian/plugins.overview", "w") as f:
        for p in PLUGINS:
            if (p != PLUGINS[0]):
                f.write("\n")
            f.write("\n".join(["%s: %s"%(k,p[k]) for k in ("Plugin", "Source", "Version")]))
            if p.get("Exclude-patterns"):
                f.write("\nExclude-patterns:\n %s"%(p["Exclude-patterns"]))
            f.write("\n")


def unpack(plugin):
    fname="../{pname}_{uversion}*.orig-{name}.tar*".format(pname=c.package, uversion=UPSTREAM_VERSION, name=plugin["Plugin"])
    fname = glob.glob(fname)[0]
    try:
        os.mkdir(plugin["Plugin"])
    except FileExistsError:
        shutil.rmtree(plugin["Plugin"])
        os.mkdir(plugin["Plugin"])
    sh.tar(["-C", plugin["Plugin"], "--strip-components=1", "-axf", fname])

def update(plugin):
    try:
        retcode = sh.uscan(["--watchfile", "debian/plugins/{Plugin}/watch".format(**plugin),
                            "--upstream-version", "{Version}".format(**plugin),
                            "--download", "--dehs"])
        newversion = re.search(r"<upstream-version>([\d.]+)</upstream-version>", str(retcode)).group(1)
        downloadurl = re.search(r"<upstream-url>(.+)</upstream-url>", str(retcode)).group(1)
        oldversion = plugin["Version"]
        plugin["Version"] = newversion
        plugin["Source"] = downloadurl
        print("New version found for {Plugin} ({oldversion}) -> ({Version}) {Source}".format(oldversion=oldversion, **plugin))
    except sh.ErrorReturnCode_1:
        pass

#read all plugins form overview
read()

#search for updates & download
for p in PLUGINS:
    update(p)

#write back
write()

#extract all plugins
for p in PLUGINS:
    unpack(p)