-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
135 lines (109 loc) · 3.74 KB
/
lib.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
# coding=utf-8
# Enkeltlib 1.1
# Copyright 2018, 2019 Edvard Busck-Nielsen
# This file is part of Enkelt.
#
# Enkelt 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.
#
# Enkelt 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 Enkelt. If not, see <https://www.gnu.org/licenses/>.
import urllib.request
import sys
import os
def show_help_message(sys_args):
if len(sys_args) > 1:
print('\nOgiltigt argument:', sys_args[1])
print('Prova hjälpkommandot:\npython3 lib.py hjälp\n')
def install(enkelt_module):
local_path = 'bib/' + enkelt_module + '.e'
web_path = web_import_location + enkelt_module + '.e'
if os.path.isfile(local_path):
print('Modulen ', enkelt_module, ' är redan installerad.')
ans = input('Vill du uppdatera den? (J/n) ')
if ans.lower() == 'j' or ans == '':
update(enkelt_module)
else:
try:
response = urllib.request.urlopen(web_path)
module_code = response.read().decode('utf-8')
with open(local_path, 'w') as f:
f.write(module_code)
f.close()
if os.path.isfile(local_path):
print('Modulen ', enkelt_module, ' installerad.')
except Exception:
print('Modulen ', enkelt_module, ' kunde inte hittas.')
def update(enkelt_module):
local_path = 'bib/' + enkelt_module + '.e'
web_path = web_import_location + enkelt_module + '.e'
if os.path.isfile(local_path):
try:
response = urllib.request.urlopen(web_path)
web_module_code = response.read().decode('utf-8')
with open(local_path, 'r') as f:
local_module_code = f.read()
f.close()
if local_module_code != web_module_code:
with open(local_path, 'w') as f:
f.write(web_module_code)
f.close()
print('Modulen', enkelt_module, 'uppdaterades.')
else:
print('Redan uppdaterad.')
except Exception:
print('Modulen', enkelt_module, 'kunde inte hittas.')
else:
print('Ingen installerad modul vid namnet', enkelt_module, ' kunde hittas.')
ans = input('Vill du installera den? (J/n) ')
if ans.lower() == 'j' or ans == '':
install(enkelt_module)
def uninstall(enkelt_module):
local_path = 'bib/' + enkelt_module + '.e'
if os.path.isfile(local_path):
os.remove(local_path)
if not os.path.isfile(local_path):
print('Modulen', enkelt_module, 'avinstallerades.')
else:
print('Ingen installerad modul vid namnet ', enkelt_module, ' kunde hittas.')
def list_installed_modules():
for file_name in os.listdir('bib'):
if file_name.endswith('.e'):
print(file_name[:-2])
web_import_location = 'https://raw.githubusercontent.com/Enkelt/EnkeltBibliotek/master/bib/'
help_message = '''
Hur man använder lib:
python3 lib.py <kommando> [modulnamn]
Kommandon:
installera installera modul
uppdatera uppdatera modul
avinstallera avinstallera modul
lista visa alla installerade moduler
hjälp visa det här meddelandet
'''
args = sys.argv
if len(args) > 2:
if args[1].lower() == 'installera':
install(args[2])
elif args[1].lower() == 'uppdatera':
update(args[2])
elif args[1].lower() == 'avinstallera':
uninstall(args[2])
elif args[1].lower() == 'lista':
list_installed_modules()
else:
show_help_message(args)
elif len(args) > 1:
if args[1].lower() == 'hjälp':
print(help_message)
else:
show_help_message(args)
else:
show_help_message(args)