forked from retorquere/zotero-better-bibtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zotero5_installer.py
executable file
·143 lines (124 loc) · 5.45 KB
/
zotero5_installer.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
#!/usr/bin/env python
import platform
import glob
import argparse
import re
import urllib2
import json
import os
import sys
from HTMLParser import HTMLParser
import tempfile
import urllib
def zotero_latest():
response = urllib2.urlopen('https://www.zotero.org/download/')
for line in response.read().split('\n'):
if not '"standaloneVersions"' in line: continue
line = re.sub(r'.*Downloads,', '', line)
line = re.sub(r'\),', '', line)
versions = json.loads(line)
return versions['standaloneVersions']['linux-' + platform.machine()]
def jurism_latest():
class Parser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag != 'a': return
href = [attr[1] for attr in attrs if attr[0] == 'href']
if len(href) == 0: return
href = href[0]
m = re.match(r'https://our.law.nagoya-u.ac.jp/download/client/Jurism-(.+)_linux-' + platform.machine() + '.tar.bz2', href)
if m is None: return
self.version = m.group(1)
response = urllib2.urlopen('https://juris-m.github.io/downloads/')
parser = Parser()
parser.feed(response.read())
return parser.version
class LocationAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
location = values.lower()
if len(location) == 0:
parser.error('Missing location')
elif 'local'[:len(location)] == location:
setattr(namespace, self.dest, 'local')
elif 'global'[:len(location)] == location:
setattr(namespace, self.dest, 'global')
else:
parser.error('Unexpected location "' + values + '", expected "local" or "global"')
class ClientAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
client = re.sub(r"[^a-z]", '', values.lower())
if len(client) == 0:
parser.error('Missing client')
elif 'jurism'[:len(client)] == client:
setattr(namespace, self.dest, 'jurism')
elif 'zotero'[:len(client)] == client:
setattr(namespace, self.dest, 'zotero')
else:
parser.error('Unexpected client "' + values + '", expected "Zotero" or "Juris-M"')
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--client', action=ClientAction, required=True)
parser.add_argument('-v', '--version')
parser.add_argument('-d', '--destination', action=LocationAction, required=True)
parser.add_argument('-r', '--replace', action='store_true')
parser.add_argument('--cache')
args = parser.parse_args()
if args.cache is not None and not os.path.exists(args.cache):
print args.cache + ' does not exist'
sys.exit(1)
if args.version == 'latest' or args.version is None:
version = zotero_latest() if args.client == 'zotero' else jurism_latest()
if args.version is None:
args.version = raw_input(args.client + ' version (' + version + '):')
if args.version == '': args.version = version
else:
args.version = version
if args.destination is None:
installdir = raw_input('Installation directory: ')
if installdir == '': raise Exception("Installation directory is mandatory")
menudir = None
elif args.destination == 'local':
installdir = os.path.join(os.path.expanduser('~/bin'), args.client)
menudir = os.path.expanduser('~/.local/share/applications')
else:
installdir = '/opt'
menudir = '/usr/share/applications'
if os.path.exists(installdir) and not args.replace: raise Exception('Installation directory "' + installdir + '"exists')
if args.client == 'zotero':
if args.version == 'beta':
args.url = "https://www.zotero.org/download/client/dl?channel=beta&platform=linux-" + platform.machine()
else:
args.url = "https://www.zotero.org/download/client/dl?channel=release&platform=linux-" + platform.machine() + '&version=' + args.version
else:
args.url = 'https://our.law.nagoya-u.ac.jp/download/client/Jurism-' + args.version + '_linux-' + platform.machine() + '.tar.bz2'
tarball = args.client + '-' + platform.machine() + '-' + args.version + '.tar.bz2'
if args.cache is None:
tarball = tempfile.NamedTemporaryFile().name
else:
tarball = args.client + '-' + platform.machine() + '-' + args.version + '.tar.bz2'
for junk in glob.glob(os.path.join(args.cache, args.client + '-*.tar.bz2')):
if os.path.basename(junk) != tarball: os.remove(junk)
tarball = os.path.join(args.cache, tarball)
if os.path.exists(tarball):
print 'Retaining ' + tarball
else:
print "Downloading " + args.client + " standalone " + args.version + ' for ' + platform.machine() + ' from ' + args.url
urllib.urlretrieve (args.url, tarball)
extracted = tempfile.mkdtemp()
def shellquote(s):
return "'" + s.replace("'", "'\\''") + "'"
os.system('tar --strip 1 -xpf ' + shellquote(tarball) + ' -C ' + shellquote(extracted))
if os.path.exists(installdir): os.system('rm -rf ' + shellquote(installdir))
os.system('mkdir -p ' + shellquote(os.path.dirname(installdir)))
os.system('mv ' + shellquote(extracted) + ' ' + shellquote(installdir))
if not menudir is None:
if not os.path.exists(menudir): os.system('mkdir -p ' + shellquote(menudir))
with open(os.path.join(menudir, args.client + '.desktop'), 'w') as desktop:
desktop.write("[Desktop Entry]\n")
if args.client == 'zotero':
desktop.write("Name=Zotero\n")
else:
desktop.write("Name=Juris-M\n")
desktop.write("Comment=Open-source reference manager\n")
desktop.write("Exec=" + installdir + '/' + args.client + " -datadir profile\n")
desktop.write("Icon=" + installdir + "/chrome/icons/default/default48.png\n")
desktop.write("Type=Application\n")
desktop.write("StartupNotify=true")