예제 #1
0
def initialisingPath():
    if not sys_utils.isWin():
        addition_paths = [
            '/opt/local/bin', '/opt/local/sbin', '/usr/local/bin'
        ]
        os.environ['PATH'] = os.environ['PATH'] + os.pathsep + os.pathsep.join(
            addition_paths)
예제 #2
0
def getCurrentDockerURL():
    try:
        return getPreference(PROTOCOL_KEY).value
    except DoesNotExist:
        if sys_utils.isWin():
            return 'tcp://127.0.0.1:2375'
        else:
            return 'unix://var/run/docker.sock'
def runAsAdmin(argv):
    commands = []
    if sys_utils.isMac():
        # For MacOS, we will use osascript for asking privileges permission
        commands.append([
            "osascript", "-e",
            "do shell script " + quoteAppleScript(quoteShell(argv)) +
            " with administrator privileges"
        ])
    elif sys_utils.isLinux():
        # For Linux, there are many different distro, so, we will try each of them
        # If all are failed, the fall back to sudo
        if os.environ.get("DISPLAY"):
            commands.append(["pkexec"] + argv)
            commands.append(["gksudo"] + argv)
            commands.append(["kdesudo"] + argv)
        commands.append(["sudo"] + argv)
    elif sys_utils.isWin():
        # For window machine, we expect to have the script to ask for permission inside the .bat file already
        commands.append(argv)
    else:
        raise NotImplementedError('Unable to recognise platform %s' %
                                  sys.platform)
    for command in commands:
        try:
            if sys_utils.isWin():
                return subprocess.call(command,
                                       stdin=None,
                                       stdout=None,
                                       stderr=None,
                                       shell=False,
                                       creationflags=CREATE_NO_WINDOW)
            else:
                return subprocess.call(command,
                                       stdin=None,
                                       stdout=None,
                                       stderr=None,
                                       shell=False)
        except OSError as e:
            if e.errno != errno.ENOENT or command[0] == "sudo":
                raise e
예제 #4
0
def getPhysicalMemory():
    global total_mem
    if total_mem:
        return total_mem / (1024.**2)
    if sys_utils.isWin():
        res = subprocess.run(
            ['wmic', 'ComputerSystem', 'get', 'TotalPhysicalMemory'],
            stdout=subprocess.PIPE)
        output = res.stdout.decode('utf-8')
        total = int(output.replace('TotalPhysicalMemory', '').strip())
    elif sys_utils.isLinux():
        mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf(
            'SC_PHYS_PAGES')  # e.g. 4015976448
        total = mem_bytes
    else:
        res = subprocess.run(['sysctl', '-n', 'hw.memsize'],
                             stdout=subprocess.PIPE)
        output = res.stdout.decode('utf-8')
        total = int(output.strip())
    total_mem = total
    return total / (1024.**2)
예제 #5
0
    def fromJson(object_json):

        version = object_json['tag_name']
        release = Release(version)
        release.changelog = object_json['body']

        assets = object_json["assets"]

        if sys_utils.isMac():
            # For mac, always x64
            plf = 'macOS'
        elif sys_utils.isWin():
            # For windows, depending on the architecture x32/x64. But we always looking for the portable version of it
            plf = 'win-%s-portable' % sys_utils.getArchitecture()
        else:
            plf = 'linux-%s' % sys_utils.getArchitecture()

        for asset in assets:
            download_info = asset
            if plf in download_info['name']:
                release.download_url = download_info["browser_download_url"]
                release.download_size = download_info["size"]
        return release
예제 #6
0
def startTerminalWithCommand(command):
    if sys_utils.isMac():
        term = resources_utils.getExternalResource('run_with_mac_terminal.sh')
        if os.path.exists("/Applications/iTerm.app"):
            term = resources_utils.getExternalResource('run_with_iterm.sh')
        os.system('chmod u+x ' + term)
        os.system("%s \"%s\" &" % (term, command))
    elif sys_utils.isWin():
        os.system("start cmd /c %s" % command)
    else:
        default_linux_term = 'xterm'
        if os.path.exists('/etc/debian_version'):
            default_linux_term = 'x-terminal-emulator'
        elif os.path.exists('/usr/bin/xfce4-terminal'):
            default_linux_term = 'xfce4-terminal'
        elif os.environ['DESKTOP_SESSION'] == 'gnome':
            default_linux_term = 'gnome-terminal'
        elif os.environ['DESKTOP_SESSION'] == 'kde-plasma':
            default_linux_term = 'konsole'
        elif 'COLORTERM' in os.environ:
            default_linux_term = os.environ['COLORTERM']
        elif 'TERM' in os.environ:
            default_linux_term = os.environ['TERM']
        os.system("%s -e %s &" % (default_linux_term, command))
예제 #7
0
logger.setLevel(logging.INFO)
# create file handler which logs even debug messages
fh = logging.FileHandler(log_file)
fh.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter(
    '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
    "%Y-%m-%d %H:%M:%S")
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(fh)


class LoggerWriter:
    def __init__(self, _logger, level):
        self.logger = _logger
        self.level = level

    def write(self, message):
        if message != '\n':
            self.logger.log(self.level, message)

    def flush(self):
        pass


if sys_utils.isWin() and 'BOATSWAIN_DEBUG' not in os.environ:
    # For windows, all logs must not be printed into stdout/stderr when building .exe release
    sys.stdout = LoggerWriter(logger, logging.INFO)
    sys.stderr = LoggerWriter(logger, logging.ERROR)