1 | #!/usr/bin/python3 |
---|
2 | import sys,os,signal |
---|
3 | import argparse |
---|
4 | #sys.path.append('/usr/share/lliurex-store') |
---|
5 | #sys.path.append('/home/lliurex/lliurex-store/trunk/fuentes/lliurex-appstore.install/usr/share/lliurex-store') |
---|
6 | import lliurexstore.storeManager as storeManager |
---|
7 | import time |
---|
8 | import html2text |
---|
9 | import gettext |
---|
10 | import threading |
---|
11 | gettext.textdomain('python3-lliurex-store') |
---|
12 | _=gettext.gettext |
---|
13 | |
---|
14 | class color: |
---|
15 | PURPLE = '\033[95m' |
---|
16 | CYAN = '\033[96m' |
---|
17 | DARKCYAN = '\033[36m' |
---|
18 | BLUE = '\033[94m' |
---|
19 | GREEN = '\033[92m' |
---|
20 | YELLOW = '\033[93m' |
---|
21 | RED = '\033[91m' |
---|
22 | BOLD = '\033[1m' |
---|
23 | UNDERLINE = '\033[4m' |
---|
24 | END = '\033[0m' |
---|
25 | |
---|
26 | def signal_handler(*args): |
---|
27 | if args[0]==17: |
---|
28 | return |
---|
29 | os.system('setterm -cursor on') |
---|
30 | sys.exit(0) |
---|
31 | |
---|
32 | |
---|
33 | def main(): |
---|
34 | def print_results(action=None): |
---|
35 | printed=False |
---|
36 | results=store.get_result(action) |
---|
37 | status=store.get_status(action) |
---|
38 | if not 'status' in status.keys(): |
---|
39 | status['status']=1 |
---|
40 | status['msg']='package not found' |
---|
41 | processed=[] |
---|
42 | print ("") |
---|
43 | for action in results.keys(): |
---|
44 | if action in actions and not actionList[action]: |
---|
45 | if status['status']==0: |
---|
46 | print (_(u"Results for ")+_(action)) |
---|
47 | for data in results[action]: |
---|
48 | if action=='info': |
---|
49 | try: |
---|
50 | print(color.DARKCYAN+_(u'Package')+': '+color.END + data['package']) |
---|
51 | print(_(u'Name')+': '+data['name']) |
---|
52 | print(_(u'ID')+': '+data['id']) |
---|
53 | print(_(u'Version')+': '+data['version']) |
---|
54 | print(_(u'Size')+': '+data['size']) |
---|
55 | print(_(u'License')+': '+data['license']) |
---|
56 | listCat=[] |
---|
57 | for cat in data['categories']: |
---|
58 | listCat.append(_(cat)) |
---|
59 | print(_(u'Categories')+': '+','.join(listCat)) |
---|
60 | msg='' |
---|
61 | if data['state']=='installed': |
---|
62 | msg=_('installed') |
---|
63 | else: |
---|
64 | msg=_('available') |
---|
65 | if data['updatable']: |
---|
66 | msg +=_(' (updatable)') |
---|
67 | print(_(u'Status')+': '+msg) |
---|
68 | print(_(u'Summary')+': '+data['summary']) |
---|
69 | desc=(html2text.html2text(data['description'],"lxml")) |
---|
70 | print(_(u'Description')+': '+desc) |
---|
71 | pkgString=[] |
---|
72 | for dependency in data['depends']: |
---|
73 | pkgName=dependency.split(';')[0] |
---|
74 | pkgString.append(pkgName) |
---|
75 | print(_(u'Depends')+': '+', '.join(pkgString)) |
---|
76 | print("") |
---|
77 | except Exception as e: |
---|
78 | print("CLI: Error printing key %s"%e) |
---|
79 | elif action=='search': |
---|
80 | #Only print name and summary |
---|
81 | data_id='' |
---|
82 | printcolor=color.DARKCYAN |
---|
83 | if data['bundle']: |
---|
84 | printcolor=color.PURPLE |
---|
85 | elif (data['package'] not in data['id'] or data['package'] in processed): |
---|
86 | data_id=" (%s)"%data['id'] |
---|
87 | else: |
---|
88 | processed.append(data['package']) |
---|
89 | print("%s%s%s%s: %s"%(printcolor,data['package'],data_id,color.END,data['summary'])) |
---|
90 | |
---|
91 | elif action=='list': |
---|
92 | #Print package, summary and status |
---|
93 | try: |
---|
94 | if data['package']: |
---|
95 | package=data['package'] |
---|
96 | else: |
---|
97 | package=data['name'] |
---|
98 | if data['state']=='installed': |
---|
99 | msg=_('installed') |
---|
100 | else: |
---|
101 | msg=_('available') |
---|
102 | print(color.DARKCYAN+package+color.END+": "+data['summary']+' ('+','.join(data['categories'])+')'+' ('+msg+')') |
---|
103 | except Exception as e: |
---|
104 | print(_(u'Error listing')+ ':'+str(e)) |
---|
105 | pass |
---|
106 | elif action=='install': |
---|
107 | print(color.DARKCYAN+data['package']+color.END+" "+ _(u"installed")+" "+color.BOLD+ _(u"succesfully")+color.END) |
---|
108 | elif action=='remove': |
---|
109 | print(color.DARKCYAN+data['package']+color.END+" "+ _(u"removed")+" "+color.BOLD+ _(u"succesfully")+color.END) |
---|
110 | else: |
---|
111 | print("RESULT:\n%s"%data) |
---|
112 | else: |
---|
113 | msg=_(u"Unable to")+' '+_(action) |
---|
114 | failed=parms[action] |
---|
115 | if (action=='search' or action=='info'): |
---|
116 | msg=_(u"Unable to show") |
---|
117 | if action=='list': |
---|
118 | failed=', '.join(failed) |
---|
119 | |
---|
120 | print (color.RED+_(u"Error")+": "+color.END+msg+' '+failed+' ('+_(status['msg'])+')') |
---|
121 | printed=True |
---|
122 | return(printed) |
---|
123 | #def print_results |
---|
124 | |
---|
125 | CURSOR_UP='\033[F' |
---|
126 | ERASE_LINE='\033[K' |
---|
127 | actions=[] |
---|
128 | parms={} |
---|
129 | dbg=False |
---|
130 | appimage=False |
---|
131 | snap=False |
---|
132 | autostart=True |
---|
133 | args=process_Args(sys.argv) |
---|
134 | # if args.debug: |
---|
135 | # dbg=True |
---|
136 | if args.appimage: |
---|
137 | appimage=True |
---|
138 | if args.snap: |
---|
139 | snap=True |
---|
140 | if args.update: |
---|
141 | actions.append('cache') |
---|
142 | parms['cache']=None |
---|
143 | autostart=False |
---|
144 | if args.view: |
---|
145 | actions.append('info') |
---|
146 | parms['info']=args.view |
---|
147 | if args.search: |
---|
148 | actions.append('search') |
---|
149 | parms['search']=args.search |
---|
150 | if args.install: |
---|
151 | actions.append('install') |
---|
152 | parms['install']=args.install |
---|
153 | if args.remove: |
---|
154 | actions.append('remove') |
---|
155 | parms['remove']=args.remove |
---|
156 | # if args.random: |
---|
157 | # actions.append('random') |
---|
158 | # parms['random']=args.random |
---|
159 | # if args.list: |
---|
160 | # actions.append('list') |
---|
161 | # parms['list']=args.list |
---|
162 | |
---|
163 | actionList={'search':False,'info':False,'pkgInfo':False,'install':False,'remove':False,'list':False,'list-sections':False,'random':False,'cache':False} |
---|
164 | start_time=time.time() |
---|
165 | store=storeManager.StoreManager(appimage=appimage,snap=snap,dbg=dbg,autostart=autostart,cli=True) |
---|
166 | for action in actions: |
---|
167 | th=threading.Thread(target=store.execute_action, args = (action,parms[action])) |
---|
168 | th.start() |
---|
169 | actionList[action]=False |
---|
170 | |
---|
171 | inc=0 |
---|
172 | banner=' '.join(actions) |
---|
173 | banner='LliureX Store' |
---|
174 | numchar=len(banner) |
---|
175 | os.system('setterm -cursor off') |
---|
176 | while store.is_action_running(): |
---|
177 | ini=banner[0:numchar] |
---|
178 | end=banner[numchar:inc] |
---|
179 | text=ini+' '+end |
---|
180 | print(text+' ',end='\r') |
---|
181 | numchar-=1 |
---|
182 | inc+=1 |
---|
183 | time.sleep(0.2) |
---|
184 | if numchar<0: |
---|
185 | numchar=len(banner) |
---|
186 | inc=0 |
---|
187 | print("") |
---|
188 | print (CURSOR_UP + ERASE_LINE) |
---|
189 | for key in actionList: |
---|
190 | progressDic=store.get_progress(key) |
---|
191 | if key in progressDic: |
---|
192 | if progressDic[key]==100 and not actionList[key]: |
---|
193 | actionList[key]=print_results(key) |
---|
194 | print_results() |
---|
195 | os.system('setterm -cursor on') |
---|
196 | |
---|
197 | def process_Args(args): |
---|
198 | parser=argparse.ArgumentParser(description=(u'Lliurex Store.')) |
---|
199 | parser.add_argument('-s','--search',metavar='Name',nargs='?',help=(_(u"Search a package"))) |
---|
200 | parser.add_argument('-v','--view',metavar='Name',nargs='?',help=(_(u"Show all info from a package"))) |
---|
201 | parser.add_argument('-i','--install',metavar='Package',help=(_(u"Install a package"))) |
---|
202 | parser.add_argument('-r','--remove',metavar='Package',help=(_(u"Remove a package"))) |
---|
203 | # parser.add_argument('--random',metavar='Results',help=(_(u"List random packages"))) |
---|
204 | # parser.add_argument('--debug',action='store_true',help=(_(u"Prints debug information"))) |
---|
205 | parser.add_argument('--appimage',action='store_true',help=(_(u"Load appimage catalog"))) |
---|
206 | parser.add_argument('--snap',action='store_true',help=(_(u"Load snap catalog"))) |
---|
207 | parser.add_argument('--update',action='store_true',help=(_(u"Update cache"))) |
---|
208 | # parser.add_argument('--list',metavar='list',nargs='?',help=(_(u"List category"))) |
---|
209 | |
---|
210 | args=parser.parse_args() |
---|
211 | return args |
---|
212 | |
---|
213 | #Cause the cli hides the cursor we must assure that the cursor is showing when the program ends |
---|
214 | sigs=set(signal.Signals) - {signal.SIGKILL, signal.SIGSTOP} |
---|
215 | for sig in sigs: |
---|
216 | signal.signal(sig,signal_handler) |
---|
217 | main() |
---|