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())
 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())
 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())
Beispiel #4
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))
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
Beispiel #6
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'')
    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
Beispiel #8
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))
Beispiel #9
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
Beispiel #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
Beispiel #11
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
Beispiel #12
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
    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
Beispiel #14
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')
Beispiel #15
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
    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
Beispiel #17
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)
Beispiel #18
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
Beispiel #19
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"])
Beispiel #20
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
Beispiel #21
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
Beispiel #22
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
Beispiel #23
0
def get_data(remote, path, begin, end):
    # TODO: exponential increase/backoff
    # make this configurable
    chunk_size = 1000000
    current_pointer = begin

    while current_pointer <= end:
        if current_pointer + chunk_size > end:
            chunk_size = end - current_pointer
        elif current_pointer == end:
            chunk_size = 1

        # TODO: unsure if the math here is right
        cmd = 'rclone cat "' + remote + path + '" --offset ' + str(
            current_pointer) + ' --count ' + str(chunk_size)
        print(cmd)
        print(begin, end, current_pointer, chunk_size)
        current_pointer += chunk_size

        #data = execute_rclone(cmd)
        p = run(cmd, stdout=Capture(), async_=False)
        data = p.stdout.read(100000)
        while data:
            print('Read chunk: %d bytes' % (len(data)))
            yield data
            data = p.stdout.read(100000)
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')
Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #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))
Beispiel #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
Beispiel #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))