def __organize(self, path): try: files = os.listdir(path) if len(files) == 0: print('[W] %s' % _('Directory %s is empty...') % path) except OSError: print('[W] %s' % _('Cannot list directory %s...') % path) return covers = [] lastTag = None for f in files: if os.path.islink(path + f) and not self.__follow: self.v(_('Skipping link %s...') % (path + f)) continue if os.path.isdir(path + f) and self.__recursive: self.__organize(path + f + utils.DIR_SEPARATOR) continue if f[-4:].lower() == '.mp3': lastTag = utils.getTag(path + f) if utils.moveTrack(path + f, lastTag, self.__target, self.__scheme, self.__copy): self.__numOk += 1 else: self.__numSkipped += 1 if self.__recognizeCovers: if f[f.rfind('.'):].lower() in ('.jpg', '.gif', '.png', '.bmp', '.jpeg'): covers.append(path + f) if lastTag != None and len(covers) != 0: outputDir = self.__target + self.__scheme.format(**lastTag) utils.moveCovers(covers, outputDir, self.__copy) if not self.__copy and self.__delete and path != self.__path: try: utils.verbose(_('Removing %s') % path) os.rmdir(path) except Exception as err: utils.verbose(_('Cannot remove %s') % path) try: if self.__force: utils.v(_('Force removing %s') % path) os.rmdirs(path) else: raise Exception() except: print('[W] %s' % _('Unable to remove directory %s...') % path)
def clean_up(): """ Perform any script cleanup that is required here. """ nzbid = nzb.get_nzb_id() # Remove the state file. update_filepath = get_update_filepath(nzbid) if os.path.isfile(update_filepath): os.remove(update_filepath) if SCRIPT_STATE == "Debug": # Check if the script path has no other files and delete it. # NOTE: We can't blindly remove the directory because there might be # other instances waiting for their retries and need the state. tempdir = nzb.get_script_tempfolder(SCRIPT_NAME) if len(os.path.listdir(tempdir)) == 0: os.rmdirs(tempdir)
def clean_up(): """ Perform any script cleanup that is required here. """ nzbid = nzb.get_nzb_id() # Remove the state file. update_filepath = get_update_filepath(nzbid) if os.path.isfile(update_filepath): os.remove(update_filepath) if SCRIPT_STATE == 'Debug': # Check if the script path has no other files and delete it. # NOTE: We can't blindly remove the directory because there might be # other instances waiting for their retries and need the state. tempdir = nzb.get_script_tempfolder(SCRIPT_NAME) if len(os.path.listdir(tempdir)) == 0: os.rmdirs(tempdir)
def __organize(self, path): try: files = os.listdir(path); if len(files) == 0: print("[W] Directory %s is empty..." % path); except OSError: print('[W] Cannot list directory %s...' % path); return; covers = []; lastTag = None; for f in files: if os.path.islink(path + f) and not self.__follow: self.v('Skipping link %s...' % (path + f)); continue; if os.path.isdir(path + f) and self.__recursive: self.__organize(path + f + utils.DIR_SEPARATOR); continue; if f[-4:].lower() == '.mp3': lastTag = utils.getTag(path + f); if utils.moveTrack(path + f, lastTag, self.__target, self.__scheme, self.__copy): self.__numOk += 1; else: self.__numSkipped += 1; if self.__recognizeCovers: if f[f.rfind('.'):].lower() in ('.jpg', '.gif', '.png', '.bmp', '.jpeg'): covers.append(path + f); if lastTag != None and len(covers) != 0: outputDir = self.__target + self.__scheme.format(**lastTag); utils.moveCovers(covers, outputDir, self.__copy) if not self.__copy and self.__delete and path != self.__path: try: utils.verbose('Removing %s' % path); os.rmdir(path); except Exception as err: utils.verbose('I can\'t remove %s' % path); try: if self.__force: utils.v('Force removing %s' % path); os.rmdirs(path); else: raise Exception(); except: print('[W] Unable to remove directory %s...' % path);
def delete( *arguments ): end_of_switch = False force_switch = False pathnames = [] for arg in arguments: if arg.strip() == "-force" and not end_of_switch: force_switch = True elif arg.strip() == "--" and not end_of_switch: end_of_switch = True else: pathnames.append(arg) for filename in pathnames: #dir is deleted only if force is specified. try: if is_dir(filename): if force_switch: os.rmdirs(filename) else: os.remove(filename) except: pass
def delete(*arguments): end_of_switch = False force_switch = False pathnames = [] for arg in arguments: if arg.strip() == "-force" and not end_of_switch: force_switch = True elif arg.strip() == "--" and not end_of_switch: end_of_switch = True else: pathnames.append(arg) for filename in pathnames: #dir is deleted only if force is specified. try: if is_dir(filename): if force_switch: os.rmdirs(filename) else: os.remove(filename) except: pass
def execute(self): file_path = self.kwargs.get("path") site = self.kwargs.get("site") project_code = self.kwargs.get("project_code") base_dir = self.kwargs.get("base_dir") search_type = self.kwargs.get("search_type") process = self.kwargs.get("process") watch_script_path = self.kwargs.get("script_path") if not process: process = "publish" basename = os.path.basename(file_path) context = self.kwargs.get("context") if not context: context = '%s/%s' % (process, basename) # find the relative_dir and relative_path relative_path = file_path.replace("%s/" % base_dir, "") relative_dir = os.path.dirname(relative_path) file_name = os.path.basename(file_path) log_path = '%s/TACTIC_log.txt' % (base_dir) self.create_checkin_log() # Define asset type of the file asset_type = self.get_asset_type(file_path) description = "drop folder check-in of %s" % file_name from client.tactic_client_lib import TacticServerStub server = TacticServerStub.get(protocol='local') server.set_project(project_code) transaction = Transaction.get(create=True) server.start(title='Check-in of media', description='Check-in of media') server_return_value = {} try: filters = [ ['name', '=', file_name], #[ 'relative_dir', '=', relative_dir ] ] sobj = server.query(search_type, filters=filters, single=True) if not sobj: # create sobject if it does not yet exist sobj = SearchType.create(search_type) if SearchType.column_exists(search_type, "name"): sobj.set_value("name", basename) if SearchType.column_exists(search_type, "media_type"): sobj.set_value("media_type", asset_type) if SearchType.column_exists(search_type, "relative_dir"): sobj.set_value("relative_dir", relative_dir) if SearchType.column_exists(search_type, "keywords"): relative_path = relative_path keywords = Common.extract_keywords_from_path(relative_path) keywords = " ".join(keywords) sobj.set_value("keywords", keywords) sobj.commit() search_key = sobj.get_search_key() else: search_key = sobj.get("__search_key__") #task = server.create_task(sobj.get('__search_key__'),process='publish') #server.update(task, {'status': 'New'}) """ #TEST: simulate different check-in duration from random import randint sec = randint(1, 5) print "checking in for ", sec, "sec" server.eval("@SOBJECT(sthpw/login)") import shutil dir_name,base_name = os.path.split(file_path) dest_dir = 'C:/ProgramData/Southpaw/watch_temp' if not os.path.exists(dest_dir): os.makedirs(dest_dir) shutil.move(file_path, '%s/%s'%(dest_dir, base_name)) time.sleep(sec) # move back the file in a few seconds shutil.move('%s/%s'%(dest_dir, base_name), file_path) """ server_return_value = server.simple_checkin( search_key, context, file_path, description=description, mode='move') if watch_script_path: cmd = PythonCmd(script_path=watch_script_path, search_type=search_type, drop_path=file_path, search_key=search_key) cmd.execute() except Exception as e: print "Error occurred", e error_message = str(e) import traceback tb = sys.exc_info()[2] stacktrace = traceback.format_tb(tb) stacktrace_str = "".join(stacktrace) print "-" * 50 print stacktrace_str version_num = 'Error:' system_time = strftime("%Y/%m/%d %H:%M", gmtime()) pre_log=file_name+(50-len(file_name))*' '+system_time+(33-len(system_time))*' '+version_num+(15-len(version_num))*' ' +error_message+'\n'\ + stacktrace_str + '\n' + watch_script_path # Write data into TACTIC_log file under /tmp/drop f = open(log_path, 'a') f.write(pre_log) f.close() #server.abort() transaction.rollback() raise else: transaction.commit() #server.finish() if server_return_value: # Create the TACTIC_log file to record every check-in. # Search for all required data checkin_time = server_return_value.get('timestamp') version_nu = server_return_value.get('version') version_num = str(version_nu) try: value = parser.parse(checkin_time) value = value.strftime("%Y/%m/%d %H:%M") except: value = checkin_time pre_log = file_name + (50 - len(file_name)) * ' ' + value + ( 33 - len(value)) * ' ' + version_num + ( 15 - len(version_num)) * ' ' + 'ok\n' # Write data into TACTIC_log file under /tmp/drop f = open(log_path, 'a') f.write(pre_log) f.close() # Invoke Trigger:called_triggers from pyasm.command import Trigger Trigger.call_all_triggers() # Delete the source file after check-in step. print "File handled." if os.path.exists(file_path): if os.path.isdir(file_path): os.rmdirs(file_path) else: os.unlink(file_path) print "Source file [%s] deleted: " % file_name
def execute(self): file_path = self.kwargs.get("path") site = self.kwargs.get("site") project_code = self.kwargs.get("project_code") base_dir = self.kwargs.get("base_dir") search_type = self.kwargs.get("search_type") process = self.kwargs.get("process") watch_script_path = self.kwargs.get("script_path") if not process: process = "publish" basename = os.path.basename(file_path) context = self.kwargs.get("context") if not context: context = '%s/%s' % (process, basename) # find the relative_dir and relative_path relative_path = file_path.replace("%s/" % base_dir, "") relative_dir = os.path.dirname(relative_path) file_name = os.path.basename(file_path) log_path = '%s/TACTIC_log.txt' %(base_dir) self.create_checkin_log() # Define asset type of the file asset_type = self.get_asset_type(file_path) description = "drop folder check-in of %s" %file_name from client.tactic_client_lib import TacticServerStub server = TacticServerStub.get(protocol='local') server.set_project(project_code) transaction = Transaction.get(create=True) server.start(title='Check-in of media', description='Check-in of media') server_return_value = {} try: filters = [ [ 'name', '=', file_name ], #[ 'relative_dir', '=', relative_dir ] ] sobj = server.query(search_type, filters=filters, single=True) if not sobj: # create sobject if it does not yet exist sobj = SearchType.create(search_type) if SearchType.column_exists(search_type, "name"): sobj.set_value("name", basename) if SearchType.column_exists(search_type, "media_type"): sobj.set_value("media_type", asset_type) if SearchType.column_exists(search_type, "relative_dir"): sobj.set_value("relative_dir", relative_dir) if SearchType.column_exists(search_type, "keywords"): relative_path = relative_path keywords = Common.extract_keywords_from_path(relative_path) keywords = " ".join( keywords ) sobj.set_value("keywords", keywords) sobj.commit() search_key = sobj.get_search_key() else: search_key = sobj.get("__search_key__") #task = server.create_task(sobj.get('__search_key__'),process='publish') #server.update(task, {'status': 'New'}) """ #TEST: simulate different check-in duration from random import randint sec = randint(1, 5) print "checking in for ", sec, "sec" server.eval("@SOBJECT(sthpw/login)") import shutil dir_name,base_name = os.path.split(file_path) dest_dir = 'C:/ProgramData/Southpaw/watch_temp' if not os.path.exists(dest_dir): os.makedirs(dest_dir) shutil.move(file_path, '%s/%s'%(dest_dir, base_name)) time.sleep(sec) # move back the file in a few seconds shutil.move('%s/%s'%(dest_dir, base_name), file_path) """ server_return_value = server.simple_checkin(search_key, context, file_path, description=description, mode='move') if watch_script_path: cmd = PythonCmd(script_path=watch_script_path,search_type=search_type,drop_path=file_path,search_key=search_key) cmd.execute() except Exception as e: print "Error occurred", e error_message=str(e) import traceback tb = sys.exc_info()[2] stacktrace = traceback.format_tb(tb) stacktrace_str = "".join(stacktrace) print "-"*50 print stacktrace_str version_num='Error:' system_time=strftime("%Y/%m/%d %H:%M", gmtime()) pre_log=file_name+(50-len(file_name))*' '+system_time+(33-len(system_time))*' '+version_num+(15-len(version_num))*' ' +error_message+'\n'\ + stacktrace_str + '\n' + watch_script_path # Write data into TACTIC_log file under /tmp/drop f = open(log_path, 'a') f.write(pre_log) f.close() #server.abort() transaction.rollback() raise else: transaction.commit() #server.finish() if server_return_value: # Create the TACTIC_log file to record every check-in. # Search for all required data checkin_time=server_return_value.get('timestamp') version_nu=server_return_value.get('version') version_num=str(version_nu) try: value = parser.parse(checkin_time) value = value.strftime("%Y/%m/%d %H:%M") except: value = checkin_time pre_log=file_name+(50-len(file_name))*' '+value+(33-len(value))*' '+version_num+(15-len(version_num))*' ' +'ok\n' # Write data into TACTIC_log file under /tmp/drop f = open(log_path, 'a') f.write(pre_log) f.close() # Invoke Trigger:called_triggers from pyasm.command import Trigger Trigger.call_all_triggers() # Delete the source file after check-in step. print "File handled." if os.path.exists(file_path): if os.path.isdir(file_path): os.rmdirs(file_path) else: os.unlink(file_path) print "Source file [%s] deleted: " %file_name
import os os.rmdirs('/home/ratul/b') #file path
import os print(os.getcwd()) #print(os.mkdir('python')) #print(os.mkdirs('python\python1\python2\python3')) print(os.rmdir('python')) print(os.rmdirs('python\python1\python2\python3')) print(os.chdir()) print(os.rename('python', 'python folder'))
def rm_dir(dirpath): if os.path.exists(dirpath): try: os.rmdirs(dirpath) except OSError, exc: raise
def rmTrees(topPath): for root,dirs,files in os.walk(topPath,topdown=False): for name in files: os.remove(os.path.join(root,name)) for name in dirs: os.rmdirs(os.path.join(root,name))
################################ # Load module AT = AnalysisTrajectories(out_dirs, dir_name=dir_head, analysis_dir=analys_dir, nproc=nproc) # Create dir gsms_A_dir = analys_dir + 'GSMs_cl-1/sample_A/' gsms_B_dir = analys_dir + 'GSMs_cl-1/sample_B/' check_dir = analys_dir + 'GSMs_cl-1' print check_dir if os.path.isdir(gsms_A_dir): print gsms_A_dir exit() os.rmdirs() AT.create_gsms_dir(gsms_A_dir) AT.create_gsms_dir(gsms_B_dir) HA1 = AT.get_models_to_extract(analys_dir + '/selected_models_A_cluster3_detailed.csv') HB1 = AT.get_models_to_extract(analys_dir + '/selected_models_B_cluster3_detailed.csv') AT.do_extract_models(HA1, 'h1', gsms_A_dir) AT.do_extract_models(HB1, 'h2', gsms_B_dir) exit()