def run_tvnamer(with_files, with_flags = None, with_input = "", with_config = None, run_on_directory = False): """Runs tvnamer on list of file-names in with_files. with_files is a list of strings. with_flags is a list of command line arguments to pass to tvnamer. with_input is the sent to tvnamer's stdin with_config is a string containing the tvnamer to run tvnamer with. Returns a dict with stdout, stderr and a list of files created """ # Create dummy files (config and episodes) tvnpath = get_tvnamer_path() episodes_location = make_temp_dir() dummy_files = make_dummy_files(with_files, episodes_location) if with_config is not None: configfname = make_temp_config(with_config) conf_args = ['-c', configfname] else: conf_args = [] if with_flags is None: with_flags = [] if run_on_directory: files = [episodes_location] else: files = dummy_files # Construct command cmd = [sys.executable, tvnpath] + conf_args + with_flags + files p("Running command:") p(" ".join(cmd)) proc = Popen( cmd, stdout = PIPE, stderr = PIPE, stdin = PIPE) proc.stdin.write(with_input) stdout, stderr = proc.communicate() stdout, stderr = [unicodify(x) for x in (stdout, stderr)] created_files = [] for walkroot, walkdirs, walkfiles in os.walk(unicode(episodes_location)): curlist = [os.path.join(walkroot, name) for name in walkfiles] # Remove episodes_location from start of path curlist = [relpath(x, episodes_location) for x in curlist] created_files.extend(curlist) # Clean up dummy files and config clear_temp_dir(episodes_location) if with_config is not None: os.unlink(configfname) return { 'stdout': stdout, 'stderr': stderr, 'files': created_files, 'returncode': proc.returncode}
def run_tvnamer(with_files, with_flags = None, with_input = "", with_config = None, run_on_directory = False): """Runs tvnamer on list of file-names in with_files. with_files is a list of strings. with_flags is a list of command line arguments to pass to tvnamer. with_input is the sent to tvnamer's stdin with_config is a string containing the tvnamer to run tvnamer with. Returns a dict with stdout, stderr and a list of files created """ # Create dummy files (config and episodes) tvnpath = get_tvnamer_path() episodes_location = make_temp_dir() dummy_files = make_dummy_files(with_files, episodes_location) if with_config is not None: configfname = make_temp_config(with_config) conf_args = ['-c', configfname] else: conf_args = [] if with_flags is None: with_flags = [] if run_on_directory: files = [episodes_location] else: files = dummy_files # Construct command cmd = [sys.executable, tvnpath] + conf_args + with_flags + files p("Running command:") p(" ".join(cmd)) proc = Popen( cmd, stdout = PIPE, stderr = PIPE, stdin = PIPE) proc.stdin.write(with_input) stdout, stderr = proc.communicate() stdout, stderr = [unicodify(x) for x in (stdout, stderr)] created_files = [] for walkroot, walkdirs, walkfiles in os.walk(unicode(episodes_location)): curlist = [os.path.join(walkroot, name) for name in walkfiles] # Remove episodes_location from start of path curlist = [relpath(x, episodes_location) for x in curlist] created_files.extend(curlist) # Clean up dummy files and config clear_temp_dir(episodes_location) if with_config is not None: os.unlink(configfname) return { 'stdout': stdout, 'stderr': stderr, 'files': created_files}
def run_tvnamer(with_files, with_flags = None, with_input = "", with_config = None, run_on_directory = False): """Runs tvnamer on list of file-names in with_files. with_files is a list of strings. with_flags is a list of command line arguments to pass to tvnamer. with_input is the sent to tvnamer's stdin with_config is a string containing the tvnamer to run tvnamer with. Returns a dict with stdout, stderr and a list of files created """ # Create dummy files (config and episodes) tvnpath = get_tvnamer_path() episodes_location = make_temp_dir() # register cleanup function atexit.register(shutil.rmtree, episodes_location) dummy_files = make_dummy_files(with_files, episodes_location) if with_config is not None: # insert correct version into config with_config = with_config.replace("{", """{"__version__": "%s",\n""" % __version__, 1) configfname = make_temp_config(with_config) # register cleanup function atexit.register(os.unlink, configfname) conf_args = ['-c', configfname] else: conf_args = [] if with_flags is None: with_flags = [] if run_on_directory: files = [episodes_location] else: files = dummy_files # Construct command cmd = [sys.executable, tvnpath] + conf_args + with_flags + files p("Running command:") p(" ".join(cmd)) proc = subprocess.Popen( cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, # All stderr to stdout stdin = subprocess.PIPE) proc.stdin.write(with_input) output, _ = proc.communicate() output = unicodify(output) created_files = [] for walkroot, walkdirs, walkfiles in os.walk(unicode(episodes_location)): curlist = [os.path.join(walkroot, name) for name in walkfiles] # Remove episodes_location from start of path curlist = [relpath(x, episodes_location) for x in curlist] created_files.extend(curlist) return { 'output': output, 'files': created_files, 'returncode': proc.returncode}