def chmod(path, ch_mod, su=False, silent=False): """ Cambia los permisos de un archivo o carpeta en sistemas Linux y derivados @param path: ruta @type path: str @param ch_mod: permisos @type ch_mod: str @param su: super-user, con diferentes variantes según plataforma @type su: bool @rtype: str @return: File-Info de un archivo o carpeta """ path = encode(path) res = False error_cmd = True if KODI and xbmc.getCondVisibility("system.platform.windows"): if not silent: logger.info('Command ERROR: CHMOD no soportado en esta plataforma', force=True) else: try: import subprocess from platformcode import config if not su: command = ['chmod', ch_mod, path] if not silent: logger.info('Command: %s' % str(command), force=True) p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output_cmd, error_cmd = p.communicate() elif su and config.is_rooted(silent=True) == 'rooted': for subcmd in ['-c', '-0']: for cmdtype in [['chmod', ch_mod, path], ['chmod %s %s' % (ch_mod, path)]]: command = ['su', subcmd] + cmdtype output_cmd, error_cmd = config.su_command( command, silent=silent) if not error_cmd: break if not error_cmd: break else: raise if not silent and error_cmd: logger.error('Command ERROR: %s, %s' % (str(command), str(error_cmd))) except: if not silent: logger.error(traceback.format_exc()) # Pedir file_info del archivo o carpeta res = file_info(path, silent=silent) if not silent: logger.info('File-stat: %s' % str(res), force=True) return res
def remove(path, silent=False, vfs=True, su=False): """ Elimina un archivo, con alternativa de usar SU @param path: ruta del fichero a eliminar @type path: str @rtype: bool @return: devuelve False en caso de error """ path = encode(path) try: if xbmc_vfs and vfs: result = bool(xbmcvfs.delete(path)) # Si el borrado no ha funcionado y se especificado su=True, se intenta el comando RM vía SU del sistema from platformcode import config if not result and su and config.is_rooted(silent=True) == 'rooted': error_cmd = True for subcmd in ['-c', '-0']: for cmdtype in [['rm', path], ['rm %s' % path]]: command = ['su', subcmd] + cmdtype output_cmd, error_cmd = config.su_command( command, silent=silent) if not error_cmd: break if not error_cmd: result = True break else: logger.error('Sin PERMISOS ROOT: %s' % str(command)) result = False return result elif path.lower().startswith("smb://"): samba.remove(path) else: os.remove(path) except: logger.error("ERROR al eliminar el archivo: %s" % path) if not silent: logger.error(traceback.format_exc()) return False else: return True
def copy(path, dest, silent=False, vfs=True, ch_mod='', su=False): """ Copia un archivo @param path: ruta del fichero a copiar @type path: str @param dest: ruta donde copiar @type dest: str @param silent: se muestra o no el cuadro de dialogo @type silent: bool @rtype: bool @return: devuelve False en caso de error """ try: if xbmc_vfs and vfs: path = encode(path) dest = encode(dest) if not silent: logger.info("Copiando archivo %s a %s" % (path, dest), force=True) result = bool(xbmcvfs.copy(path, dest)) # Si la copia no ha funcionado y se ha especificado su=True, se intenta el comando CP vía SU del sistema from platformcode import config if not result and su and config.is_rooted(silent=True) == 'rooted': error_cmd = True for subcmd in ['-c', '-0']: for cmdtype in [['cp', path, dest], ['cp %s %s' % (path, dest)]]: command = ['su', subcmd] + cmdtype output_cmd, error_cmd = config.su_command( command, silent=silent) if not error_cmd: break if not error_cmd: result = True break else: logger.error('Sin PERMISOS ROOT: %s' % str(command)) result = False elif result: su = '' if result and ch_mod: result = chmod(dest, ch_mod, silent=silent, su=su) return result fo = file_open(path, "rb") fd = file_open(dest, "wb") if fo and fd: if not silent: dialogo = platformtools.dialog_progress("Copiando archivo", "") size = getsize(path) copiado = 0 while True: if not silent: dialogo.update(old_div(copiado * 100, size), basename(path)) buf = fo.read(1024 * 1024) if not buf: break if not silent and dialogo.iscanceled(): dialogo.close() return False fd.write(buf) copiado += len(buf) if not silent: dialogo.close() except: logger.error("ERROR al copiar el archivo: %s" % path) if not silent: logger.error(traceback.format_exc()) return False else: return True