def copy_to(filepath, to_path): filepath = encoding_path(filepath) to_path = encoding_path(to_path) try: log.debug('copying %s to %s' % (filepath, to_path)) sh.copy2(filepath, to_path) except IOError, e: log.error(e) raise e
def move_to(filepath, to_path): filepath = encoding_path(filepath) to_path = encoding_path(to_path) if os.path.isfile(filepath): try: log.debug('moving from %s to %s' % (filepath, to_path)) if not os.path.exists(to_path): make_dir(to_path) sh.copy2(filepath, to_path) except Exception, e: log.warning(e) raise e
def size(filepath): filepath = encoding_path(filepath) if os.access(filepath, os.R_OK) and os.path.isfile(filepath): try: size = os.path.getsize(filepath) return size except Exception, e: log.warning(e)
def make_dir(path): path = encoding_path(path) if not os.path.exists(path): try: os.makedirs(path) log.debug('new sudirectory: %s' % path) except OSError, e: log.error(e)
def remove(filepath): filepath = encoding_path(filepath) if os.path.exists(filepath): if os.path.isfile(filepath): log.debug('removing file %s' % filepath) try: os.remove(filepath) except IOError, e: log.warning(e) raise e else: log.warning('remove file, %s is not a file' % filepath)
def __init__(self, file_path, check=False): if not file_path: raise IOError('file_path: %s' % file_path) log.debug('extracting metadata from file: %s' % file_path) self.path = os.path.abspath(encoding_path(file_path)) if check: if not os.path.isfile(self.path): raise IOError('It is not a file or does not exist') if not os.access(self.path, os.R_OK): raise OSError('The file is not readable or missing') try: self.basename = os.path.basename(self.path) name = os.path.splitext(self.basename)[0] self.extension = utils.extract_ext(self.basename) self.name = name self.dirname = os.path.dirname(self.path) except Exception, e: log.error(e)
def parser_opts(opts): assert isinstance(opts, dict) opts = parser_opts_ocr(opts) opts['--from'] = encoding_path(opts.get('--from', '')) if os.path.isdir(opts['--from']): opts['--from'] = os.path.abspath(opts['--from']) opts['<source>'] = union(opts.get('<source>', [])) opts['<file>'] = union(opts.get('<file>', [])) opts['<path>'] = union(opts.get('<path>', [])) assert isinstance(opts['<source>'], list) assert isinstance(opts['<file>'], list) assert isinstance(opts['<file>'], list) opts['<source>'] = fmap(standardpath, opts['<source>']) opts['<path>'] = fmap(standardpath, opts['<path>']) opts['<file>'] = fmap(standardpath, opts['<file>']) for path in opts['<source>']: if os.path.isdir(path): opts['<path>'].append(path) elif os.path.isfile(path): opts['<file>'].append(path) else: if os.path.isdir(opts['--from']): path = os.path.join(opts['--from'], path) if os.path.isfile(path): opts['<file>'].append(path) opts['<file>'] = union(opts['<file>']) opts['<path>'] = union(opts['<path>']) opts['<file>'] = ffilter(readable, opts['<file>']) opts['<path>'] = ffilter(readable, opts['<path>']) opts['--file'] = (len(opts['<file>']) > 0) opts['--path'] = (len(opts['<path>']) > 0) opts['--depth'] = int(opts['--depth']) return opts.copy()
def __repr__(self): return encoding_path(self.path)
def extract_ext(filepath): filepath = encoding_path(filepath) ext = os.path.splitext(filepath)[1].lower() return ext[1:] if ext.startswith('.') else ext