-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.py
executable file
·203 lines (173 loc) · 6.95 KB
/
cli.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
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
#! /usr/bin/python
############################################################################
#
# Copyright 2012 Lee Smith
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
############################################################################
import sys
import os
import time
from argparse import ArgumentParser, RawTextHelpFormatter
from urlparse import urlparse
import bz2
import logging
from resources.lib.funcs import size_fmt, add_deps_to_path
add_deps_to_path()
import requests
from resources.lib import libreelec, config, sources, builds, history, log, funcs
arch_default = None
if libreelec.OS_RELEASE['NAME'] != "LibreELEC":
from resources.lib import mock
mock.mock_libreelec()
libreelec.UPDATE_DIR = os.path.expanduser('~')
arch_default = 'RPi2.arm'
parser = ArgumentParser(description='LibreELEC Dev Updater CLI')
subparsers = parser.add_subparsers(title='commands')
def get_choice(items, suffix=lambda item: " ", reverse=False):
num_width = len(str(len(items) - 1))
if reverse:
items_iter = reversed(list(enumerate(items)))
else:
items_iter = enumerate(items)
for i, item in items_iter:
print "[{num:{width}d}] {item:s}\t{suffix}".format(
num=i, item=item, width=num_width, suffix=suffix(item))
print '-' * 50
choice = raw_input('Choose an item or "q" to quit: ')
while choice != 'q':
try:
item = items[int(choice)]
return item
except ValueError:
choice = raw_input('You entered a non-integer. Choice must be an'
' integer or "q": ')
except IndexError:
choice = raw_input('You entered an invalid integer. Choice must be'
' from above list or "q": ')
sys.exit()
def read_chunk(f):
return f.read(131072)
decompressor = bz2.BZ2Decompressor()
def decompress(f):
data = read_chunk(f)
return decompressor.decompress(data)
def process(fin, fout, size, read_func=read_chunk):
start_time = time.time()
done = 0
while done < size:
data = read_func(fin)
done = fin.tell()
fout.write(data)
percent = int(done * 100 / size)
bytes_per_second = done / (time.time() - start_time)
print "\r {0:3d}% ({1}/s) ".format(percent, size_fmt(bytes_per_second)),
sys.stdout.flush()
print
def download(args):
if args.arch:
config.arch = args.arch
installed_build = builds.get_installed_build()
def build_suffix(build):
if build > installed_build:
symbol = '+'
elif build < installed_build:
symbol = '-'
else:
symbol = '='
return symbol
build_sources = sources.build_sources()
if args.source:
source_name = args.source
try:
build_source = build_sources[source_name]
except KeyError:
parsed = urlparse(source_name)
if parsed.scheme in ('http', 'https') and parsed.netloc:
if args.releases:
build_url = builds.BuildsURL(source_name,
extractor=builds.ReleaseLinkExtractor)
else:
build_url = builds.BuildsURL(source_name)
else:
print ('"{}" is not in the list of available sources '
'and is not a valid HTTP URL').format(args.source)
print 'Valid options are:\n\t{}'.format("\n\t".join(build_sources.keys()))
sys.exit(1)
else:
source_name = get_choice(build_sources.keys())
build_source = build_sources[source_name]
print
print "Arch: {}".format(config.arch)
print "Installed build: {}".format(installed_build)
try:
links = build_source.builds()
except requests.RequestException as e:
print str(e)
except builds.BuildURLError as e:
print str(e)
else:
if links:
build = get_choice(links, build_suffix, reverse=True)
remote = build.remote_file()
file_path = os.path.join(libreelec.UPDATE_DIR, build.filename)
print
print "Downloading {0} ...".format(build.url)
try:
with open(file_path, 'w') as out:
process(remote, out, build.size)
except KeyboardInterrupt:
os.remove(file_path)
print
print "Download cancelled"
sys.exit()
if build.compressed:
tar_path = os.path.join(libreelec.UPDATE_DIR, build.tar_name)
size = os.path.getsize(file_path)
print
print "Decompressing {0} ...".format(file_path)
with open(file_path, 'r') as fin, open(tar_path, 'w') as fout:
process(fin, fout, size, decompress)
os.remove(file_path)
funcs.create_notify_file(source_name, build)
print
print "The update is ready to be installed. Please reboot."
else:
print
print "No builds available"
def print_install_history(args):
if args.logdebug:
logging.getLogger().setLevel(logging.DEBUG)
print history.BuildHistory(args.dbpath)
download_parser = subparsers.add_parser('download', help="Download an update")
download_parser.add_argument('-a', '--arch',
help='Set the build type (e.g. Generic.x86_64, RPi.arm)',
default=arch_default)
download_parser.add_argument('-s', '--source', help='Set the build source')
download_parser.add_argument('-r', '--releases', action='store_true',
help='Look for unofficial releases instead of development builds')
download_parser.set_defaults(func=download)
history_parser = subparsers.add_parser('history', help="Show the install history",
formatter_class=RawTextHelpFormatter)
history_parser.add_argument(
'dbpath', nargs='?',
default="/storage/.kodi/userdata/addon_data/script.libreelec.devupdater/",
help="path to the install history database \n (default: %(default)s)")
history_parser.add_argument(
'--logdebug', action='store_true',
help="log all debug messages to the log file ({})".format(log.log_path))
history_parser.set_defaults(func=print_install_history)
args = parser.parse_args()
args.func(args)