Exemplo n.º 1
0
    def checkRaspiTemp(self):
        from sarge import run, Capture

        self._logger.debug("Checking Raspberry Pi internal temperature")

        mem = 0
        if sys.platform == "linux2":
            p = run("/opt/vc/bin/vcgencmd measure_temp", stdout=Capture())
            p = p.stdout.text
            if self.displayRaspiMem:
                m = run("/home/pi/bin/memavail", stdout=Capture())
                mem = int(m.stdout.text)

        elif self.debugMode:
            import random

            def randrange_float(start, stop, step):
                return random.randint(0, int(
                    (stop - start) / step)) * step + start

            p = "temp=%s'C" % randrange_float(5, 60, 0.1)

        self._logger.debug("response from sarge: %s" % p)

        match = re.search('=(.*)\'', p)
        if not match:
            self.isRaspi = False
        else:
            temp = match.group(1)
            self._logger.debug("match: %s" % temp)
            self._plugin_manager.send_plugin_message(
                self._identifier,
                dict(israspi=self.isRaspi, raspitemp=temp, memavail=mem))
Exemplo n.º 2
0
 def upgrade_package(self):
     """
     Runs the upgrade command for the package given in a sub-process.
     """
     run(sys.executable + ' -m pip install ' + self.package + ' --upgrade',
         stdout=Capture(),
         stderr=Capture())
Exemplo n.º 3
0
 def install_package(self):
     """
     Runs the install command for the package given in a sub-process.
     """
     run(" ".join(self.install_command()),
         stdout=Capture(),
         stderr=Capture())
Exemplo n.º 4
0
 def uninstall_package(self):
     """
     Runs the uninstall command for the package given in a sub-process.
     """
     run(sys.executable + ' -m pip uninstall -y ' + self.package,
         stdout=Capture(),
         stderr=Capture())
Exemplo n.º 5
0
    def __init__(self, shell_binary, shell_argstr, return_code_echo_command,
                 command_separator, is_interactive):
        """Sets up the context for a system shell process.

        Parameters
        ----------
        shell_binary : str
            The shell binary, e.g. ``/bin/bash``, to start.
        shell_argstr : str
            Additional arguments to be passed to the shell at start.
        return_code_echo_command : str
            A command string in the shell's own language that prints to
            standard output the return code of the previously executed command.
        command_separator : str
            The character sequence to separate commands with.
        is_interactive : bool
            Whether the started shell is an interactive one.
            This does only change the behaviour of the context manager, to make
            the shell itself interactive, additional arguments in
            `shell_argstr` might need to be passed.
        """
        self._args = shell_argstr
        self._binary = shell_binary
        # Note: The execution of the command and the reading of the output
        # has to happen BEFORE this timeout is hit, but a large timeout would
        # also mean waiting a lot for small commands, so this has to be
        # balanced carefully.
        self._capture = Capture(timeout=0.5, buffer_size=-1)
        self._command = Command(shell_binary + ' ' + shell_argstr,
                                stdout=self._capture)
        self._echo = return_code_echo_command + command_separator
        self._interactive = is_interactive
        self._separator = command_separator
        self._started = False
def get_tags():
    global system_tags
    if system_tags:
        return system_tags

    (os, _, ver, _, arch, _) = platform.uname()
    tags = dict(os=os, os_ver=ver, arch=arch)
    try:
        v4l2 = run('v4l2-ctl --list-devices', stdout=Capture())
        v4l2_out = ''.join(
            re.compile(r"^([^\t]+)",
                       re.MULTILINE).findall(v4l2.stdout.text)).replace(
                           '\n', '')
        if v4l2_out:
            tags['v4l2'] = v4l2_out
    except:
        pass

    try:
        usb = run(
            "lsusb | cut -d ' ' -f 7- | grep -vE ' hub| Hub' | grep -v 'Standard Microsystems Corp'",
            stdout=Capture())
        usb_out = ''.join(usb.stdout.text).replace('\n', '')
        if usb_out:
            tags['usb'] = usb_out
    except:
        pass

    system_tags = tags
    return system_tags
Exemplo n.º 7
0
 def test_logical_or(self):
     with Capture() as out:
         with Pipeline('false || echo foo', stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().strip(), b'foo')
     with Capture() as out:
         with Pipeline('true || echo foo', stdout=out) as pl:
             pl.run()
     self.assertEqual(out.read().strip(), b'')
Exemplo n.º 8
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        cmd = 'gem list -i ' + self.package

        return not run(cmd, stdout=Capture(), stderr=Capture()).returncode
Exemplo n.º 9
0
    def checkRaspiTemp(self):
        from sarge import run, Capture

        self._logger.debug("Checking Raspberry Pi internal temperature")
        #do we really need to check platform == linux2? aren't these only called if the device has already
        #been determined to be compatible?
        if sys.platform == "linux2":
            if self.isAwinner:
                p = run(
                    "cat /etc/armbianmonitor/datasources/soctemp",
                    stdout=Capture()
                )  #this assumes an armbian OS, not sure if there's a universal way to check allwinner SoC temps on every possible OS
            elif self.isRaspi:
                p = run("/opt/vc/bin/vcgencmd measure_temp", stdout=Capture())
            elif self.isNano:
                p = run("cat /sys/class/hwmon/hwmon0/device/temp_label",
                        stdout=Capture())

            if p.returncode == 1:
                self.isAwinner = False
                self.isRaspi = False
                self._logger.info("SoC temperature not found.")
            else:
                p = p.stdout.text

        elif self.debugMode:  #doesn't work on linux
            import random

            def randrange_float(start, stop, step):
                return random.randint(0, int(
                    (stop - start) / step)) * step + start

            p = "temp=%s'C" % randrange_float(5, 60, 0.1)

        self._logger.debug("response from sarge: %s" % p)

        if self.isRaspi:
            match = re.search('=(.*)\'', p)
        elif self.isAwinner:
            match = re.search('(\d+)', p)
        elif self.isNano:
            match = re.search('(\d+)', p)
        if not match:
            self.isRaspi = False
            self.isAwinner = False
        else:
            if self.isAwinner:
                temp = float(match.group(1)) / 1000
            else:
                temp = match.group(1)
            self._logger.info("match: %s" % temp)
            self._plugin_manager.send_plugin_message(
                self._identifier,
                dict(israspi=self.isRaspi,
                     isawinner=self.isAwinner,
                     isnano=self.isNano,
                     raspitemp=temp))
Exemplo n.º 10
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        return not run('cabal info ' + self.package,
                       stdout=Capture(),
                       stderr=Capture()).returncode
Exemplo n.º 11
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        return not run(sys.executable + ' -m pip show ' + self.package,
                       stdout=Capture(),
                       stderr=Capture()).returncode
Exemplo n.º 12
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        cmd = 'gem list -i ' + self.package
        if platform.system() == 'Windows':  # pragma: no cover
            cmd = 'cmd /c ' + cmd
        return not run(cmd, stdout=Capture(), stderr=Capture()).returncode
Exemplo n.º 13
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        return not run(
            ('R -e \'library(\"{}\", quietly=TRUE)\''.format(self.package)),
            stdout=Capture(),
            stderr=Capture()).returncode
Exemplo n.º 14
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        return not run('mvn dependency:get -Dartifact={}:{} --offline'.format(
            self.package, self.version),
                       stdout=Capture(),
                       stderr=Capture()).returncode
Exemplo n.º 15
0
 def test_capture_bytes(self):
     with Capture() as err:
         self.assertEqual(
             run('cat >&2', stderr=err, shell=True, input='bar').returncode,
             0)
     self.assertEqual(err.bytes, b'bar')
     with Capture() as err:
         self.assertEqual(
             run('cat >&2', stderr=err, shell=True, input='bar').returncode,
             0)
     self.assertEqual(err.text, 'bar')
Exemplo n.º 16
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        cmd = 'luarocks show ' + self.package

        if not run(cmd, stdout=Capture(), stderr=Capture()).returncode:
            return True

        return False
Exemplo n.º 17
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        for cmd in ('npm list ' + self.package, 'npm list -g ' + self.package):

            if not run(cmd, stdout=Capture(), stderr=Capture()).returncode:
                return True

        return False
Exemplo n.º 18
0
def restart_server(**extra_env):
    global server, server_out, server_options
    for key in extra_env:
        extra_env[key] = str(extra_env[key])
    if server and server_options == extra_env:
        print("  Server already started with correct options")
        return
    stop_server()
    env = os.environ.copy()
    env.update(extra_env)
    env['PORT'] = '10180'
    env['SITE_ORIGIN'] = 'localhost:10180'
    env['CONTENT_ORIGIN'] = 'localhost:10180'
    env['GA_ID'] = ''
    env['DEBUG_GOOGLE_ANALYTICS'] = ''
    env['NODE_ENV'] = 'production'
    env['LOG_QUERY_LIMIT'] = '0'
    env['ENABLE_WATCHDOG'] = 'false'
    server_out = Capture()
    env_print = [
        '%s=%s' % (name, value) for name, value in sorted(extra_env.items())
    ]
    if env_print:
        env_print = ' (%s)' % ' '.join(env_print)
    else:
        env_print = ''
    print('  Starting server%s' % env_print)
    server = run('./bin/run-server --no-auto',
                 cwd=project_base,
                 env=env,
                 stdout=server_out,
                 async=True)
    server_options = extra_env
    time.sleep(3)
    text = []
    while True:
        if server.commands[0].process and server.commands[0].poll():
            print('    Server exited with code %s' % server.commands[0].poll())
            text.extend(server_out.readlines())
            for line in text:
                print('      %s' % line.rstrip())
            print("    %s" % ("-" * 60))
            raise Exception("Server didn't start")
        line = server_out.readline()
        if line:
            text.append(line)
        if 'Database is now at level' in line:
            # Last log message before the server is running
            break
        if 'skip-db-down-patches' in line:
            break
    print("  Server started")
Exemplo n.º 19
0
 def test_double_redirect(self):
     with Capture() as out:
         self.assertRaises(ValueError,
                           run,
                           'echo foo > %s' % os.devnull,
                           stdout=out)
     with Capture() as out:
         with Capture() as err:
             self.assertRaises(ValueError,
                               run,
                               'echo foo 2> %s' % os.devnull,
                               stdout=out,
                               stderr=err)
Exemplo n.º 20
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :return: ``True`` if dependency is installed, ``False`` otherwise.
        """
        # We need to check explicitly if `nothing` is returned, as this happens
        # when the package is *registered*, but *not installed*. If it's not
        # even registered, julia will throw an exception which lets julia exit
        # with an error code different from 0.
        code = 'Pkg.installed("{}")==nothing?exit(1):exit(0)'.format(
            escape(self.package, '\\"'))
        args = 'julia -e ' + code
        return not run(args, stdout=Capture(), stderr=Capture()).returncode
Exemplo n.º 21
0
    def test_run_task(self, sarge):
        p = mock.Mock()
        p.returncode = 0
        p.stdout = Capture()
        p.stdout.add_stream(BytesIO(b"testing testing 123"))
        p.stderr = Capture()
        p.stderr.add_stream(BytesIO(b"e"))
        sarge.Command.return_value = p

        self.task_config.config["options"] = {"command": "ls -la"}
        task = Command(self.project_config, self.task_config)
        task()

        assert any("testing testing" in s for s in self.task_log["info"])
Exemplo n.º 22
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        cmd = 'conda list {} | grep "^{}"'

        if not run(cmd.format(self.package, self.package),
                   stdout=Capture(),
                   stderr=Capture()).returncode:
            return True

        return False
Exemplo n.º 23
0
    def is_installed(self):
        """
        Checks if the dependency is installed.

        :param return: True if dependency is installed, false otherwise.
        """
        for cmd in ('npm list ' + self.package, 'npm list -g ' + self.package):

            if platform.system() == 'Windows':  # pragma: no cover
                cmd = 'cmd + /c ' + cmd

            if not run(cmd, stdout=Capture(), stderr=Capture()).returncode:
                return True

        return False
Exemplo n.º 24
0
def make_server():
    with Capture() as out:
        print("Running make server:")
        run('make server', cwd=project_base, stdout=out).wait()
        for line in out:
            print('  %s' % line.rstrip())
        print('make server completed')
Exemplo n.º 25
0
    def is_installed(self):
        """
        Check if the requirement is satisfied by calling various package
        managers that are mentioned.

        Raises NotImplementedError if executable is not defined when
        there's no supported package manager.

        :return: True if the dependency is installed, false otherwise
        """

        package_manager = self.get_available_package_manager()
        command = self.CHECKER_COMMANDS[package_manager]
        package = self.package[package_manager]
        return not run(command.format(package),
                       stdout=Capture(), stderr=Capture()).returncode
Exemplo n.º 26
0
    def check_soc_temp(self):
        if self.debugMode:
            import random
            return str(round(random.uniform(5, 60), 2))

        if self.is_supported:
            from sarge import run, Capture

            p = run(self.temp_cmd, stdout=Capture())
            if p.returncode == 1:
                self.is_supported = False
                self._logger.debug("SoC temperature not found.")
            else:
                p = p.stdout.text

            self._logger.debug("response from sarge: %s" % p)
            self._logger.debug("used pattern: %r" % self.parse_pattern)
            match = re.search(self.parse_pattern, p)
            temp = 0
            if not match:
                self._logger.debug("match: not found")
                self.is_supported = False
            else:
                temp = self.parse_temperature(match)
                self._logger.debug("match: %s" % str(temp))

            return temp

        return 0
Exemplo n.º 27
0
    def checkSoCTemp(self):
        if self.is_supported:
            from sarge import run, Capture
            
            #The following generate a log entry every 30 sec, not very good to write so much to the SD card. uncomment for debugging purposes.
            #self._logger.info("Checking SoC internal temperature")
            p = run(self.temp_cmd, stdout=Capture())
            if p.returncode == 1:
                self.is_supported = False
                self._logger.info("SoC temperature not found.")
            else:
                p = p.stdout.text

            # elif self.debugMode:  # doesn't work on linux
            #     import random
            #     def randrange_float(start, stop, step):
            #         return random.randint(0, int((stop - start) / step)) * step + start
            #
            #     p = "temp=%s'C" % randrange_float(5, 60, 0.1)

            self._logger.debug("response from sarge: %s" % p)
            self._logger.debug("used pattern: %r" % self.parse_pattern)
            match = re.search(self.parse_pattern, p)
            temp = 0
            if not match:
                self._logger.debug("match: not found")
                self.is_supported = False
            else:
                temp = self.parse_tepmerature(match)
                temp = (temp * (9/5)) + 32
                self._logger.debug("match: %s" % str(temp))

            return temp
        return 0
Exemplo n.º 28
0
    def check_temp(self):
        self.temp_cmd = '/opt/vc/bin/vcgencmd measure_temp'
        self.parse_pattern = '=(.*)\''
        from sarge import run, Capture
        import os.path
        try:
            soc_file = self._settings.get(["socfile"])
            if os.path.isfile(soc_file):
                p = run(self.temp_cmd, stdout=Capture())
                p = p.stdout.text
                match = re.search(self.parse_pattern, p)
            else:
                self._logger.error(
                    "SoCTemp: can't determine the temperature," +
                    " are you sure you're using RasPi?")
                return

            self.temp = match.group(1)

            self.set_text_color()
            self._plugin_manager.send_plugin_message(
                self._identifier,
                dict(soctemp=self.temp,
                     emoji=self._settings.get(["emoji"]),
                     color=self.color))
            self._logger.debug("SoCTemp REFRESH")
        except Exception as e:
            self._logger.warning("SoCTemp REFRESH FAILED: {0}".format(e))
Exemplo n.º 29
0
    def checkSoCTemp(self):
        if self.is_supported:
            from sarge import run, Capture

            self._logger.info("Checking SoC internal temperature")
            p = run(self.temp_cmd, stdout=Capture())
            if p.returncode == 1:
                self.is_supported = False
                self._logger.info("SoC temperature not found.")
            else:
                p = p.stdout.text

            # elif self.debugMode:  # doesn't work on linux
            #     import random
            #     def randrange_float(start, stop, step):
            #         return random.randint(0, int((stop - start) / step)) * step + start
            #
            #     p = "temp=%s'C" % randrange_float(5, 60, 0.1)

            self._logger.debug("response from sarge: %s" % p)
            self._logger.debug("used pattern: %r" % self.parse_pattern)
            match = re.search(self.parse_pattern, p)
            temp = 0
            if not match:
                self._logger.debug("match: not found")
                self.is_supported = False
            else:
                temp = self.parse_tepmerature(match)
                self._logger.debug("match: %s" % str(temp))

            return temp
        return 0
Exemplo n.º 30
0
 def check_temp(self):
     from sarge import run, Capture
     import os.path
     try:
         soc_file = self._settings.get(["socfile"])
         if os.path.isfile(soc_file):
             p = run("cat " + soc_file, stdout=Capture())
             p = p.stdout.text
             match = re.search(r"(\d+)", p)
         else:
             self._logger.error(
                 "OpiTemp: can't determine the temperature," +
                 " are you sure you're using Armbian?")
             return
         # lazy fix for #3, yeah I known...
         if platform.release().startswith(
                 "4") or platform.release().startswith("5"):
             self.temp = "{0:.1f}".format(float(match.group(1)) / 1000)
         elif platform.release().startswith("3"):
             self.temp = match.group(1)
         else:
             self._logger.warning("OpiTemp: unknown kernel version!")
             self.temp = 0
         self.set_text_color()
         self._plugin_manager.send_plugin_message(
             self._identifier,
             dict(soctemp=self.temp,
                  emoji=self._settings.get(["emoji"]),
                  color=self.color))
         self._logger.debug("OpiTemp REFRESH")
     except Exception as e:
         self._logger.warning("OpiTemp REFRESH FAILED: {0}".format(e))
def restart_server(**extra_env):
    global server, server_out, server_options
    for key in extra_env:
        extra_env[key] = str(extra_env[key])
    if server and server_options == extra_env:
        print("  Server already started with correct options")
        return
    stop_server()
    env = os.environ.copy()
    env.update(extra_env)
    env['PORT'] = '10180'
    env['SITE_ORIGIN'] = 'localhost:10180'
    env['CONTENT_ORIGIN'] = 'localhost:10180'
    env['GA_ID'] = ''
    env['NODE_ENV'] = 'production'
    env['LOG_QUERY_LIMIT'] = '0'
    env['ENABLE_WATCHDOG'] = 'false'
    server_out = Capture()
    env_print = ['%s=%s' % (name, value) for name, value in sorted(extra_env.items())]
    if env_print:
        env_print = ' (%s)' % ' '.join(env_print)
    else:
        env_print = ''
    print('  Starting server%s' % env_print)
    server = run('./bin/run-server --no-auto', cwd=project_base, env=env, stdout=server_out, async_=True)
    server_options = extra_env
    time.sleep(3)
    text = []
    while True:
        if server.commands[0].process and server.commands[0].poll():
            print('    Server exited with code %s' % server.commands[0].poll())
            text.extend(server_out.readlines())
            for line in text:
                print('      %s' % line.rstrip())
            print("    %s" % ("-" * 60))
            raise Exception("Server didn't start")
        line = server_out.readline()
        if line:
            text.append(line)
        if 'Database is now at level' in line:
            # Last log message before the server is running
            break
        if 'skip-db-down-patches' in line:
            break
    print("  Server started")
Exemplo n.º 32
0
 def test_expect(self):
     cap = Capture(buffer_size=-1)  # line buffered
     p = run("%s lister.py -d 0.01" % sys.executable, async=True, stdout=cap)
     timeout = 1.0
     m1 = cap.expect("^line 1\r?$", timeout)
     self.assertTrue(m1)
     m2 = cap.expect("^line 5\r?$", timeout)
     self.assertTrue(m2)
     m3 = cap.expect("^line 1.*\r?$", timeout)
     self.assertTrue(m3)
     cap.close(True)
     p.commands[0].kill()
     data = cap.bytes
     self.assertEqual(data[m1.start() : m1.end()].rstrip(), b"line 1")
     self.assertEqual(data[m2.start() : m2.end()].rstrip(), b"line 5")
     self.assertEqual(data[m3.start() : m3.end()].rstrip(), b"line 10")
Exemplo n.º 33
0
import logging
from sarge import run, Capture
import time

logger = logging.getLogger(__name__)

logging.basicConfig(filename='test_expect.log', filemode='w',
                    level=logging.INFO,
                    format='%(asctime)s %(levelname)-8s %(name)s %(threadName)s %(lineno)4d %(message)s')
cap = Capture(buffer_size=-1)   # line buffered
p = run('python lister.py -d 0.01', async=True,
        stdout=cap)

WAIT_TIME = 1.0

def do_expect(pattern, timeout=None):
    stime = time.time()
    cap.expect(pattern, timeout or WAIT_TIME)
    elapsed = time.time() - stime
    if not cap.match:
        print('%r not found within time limit.' % pattern)
        result = False
    else:
        print('%r found at %s in %.1f seconds.' % (pattern, cap.match.span(),
                                                   elapsed))
        result = True
    return result

if do_expect('line 1$'):
    if do_expect('line 5$'):
        if do_expect('line 1.*$'):
Exemplo n.º 34
0
import logging
from sarge import run, Capture
import time

logger = logging.getLogger(__name__)

logging.basicConfig(filename='test_expect.log', filemode='w',
                    level=logging.INFO,
                    format='%(asctime)s %(levelname)-8s %(name)s %(threadName)s %(lineno)4d %(message)s')
cap = Capture(buffer_size=-1)   # line buffered
p = run('python lister.py -d 0.01 -i "<head|body>" docs/_build/html/tutorial.html', async=True,
        stdout=cap)
stime = time.time()
logger.info('Calling expect for head')
cap.expect('<head>', 60.0)
logger.info('Returned from expect for head')
elapsed = time.time() - stime
if not cap.match:
    print('<head> not found within time limit.')
else:
    print('<head> found at %s in %.1f seconds.' % (cap.match.span(), elapsed))
    stime = time.time()
    logger.info('Calling expect for body')
    cap.expect('<body>', 60.0)
    logger.info('Returned from expect for body')
    elapsed = time.time() - stime
    if not cap.match:
        print('<body> not found within time limit.')
    else:
        print('<body> found at %s in %.1f seconds.' % (cap.match.span(), elapsed))
logger.debug('Killing subprocess')