-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
executable file
·153 lines (131 loc) · 4.53 KB
/
helper.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import fnmatch
import os
import subprocess
import re
import shutil
import sys
import urllib.parse
import xmlrpc.client
import zipfile
from configparser import ConfigParser
from io import StringIO
def package(version=None):
if not version or version.startswith("dev-"):
# CI uses dev-{SHA}
archive = "koordinates.zip"
else:
archive = f"koordinates-{version}.zip"
print(f"Creating {archive} ...")
with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as zipFile:
excludes = {"test", "test", "*.pyc", ".git", "metadata.txt"}
src_dir = os.path.join(os.path.dirname(__file__), "koordinates")
exclude = lambda p: any([fnmatch.fnmatch(p, e) for e in excludes])
cfg = ConfigParser()
cfg.optionxform = str
cfg.read(os.path.join(src_dir, "metadata.txt"))
if version:
cfg.set("general", "version", re.sub(r"^v", "", version))
buf = StringIO()
cfg.write(buf)
zipFile.writestr("koordinates/metadata.txt", buf.getvalue())
zipFile.write("LICENSE", "koordinates/LICENSE")
def filter_excludes(files):
if not files:
return []
for i in range(len(files) - 1, -1, -1):
f = files[i]
if exclude(f):
files.remove(f)
return files
for root, dirs, files in os.walk(src_dir):
for f in filter_excludes(files):
relpath = os.path.relpath(root, ".")
zipFile.write(os.path.join(root, f), os.path.join(relpath, f))
filter_excludes(dirs)
def install(profile: str = None):
if not profile:
profile = 'default'
src = os.path.join(os.path.dirname(__file__), "koordinates")
if os.name == "nt":
default_profile_plugins = (
f"~/AppData/Roaming/QGIS/QGIS3/profiles/{profile}/python/plugins"
)
elif sys.platform == "darwin":
default_profile_plugins = (
f"~/Library/Application Support/QGIS/QGIS3/profiles/{profile}/python/plugins"
)
else:
default_profile_plugins = (
f"~/.local/share/QGIS/QGIS3/profiles/{profile}/python/plugins"
)
dst_plugins = os.path.expanduser(default_profile_plugins)
os.makedirs(dst_plugins, exist_ok=True)
dst = os.path.abspath(os.path.join(dst_plugins, "koordinates"))
print(f"Installing to {dst} ...")
src = os.path.abspath(src)
if os.path.exists(dst):
try:
os.remove(dst)
except IsADirectoryError:
shutil.rmtree(dst)
if not hasattr(os, "symlink"):
shutil.copytree(src, dst)
elif not os.path.exists(dst):
os.symlink(src, dst, True)
def setup():
extlibs = os.path.join(os.path.dirname(__file__), "koordinates", "extlibs")
os.makedirs(extlibs, exist_ok=True)
reqs = open("requirements.txt").readlines()
os.environ["PYTHONPATH"] = extlibs
for req in reqs:
try:
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"--upgrade",
"-t",
extlibs,
req,
]
)
except subprocess.CalledProcessError:
print(f"Error installing {req} with pip.")
sys.exit(1)
def publish(archive):
try:
creds = os.environ["QGIS_CREDENTIALS"]
except KeyError:
print("QGIS_CREDENTIALS not set")
sys.exit(2)
url = f"https://{creds}@plugins.qgis.org/plugins/RPC2/"
conn = xmlrpc.client.ServerProxy(url)
print(f"Uploading {archive} to https://plugins.qgis.org ...")
with open(archive, "rb") as fd:
blob = xmlrpc.client.Binary(fd.read())
conn.plugin.upload(blob)
print(f"Upload complete")
def usage():
print(
(
"Usage:\n"
f" {sys.argv[0]} package [VERSION] Build a QGIS plugin zip file\n"
f" {sys.argv[0]} install [profile name] Install in your local QGIS (for development)\n"
),
file=sys.stderr,
)
sys.exit(2)
if len(sys.argv) >= 2 and sys.argv[1] == "install":
install(profile=(None if len(sys.argv) < 3 else sys.argv[2]))
elif len(sys.argv) == 2 and sys.argv[1] == "setup":
setup()
elif len(sys.argv) in [2, 3] and sys.argv[1] == "package":
package(*sys.argv[2:])
elif len(sys.argv) == 3 and sys.argv[1] == "publish":
publish(sys.argv[2])
else:
usage()