def clean(root, dir=None, ignore_list=[]): # set dir paths dir_orig = os.getcwd() if not dir: dir = root # change dir, get contents, set counter os.chdir(dir) items = os.listdir() counter = 0 # process every item in current dir for item in items: # get absolute path and turn it into relative from root item_abspath = os.path.abspath(item) item_path = re.sub("^%s" % re.escape(root), '', item_abspath) # check noremoves if item_path not in ignore_list: # if dir, search recursively, remove dir if empty if os.path.isdir(item) and not os.path.islink(item): count, empty = clean(root, item_abspath, ignore_list) counter += count if empty: remove(item_abspath, item_path, isdir=True) # else, remove item elif remove(item_abspath, item_path): counter += 1 else: write('ignored %s' % item_path) else: write('ignored %s' % item_path) # get empty-state empty = len(os.listdir()) == 0 # change dir back os.chdir(dir_orig) # return counter and empty-state return counter, empty
def parse_file(conf_path=CONF_PATH): if ARG.CONF: conf_path = ARG.CONF # read configuration file try: conf_raw = open(conf_path).read() except IOError: write("failed: file '%s' doesn't exist" % conf_path, force=True) return None # parse text, pass arguments to Berthes return Berthes(*parse(conf_raw))
def copy(src, trg, src_short=None, trg_short=None): if not src_short: src_short = src if not trg_short: trg_short = trg # ask first (if set) if ARG.ASKCOPY and not confirm('Copy '+src+' to '+trg_short+'?'): write('ignored %s' % src_short) return 0 # check if target dir tree exists trg_tree = '/'.join(trg.split('/')[:-1]) if not os.path.exists(trg_tree) and not create_dir(trg_tree): return 0 # get items matching pattern, set counter src_items = glob.glob(src) counter = 0 # copy each matched item for src_item in src_items: counter += copy_item(src_item, trg, trg_short=trg_short) return counter
def run(self): try: for root in self.root: # clean root directory (if set) if ARG.CLEAN: write(':: Cleaning files from %s' % root) rm_count, empty = clean(root, ignore_list=self.noremove) write('%i files removed' % rm_count) # copy to root directory (if set) if ARG.COPY: write(':: Copying files to %s' % root) cp_count = self.copy(root) write('%i files copied' % cp_count) except KeyboardInterrupt: print('') finally: final_msg = ':: Done' if ARG.DRYRUN: final_msg += ' (note: this was just a DRYRUN)' write(final_msg)
def remove(path, path_short=None, isdir=False): # set shortened path if not given if not path_short: path_short = path # remove file, but ask first (if set) if not ARG.ASKREMOVE or confirm('Remove '+path_short+'?'): # create arg list args = ['rm'] if isdir: args.append('-r') args.append(path) # combine into command, execute cmd = ' '.join(args) retcode, output = file_op(cmd) # check return code if retcode == 0: write('removed '+path_short) else: write('failed: '+cmd) write(output) return (retcode == 0) return False
def copy_item(src, trg, src_short=None, trg_short=None): if not src_short: src_short = src if not trg_short: trg_short = trg # create arg list args = ['cp', '-v'] # add recursive option if source is dir if os.path.isdir(src): args.append('-r') # add source and target args args.extend([src, trg]) # finally, copy cmd = ' '.join(args) retcode, output = file_op(cmd) # check return code if retcode == 0: write('copied '+src+' to '+trg_short) # count all copied files files = len(output.split('\n')) return files else: write('failed: '+cmd) write(output) return 0