def __init__(self, url, working_dir='./.module_installer', root_dir=None):
'''Initialize ModuleInstaller. Url should contain a fragment (#) with
:param url: URL of file to install
query parameters:
http(s)://url/file.tar.gz#module_name=modname&module_path=Some/Path&move_to=/
module_name = local name of the module
save_as = force download to save as name
module_path = relative path, once the module has been extracted, to the
module dir to "install" (copy the folder/file to the move_to path)
move_to = path to extract the module to, relative to install_root
'''
mimetypes.init()
self.url = url
parsed_url = urlparse.urlparse(url)
qs = urlparse.parse_qs(parsed_url.fragment)
#Make sure required query params exist
if not qs.get('module_name') \
or not qs.get('module_path') \
or not qs.get('move_to'):
raise ModuleDownloadException('ModuleInstaller: Missing query string parameters')
self.module_path = qs['module_path'][0]
self.move_to = qs['move_to'][0]
self.mime_type = mimetypes.guess_type(parsed_url.path)
self.working_dir = os.path.abspath(working_dir)
self.module_name = qs['module_name'][0]
ext = os.path.splitext(parsed_url.path)
ext = os.path.splitext(ext[0])[1] + ext[1] #In case it's a .tar.gz
save_as = qs.get('save_as')
self.download_name = save_as[0] if save_as else self.module_name + ext
#Try to get the mime type from save_as name, if one doesn't exist
if not self.mime_type[0]:
self.mime_type = mimetypes.guess_type(self.download_name)
self.install_root = root_dir if root_dir else os.getcwd()
#Absolute path where the modules will be installed
self.full_install_path = os.path.join(self.install_root, self.move_to)
评论列表
文章目录