def do_it(): if HC.PLATFORM_WINDOWS: os.startfile(path) else: if HC.PLATFORM_OSX: cmd = 'open' elif HC.PLATFORM_LINUX: cmd = 'xdg-open' # setsid call un-childs this new process process = subprocess.Popen( [cmd, path], preexec_fn=os.setsid, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) process.wait() process.communicate()
def GetExternalIP(): if 'external_host' in HC.options and HC.options[ 'external_host'] is not None: return HC.options['external_host'] if HydrusData.TimeHasPassed(EXTERNAL_IP['time'] + (3600 * 24)): cmd = '"' + upnpc_path + '" -l' p = subprocess.Popen( shlex.split(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) HydrusData.WaitForProcessToFinish(p, 30) (output, error) = p.communicate() if error is not None and len(error) > 0: raise Exception('Problem while trying to fetch External IP:' + os.linesep * 2 + HydrusData.ToUnicode(error)) else: try: lines = HydrusData.SplitByLinesep(output) i = lines.index( ' i protocol exPort->inAddr:inPort description remoteHost leaseTime' ) '''ExternalIPAddress = ip''' (gumpf, external_ip_address) = lines[i - 1].split(' = ') except ValueError: raise Exception('Could not parse external IP!') if external_ip_address == '0.0.0.0': raise Exception( 'Your UPnP device returned your external IP as 0.0.0.0! Try rebooting it, or overwrite it in options!' ) EXTERNAL_IP['ip'] = external_ip_address EXTERNAL_IP['time'] = HydrusData.GetNow() return EXTERNAL_IP['ip']
def GetFFMPEGVersion(): # open the file in a pipe, provoke an error, read output cmd = [FFMPEG_PATH, '-version'] try: proc = subprocess.Popen( cmd, bufsize=10**5, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) except Exception as e: if not os.path.exists(FFMPEG_PATH): return 'no ffmpeg found' else: HydrusData.ShowException(e) return 'unable to execute ffmpeg' infos = proc.stdout.read().decode('utf8') proc.terminate() del proc lines = infos.splitlines() if len(lines) > 0: # typically 'ffmpeg version [VERSION] Copyright ... top_line = lines[0] if top_line.startswith('ffmpeg version '): top_line = top_line.replace('ffmpeg version ', '') if ' ' in top_line: version_string = top_line.split(' ')[0] return version_string return 'unknown'
def AddUPnPMapping(internal_client, internal_port, external_port, protocol, description, duration=3600): cmd = '"' + upnpc_path + '" -e "' + description + '" -a ' + internal_client + ' ' + str( internal_port) + ' ' + str(external_port) + ' ' + protocol + ' ' + str( duration) p = subprocess.Popen( shlex.split(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) HydrusData.WaitForProcessToFinish(p, 30) (output, error) = p.communicate() if 'x.x.x.x:' + str( external_port ) + ' TCP is redirected to internal ' + internal_client + ':' + str( internal_port) in output: raise HydrusExceptions.FirewallException( 'The UPnP mapping of ' + internal_client + ':' + internal_port + '->external:' + external_port + ' already exists as a port forward. If this UPnP mapping is automatic, please disable it.' ) if output is not None and 'failed with code' in output: if 'UnknownError' in output: raise HydrusExceptions.FirewallException( 'Problem while trying to add UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode(output)) else: raise Exception('Problem while trying to add UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode(output)) if error is not None and len(error) > 0: raise Exception('Problem while trying to add UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode(error))
def RemoveUPnPMapping(external_port, protocol): cmd = '"' + upnpc_path + '" -d ' + str(external_port) + ' ' + protocol p = subprocess.Popen( shlex.split(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) HydrusData.WaitForProcessToFinish(p, 30) (output, error) = p.communicate() if error is not None and len(error) > 0: raise Exception('Problem while trying to remove UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode(error))
def RenderPageToFile(path, temp_path, page_index): cmd = '"' + SWFRENDER_PATH + '" "' + path + '" -o "' + temp_path + '" -p ' + str( page_index) timeout = HydrusData.GetNow() + 60 p = subprocess.Popen( shlex.split(cmd), startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) while p.poll() is None: if HydrusData.TimeHasPassed(timeout): p.terminate() raise Exception('Could not render the swf page within 60 seconds!') time.sleep(0.5) p.communicate()
def do_it( launch_path ): if HC.PLATFORM_WINDOWS and launch_path is None: os.startfile( path ) else: if launch_path is None: launch_path = GetDefaultLaunchPath() cmd = launch_path.replace( '%path%', path ) if HC.PLATFORM_WINDOWS: preexec_fn = None else: # setsid call un-childs this new process preexec_fn = os.setsid cmd = shlex.split( cmd ) if HG.callto_report_mode: message = 'Attempting to launch ' + path + ' using command ' + repr( cmd ) + '.' try: process = subprocess.Popen( cmd, preexec_fn = preexec_fn, startupinfo = HydrusData.GetHideTerminalSubprocessStartupInfo() ) process.wait() ( stdout, stderr ) = process.communicate() if HG.callto_report_mode: HydrusData.ShowText( message ) if stdout is None and stderr is None: HydrusData.ShowText( 'No stdout or stderr came back.' ) if stdout is not None: HydrusData.ShowText( 'stdout: ' + repr( stdout ) ) if stderr is not None: HydrusData.ShowText( 'stderr: ' + repr( stderr ) ) except Exception as e: HydrusData.ShowText( 'Could not launch a file! Command used was:' + os.linesep + HydrusData.ToUnicode( cmd ) ) HydrusData.ShowException( e )
def GetUPnPMappings(): external_ip_address = GetExternalIP() cmd = '"' + upnpc_path + '" -l' p = subprocess.Popen( shlex.split(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) HydrusData.WaitForProcessToFinish(p, 30) (output, error) = p.communicate() if error is not None and len(error) > 0: raise Exception('Problem while trying to fetch UPnP mappings:' + os.linesep * 2 + HydrusData.ToUnicode(error)) else: try: lines = HydrusData.SplitByLinesep(output) i = lines.index( ' i protocol exPort->inAddr:inPort description remoteHost leaseTime' ) data_lines = [] i += 1 while i < len(lines): if not lines[i][0] in (' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'): break data_lines.append(lines[i]) i += 1 processed_data = [] for line in data_lines: ''' 0 UDP 65533->192.168.0.197:65533 'Skype UDP at 192.168.0.197:65533 (2665)' '' 0''' while ' ' in line: line = line.replace(' ', ' ') if line.startswith(' '): (empty, number, protocol, mapping_data, rest_of_line) = line.split(' ', 4) else: (number, protocol, mapping_data, rest_of_line) = line.split(' ', 3) (external_port, rest_of_mapping_data) = mapping_data.split('->') external_port = int(external_port) (internal_client, internal_port) = rest_of_mapping_data.split(':') internal_port = int(internal_port) (empty, description, space, remote_host, rest_of_line) = rest_of_line.split('\'', 4) lease_time = int(rest_of_line[1:]) processed_data.append( (description, internal_client, internal_port, external_ip_address, external_port, protocol, lease_time)) return processed_data except Exception as e: HydrusData.Print(traceback.format_exc()) raise Exception('Problem while trying to parse UPnP mappings:' + os.linesep * 2 + HydrusData.ToUnicode(e))
def GetFFMPEGInfoLines(path, count_frames_manually=False): # open the file in a pipe, provoke an error, read output try: path.encode( 'ascii' ) # throwing unicode at the console is a mess best left for Python 3 except UnicodeEncodeError: (os_file_handle, temp_path) = HydrusPaths.GetTempPath() with open(path, 'rb') as source: with open(temp_path, 'wb') as dest: HydrusPaths.CopyFileLikeToFileLike(source, dest) try: return GetFFMPEGInfoLines(temp_path, count_frames_manually) finally: HydrusPaths.CleanUpTempPath(os_file_handle, temp_path) cmd = [FFMPEG_PATH, "-i", path] if count_frames_manually: if HC.PLATFORM_WINDOWS: cmd += ["-f", "null", "NUL"] else: cmd += ["-f", "null", "/dev/null"] try: proc = subprocess.Popen( cmd, bufsize=10**5, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) except: if not os.path.exists(FFMPEG_PATH): raise Exception('FFMPEG was not found!') else: raise raw_info = proc.stderr.read() try: info = raw_info.decode('utf8') except UnicodeDecodeError: info = raw_info proc.wait() proc.communicate() del proc lines = info.splitlines() try: CheckFFMPEGError(lines) except: HydrusData.Print('FFMPEG had problem with file: ' + path) raise return lines
def initialize(self, start_index=0): self.close() if self._mime in (HC.IMAGE_APNG, HC.IMAGE_GIF): do_ss = False ss = 0 self.pos = 0 skip_frames = start_index else: if start_index == 0: do_ss = False else: do_ss = True ss = float(start_index) / self.fps self.pos = start_index skip_frames = 0 (w, h) = self._target_resolution cmd = [FFMPEG_PATH] if do_ss: cmd.extend(['-ss', "%.03f" % ss]) cmd.extend([ '-i', self._path, '-loglevel', 'quiet', '-f', 'image2pipe', "-pix_fmt", self.pix_fmt, "-s", str(w) + 'x' + str(h), '-vsync', '0', '-vcodec', 'rawvideo', '-' ]) try: self.process = subprocess.Popen( cmd, bufsize=self.bufsize, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=HydrusData.GetHideTerminalSubprocessStartupInfo()) except: if not os.path.exists(FFMPEG_PATH): raise Exception('FFMPEG was not found!') else: raise if skip_frames > 0: self.skip_frames(skip_frames)
def do_it( launch_path ): if HC.PLATFORM_WINDOWS and launch_path is None: os.startfile( path ) else: if launch_path is None: launch_path = GetDefaultLaunchPath() cmd = launch_path.replace( '%path%', path ) if HC.PLATFORM_WINDOWS: preexec_fn = None else: # setsid call un-childs this new process preexec_fn = os.setsid cmd = shlex.split( cmd ) try: process = subprocess.Popen( cmd, preexec_fn = preexec_fn, startupinfo = HydrusData.GetHideTerminalSubprocessStartupInfo() ) process.wait() process.communicate() except Exception as e: HydrusData.ShowText( 'Could not launch a file! Command used was:' + os.linesep + HydrusData.ToUnicode( cmd ) ) HydrusData.ShowException( e )