def download_handler(block, static = [size, 0, cli.formatSize(size)]) : local_file.write(block) static[1] += len(block) percent = (100.0 * static[1]) / static[0] cli.printLine("--- retr :: [%s] %3.1f%% %s of %s" % ( ("=" * int(percent / 2.5)) + (" " * int(40 - percent / 2.5)), percent, cli.formatSize(static[1]), static[2] ), short_flag=True)
def fetchResources(types_list=["dicts", "sounds"]): resources_dict = dict(map((lambda item: (item, {})), types_list)) cli.printLine() for repos_list_item in repos.ReposList: host = repos_list_item["host"] port = repos_list_item["port"] user = repos_list_item["user"] passwd = repos_list_item["passwd"] cli.printLine('Processing remote repository "%s" for %s...' % (host, str(types_list))) try: server = ftp.connectServer(host, port, user, passwd) except: continue for resource_type in resources_dict.keys(): count = 0 for file_path in server.nlst(os.path.join(repos_list_item["root"], resource_type)): if file_path.startswith("."): continue try: file_size = server.size(file_path) if file_size <= 0: cli.printLine('--- file "%s" :: warning, non-positive size' % (file_path)) continue except Exception, err: cli.printLine('--- file "%s" :: warning, error while getsize: %s' % (file_path, str(err))) continue resource_name = os.path.basename(file_path).replace( {"dicts": ".bz2", "sounds": ".tar.bz2"}[resource_type], "" ) item_dict = { "host": host, "port": port, "user": user, "passwd": passwd, "file_path": file_path, "file_size": file_size, } if not resources_dict[resource_type].has_key(resource_name): resources_dict[resource_type][resource_name] = [item_dict] else: resources_dict[resource_type][resource_name].append(item_dict) count += 1 cli.printLine('--- server "%s" :: %d %s' % (host, count, resource_type)) ftp.closeServer(server)
def removeResource(resource_name, resource_type) : cli.printLine("--- resource \"%s\" :: begin to remove..." % (resource_name), short_flag=True) file_path = os.path.join(const.SL_SHARES_DIR, resource_type, resource_name) try : if resource_type == "dicts" : os.remove(file_path) elif resource_type == "sounds" : shutil.rmtree(file_path) except Exception, err : cli.printLine("--- resource \"%s\" :: removal failure: %s" % (resource_name, str(err)), short_flag=( err.errno == errno.ENOENT )) if err.errno != errno.ENOENT : raise
def connectServer(host, port, user = "", passwd = "", short_flag = False) : server = None try : server = ftplib.FTP() cli.printLine("--- server \"%s\" -> %s" % (host, server.connect(host, port)), short_flag=short_flag) cli.printLine("--- server \"%s\" -> %s" % (host, server.login(user, passwd)), short_flag=short_flag) except Exception, err : cli.printLine("Cannot connect to \"%s:%d\": %s" % (host, port, str(err))) closeServer(server, short_flag=short_flag) raise
def fetchResources(types_list = ["dicts", "sounds"]) : cli.printLine("\nProcessing local repository for %s..." % (str(types_list))) resources_dict = dict(map(( lambda item : (item, {}) ), types_list)) for resource_type in resources_dict.keys() : count = 0 for file_name in os.listdir(os.path.join(const.SL_SHARES_DIR, resource_type)) : if file_name.startswith(".") : continue file_path = os.path.join(const.SL_SHARES_DIR, resource_type, file_name) try : if resource_type == "dicts" : file_size = os.stat(file_path).st_size elif resource_type == "sounds" : file_size = 0 for root_dir_path, dirs_list, files_list in os.walk(file_path) : for files_list_item in dirs_list + files_list : file_size += os.stat(os.path.join(root_dir_path, files_list_item)).st_size if file_size <= 0 : cli.printLine("--- file \"%s\" :: warning, non-positive size" % (file_path)) continue except Exception, err : cli.printLine("--- file \"%s\" :: warning, error while getsize: %s" % (file_path, str(err))) continue resources_dict[resource_type][file_name] = { "file_path" : file_path, "file_size" : file_size } count += 1 cli.printLine("--- local :: %d %s" % (count, resource_type))
def installResource(resource_name, resource_type, resources_dict): for resource in resources_dict[resource_type][resource_name]: local_file_name = "." + os.path.basename(resource["file_path"]) local_file_path = os.path.join(const.SL_SHARES_DIR, resource_type, local_file_name) try: server = ftp.connectServer(resource["host"], resource["port"], resource["user"], resource["passwd"], True) ftp.downloadFile(server, resource["file_path"], local_file_path) cli.printLine('--- file "%s" :: download complete' % (local_file_path), short_flag=True) except Exception, err: cli.printLine('--- file "%s" :: download error: %s' % (local_file_path, str(err))) continue if resource_type == "dicts": try: dict_bz2 = bz2.BZ2File(local_file_path) dict_file = open(os.path.join(os.path.join(const.SL_SHARES_DIR, resource_type, resource_name)), "w") while True: line = dict_bz2.readline() if len(line) == 0: break dict_file.write(line) dict_bz2.close() dict_file.close() except Exception, err: try: dict_bz2.close() except: pass try: dict_file.close() except: pass try: os.remove(local_file_path) except: pass cli.printLine('--- file "%s" :: bunzip2 error: %s' % (local_file_path, str(err))) continue
def closeServer(server, short_flag = False) : try : ( server != None and cli.printLine("--- server \"%s\" -> %s" % (server.host, server.quit()), short_flag=short_flag) ) except : pass
if file_size <= 0 : cli.printLine("--- file \"%s\" :: warning, non-positive size" % (file_path)) continue except Exception, err : cli.printLine("--- file \"%s\" :: warning, error while getsize: %s" % (file_path, str(err))) continue resources_dict[resource_type][file_name] = { "file_path" : file_path, "file_size" : file_size } count += 1 cli.printLine("--- local :: %d %s" % (count, resource_type)) cli.printLine() return resources_dict def removeResource(resource_name, resource_type) : cli.printLine("--- resource \"%s\" :: begin to remove..." % (resource_name), short_flag=True) file_path = os.path.join(const.SL_SHARES_DIR, resource_type, resource_name) try : if resource_type == "dicts" : os.remove(file_path) elif resource_type == "sounds" : shutil.rmtree(file_path) except Exception, err : cli.printLine("--- resource \"%s\" :: removal failure: %s" % (resource_name, str(err)), short_flag=( err.errno == errno.ENOENT )) if err.errno != errno.ENOENT :
"passwd": passwd, "file_path": file_path, "file_size": file_size, } if not resources_dict[resource_type].has_key(resource_name): resources_dict[resource_type][resource_name] = [item_dict] else: resources_dict[resource_type][resource_name].append(item_dict) count += 1 cli.printLine('--- server "%s" :: %d %s' % (host, count, resource_type)) ftp.closeServer(server) cli.printLine() return resources_dict def installResource(resource_name, resource_type, resources_dict): for resource in resources_dict[resource_type][resource_name]: local_file_name = "." + os.path.basename(resource["file_path"]) local_file_path = os.path.join(const.SL_SHARES_DIR, resource_type, local_file_name) try: server = ftp.connectServer(resource["host"], resource["port"], resource["user"], resource["passwd"], True) ftp.downloadFile(server, resource["file_path"], local_file_path) cli.printLine('--- file "%s" :: download complete' % (local_file_path), short_flag=True) except Exception, err: cli.printLine('--- file "%s" :: download error: %s' % (local_file_path, str(err)))