def _subprocess_check(self, subprocess): subprocess.wait() if subprocess.returncode in [0, 2, 3]: pass else: raise RuntimeError("Process returned: %s" % (subprocess.communicate()[1].decode("utf-8")))
def wait(self): """Wait for the child process to exit, or the timeout to expire, whichever comes first. This function returns the same thing as Python's subprocess.wait(). That is, this function returns the exit status of the child process; if the return value is -N, it indicates that the child was killed by signal N. """ try: self.subprocess.wait() except OSError as e: print("timeout.py exception (timedout?): {}".format(e)) # If the child times out, the wait() syscall can get # interrupted by the SIGALRM. We should then only need to # wait() once more for the child to actually exit. if e.errno == errno.EINTR: self.subprocess.wait() else: raise e pass self.cancelTimeout() assert self.subprocess.poll() is not None val = self.subprocess.poll() #print("timeout.py return value: {}".format(val)) return val
def stop_etcd(subprocess, data_dir): if subprocess and subprocess.poll() is None: log.info(f"stopping etcd server") subprocess.terminate() subprocess.wait() log.info(f"deleting etcd data dir: {data_dir}") shutil.rmtree(data_dir, ignore_errors=True)
def pipe(p): scm = p.split("|") seg1 = scm[0].split(" ") seg1.remove("") seg2 = scm[1].split(" ") seg2.remove("") ps = sp.Popen(seg1, stdout=sp.PIPE).communicate() output = sp.Popen(seg2, stdin=ps.stdout) sp.wait()
def _subprocess_check(self, subprocess): subprocess.wait() no_error_codes = [ ExitCodes.OK, ExitCodes.NO_TEMPLATE, ExitCodes.UNKNOWN_TARGET ] if subprocess.returncode in no_error_codes: pass else: comm = subprocess.communicate() raise RuntimeError( "Process returned: %s" % (comm[0].decode("utf-8") + comm[1].decode("utf-8")))
def end_process(self, subprocess, signal=signal.SIGINT): print('ending process pid', subprocess.pid) ppid = subprocess.pid try: process = psutil.Process(ppid) except psutil.NoSuchProcess: return pids = process.children(recursive=True) for pid in pids: os.kill(pid.pid, signal) subprocess.terminate() try: subprocess.wait() except: subprocess.kill()
def daemonize(self, command, argstr, stdout=None, stderr=None, pidfilename=None, genpidfile=True, pidincrement=0, starttime=None, extra_paths=[]): commandstr = self._build_commandstr(command, argstr, extra_paths) # print the commandstr and return on a dryrun if self._trialargs['dryrun']: print(commandstr) sys.stdout.flush() return # 1. call self.stop(pidfilename) self.stop(pidfilename) # run the command pid, subprocess = etce.utils.daemonize_command(commandstr, stdout, stderr, starttime) # return on parent if pid > 0: return # 2. if genpidfile is True, and pidfilename is None, # generate the pidfilename if genpidfile and pidfilename is None: pidfilename = self._default_pidfilename # 3. write the pid to pidfilename if genpidfile: with open(pidfilename, 'w') as pidfile: pidfile.write(str(subprocess.pid + pidincrement)) # 4. wait on subprocess subprocess.wait() # 5. exit, do not return, because returning # will cause any subsequent wrappers in this # step to be rerun sys.exit(0)
def main_activity(): threading.Timer(20.0, main_activity).start() # called every 20 seconds r = requests.get( "https://fancam.com/ssl/install/rig/rigGetStatusTiler.php?pss=yoqwryhg28!244G6534!&ven=tiler" ) status = r.content if status == "30": print("status : Rig IDLE") if status == "90": print("status : Get AWS Directory copy to local DIR") awsdir = requests.get( "https://fancam.com/ssl/install/rig/tileGetAwsDir.php?pss=yoqwryhg28!244G6534!" ) subprocess.call([ 'aws s3 cp --recursive s3://in3sixty-source/' + awsdir + ' ~/from_source/' ]) subprocess.wait() r = requests.get( "https://fancam.com/ssl/install/rig/rigSetStatus.php?pss=yoqwryhg28!244G6534!&ven=tiler&sta=92" ) if status == "91": print("status : Get Directory") subprocess.call(['~/apg/', '~/msg.pno']) subprocess.wait() r = requests.get( "https://fancam.com/ssl/install/rig/rigSetStatus.php?pss=yoqwryhg28!244G6534!&ven=tiler&sta=92" ) if status == "92": print("status : Make Flat tile") subprocess.call(['~/apg/', '~/msg.pno']) subprocess.wait() r = requests.get( "https://fancam.com/ssl/install/rig/rigSetStatus.php?pss=yoqwryhg28!244G6534!&ven=tiler&sta=93" )
def wait(self): """Wait for the child process to exit, or the timeout to expire, whichever comes first. This function returns the same thing as Python's subprocess.wait(). That is, this function returns the exit status of the child process; if the return value is -N, it indicates that the child was killed by signal N. """ try: self.subprocess.wait() except OSError, e: # If the child times out, the wait() syscall can get # interrupted by the SIGALRM. We should then only need to # wait() once more for the child to actually exit. if e.errno == errno.EINTR: self.subprocess.wait() else: raise e pass
def client_io(storage_driver, selection_constructor, subprocess): write_framed = partial( common.write_framed, '!I', subprocess.stdin ) read_framed = partial( common.read_framed, '!I', subprocess.stdout ) # load graph subprocess.stdin.write(common.magic_number) graph = wire_pb2.Graph() graph.ParseFromString(read_framed()) # select parent and make edge (parent, current) local_nodes = storage_driver.get_local_nodes() policy = selection_constructor(graph, local_nodes) best_parent = policy.best_parent() newshot_node = policy.generate_node_name() edge = wire_pb2.Graph.GraphEdge() edge.from_node = best_parent or 'FULL' edge.to_node = newshot_node write_framed(edge.SerializeToString()) subprocess.stdout.close() with yield_pieces_output_manager(subprocess.stdin) as sink: with storage_driver.get_snapstream(best_parent, newshot_node, True) as btrfs_send: for piece in yield_pieces(btrfs_send.stdout, with_magic=False): sink.write(piece) subprocess.stdin.close() subprocess.wait() policy.clean_local_nodes(storage_driver)
def edit_file(A, filename): import subprocess as D B = A.get_editor() if A.env: C = os.environ.copy() C.update(A.env) else: C = _A try: E = D.Popen(f'{B} "{filename}"', env=C, shell=_B) F = E.wait() if F != 0: raise ClickException(f"{B}: Editing failed!") except OSError as G: raise ClickException(f"{B}: Editing failed: {G}")
''' return the number of open file descriptors for current process .. warning: will only work on UNIX-like os-es. ''' import subprocess import os pid = os.getpid() procs = subprocess.check_output( [ "lsof", '-w', '-Ff', "-p", str( pid ) ] ) procs = filter( lambda s: s and s[ 0 ] == 'f' and s[1: ].isdigit(), procs.split( '\n' ) ) fds = [int(a[1:]) for a in procs] return fds if (args.nodel is False): openFds = get_open_fds() if (len(openFds) > 0): os.closerange(openFds[0], openFds[-1]) for tmpFile in tmpFiles: command = "/bin/rm -f " + tmpFile try: print "command: " + command subprocesss.Popen(command.split()) subprocess.wait() except: print "Cannot delete " + tmpFile
def get_open_fds(): """ return the number of open file descriptors for current process .. warning: will only work on UNIX-like os-es. """ import subprocess import os pid = os.getpid() procs = subprocess.check_output(["lsof", "-w", "-Ff", "-p", str(pid)]) procs = filter(lambda s: s and s[0] == "f" and s[1:].isdigit(), procs.split("\n")) fds = [int(a[1:]) for a in procs] return fds if args.nodel is False: openFds = get_open_fds() if len(openFds) > 0: os.closerange(openFds[0], openFds[-1]) for tmpFile in tmpFiles: command = "/bin/rm -f " + tmpFile try: print "command: " + command subprocesss.Popen(command.split()) subprocess.wait() except: print "Cannot delete " + tmpFile
def play_url(url): videoFilename = '' opts = { # 'format': 'best', # sadly doesn't give the highest quality # removes "WARNING: Requested formats are incompatible for merge and will be merged into mkv." # https://askubuntu.com/questions/806258/requested-formats-are-incompatible-for-merge # https://stackoverflow.com/questions/31631535/youtube-dl-dash-video-and-audio-in-highest-quality-without-human-intervention # 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', 'format': 'bestvideo[height>=1080]+bestaudio/bestvideo+bestaudio/best', # 'nopart': True, # 'progress_hooks': [hook], 'outtmpl': args.download_dir + "/%(title)s.%(ext)s", 'verbose': args.v, 'noplaylist': True, } with youtube_dl.YoutubeDL(opts) as ydl: try: info = ydl.extract_info(url, download=False) videoFilename = ydl.prepare_filename(info) if args.v: print("download dir: ", args.download_dir, flush=True) ydl.download([url]) except Exception as e: print(traceback.format_exception(*sys.exc_info())) print(e) print('----------------- failed to youtube-dl ', url, flush=True) pass # try to salvage wrong extension name if not os.path.isfile(videoFilename): videoFilename = os.path.splitext(videoFilename)[0] + '.mkv' if not os.path.isfile(videoFilename): print('video file not found ', url, ' filename: ', flush=True) try: print(videoFilename, flush=True) except: print("couldn't print filename", flush=True) pass return try: video_should_open = not args.no_autoplay video_should_delete = args.keep_download # Open video if video_should_open: command = [args.video_player, videoFilename] print("executing:", flush=True) try: print(command, flush=True) except: print("couldn't print command (videoFilename)", flush=True) pass try: import subprocess subprocess = subprocess.Popen(command) subprocess.wait() except Exception as e: print(traceback.format_exception(*sys.exc_info())) print(e) print('Failed to launch video player', flush=True) pass # Delete video if video_should_delete: try: print(f'deleting video: {videoFilename}') os.remove(videoFilename) except Exception as e: print(traceback.format_exception(*sys.exc_info())) print(e) print('did not delete ', videoFilename, flush=True) pass except SystemExit as e: print(f'SystemExit caught :)') except Exception as e: print(f'Exception caught {e}')