11# -*- coding: utf-8 -*-
22from imagepy import root_dir
33from sciapp .action import Free
4- import os , subprocess , zipfile , shutil
5-
6- import zipfile , sys , urllib
7- path = 'https://github.com/Image-Py/imagepy/archive/main.zip'
8-
9- from urllib .request import urlretrieve
10- import urllib
11- from io import BytesIO as StringIO
4+ import os , shutil , sys , subprocess
5+ from dulwich import porcelain
126
137path_plgs = os .path .join (root_dir , 'plugins' )
14- path_cache = os .path .join (path_plgs , 'cache' )
15- if not os .path .exists (path_plgs ):
16- os .mkdir (path_plgs )
17- if not os .path .exists (path_cache ):
18- os .mkdir (path_cache )
8+ if not os .path .exists (path_plgs ): os .mkdir (path_plgs )
199
2010def Schedule (a ,b ,c , plg ):
2111 per = 100.0 * a * b / c
@@ -25,50 +15,16 @@ def Schedule(a,b,c, plg):
2515
2616class Install (Free ):
2717 title = 'Install Plugins'
28- para = {'repo' :'https://github.com/Image-Py/IBook' , 'proxy' : False , 'Protocol' : 'https' , 'IP' : '127.0.0.1' , 'Port' : '1080' }
29- view = [('lab' , None , 'input a zipfile url or github url as http://github.com/username/project' ),
30- (str , 'repo' , 'package' , '' ),
31- (bool , 'proxy' , 'Use proxy' ),
32- (list , 'Protocol' , ['socks5' , 'http' , 'https' ], str , 'Protocol' , '' ),
33- (str , 'IP' , 'IP Address' , '' ),
34- (str , 'Port' , 'Port' , '' )]
18+ para = {'repo' :'https://github.com/Image-Py/IBook' }
19+ view = [('lab' , None , 'input git url as http://github.com/username/project' ),
20+ (str , 'repo' , 'package' , '' )]
3521
3622 def run (self , para = None ):
37- url = para ['repo' ]
38- if 'github.com' in url :
39- if url [- 4 :] == '.git' :
40- url = url .replace ('.git' , '/archive/main.zip' )
41- elif url [- 4 :] != '.zip' :
42- url = url + '/archive/main.zip'
43- domain , name = url .split ('/' )[- 4 :- 2 ]
44- else :
45- domain , name = (url [:- 4 ].replace ('.' ,'-' )).split ('/' )[- 2 :]
46- domain , name = domain .replace ('_' , '-' ), name .replace ('_' , '-' )
47-
48- self .app .info ('downloading plugin from %s' % para ['repo' ])
49-
50- if True == para ['proxy' ]:
51- proxy = para ['Protocol' ]+ "://" + para ['IP' ]+ ":" + para ['Port' ]
52- print ("proxy = " , proxy )
53- # Build ProxyHandler object by given proxy
54- proxy_support = urllib .request .ProxyHandler ({para ['Protocol' ]:proxy })
55- # Build opener with ProxyHandler object
56- opener = urllib .request .build_opener (proxy_support )
57- # Install opener to request
58- urllib .request .install_opener (opener )
59-
60- urlretrieve (url , os .path .join (path_cache , domain + '_' + name + '.zip' ),
61- lambda a ,b ,c , p = self : Schedule (a ,b ,c ,p ))
62- zipf = zipfile .ZipFile (os .path .join (path_cache , domain + '_' + name + '.zip' ))
63- folder = zipf .namelist ()[0 ]
64- zipf .extractall (path_cache )
65- destpath = os .path .join (path_plgs , domain + '_' + folder .replace ('-main' ,'' ))
66- if os .path .exists (destpath ): shutil .rmtree (destpath )
67- os .rename (os .path .join (path_cache , folder ), destpath )
68- zipf .close ()
23+ path = os .path .join (path_plgs , os .path .split (para ['repo' ])[- 1 ])
24+ porcelain .clone (para ['repo' ], path , depth = 1 ).close ()
25+ shutil .rmtree (os .path .join (path , '.git' ))
6926 self .app .info ('installing requirement liberies' )
70- self .prgs = None
71- cmds = [sys .executable , '-m' , 'pip' , 'install' , '-r' , '%s/requirements.txt' % destpath ]
27+ cmds = [sys .executable , '-m' , 'pip' , 'install' , '-r' , '%s/requirements.txt' % path ]
7228 subprocess .call (cmds )
7329 self .app .load_all ()
7430
0 commit comments