def do_info(self, script_name): if script_name not in self.remote_index['scripts'].keys(): raise PsicException('%s: script not found' % script_name) else: entry = self.remote_index['scripts'][script_name] if 'info' in entry.keys(): return script_name, entry['info'], self.merged_index[ 'scripts'][script_name].get('installed', None) meta_url, tag = urllib2.splittag( self.remote_index['scripts'][script_name]['meta_url']) jmsg = json.loads( urllib2.urlopen(refreshable_url(meta_url)).read()) jmsg = jmsg[tag] if tag else jmsg jmsg['author'] = jmsg.get('author', '') jmsg['email'] = jmsg.get('email', '') jmsg['website'] = jmsg.get('website', '') jmsg['long_description'] = jmsg.get('long_description', 'N/A') jmsg['releases'] = jmsg.get('releases', []) releases = [] for release in jmsg.get('releases', []): release['version'] = release.get('version', None) releases.append(release) jmsg['releases'] = releases self.remote_index['scripts'][script_name]['info'] = jmsg installed = self.merged_index['scripts'][script_name].get( 'installed', None) return script_name, jmsg, installed
def __init__(self, url): self.url = url self.schema, url = urllib2.splittype(url) host, path = urllib2.splithost(url) userpass, host = urllib2.splituser(host) if userpass: self.user, self.password = urllib2.splitpasswd(userpass) path, self.querystring = urllib.splitquery(path) self.query = self.querystring and self.querystring.split('&') or [] #urllib.splitquery(url) self.host, self.port = urllib2.splitport(host) path, self.tag = urllib2.splittag(path) self.path = path.strip('/')
def do_install(self, script_name, ver_num=None, dest_dir=None): if script_name not in self.merged_index['scripts'].keys(): raise PsicException('%s: not found' % script_name) elif self.merged_index['scripts'][script_name].get('installed', None): raise PsicException('%s: already installed' % script_name) else: meta_url, tag = urllib2.splittag( self.merged_index['scripts'][script_name]['meta_url']) jmsg = json.loads( urllib2.urlopen(refreshable_url(meta_url)).read()) jmsg = jmsg[tag] if tag else jmsg releases = jmsg['releases'] release = get_release_for_version(releases, ver_num) if release is None: raise PsicException('%s: release not found' % script_name) if 'filetype' not in release: if release['url'].endswith('.py') or is_gist(release['url']): release['filetype'] = 'SingleFile' elif release['url'].endswith('.zip'): release['filetype'] = 'ZippedFiles' else: raise PsicException('%s: unknown filetype' % script_name) if not dest_dir: dest_dir = self.dest_dir else: dest_dir = os.path.expanduser(dest_dir) dest_dir = os.path.abspath(dest_dir) if not os.path.exists(dest_dir) or not os.path.isdir(dest_dir): os.makedirs(dest_dir) filetype = release['filetype'] files_installed = [] full_name = jmsg.get('name', None) if full_name is None: full_name = script_name saved_dir = os.getcwd() try: if filetype == 'SingleFile': os.chdir(dest_dir) filename = full_name if full_name.endswith('.py') else ( full_name + '.py') if release['url'].endswith('.py'): wget(release['url'], output_file=filename) files_installed.append(filename) raw_match = re.match('http(s?)://raw.github.com/gist/', release['url']) raw_match2 = re.match( 'http(s?)://gist.githubusercontent.com/([0-9a-zA-Z]*)/([0-9a-f]*)/raw/', release['url']) if not raw_match and not raw_match2: try: filename_pyui = filename + 'ui' wget(release['url'] + 'ui', output_file=filename_pyui) files_installed.append(filename_pyui) except WgetException: pass elif is_gist(release['url']): try: _, content, nrevisions = download_gist( release['url']) except (InvalidGistURLError, MultipleFilesInGistError, NoFilesInGistError, GistDownloadError): raise PsicException('%s: Gist url error' % release['url']) with open(filename, 'w') as outs: outs.write(content) files_installed.append(filename) if release['version'] is None: release['version'] = '%d.0.0' % nrevisions else: raise PsicException('%s: invalid single file url' % release['url']) elif filetype == 'ZippedFiles': os.chdir(os.environ['TMPDIR']) wget(release['url'], output_file='files.zip') this_dest_dir = os.path.join(dest_dir, full_name) if not os.path.exists(this_dest_dir) or not os.path.isdir( this_dest_dir): os.mkdir(this_dest_dir) files_extracted = unzip('files.zip', exdir=this_dest_dir) for f in files_extracted: if f.strip() == '': continue f = os.path.relpath(f, dest_dir) if f != '.': files_installed.append(f) os.remove('files.zip') else: raise PsicException('%s: unknown filetype' % script_name) finally: os.chdir(saved_dir) installed = { 'version': release['version'], 'dest_dir': dest_dir, 'filetype': release['filetype'], 'files': files_installed } if self.remote_index_url not in self.local_indices: self.local_indices[self.remote_index_url] = {'scripts': {}} self.local_indices[ self.remote_index_url]['scripts'][script_name] = { 'installed': installed } self.save_local_record() self.merge_remote_and_local() return script_name, installed