Пример #1
0
def dont_echo() -> None:
    """Don't echo user input"""

    if not _name == "posix":
        raise NotImplementedError(
            "This method is only implemented on POSIX systems.")

    _Popen(["stty", "-echo"])
Пример #2
0
def _drInfo2df(dr_file):
    '''Calls a dataRecordInfo script on already converted to dr format RNX file with rnxEditGde.py.
    dr_file is an absolute path but if switching machines/dr_location we need to rerun'''
    drInfo_process = _Popen(
        args=['dataRecordInfo', '-file',
              _os.path.basename(dr_file)],
        stdout=_PIPE,
        stderr=_STDOUT,
        cwd=_os.path.dirname(dr_file))
    out = drInfo_process.communicate()[0]
    dr_Info_raw = _pd.Series(out.decode('ascii').splitlines()).str.split(
        pat=r':\s', expand=True)

    df = _pd.DataFrame(index=[0])
    df['n_records'] = int(dr_Info_raw.iloc[0, 1])
    time_vals = _pd.to_datetime(dr_Info_raw.iloc[1:3, 1]).values
    df['begin'] = time_vals[0]
    df['end'] = time_vals[1]
    df['n_receivers'] = _pd.to_numeric(dr_Info_raw.iloc[3, 1])
    df['n_transmitters'] = _pd.to_numeric(dr_Info_raw.iloc[5, 1])
    df['station_name'] = dr_Info_raw.iloc[4, 0].strip()

    transmitters = dr_Info_raw.iloc[6:, 0]
    df['GPS'] = transmitters.str.contains(pat=r'GPS\d{2}').sum(
    )  #number of GPS satellites present in the dr file
    df['GLONASS'] = transmitters.str.contains(pat=r'R\d{3}').sum(
    )  #number of GLONASS satellites present in the dr file
    df['path'] = _pd.Series(dr_file).str.extract(r'(\/rnx_dr.+)')

    return df
Пример #3
0
def _2dr(rnx2dr_path):
    '''Opens process rxEditGde.py to convert specified rnx to dr file for GipsyX. The subprocess is used in order to run multiple instances at once.
    If converted file is already present, nothing happens
    We might want to dump and kill service tree files and stats'''
    in_file_path = rnx2dr_path[0]
    out_file_path = rnx2dr_path[1]
    cache_path = rnx2dr_path[2]
    staDb_path = rnx2dr_path[3]

    out_dir = _os.path.dirname(out_file_path)

    cache_dir = _os.path.join(cache_path, _os.path.basename(
        out_file_path))  #smth like /cache/anau2350.10d.dr.gz/
    if not _os.path.exists(cache_dir):
        _os.makedirs(cache_dir)
    _copy(src=in_file_path, dst=cache_dir)  #copy
    in_file_cache_path = _os.path.join(cache_dir,
                                       _os.path.basename(in_file_path))
    out_file_cache_path = _os.path.join(cache_dir,
                                        _os.path.basename(out_file_path))

    process = _Popen([
        'rnxEditGde.py', '-dataFile', in_file_cache_path, '-staDb', staDb_path,
        '-o', out_file_cache_path
    ],
                     cwd=cache_dir)
    process.wait()
    _copy(src=out_file_cache_path, dst=out_dir)  #copy result to destination
    #clear folder in ram
    _rmtree(cache_dir)
Пример #4
0
def _focus_client_app():
    scpt = '''
            delay .5
            tell application \"Turi Create Visualization\" to activate
            '''
    focus = _Popen(['osascript', '-'], stdout=_PIPE, stdin=_PIPE)
    focus.communicate(scpt)
Пример #5
0
def _call(command, check_file=None, **exc_args):
    """
    Call command in subprocess.

    Args:
        command (list or tuple of str): Command to call.
        check_file (str): Returns file content in exception if exists.
        exc_args: Extra arguments for exception to raise
            if error.

    Raises:
        apyfal.exceptions.ClientRuntimeException:
            Error while calling command.
    """
    _get_logger().debug("Running shell command: '%s'" % ' '.join(command))
    try:
        process = _Popen(command,
                         stdout=_PIPE,
                         stderr=_PIPE,
                         universal_newlines=True)
        outputs = list(process.communicate())
        in_error = process.returncode
    except OSError as exception:
        in_error = True
        outputs = [str(exception)]
    if in_error:
        if check_file and _exists(check_file):
            with open(check_file, 'rt') as file:
                outputs.append(file.read())
        raise _exc.ClientRuntimeException(exc='\n'.join(
            [command if isinstance(command, str) else ' '.join(command)] +
            [output for output in outputs if output]),
                                          **exc_args)
Пример #6
0
def _gen_orbclk(input_set):
    startTime = input_set[0]
    endTime = input_set[1]
    GNSSproducts = input_set[2]
    targetDir = input_set[3]
    h24 = input_set[4]
    makeShadow = input_set[5]
    products_day = input_set[6]
    run_dir = input_set[7]
    #check if target folder exists and create one if not

    if _os.path.exists(run_dir): _rmtree(run_dir) #cleaning ram if failed
    if not _os.path.exists(run_dir):
        _os.makedirs(run_dir)
    
    
    args = ['/home/bogdanm/Desktop/GipsyX_Wrapper/fetchGNSSproducts_J2000.py',
                      '-startTime',str(startTime),
                      '-endTime', str(endTime),
                      '-GNSSproducts', GNSSproducts,
                      '-targetDir', run_dir]
    args.append( '-hr24') if h24 else None
    args.append ('-makeShadowFile') if makeShadow else None


    process = _Popen(args,stdout=_PIPE)
    out, err = process.communicate()

    #rename
    files_ori = _glob.glob('{}/GNSS.*'.format(run_dir))

    try:
        files_ori_df = _pd.Series(files_ori).str.split('.',expand=True)
    except: print(str(products_day),'problem found')
        
    files_renamed = files_ori_df[0].str.slice(0,-4) + str(products_day) + '.' + files_ori_df[1]
    for i in range(files_renamed.shape[0]):
        _os.rename(files_ori[i],files_renamed[i])
    #gzip
    _Popen(['gzip *'],cwd=run_dir,shell=True).communicate()
    #move one level up
    if not _os.path.exists(targetDir):
        _os.makedirs(targetDir)
    for i in range(files_renamed.shape[0]):
        _move(src=files_renamed[i]+'.gz',dst=targetDir)
    _rmtree(run_dir)
    return out,err
Пример #7
0
def _tput(command: list[str]) -> None:
    """Shorthand for tput calls"""

    waited_commands = [
        "clear",
        "smcup",
        "cup",
    ]

    command.insert(0, "tput")
    str_command = [str(c) for c in command]

    if command[1] in waited_commands:
        _run(str_command, check=True)
        return

    _Popen(str_command)
Пример #8
0
 def __init__(self):
     proc = _Popen(['gnuplot', '-p'],
                   shell=False,
                   stdin=_PIPE,
                   universal_newlines=True)  # persitant -p
     self.instance = {
         0: [proc, default_term]
     }  # {figure number : [process, terminal type]}
     self.n = 0  # currently selected Figure
Пример #9
0
def _gd2e(gd2e_set):

    out_dir = _os.path.dirname(gd2e_set['output'])
    if not _os.path.exists(out_dir): _os.makedirs(out_dir)  #creating out dir

    if not _os.path.exists(gd2e_set['cache']):
        _os.makedirs(gd2e_set['cache'])  #creatign cache dir
    runAgain = 'gd2e.py -drEditedFile {0} -recList {1} -runType PPP -GNSSproducts {2} -treeSequenceDir {3} -tdpInput {4} -staDb {5} -selectGnss \'{6}\' -gdCov'.format(
        gd2e_set['filename'], gd2e_set['station_name'],
        gd2e_set['gnss_products_dir'], gd2e_set['tree_path'], gd2e_set['tdp'],
        gd2e_set['staDb_path'], gd2e_set['selectGnss'])
    if not gd2e_set['tqdm']: print(runAgain)
    # print(runAgain)
    # try:
    process = _Popen(
        [
            'gd2e.py',
            '-drEditedFile',
            gd2e_set['filename'],
            '-recList',
            gd2e_set['station_name'],
            '-runType',
            'PPP',
            '-GNSSproducts',
            gd2e_set[
                'gnss_products_dir'],  #used to be '-GNSSproducts', gd2e_set['gnss_products_dir'],
            '-treeSequenceDir',
            gd2e_set['tree_path'],
            '-tdpInput',
            gd2e_set['tdp'],
            '-staDb',
            gd2e_set['staDb_path'],
            '-selectGnss',
            gd2e_set['selectGnss']
        ],
        cwd=gd2e_set['cache'],
        stdout=_PIPE)
    # Do we really need a -gdCov option?
    out, err = process.communicate()

    solutions = _get_tdps_pn(gd2e_set['cache'])
    residuals = _get_residuals(gd2e_set['cache'])
    debug_tree = _get_debug_tree(gd2e_set['cache'])

    rtgx_log = _get_rtgx_log(gd2e_set['cache'])
    rtgx_err = _get_rtgx_err(gd2e_set['cache'])
    summary = _get_summary(gd2e_set['cache'])
    _rmtree(path=gd2e_set['cache'])  #clearing cache after run

    _dump_write(data=[
        solutions, residuals, debug_tree, runAgain, rtgx_log, rtgx_err, out,
        err, summary
    ],
                filename=gd2e_set['output'],
                cname='zstd')
Пример #10
0
def _merge(merge_set):
    '''Expects a merge set of 3 files [merge_start, merge_end,file_prev,file,file_next]. Merges all files into file120h. file1 must be a class 3 file
    Constructs 32h files with drMerge.py.
    Sample input:
    drMerge.py -i isba0940.15o.dr ohln0940.15o.dr -start 2015-04-04 00:00:00 -end 2015-04-04 04:00:00
    '''
    #Computing time boundaries of the merge. merge_set[1] is file begin time
    drMerge_proc = _Popen(['drMerge', str(merge_set['merge_begin']), str(merge_set['merge_end']), _os.path.basename(merge_set['path'])+'.30h',\
                merge_set['path_prev'], merge_set['path'], merge_set['path_next'] ],\
                cwd=_os.path.dirname(merge_set['path']))
    drMerge_proc.wait()
Пример #11
0
def _ce2cm_single_thread(pos_path_series):
    pos_src,pos_dst,cache_path = pos_path_series
    # fuction will rewrite the input pos file by cm corrected version
#     pos_src = _os.path.abspath(pos_src)
    
    #copy to cache. create single test folder
    _copy(src=pos_src,dst=cache_path)
    
    input_path = _os.path.join(cache_path,_os.path.basename(pos_src))

    process = _Popen(['orbitCmCorrection','-s','-i',input_path,'-o',pos_dst])
    process.wait()
    _os.remove(input_path)
Пример #12
0
def pipe(cmd, stdin=None):
    import os, select
    bufsize = 3000
    p = _Popen(cmd,
               shell=True,
               bufsize=bufsize,
               stdin=_PIPE,
               stdout=_PIPE,
               stderr=_PIPE,
               close_fds=True)
    i, o, e = p.stdin, p.stdout, p.stderr
    stdout = ""
    stderr = ""

    ni, no, ne = i.fileno(), o.fileno(), e.fileno()

    if stdin != None:
        size = 0
        for n in range(0, len(stdin), bufsize):
            try:
                os.write(ni, stdin[n:n + bufsize])
            except OSError:
                break
            ii = []
            while len(ii) == 0:
                oo, ii, ee = select.select([o, e], [i], [])
                change = False
                if o in oo:
                    stdout += os.read(no, bufsize)
                    change = True
                if e in oo:
                    stderr += os.read(ne, bufsize)
                    change = True
                if change == False: break
    i.close()
    oo, ii, ee = select.select([o, e], [], [])

    change = True
    while (len(oo) > 0 or len(ee) > 0) and change:
        change = False
        stdoutlen = len(stdout)
        if o in oo:
            stdout += os.read(no, bufsize)
            if len(stdout) > stdoutlen: change = True
        stderrlen = len(stderr)
        if e in oo:
            stderr += os.read(ne, bufsize)
            if len(stderr) > stderrlen: change = True
        oo, ii, ee = select.select([o, e], [], [])

    return stdout, stderr
Пример #13
0
def _run_command(args, stdout=_PIPE, stderr=_PIPE, encoding=None, stream=0):
    # regarding the shell argument, see: http://bugs.python.org/issue8557
    try:
        proc = _Popen(args, stdout=stdout, stderr=stderr, shell=(sys.platform == "win32"))

        data = proc.communicate()[stream]
    except OSError:
        return 1, ""

    # doubled checked and
    data = decode_as_string(data, encoding)

    # communciate calls wait()
    return proc.returncode, data
Пример #14
0
def _run_command(args, stdout=_PIPE, stderr=_PIPE):
    # regarding the shell argument, see: http://bugs.python.org/issue8557
    try:
        args = [fsdecode(x) for x in args]
        proc = _Popen(args, stdout=stdout, stderr=stderr, shell=(sys.platform == "win32"))

        data = proc.communicate()[0]
    except OSError:
        return 1, ""

    data = consoledecode(data)

    # communciate calls wait()
    return proc.returncode, data
Пример #15
0
def run_setup_py(cmd, pypath=None, path=None, data_stream=0, env=None):
    """
    Execution command for tests, separate from those used by the
    code directly to prevent accidental behavior issues
    """
    if env is None:
        env = dict()
        for envname in os.environ:
            env[envname] = os.environ[envname]

    # override the python path if needed
    if pypath is not None:
        env["PYTHONPATH"] = pypath

    # overide the execution path if needed
    if path is not None:
        env["PATH"] = path
    if not env.get("PATH", ""):
        env["PATH"] = _which_dirs("tar").union(_which_dirs("gzip"))
        env["PATH"] = os.pathsep.join(env["PATH"])

    cmd = [sys.executable, "setup.py"] + list(cmd)

    # http://bugs.python.org/issue8557
    shell = sys.platform == 'win32'

    try:
        proc = _Popen(
            cmd,
            stdout=_PIPE,
            stderr=_PIPE,
            shell=shell,
            env=env,
        )

        if isinstance(data_stream, tuple):
            data_stream = slice(*data_stream)
        data = proc.communicate()[data_stream]
    except OSError:
        return 1, ''

    # decode the console string if needed
    if hasattr(data, "decode"):
        # use the default encoding
        data = data.decode()
        data = unicodedata.normalize('NFC', data)

    # communicate calls wait()
    return proc.returncode, data
Пример #16
0
def _run_command(args, stdout=_PIPE, stderr=_PIPE, encoding=None, stream=0):
    #regarding the shell argument, see: http://bugs.python.org/issue8557
    try:
        proc = _Popen(args, stdout=stdout, stderr=stderr,
                      shell=(sys.platform == 'win32'))

        data = proc.communicate()[stream]
    except OSError:
        return 1, ''

    #doubled checked and
    data = decode_as_string(data, encoding)

    #communciate calls wait()
    return proc.returncode, data
Пример #17
0
def _run_command(args, stdout=_PIPE, stderr=_PIPE):
    #regarding the shell argument, see: http://bugs.python.org/issue8557
    try:
        args = [fsdecode(x) for x in args]
        proc = _Popen(args, stdout=stdout, stderr=stderr,
                      shell=(sys.platform == 'win32'))

        data = proc.communicate()[0]
    except OSError:
        return 1, ''

    data = consoledecode(data)

    #communciate calls wait()
    return proc.returncode, data
Пример #18
0
def gen_synth_otl(dataset, station_name, hardisp_path, blq_file, sampling):
    '''Expects stretched dataset. Otherwise can get wrong sampling. Outputs (T, E, N, V) array'''
    _pd.options.display.max_colwidth = 200  #By default truncates text
    begin_J2000 = dataset.index[0]
    end_J2000 = dataset.index[-1]
    begin = (begin_J2000.astype(int) + _J2000origin).astype(_datetime)
    # end = (end_J2000.astype(int) + _J2000origin).astype(_datetime)
    n_observations = int((end_J2000 - begin_J2000) / sampling) + 1
    input_blq = blq2hardisp(blq_file)
    input_blq = input_blq[input_blq[:, 0] == station_name][0][1]
    '''Takes datetime.datetime format'''
    process = _Popen([
        hardisp_path,
        str(begin.year),
        str(begin.month),
        str(begin.day),
        str(begin.hour),
        str(begin.minute),
        str(begin.second),
        str(n_observations),
        str(sampling)
    ],
                     stdin=_PIPE,
                     stdout=_PIPE,
                     stderr=_PIPE)
    out = _StringIO((process.communicate(
        input=reformat_blq(input_blq).encode())[0]).decode())

    synth_otl = _pd.read_csv(out,
                             error_bad_lines=False,
                             header=None,
                             delim_whitespace=True,
                             names=['dU', 'dS', 'dW'
                                    ]) * 1000  #convert to mm as returns in m

    tmp = _pd.DataFrame()
    index = _np.arange(begin_J2000, end_J2000 + 1, sampling)

    tmp['east'] = synth_otl[
        'dW'] * -1  #Conversion to dE. Already checked that it is a correct way (phases) as same as GipsyX does correction (no otl - otl_corrected)
    tmp['north'] = synth_otl['dS'] * -1  #Convertion to dN
    tmp['up'] = synth_otl['dU']
    synth_otl['Time'] = _pd.Series(
        _np.arange(begin_J2000, end_J2000 + 1, sampling))
    hardisp = tmp.set_index(index)
    return hardisp
Пример #19
0
def Popen(*args, **kwargs):
    """subprocess.Popen() with resilient process termination at end of scope."""
    with _Popen(*args, **kwargs) as process:
        try:
            yield process
        finally:
            # Process has not yet terminated, kill it.
            if process.poll() is None:
                # kill() was added in Python 3.7.
                if sys.version_info >= (3, 7, 0):
                    process.kill()
                else:
                    process.terminate()
                # Wait for shutdown to complete.
                try:
                    process.communicate(timeout=60)
                except subprocess.TimeoutExpired:
                    pass  # Stubborn process won't die, nothing can be done.
Пример #20
0
def run_setup_py(cmd, pypath=None, path=None,
                 data_stream=0, env=None):
    """
    Execution command for tests, separate from those used by the
    code directly to prevent accidental behavior issues
    """
    if env is None:
        env = dict()
        for envname in os.environ:
            env[envname] = os.environ[envname]

    # override the python path if needed
    if pypath is not None:
        env["PYTHONPATH"] = pypath

    # overide the execution path if needed
    if path is not None:
        env["PATH"] = path
    if not env.get("PATH", ""):
        env["PATH"] = _which_dirs("tar").union(_which_dirs("gzip"))
        env["PATH"] = os.pathsep.join(env["PATH"])

    cmd = [sys.executable, "setup.py"] + list(cmd)

    # http://bugs.python.org/issue8557
    shell = sys.platform == 'win32'

    try:
        proc = _Popen(
            cmd, stdout=_PIPE, stderr=_PIPE, shell=shell, env=env,
        )

        data = proc.communicate()[data_stream]
    except OSError:
        return 1, ''

    # decode the console string if needed
    if hasattr(data,  "decode"):
        # use the default encoding
        data = data.decode()
        data = unicodedata.normalize('NFC', data)

    # communciate calls wait()
    return proc.returncode, data
Пример #21
0
def figure(number=None):
    '''Make Gnuplot plot in a new Window or update a defined one figure(num=None, term='x11'):
    >>> figure(2)  # would create or update figure 2
    >>> figure()  # simply creates a new figure
    returns the new figure number
    '''
    if not isinstance(number, int):  # create new figure if no number was given
        number = max(fl.instance) + 1

    if number not in fl.instance:  # number is new
        proc = _Popen(['gnuplot', '-p'],
                      shell=False,
                      stdin=_PIPE,
                      universal_newlines=True)
        fl.instance[number] = [proc, default_term]

    fl.n = number
    c('set term ' + str(fl.instance[fl.n][1]) + ' ' + str(fl.n))
    return number
Пример #22
0
    def do_Popen(*args, **kwargs):
        """
        Do the actual running of Popen.
        """

        output_prefix = kwargs.pop("output_prefix", None)
        if output_prefix is not None:
            sout_fname = f"{tempdir}/{output_prefix}.out"
            serr_fname = f"{tempdir}/{output_prefix}.err"

            sout = open(sout_fname, "wb")
            serr = open(serr_fname, "wb")
            kwargs["stdin"] = DEVNULL
            kwargs["stdout"] = sout.fileno()
            kwargs["stderr"] = serr.fileno()

        proc = _Popen(*args, **kwargs)
        procs.append(proc)
        outs.append(sout)
        errs.append(serr)
        return proc
Пример #23
0
    def communicate(self, input=None):
        """see ubprocess.Popen.communicate()
        capture out and err of excuted process
        """

        # check that we have it or not in the store
        if input:
            self.command.setInput(input)
            # input is part of the signature (i.e the key)
            # fetch it again
            _cmdXML = getCommandStore().fetch(self.command.getKey())
            if _cmdXML is not None:
                # fetch it again now that we now the input
                self.command = Command.fromXMLElement(_cmdXML)

        if self.command is not None:
            self.returncode = self.command.getExitCode()
            return (self.command.getOutput(), self.command.getErrorOutput())
        else:
            # we do not know this command trigger real execution
            return subprocess._Popen(self.args, self.kwargs).communicate(input)
Пример #24
0
    def run(self):
        try:
            p = _Popen(
                self._args,
                stdout=_Pipe,
                stderr=_Pipe,
                text=True,
                env=self._env,
                cwd=self._cwd,
            )
            (out, err) = p.communicate(timeout=self._timeout)
            self.isTimeout = False

            self.data = out
            self.code = p.returncode
            self.error = err
        except _TimeoutExpired as e:
            self.isTimeout = True
            self.code = -1
            self.data = ""
            self.error = f"{self._args[0]}: Timeout({e.timeout})"
        except:
            raise Exception(str([self._args, self._env, self._cwd]))
Пример #25
0
def _run_cmdline(command):
    # runs a shell command
    p = _Popen(args=command, stdout=_PIPE, stderr=_PIPE, shell=True)
    stdout_feed, stderr_feed = p.communicate()  # wait for completion
    exit_code = p.poll()
    return (exit_code, stdout_feed, stderr_feed)
Пример #26
0
def uncompress(file_path):
    process = _Popen(['uncompress', file_path])
    process.wait()
Пример #27
0
class Host(object):
    ''' Object class for getting and setting details on a gridengine host/client '''
    def __init__(self, h_ip=""):

        #TODO: check the h_ip is a valid ip or hostname, for now assume it's a hostname

        self.hostname = h_ip
        self.ip = '10.40.50.1'
        self.enabled = False
        self.queues = {}
        self.jobs = {}
        #self.setStats(h_ip)

    def getJobs(self):
        ''' Return a list of Jobs that are running on this host '''

        job_list = []

        return job_list

    def setStats(self, host_ip=None):
        ''' given a hostname or ip address populate the attributes of this host object '''

        # check host exists
        if not host_ip:
            host_ip = self.hostname

        connection = True

        try:
            self.ip = _socket.gethostbyname(host_ip)
        except _socket.gaierror, msg:
            connection = False

        self.alive = connection

        # We need an xml parser to clean this up a bit, we don't need to repeat all this code in each Class

        cmd = ['qhost', '-cb', '-q', '-j', '-h', host_ip, '-xml']
        qstat_bin = _Popen(cmd, stdout=_PIPE, stderr=_PIPE)
        xml_out, xml_err = qstat_bin.communicate()

        if len(xml_err):
            return "Error in querying qhost:", xml_err

        xml_root = _fromstring(xml_out)

        if xml_root.tag == 'qhost' and len(xml_root.getchildren()) >= 2:
            this_host_info = xml_root.getchildren()[1]

            self.hostname = this_host_info.attrib['name']

            attributes = this_host_info.getchildren()

            queue_info = {}
            job_info = {}

            for attr in attributes:
                # Queue info
                if attr.tag == 'queue':
                    queue_info[attr.attrib['name']] = {}

                    for queue_st in attr.getchildren():
                        queue_info[attr.attrib['name']][
                            queue_st.attrib['name']] = queue_st.text

                    self.queues = queue_info

                elif attr.tag == 'job':
                    job_info[str(attr.attrib['name'])] = {}

                    for job_st in attr.getchildren():
                        try:
                            job_info[str(attr.attrib['name'])][
                                job_st.attrib['name']] = job_st.text
                        except KeyError, msg:
                            print 'Job KEY ERROR when gathering host jobs:', msg
                    self.jobs = job_info

                else:
                    setattr(self, attr.attrib['name'], attr.text)
Пример #28
0
 def start(self):
     self.gnuplot = _Popen([self.gpltpath, '-p'],
                           shell=False,
                           stdin=_PIPE,
                           universal_newlines=True)
Пример #29
0
def shell_run(cmd,
              cin=None, cwd=None, timeout=10, critical=True, verbose=True):
    '''
    Runs a shell command within a controlled environment.

    .. note:: |use_photon_m|

    :param cmd: The command to run

        * A string one would type into a console like \
        :command:`git push -u origin master`.

        * Will be split using :py:func:`shlex.split`.

        * It is possible to use a list here, but then no splitting is done.

    :param cin:
        Add something to stdin of `cmd`
    :param cwd:
        Run `cmd` insde specified current working directory
    :param timeout:
        Catch infinite loops (e.g. ``ping``).
        Exit after `timeout` seconds
    :param critical:
        If set to ``True``: |appteardown| on failure of `cmd`
    :param verbose:
        Show messages and warnings
    :returns:
        A dictionary containing the results from
        running `cmd` with the following:

        * 'command': `cmd`

        * 'stdin': `cin` (If data was set in `cin`)

        * 'cwd': `cwd` (If `cwd` was set)

        * 'exception': exception message (If an exception was thrown)

        * 'timeout': `timeout` (If a timeout exception was thrown)

        * 'stdout': List from stdout (If any)

        * 'stderr': List from stderr (If any)

        * 'returncode': The returncode (If not any exception)

        * 'out': The most urgent message as joined string. \
        ('exception' > 'stderr' > 'stdout')
    '''

    res = dict(command=cmd)
    if cin:
        cin = str(cin)
        res.update(dict(stdin=cin))
    if cwd:
        res.update(dict(cwd=cwd))

    if isinstance(cmd, str):
        cmd = _split(cmd)

    try:
        p = _Popen(
            cmd, stdin=_PIPE, stdout=_PIPE, stderr=_PIPE,
            bufsize=1, cwd=cwd, universal_newlines=True
        )
    except Exception as ex:
        res.update(dict(exception=str(ex)))
    else:

        try:
            out, err = p.communicate(input=cin, timeout=timeout)
            if out:
                res.update(dict(
                    stdout=[o for o in out.split('\n') if o]
                ))
            if err:
                res.update(dict(
                    stderr=[e for e in err.split('\n') if e]
                ))
            res.update(dict(returncode=p.returncode))

        except _TimeoutExpired as ex:
            res.update(dict(exception=str(ex), timeout=timeout))
            p.kill()
        except Exception as ex:
            res.update(dict(exception=str(ex)))

    res.update(
        out=(
            res.get('exception') or
            '\n'.join(res.get('stderr') or res.get('stdout', ''))
        )
    )

    if res.get('returncode', -1) != 0:
        res.update(dict(critical=critical))

        shell_notify(
            'error in shell command \'%s\'' % (res.get('command')),
            state=True if critical else None,
            more=res,
            verbose=verbose
        )

    return res
Пример #30
0
def verify_python3_env():
    """Ensures that the environment is good for unicode on Python 3."""
    try:
        fs_enc = _codecs.lookup(_locale.getpreferredencoding()).name
    except Exception:
        fs_enc = 'ascii'
    if fs_enc != 'ascii':
        return

    extra = ''
    if _os.name == 'posix':
        rv = _Popen(
            ['locale', '-a'], stdout=_PIPE, stderr=_PIPE).communicate()[0]
        good_locales = set()
        has_c_utf8 = False

        # Make sure we're operating on text here.
        if isinstance(rv, bytes):
            rv = rv.decode('ascii', 'replace')

        for line in rv.splitlines():
            locale = line.strip()
            if locale.lower().endswith(('.utf-8', '.utf8')):
                good_locales.add(locale)
                if locale.lower() in ('c.utf8', 'c.utf-8'):
                    has_c_utf8 = True

        extra += '\n\n'
        if not good_locales:
            extra += (
                'Additional information: on this system no suitable UTF-8\n'
                'locales were discovered.  This most likely requires resolving\n'
                'by reconfiguring the locale system.')
        elif has_c_utf8:
            extra += (
                'This system supports the C.UTF-8 locale which is recommended.\n'
                'You might be able to resolve your issue by exporting the\n'
                'following environment variables:\n\n'
                '    export LC_ALL=C.UTF-8\n'
                '    export LANG=C.UTF-8')
        else:
            extra += (
                'This system lists a couple of UTF-8 supporting locales that\n'
                'you can pick from.  The following suitable locales were\n'
                'discovered: %s') % ', '.join(sorted(good_locales))

        bad_locale = None
        for locale in _os.environ.get('LC_ALL'), _os.environ.get('LANG'):
            if locale and locale.lower().endswith(('.utf-8', '.utf8')):
                bad_locale = locale
            if locale is not None:
                break
        if bad_locale is not None:
            extra += (
                '\n\nnpm2deb discovered that you exported a UTF-8 locale\n'
                'but the locale system could not pick up from it because\n'
                'it does not exist.  The exported locale is "%s" but it\n'
                'is not supported') % bad_locale

    raise RuntimeError('npm2deb will abort further execution because Python 3 '
                       'was configured to use ASCII as encoding for the '
                       'environment.' + extra)
Пример #31
0
def verify_python3_env():
    """Ensures that the environment is good for unicode on Python 3."""
    try:
        fs_enc = _codecs.lookup(_locale.getpreferredencoding()).name
    except Exception:
        fs_enc = 'ascii'
    if fs_enc != 'ascii':
        return

    extra = ''
    if _os.name == 'posix':
        rv = _Popen(['locale', '-a'], stdout=_PIPE,
                    stderr=_PIPE).communicate()[0]
        good_locales = set()
        has_c_utf8 = False

        # Make sure we're operating on text here.
        if isinstance(rv, bytes):
            rv = rv.decode('ascii', 'replace')

        for line in rv.splitlines():
            locale = line.strip()
            if locale.lower().endswith(('.utf-8', '.utf8')):
                good_locales.add(locale)
                if locale.lower() in ('c.utf8', 'c.utf-8'):
                    has_c_utf8 = True

        extra += '\n\n'
        if not good_locales:
            extra += (
                'Additional information: on this system no suitable UTF-8\n'
                'locales were discovered.  This most likely requires resolving\n'
                'by reconfiguring the locale system.')
        elif has_c_utf8:
            extra += (
                'This system supports the C.UTF-8 locale which is recommended.\n'
                'You might be able to resolve your issue by exporting the\n'
                'following environment variables:\n\n'
                '    export LC_ALL=C.UTF-8\n'
                '    export LANG=C.UTF-8')
        else:
            extra += (
                'This system lists a couple of UTF-8 supporting locales that\n'
                'you can pick from.  The following suitable locales were\n'
                'discovered: %s') % ', '.join(sorted(good_locales))

        bad_locale = None
        for locale in _os.environ.get('LC_ALL'), _os.environ.get('LANG'):
            if locale and locale.lower().endswith(('.utf-8', '.utf8')):
                bad_locale = locale
            if locale is not None:
                break
        if bad_locale is not None:
            extra += (
                '\n\nnpm2deb discovered that you exported a UTF-8 locale\n'
                'but the locale system could not pick up from it because\n'
                'it does not exist.  The exported locale is "%s" but it\n'
                'is not supported') % bad_locale

    raise RuntimeError('npm2deb will abort further execution because Python 3 '
                       'was configured to use ASCII as encoding for the '
                       'environment.' + extra)
Пример #32
0
    #overide the execution path if needed
    if path is not None:
        env["PATH"] = path
    if not env.get("PATH", ""):
        env["PATH"] = _which_dirs("tar").union(_which_dirs("gzip"))
        env["PATH"] = os.pathsep.join(env["PATH"])

    cmd = [sys.executable, "setup.py"] + list(cmd)

<<<<<<< HEAD
    # http://bugs.python.org/issue8557
    shell = sys.platform == 'win32'

    try:
        proc = _Popen(
            cmd, stdout=_PIPE, stderr=_PIPE, shell=shell, env=env,
        )
=======
    #regarding the shell argument, see: http://bugs.python.org/issue8557
    try:
        proc = _Popen(cmd, stdout=_PIPE, stderr=_PIPE,
                      shell=(sys.platform == 'win32'), env=env)
>>>>>>> e4baf504ede925f4f1e07d823c9b20b3d0dbe14c

        data = proc.communicate()[data_stream]
    except OSError:
        return 1, ''

    #decode the console string if needed
    if hasattr(data,  "decode"):
<<<<<<< HEAD
Пример #33
0
def run_eterna(input_vars):
    eterna_exec, comp_path = input_vars
    process = _Popen([eterna_exec], cwd=comp_path, stdout=_PIPE)
    process.communicate()
Пример #34
0
Файл: task.py Проект: gwk/pithy
def run(cmd, cwd=None, stdin=None, out=None, err=None, env=None, timeout=None, exp=0):
    """
  Run a command and return (exit_code, std_out, std_err).
  Cmd: str or list of str.
  Cwd: str path.
  Stdin: str, bytes, open binary file (including value of dev_null()).
  Out, err: open binary file or _pipe special.
  Env: dict of str.
  Timeout: numeric or None.
  Exp: expected exit code can be None (accept any value), an integer code,
    or `...` (Ellipsis) to indicate any nonzero code.

  The special ellipsis notation is used because a bool expectation is confusing;
  nonzero implies True in Python, but False in Unix.

  The underlying Subprocess shell option is not supported
  because the rules regarding splitting strings are complex.
  User code is made clearer by just specifying the complete shell command;
  lists are used as is, while strings are split by shlex.split.
  """

    if isinstance(cmd, str):
        cmd = _shlex.split(cmd)

    if isinstance(stdin, str):
        f_in = _pipe
        input_bytes = stdin.encode("utf-8")
    elif isinstance(stdin, bytes):
        f_in = _pipe
        input_bytes = stdin
    else:
        f_in = stdin  # presume None, _pipe, or file, which includes dev_null().
        input_bytes = None

    proc = _Popen(cmd, cwd=cwd, stdin=f_in, stdout=out, stderr=err, shell=False, env=env)

    # timeout alarm handler.
    timed_out = False
    if timeout is not None:

        def alarm_handler(signum, current_stack_frame):
            # since signal handlers carry reentrancy concerns, do not do any IO within the handler.
            nonlocal timed_out
            timed_out = True
            proc.kill()

        signal.signal(signal.SIGALRM, alarm_handler)  # set handler.
        signal.alarm(timeout)  # set alarm.

    p_out, p_err = proc.communicate(input_bytes)  # waits for process to complete.

    if timeout is not None:
        signal.alarm(0)  # disable alarm.
        if timed_out:
            raise ProcessTimeout(cmd, timeout)

    code = proc.returncode
    if exp is None:
        pass
    elif exp is Ellipsis:
        if code == 0:
            raise ProcessExpectation(cmd, "!= 0", code)
    else:
        if code != exp:  # otherwise expect exact numeric code.
            raise ProcessExpectation(cmd, exp, code)

    return code, _decode(p_out), _decode(p_err)
Пример #35
0
Example:
    import PyGnuplot as gp
    import numpy as np
    X = np.arange(10)
    Y = np.sin(X/(2*np.pi))
    Z = Y**2.0
    gp.s([X,Y,Z])  # saves data into tmp.dat
    gp.c('plot "tmp.dat" u 1:2 w lp)  # send 'plot instructions to gnuplot'
    gp.c('replot "tmp.dat" u 1:3' w lp)
    gp.p('myfigure.ps')  # creates postscript file

'''
from subprocess import Popen as _Popen, PIPE as _PIPE
from numpy import array as _array, transpose as _transpose, savetxt as _savetxt
proc = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE)  # persitant -p


class _emptyClass(object):
    def __init__(self):
        self.figNum = [0]


_vc = _emptyClass()


def c(command):
    '''
    Send command to gnuplot
    >>> c('plot sin(x)')
    >>> c('plot "tmp.dat" u 1:2 w lp)