Beispiel #1
0
    def run(self, cmd="", file_regex="", path=""):

        # Try to handle killing
        with self.proc_lock:
            if self.proc:  # if we are running, try to kill running process
                self.output("\n\n### Got request to terminate compilation ###")
                if sublime.platform() == 'windows':
                    startupinfo = subprocess.STARTUPINFO()
                    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                    subprocess.call(
                        'taskkill /t /f /pid {pid}'.format(pid=self.proc.pid),
                        startupinfo=startupinfo,
                        shell=True)
                else:
                    os.killpg(self.proc.pid, signal.SIGTERM)
                self.proc = None
                return
            else:  # either it's the first time we run, or else we have no running processes
                self.proc = None

        view = self.view = self.window.active_view()

        if view.is_dirty():
            print("saving...")
            view.run_command('save')  # call this on view, not self.window

        if view.file_name() is None:
            sublime.error_message(
                'Please save your file before attempting to build.')
            return

        self.file_name = getTeXRoot.get_tex_root(view)
        if not os.path.isfile(self.file_name):
            sublime.error_message(self.file_name + ": file not found.")
            return

        self.tex_base = get_jobname(view)
        self.tex_dir = os.path.dirname(self.file_name)

        if not is_tex_file(self.file_name):
            sublime.error_message(
                "%s is not a TeX source file: cannot compile." %
                (os.path.basename(view.file_name()), ))
            return

        # Output panel: from exec.py
        if not hasattr(self, 'output_view'):
            self.output_view = self.window.get_output_panel("latextools")

        output_view_settings = self.output_view.settings()
        output_view_settings.set("result_file_regex", file_regex)
        output_view_settings.set("result_base_dir", self.tex_dir)
        output_view_settings.set("line_numbers", False)
        output_view_settings.set("gutter", False)
        output_view_settings.set("scroll_past_end", False)

        if get_setting("highlight_build_panel", True):
            self.output_view.set_syntax_file(
                "Packages/LaTeXTools/LaTeXTools Console.hidden-tmLanguage")
            output_view_settings.set(
                "color_scheme",
                sublime.load_settings('Preferences.sublime-settings').get(
                    'color_scheme'))

        self.output_view.set_read_only(True)

        # Dumb, but required for the moment for the output panel to be picked
        # up as the result buffer
        self.window.get_output_panel("latextools")

        self.hide_panel_level = get_setting("hide_build_panel", "never")
        if self.hide_panel_level != "always":
            self.window.run_command("show_panel",
                                    {"panel": "output.latextools"})

        self.plat = sublime.platform()
        if self.plat == "osx":
            self.encoding = "UTF-8"
        elif self.plat == "windows":
            self.encoding = getOEMCP()
        elif self.plat == "linux":
            self.encoding = "UTF-8"
        else:
            sublime.error_message("Platform as yet unsupported. Sorry!")
            return

        # Get platform settings, builder, and builder settings
        platform_settings = get_setting(self.plat, {})
        builder_name = get_setting("builder", "traditional")
        self.display_bad_boxes = get_setting("display_bad_boxes", False)
        # This *must* exist, so if it doesn't, the user didn't migrate
        if builder_name is None:
            sublime.error_message(
                "LaTeXTools: you need to migrate your preferences. See the README file for instructions."
            )
            self.window.run_command('hide_panel',
                                    {"panel": "output.latextools"})
            return

        # Default to 'traditional' builder
        if builder_name in ['', 'default']:
            builder_name = 'traditional'

        # this is to convert old-style names (e.g. AReallyLongName)
        # to new style plugin names (a_really_long_name)
        builder_name = _classname_to_internal_name(builder_name)

        builder_settings = get_setting("builder_settings", {})

        # parse root for any %!TEX directives
        tex_directives = parse_tex_directives(
            self.file_name,
            multi_values=['options'],
            key_maps={'ts-program': 'program'})

        # determine the engine
        engine = tex_directives.get(
            'program', builder_settings.get("program", "pdflatex"))

        engine = engine.lower()

        # Sanity check: if "strange" engine, default to pdflatex (silently...)
        if engine not in [
                'pdflatex', "pdftex", 'xelatex', 'xetex', 'lualatex', 'luatex'
        ]:
            engine = 'pdflatex'

        options = builder_settings.get("options", [])
        if isinstance(options, strbase):
            options = [options]

        if 'options' in tex_directives:
            options.extend(tex_directives['options'])

        # filter out --aux-directory and --output-directory options which are
        # handled separately
        options = [
            opt for opt in options
            if (not opt.startswith('--aux-directory') and not opt.startswith(
                '--output-directory') and not opt.startswith('--jobname'))
        ]

        self.aux_directory = get_aux_directory(self.file_name)
        self.output_directory = get_output_directory(self.file_name)

        # Read the env option (platform specific)
        builder_platform_settings = builder_settings.get(self.plat)
        if builder_platform_settings:
            self.env = builder_platform_settings.get("env", None)
        else:
            self.env = None

        # Now actually get the builder
        builder_path = get_setting("builder_path",
                                   "")  # relative to ST packages dir!

        # Safety check: if we are using a built-in builder, disregard
        # builder_path, even if it was specified in the pref file
        if builder_name in ['simple', 'traditional', 'script', 'basic']:
            builder_path = None

        if builder_path:
            bld_path = os.path.join(sublime.packages_path(), builder_path)
            add_plugin_path(bld_path)

        try:
            builder = get_plugin('{0}_builder'.format(builder_name))
        except NoSuchPluginException:
            sublime.error_message("Cannot find builder " + builder_name + ".\n" \
                      "Check your LaTeXTools Preferences")
            self.window.run_command('hide_panel',
                                    {"panel": "output.latextools"})
            return

        print(repr(builder))
        self.builder = builder(self.file_name, self.output, engine, options,
                               self.aux_directory, self.output_directory,
                               self.tex_base, tex_directives, builder_settings,
                               platform_settings)

        # Now get the tex binary path from prefs, change directory to
        # that of the tex root file, and run!
        self.path = platform_settings['texpath']
        CmdThread(self).start()
        print(threading.active_count())
Beispiel #2
0
def launchWithoutConsole(command, args):
    """Launches 'command' windowless and waits until finished"""
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    return subprocess.Popen([command] + args, startupinfo=startupinfo).wait()
Beispiel #3
0
    return cmd, env

##########################################################################

processingSrc = "rec.wav"
processingDst = "rec.mp3"
processingChain = []
recFiles = []

processingChain = [
    ["lame", processingSrc, processingDst, "--noreplaygain", "--quiet"],
    ]

# don't show box on windows
if isWin:
    si = subprocess.STARTUPINFO()
    try:
        si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    except:
        # pylint: disable=no-member
        # python2.7+
        si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
else:
    si = None

def retryWait(proc):
    # osx throws interrupted system call errors frequently
    while 1:
        try:
            return proc.wait()
        except OSError:
def parse_file_structure(fi):
    """
    Parsing file structure from selected region (the whole file if not selected)
    """
    if fi.getDocumentCount() == 0:
        return

    length = fi.getSelectionLength()
    offset = fi.getSelectionOffset()

    if length > 0:
        data = fi.getSelection()
    else:
        offset = 0
        data = fi.getDocument()
        length = fi.getLength()

    # Do not show command prompt window
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    # Structure for mouse cursor position
    class _point_t(ctypes.Structure):
        _fields_ = [
            ("x", ctypes.c_long),
            ("y", ctypes.c_long),
        ]

    # Get DPI values
    DEFAULT_DPI = 96
    LOGPIXELSX = 88
    LOGPIXELSY = 90
    dc = ctypes.windll.user32.GetDC(0)
    dpi_x = ctypes.windll.gdi32.GetDeviceCaps(dc, LOGPIXELSX)
    dpi_y = ctypes.windll.gdi32.GetDeviceCaps(dc, LOGPIXELSY)
    ctypes.windll.user32.ReleaseDC(0, dc)

    # Get mouse cursor position
    point = _point_t()
    ctypes.windll.user32.GetCursorPos(ctypes.pointer(point))
    point.x = point.x * DEFAULT_DPI / dpi_x
    point.y = point.y * DEFAULT_DPI / dpi_y

    # Show menu
    p = subprocess.Popen([
        "py.exe", "-3", "Parsing/parse_file_structure_menu.py",
        str(point.x),
        str(point.y)
    ],
                         startupinfo=startupinfo,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    # Receive selection
    stdout_data, stderr_data = p.communicate()
    ret = p.wait()

    if stdout_data == "":
        return
    else:
        parser = stdout_data

    # Execute parse_file_structure.py to parse data
    p = subprocess.Popen(
        ["py.exe", "-3", "Parsing/parse_file_structure.py", parser],
        startupinfo=startupinfo,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

    # Receive scan result
    stdout_data, stderr_data = p.communicate(data)
    ret = p.wait()

    if ret == 1:
        print("Error: parse failed.")
        print(stderr_data)
        m = re.search(r": at pos (\d+):", stderr_data)
        if not m == None:
            failed_pos = int(m.group(1))
            fi.setBookmark(
                offset + failed_pos, 1,
                hex(offset + failed_pos) + " position of parse error",
                "#ff0000")
            print("Added bookmark to the position of parse error.")
        return

    if fi.getSelectionLength() > 0:
        print("Parsed from offset %s to %s as %s." %
              (hex(offset), hex(offset + length), parser))
    else:
        print("Parsed the whole file as %s." % parser)

    parsed_dict = json.loads(stdout_data,
                             object_pairs_hook=collections.OrderedDict)
    parsed_dict = collections.OrderedDict(
        sorted(parsed_dict.items(), key=lambda x: x[1]["start"]))
    i = 0
    parsed_data_list = []
    parsed_dict_len = len(parsed_dict)

    if parsed_dict_len > 100 and not bookmark_yesno_dialog(parsed_dict_len):
        do_bookmark = False
    else:
        do_bookmark = True

    for k in parsed_dict.keys():
        if do_bookmark:
            # Adjust start offset for one byte data
            if parsed_dict[k]["start"] - parsed_dict[k]["end"] == 1:
                parsed_dict[k]["start"] -= 1

            if parsed_dict[k]["start"] < 0:
                parsed_dict[k]["start"] = 0

            if parsed_dict[k]["end"] < 0:
                parsed_dict[k]["end"] = 0

            if i % 2 == 0:
                fi.setBookmark(
                    offset + parsed_dict[k]["start"],
                    parsed_dict[k]["end"] - parsed_dict[k]["start"] + 1,
                    hex(offset + parsed_dict[k]["start"]) + " " + str(k),
                    "#6d6dff")
            else:
                fi.setBookmark(
                    offset + parsed_dict[k]["start"],
                    parsed_dict[k]["end"] - parsed_dict[k]["start"] + 1,
                    hex(offset + parsed_dict[k]["start"]) + " " + str(k),
                    "#9f9fff")

        parsed_data_list.append((
            ("%s - %s: %s -> %s\n" %
             (hex(offset + parsed_dict[k]["start"]),
              hex(offset + parsed_dict[k]["end"]), k, parsed_dict[k]["data"])),
            parsed_dict[k]["start"], parsed_dict[k]["end"]))
        i += 1

    parsed_data_list.sort(key=lambda x: (x[1], x[2], x[0]))

    parsed_data = ""
    for p in parsed_data_list:
        parsed_data += p[0]

    if do_bookmark:
        print("Added bookmarks to the parsed data structure.")
    else:
        print("Skipped bookmarking.")

    fi.newDocument("Parsed data", 0)

    fi.setDocument(parsed_data)
    print('Parsed data is shown in the new "Parsed data" tab.')
    print(
        'Please use "Windows" tab -> "New Vertical Tab Group" to see parsed data and file contents side by side.'
    )
    print(stderr_data)
def disassemble(fi):
    """
    Disassemble selected region (the whole file if not selected)
    """
    if fi.getDocumentCount() == 0:
        return

    length = fi.getSelectionLength()
    offset = fi.getSelectionOffset()

    if length > 0:
        data = fi.getSelection()
    else:
        offset = 0
        data = fi.getDocument()
        length = fi.getLength()

    # Do not show command prompt window
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    # Execute disassemble_dialog.py to show GUI
    p = subprocess.Popen(["py.exe", "-3", "Parsing/disassemble_dialog.py"],
                         startupinfo=startupinfo,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    stdout_data, stderr_data = p.communicate()
    ret = p.wait()

    # Capstone is not installed
    if ret == -1:
        print("Capstone is not installed.")
        print("Please install it with 'py.exe -3 -m pip install capstone'.")
        print("")
        return

    # dialog is closed
    if stdout_data == "":
        return

    # Get parameters from disassemble_dialog.py
    (arch, mode) = stdout_data.split("\t")
    disasm_setting = stderr_data

    # Create a temporary file to write data
    fd, file_path = tempfile.mkstemp()
    handle = os.fdopen(fd, "wb")
    handle.write(data)
    handle.close()

    p = subprocess.Popen([
        "py.exe", "-3", "Parsing/disassemble.py", file_path,
        str(offset), arch, mode
    ],
                         startupinfo=startupinfo,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    # Receive disassembly result
    stdout_data, stderr_data = p.communicate()
    ret = p.wait()

    os.remove(file_path)  # Cleanup temporary file

    # disassembly.py exited with error
    if ret == 1:
        print(stderr_data.replace("\x0d\x0a", "\x0a")),
        return
    elif ret == -1:  # Capstone is not installed
        print("Capstone is not installed.")
        print("Please install it with 'py.exe -3 -m pip install capstone'.")
        print("")
        return

    if fi.getSelectionLength() > 0:
        print("Disassembled from offset %s to %s." %
              (hex(offset), hex(offset + length)))
    else:
        print("Disassembled the whole file.")
    print('Disassembly code is shown in the new "Disassembly" tab.')

    # Show disassembly settings
    print(disasm_setting),

    if not stderr_data == "":
        end_pos = int(stderr_data)
        fi.setBookmark(end_pos, 1,
                       hex(end_pos) + " end of disassembly", "#ff0000")
        print("Disassembly finished prematurely at offset %s." % hex(end_pos))
        print("Added bookmark to the end of disassembly.")

    fi.newDocument("Disassembly")
    fi.setDocument("".join(stdout_data))
Beispiel #6
0
def run_command(cmd, verbose=True, shell=False, hide_window=False, d=None):
    """
    run command in a subprocess
    :param cmd: string of actual command to be executed
    :param verbose: if true will re-route subprocess output to log()
    :param shell: True or False
    :param hide_window: True or False, hide shell window
    :param d: DownloadItem object mainly use "status" property to terminate subprocess
    :return: error (True or False), output (string of stdout/stderr output)
    """

    # override shell parameter currently can't kill subprocess if shell=True at least on windows, more investigation required
    shell = False

    if verbose:
        log('running command:', cmd)

    error, output = True, f'error running command {cmd}'

    try:

        # split command if shell parameter set to False
        if not shell:
            cmd = shlex.split(cmd)

        # startupinfo to hide terminal window on windows
        if hide_window and config.operating_system == 'Windows':
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = subprocess.SW_HIDE
        else:
            startupinfo = None

        # start subprocess using Popen instead of subprocess.run() to get a real-time output
        # since run() gets the output only when finished
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf-8',
                                   errors='replace', shell=shell, startupinfo=startupinfo)

        # update reference in download item, it will be cancelled with status, see DownloadItem.status property setter
        if d:
            d.subprocess = process

        output = ''

        for line in process.stdout:
            line = line.strip()
            output += line
            if verbose:
                log(line)

            # # monitor kill switch
            # if d and d.status == config.Status.cancelled:
            #     log('terminate run_command()>', cmd)
            #     process.kill()
                # return 1, 'Cancelled by user'

        # wait for subprocess to finish, process.wait() is not recommended
        process.communicate()

        # get return code
        process.poll()
        error = process.returncode != 0  # True or False

    except Exception as e:
        log('error running command: ', e, ' - cmd:', cmd)

    return error, output
def find_pe_file(fi):
    """
    Find PE file from selected region (the whole file if not selected) based on PE header information
    """
    if fi.getDocumentCount() == 0:
        return

    length = fi.getSelectionLength()
    offset = fi.getSelectionOffset()

    if length > 0:
        data = fi.getSelection()
    else:
        offset = 0
        data = fi.getDocument()
        length = fi.getLength()

    # Do not show command prompt window
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    # Execute find_pe_file.py for finding PE files
    p = subprocess.Popen(
        ["py.exe", "-3", "Parsing/find_pe_file.py",
         str(offset)],
        startupinfo=startupinfo,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)

    # Receive scan result
    stdout_data, stderr_data = p.communicate(data)
    ret = p.wait()

    if ret == -1:
        print("pefile is not installed.")
        print(
            "Please install it with 'py.exe -3 -m pip install pefile' and try again."
        )
        return

    found = ret
    print(stdout_data),

    for l in stdout_data.splitlines():
        if l[0:5] == "Win32" or l[0:5] == "Win64":
            off = int(l.split()[5], 0)
            size = int(l.split()[7], 0)
            if off + size > length:
                fi.setBookmark(off, length - off, hex(off), "#c8ffff")
            else:
                fi.setBookmark(off, size, hex(off), "#c8ffff")

    if fi.getSelectionLength() > 0:
        if found > 0:
            print("%d PE file(s) found from offset %s to %s." %
                  (found, hex(offset), hex(offset + length - 1)))
            print("Added bookmark(s) to the found PE file(s).")
        else:
            print("No PE file found from offset %s to %s." %
                  (hex(offset), hex(offset + length - 1)))
    else:
        if found > 0:
            print("%d PE file(s) found from the whole file." % found)
            print("Added bookmark(s) to the found PE file(s).")
        else:
            print("No PE file found from the whole file.")
Beispiel #8
0
def command(encoder, musicSource, musicDest, albumPath):

    cmd = []
    startMusicTime = time.time()

    if XLD:
        xldDestDir = os.path.split(musicDest)[0]
        cmd = [encoder]
        cmd.extend([musicSource])
        cmd.extend(['--profile'])
        cmd.extend([xldProfile])
        cmd.extend(['-o'])
        cmd.extend([xldDestDir])

    elif headphones.ENCODER == 'lame':
        cmd = [encoder]
        opts = []
        if headphones.ADVANCEDENCODER == '':
            opts.extend(['-h'])
            if headphones.ENCODERVBRCBR == 'cbr':
                opts.extend([
                    '--resample',
                    str(headphones.SAMPLINGFREQUENCY), '-b',
                    str(headphones.BITRATE)
                ])
            elif headphones.ENCODERVBRCBR == 'vbr':
                opts.extend(['-v', str(headphones.ENCODERQUALITY)])
        else:
            advanced = (headphones.ADVANCEDENCODER.split())
            for tok in advanced:
                opts.extend([tok.encode(headphones.SYS_ENCODING)])
        opts.extend([musicSource])
        opts.extend([musicDest])
        cmd.extend(opts)

    elif headphones.ENCODER == 'ffmpeg':
        cmd = [encoder, '-i', musicSource]
        opts = []
        if headphones.ADVANCEDENCODER == '':
            if headphones.ENCODEROUTPUTFORMAT == 'ogg':
                opts.extend(['-acodec', 'libvorbis'])
            if headphones.ENCODEROUTPUTFORMAT == 'm4a':
                opts.extend(['-strict', 'experimental'])
            if headphones.ENCODERVBRCBR == 'cbr':
                opts.extend([
                    '-ar',
                    str(headphones.SAMPLINGFREQUENCY), '-ab',
                    str(headphones.BITRATE) + 'k'
                ])
            elif headphones.ENCODERVBRCBR == 'vbr':
                opts.extend(['-aq', str(headphones.ENCODERQUALITY)])
            opts.extend(['-y', '-ac', '2', '-vn'])
        else:
            advanced = (headphones.ADVANCEDENCODER.split())
            for tok in advanced:
                opts.extend([tok.encode(headphones.SYS_ENCODING)])
        opts.extend([musicDest])
        cmd.extend(opts)

    # Encode

    logger.info('Encoding %s...' %
                (musicSource.decode(headphones.SYS_ENCODING, 'replace')))
    logger.debug(subprocess.list2cmdline(cmd))

    # stop windows opening the cmd
    startupinfo = None
    if headphones.SYS_PLATFORM == "win32":
        startupinfo = subprocess.STARTUPINFO()
        try:
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        except AttributeError:
            startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW

    p = subprocess.Popen(cmd,
                         startupinfo=startupinfo,
                         stdin=open(os.devnull, 'rb'),
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    stdout, stderr = p.communicate(headphones.ENCODER)

    # error if return code not zero
    if p.returncode:
        logger.error('Encoding failed for %s' %
                     (musicSource.decode(headphones.SYS_ENCODING, 'replace')))
        out = stdout if stdout else stderr
        out = out.decode(headphones.SYS_ENCODING, 'replace')
        outlast2lines = '\n'.join(out.splitlines()[-2:])
        logger.error('%s error details: %s' %
                     (headphones.ENCODER, outlast2lines))
        out = out.rstrip("\n")
        logger.debug(out)
        encoded = False
    else:
        logger.info('%s encoded in %s' %
                    (musicSource.decode(headphones.SYS_ENCODING, 'replace'),
                     getTimeEncode(startMusicTime)))
        encoded = True

    return encoded
Beispiel #9
0
def win_firefox_restart(message):
    """Handle 'win_firefox_restart' message."""
    reply = {}
    profile_dir = None
    browser_cmd = None

    try:
        profile_dir = message["profiledir"].strip()
        browser_cmd = message["browsercmd"].strip()
    except KeyError:
        reply = {
            "code": -1,
            "cmd": "error",
            "error": "Error parsing 'restart' message.",
        }
        return reply

    if (
        profile_dir
        and profile_dir != "auto"
        and not is_valid_firefox_profile(profile_dir)
    ):
        reply = {
            "code": -1,
            "cmd": "error",
            "error": "%s %s %s"
            % (
                "Invalid profile directory specified.",
                "Vaild profile directory path(s) can be found by",
                "navigating to 'about:support'.",
            ),
        }

    elif browser_cmd and not is_command_on_path(browser_cmd):
        reply = {
            "code": -1,
            "cmd": "error",
            "error": "%s %s %s"
            % (
                "'{0}' wasn't found on %PATH%.".format(browser_cmd),
                "Please set valid browser by",
                "'set browser [browser-command]'.",
            ),
        }

    else:
        # {{{
        # Native messenger can't seem to create detached process on
        # Windows while Firefox is quitting, which is essential to
        # trigger restarting Firefox. So, below we are resorting to
        # create a scheduled task with the task start-time set in
        # the near future.
        #

        #
        # subprocess.Popen(
        #    [ff_bin_path, "-profile", profile_dir],
        #    shell=False,
        #    creationflags=0x208 \
        #    | subprocess.CREATE_NEW_PROCESS_GROUP)
        #

        #
        # 'schtasks.exe' is limited as in it doesn't support
        # task-time with granularity in seconds. So, falling back
        # to PowerShell as the last resort.
        #

        # out_str = ""
        # task_time = time.strftime("%H:%M",
        #                           time.localtime(
        #                               time.time() + 60))
        #
        # out_str = subprocess.check_output(
        #     ["schtasks.exe",
        #      "/Create",
        #      "/F",
        #      "/SC",
        #      "ONCE",
        #      "/TN",
        #      "tridactyl",
        #      "/TR",
        #      "calc",
        #      "/IT",
        #      "/ST",
        #      task_time],
        #     shell=True)
        # }}}

        ff_lock_name = "parent.lock"

        ff_bin_name = browser_cmd
        ff_bin_path = '"%s"' % shutil.which(ff_bin_name)

        ff_bin_dir = '"%s"' % str(
            pathlib.WindowsPath(shutil.which(ff_bin_name)).parent
        )

        if profile_dir == "auto":
            ff_lock_path = ff_bin_path
            ff_args = '"%s"' % ("-foreground")
        else:
            ff_lock_path = '"%s/%s"' % (profile_dir, ff_lock_name)
            ff_args = '"%s","%s","%s"' % (
                "-foreground",
                "-profile",
                profile_dir,
            )

        try:
            restart_ps1_content = """
$env:PATH=$env:PATH;{ff_bin_dir}
Set-Location -Path {ff_bin_dir}
$profileDir = "{profile_dir}"
if ($profileDir -ne "auto") {{
    $lockFilePath = {ff_lock_path}
    $locked = $true
    $num_try = 10
}} else {{
    $locked = $false
}}
while (($locked -eq $true) -and ($num_try -gt 0)) {{
try {{
    [IO.File]::OpenWrite($lockFilePath).close()
    $locked=$false
}} catch {{
    $num_try-=1
    Write-Host "[+] Trial: $num_try [lock == true]"
    Start-Sleep -Seconds 1
}}
}}
if ($locked -eq $true) {{
$errorMsg = "Restarting Firefox failed. Please restart manually."
Write-Host "$errorMsg"
# Add-Type -AssemblyName System.Windows.Forms
# [System.Windows.MessageBox]::Show(
#     $errorMsg,
#     "Tridactyl")
}} else {{
Write-Host "[+] Restarting Firefox ..."
Start-Process `
  -WorkingDirectory {ff_bin_dir} `
  -FilePath {ff_bin_path} `
  -ArgumentList {ff_args} `
  -WindowStyle Normal
}}
""".format(
                ff_bin_dir=ff_bin_dir,
                profile_dir=profile_dir,
                ff_lock_path=ff_lock_path,
                ff_bin_path=ff_bin_path,
                ff_args=ff_args,
            )

            delay_sec = 1.5
            task_name = "firefox-restart"
            native_messenger_dirname = ".tridactyl"

            powershell_cmd = "powershell"
            powershell_args = "%s %s" % (
                "-NoProfile",
                "-ExecutionPolicy Bypass",
            )

            restart_ps1_path = "%s\\%s\\%s" % (
                os.path.expanduser("~"),
                native_messenger_dirname,
                "win_firefox_restart.ps1",
            )

            task_cmd = "cmd"
            task_arg = '/c "%s %s -File %s"' % (
                powershell_cmd,
                powershell_args,
                restart_ps1_path,
            )

            open(restart_ps1_path, "w+").write(restart_ps1_content)

            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

            subprocess.check_output(
                [
                    "powershell",
                    "-NonInteractive",
                    "-NoProfile",
                    "-WindowStyle",
                    "Minimized",
                    "-InputFormat",
                    "None",
                    "-ExecutionPolicy",
                    "Bypass",
                    "-Command",
                    "Register-ScheduledTask \
                     -TaskName '%s' \
                     -Force \
                     -Action (New-ScheduledTaskAction \
                     -Execute '%s' \
                     -Argument '%s') \
                     -Trigger (New-ScheduledTaskTrigger \
                     -Once \
                     -At \
                 (Get-Date).AddSeconds(%d).ToString('HH:mm:ss'))"
                    % (task_name, task_cmd, task_arg, delay_sec),
                ],
                shell=False,
                startupinfo=startupinfo,
            )

            reply = {
                "code": 0,
                "content": "Restarting in %d seconds..."
                % delay_sec,
            }

        except subprocess.CalledProcessError:
            reply = {
                "code": -1,
                "cmd": "error",
                "error": "error creating restart task.",
            }

    return reply
Beispiel #10
0
    def connect(self,
                settings=False,
                config=False,
                tor_status_update_func=None):
        common.log('Onion', 'connect')

        # Either use settings that are passed in, or load them from disk
        if settings:
            self.settings = settings
        else:
            self.settings = Settings(config)
            self.settings.load()

        # The Tor controller
        self.c = None

        if self.settings.get('connection_type') == 'bundled':
            if not self.bundle_tor_supported:
                raise BundledTorNotSupported(
                    strings._('settings_error_bundled_tor_not_supported'))

            # Create a torrc for this session
            self.tor_data_directory = tempfile.TemporaryDirectory()

            if self.system == 'Windows':
                # Windows needs to use network ports, doesn't support unix sockets
                torrc_template = open(
                    common.get_resource_path('torrc_template-windows')).read()
                try:
                    self.tor_control_port = common.get_available_port(
                        1000, 65535)
                except:
                    raise OSError(strings._('no_available_port'))
                self.tor_control_socket = None
                self.tor_cookie_auth_file = os.path.join(
                    self.tor_data_directory.name, 'cookie')
                try:
                    self.tor_socks_port = common.get_available_port(
                        1000, 65535)
                except:
                    raise OSError(strings._('no_available_port'))
                self.tor_torrc = os.path.join(self.tor_data_directory.name,
                                              'torrc')
            else:
                # Linux and Mac can use unix sockets
                with open(common.get_resource_path('torrc_template')) as f:
                    torrc_template = f.read()
                self.tor_control_port = None
                self.tor_control_socket = os.path.join(
                    self.tor_data_directory.name, 'control_socket')
                self.tor_cookie_auth_file = os.path.join(
                    self.tor_data_directory.name, 'cookie')
                try:
                    self.tor_socks_port = common.get_available_port(
                        1000, 65535)
                except:
                    raise OSError(strings._('no_available_port'))
                self.tor_torrc = os.path.join(self.tor_data_directory.name,
                                              'torrc')

            torrc_template = torrc_template.replace(
                '{{data_directory}}', self.tor_data_directory.name)
            torrc_template = torrc_template.replace('{{control_port}}',
                                                    str(self.tor_control_port))
            torrc_template = torrc_template.replace(
                '{{control_socket}}', str(self.tor_control_socket))
            torrc_template = torrc_template.replace('{{cookie_auth_file}}',
                                                    self.tor_cookie_auth_file)
            torrc_template = torrc_template.replace('{{geo_ip_file}}',
                                                    self.tor_geo_ip_file_path)
            torrc_template = torrc_template.replace(
                '{{geo_ipv6_file}}', self.tor_geo_ipv6_file_path)
            torrc_template = torrc_template.replace('{{socks_port}}',
                                                    str(self.tor_socks_port))
            with open(self.tor_torrc, 'w') as f:
                f.write(torrc_template)

            # Execute a tor subprocess
            start_ts = time.time()
            if self.system == 'Windows':
                # In Windows, hide console window when opening tor.exe subprocess
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                self.tor_proc = subprocess.Popen(
                    [self.tor_path, '-f', self.tor_torrc],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    startupinfo=startupinfo)
            else:
                self.tor_proc = subprocess.Popen(
                    [self.tor_path, '-f', self.tor_torrc],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)

            # Wait for the tor controller to start
            time.sleep(2)

            # Connect to the controller
            try:
                if self.system == 'Windows':
                    self.c = Controller.from_port(port=self.tor_control_port)
                    self.c.authenticate()
                else:
                    self.c = Controller.from_socket_file(
                        path=self.tor_control_socket)
                    self.c.authenticate()
            except Exception as e:
                raise BundledTorBroken(
                    strings._('settings_error_bundled_tor_broken',
                              True).format(e.args[0]))

            while True:
                try:
                    res = self.c.get_info("status/bootstrap-phase")
                except SocketClosed:
                    raise BundledTorCanceled()

                res_parts = shlex.split(res)
                progress = res_parts[2].split('=')[1]
                summary = res_parts[4].split('=')[1]

                # "\033[K" clears the rest of the line
                print("{}: {}% - {}{}".format(strings._('connecting_to_tor'),
                                              progress, summary, "\033[K"),
                      end="\r")

                if callable(tor_status_update_func):
                    if not tor_status_update_func(progress, summary):
                        # If the dialog was canceled, stop connecting to Tor
                        common.log(
                            'Onion', 'connect',
                            'tor_status_update_func returned false, canceling connecting to Tor'
                        )
                        print()
                        return False

                if summary == 'Done':
                    print("")
                    break
                time.sleep(0.2)

                # Timeout after 90 seconds
                if time.time() - start_ts > 90:
                    print("")
                    self.tor_proc.terminate()
                    raise BundledTorTimeout(
                        strings._('settings_error_bundled_tor_timeout'))

        elif self.settings.get('connection_type') == 'automatic':
            # Automatically try to guess the right way to connect to Tor Browser

            # Try connecting to control port
            found_tor = False

            # If the TOR_CONTROL_PORT environment variable is set, use that
            env_port = os.environ.get('TOR_CONTROL_PORT')
            if env_port:
                try:
                    self.c = Controller.from_port(port=int(env_port))
                    found_tor = True
                except:
                    pass

            else:
                # Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor
                try:
                    ports = [9151, 9153, 9051]
                    for port in ports:
                        self.c = Controller.from_port(port=port)
                        found_tor = True
                except:
                    pass

                # If this still didn't work, try guessing the default socket file path
                socket_file_path = ''
                if not found_tor:
                    try:
                        if self.system == 'Darwin':
                            socket_file_path = os.path.expanduser(
                                '~/Library/Application Support/TorBrowser-Data/Tor/control.socket'
                            )

                        self.c = Controller.from_socket_file(
                            path=socket_file_path)
                        found_tor = True
                    except:
                        pass

            # If connecting to default control ports failed, so let's try
            # guessing the socket file name next
            if not found_tor:
                try:
                    if self.system == 'Linux':
                        socket_file_path = '/run/user/{}/Tor/control.socket'.format(
                            os.geteuid())
                    elif self.system == 'Darwin':
                        socket_file_path = '/run/user/{}/Tor/control.socket'.format(
                            os.geteuid())
                    elif self.system == 'Windows':
                        # Windows doesn't support unix sockets
                        raise TorErrorAutomatic(
                            strings._('settings_error_automatic'))

                    self.c = Controller.from_socket_file(path=socket_file_path)

                except:
                    raise TorErrorAutomatic(
                        strings._('settings_error_automatic'))

            # Try authenticating
            try:
                self.c.authenticate()
            except:
                raise TorErrorAutomatic(strings._('settings_error_automatic'))

        else:
            # Use specific settings to connect to tor

            # Try connecting
            try:
                if self.settings.get('connection_type') == 'control_port':
                    self.c = Controller.from_port(
                        address=self.settings.get('control_port_address'),
                        port=self.settings.get('control_port_port'))
                elif self.settings.get('connection_type') == 'socket_file':
                    self.c = Controller.from_socket_file(
                        path=self.settings.get('socket_file_path'))
                else:
                    raise TorErrorInvalidSetting(
                        strings._("settings_error_unknown"))

            except:
                if self.settings.get('connection_type') == 'control_port':
                    raise TorErrorSocketPort(
                        strings._("settings_error_socket_port").format(
                            self.settings.get('control_port_address'),
                            self.settings.get('control_port_port')))
                else:
                    raise TorErrorSocketFile(
                        strings._("settings_error_socket_file").format(
                            self.settings.get('socket_file_path')))

            # Try authenticating
            try:
                if self.settings.get('auth_type') == 'no_auth':
                    self.c.authenticate()
                elif self.settings.get('auth_type') == 'password':
                    self.c.authenticate(self.settings.get('auth_password'))
                else:
                    raise TorErrorInvalidSetting(
                        strings._("settings_error_unknown"))

            except MissingPassword:
                raise TorErrorMissingPassword(
                    strings._('settings_error_missing_password'))
            except UnreadableCookieFile:
                raise TorErrorUnreadableCookieFile(
                    strings._('settings_error_unreadable_cookie_file'))
            except AuthenticationFailure:
                raise TorErrorAuthError(
                    strings._('settings_error_auth').format(
                        self.settings.get('control_port_address'),
                        self.settings.get('control_port_port')))

        # If we made it this far, we should be connected to Tor
        self.connected_to_tor = True

        # Get the tor version
        self.tor_version = self.c.get_version().version_str

        # Do the versions of stem and tor that I'm using support ephemeral onion services?
        list_ephemeral_hidden_services = getattr(
            self.c, "list_ephemeral_hidden_services", None)
        self.supports_ephemeral = callable(
            list_ephemeral_hidden_services) and self.tor_version >= '0.2.7.1'

        # Do the versions of stem and tor that I'm using support stealth onion services?
        try:
            res = self.c.create_ephemeral_hidden_service(
                {1: 1},
                basic_auth={'onionshare': None},
                await_publication=False)
            tmp_service_id = res.content()[0][2].split('=')[1]
            self.c.remove_ephemeral_hidden_service(tmp_service_id)
            self.supports_stealth = True
        except:
            # ephemeral stealth onion services are not supported
            self.supports_stealth = False
Beispiel #11
0
def Initialize():
    #-- remote PhantomJS service
    if Addon.getSetting('External_PhantomJS') == 'true':
        url = 'http://' + Addon.getSetting(
            'PhantomJS_IP') + ':' + Addon.getSetting('PhantomJS_Port')
        try:
            str = get_HTML(url)
        except:
            str = get_HTML(url)
        f = open(os.path.join(Addon.getAddonInfo('path'), 'ext_cookie.txt'),
                 'w')
        f.write(str)
        f.close()

        #-- load cookies
        cj.load(os.path.join(Addon.getAddonInfo('path'), 'ext_cookie.txt'),
                True, True)
        return

    #-- local PhantomJS service
    startupinfo = None
    if os.name == 'nt':
        prog = '"' + os.path.join(
            Addon.getAddonInfo('path'),
            'phantomjs.exe" --cookies-file="') + os.path.join(
                Addon.getAddonInfo('path'),
                'cookie.txt') + '" "' + os.path.join(
                    Addon.getAddonInfo('path'), 'seasonvar.js"')
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= 1
    else:
        prog = [
            os.path.join(Addon.getSetting('PhantomJS_Path'),
                         'phantomjs'), '--cookies-file=' +
            os.path.join(Addon.getAddonInfo('path'), 'cookie.txt'),
            os.path.join(Addon.getAddonInfo('path'), 'seasonvar.js')
        ]

    try:
        process = subprocess.Popen(prog,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   shell=False,
                                   startupinfo=startupinfo)
        process.wait()
    except:
        xbmc.log('*** PhantomJS is not found or failed.')

    #-- load cookies
    f = open(os.path.join(Addon.getAddonInfo('path'), 'cookie.txt'), 'r')
    fcookie = f.read()
    f.close()

    group = ''
    for r in fcookie.split('\n'):
        r = r.strip()
        if group == '' and r != '':
            group = r
        elif r != '':
            ck = cookielib.Cookie(version=0,
                                  name=r.split('=', 1)[0],
                                  value=r.split('=', 1)[1],
                                  port=None,
                                  port_specified=False,
                                  domain=group.replace('[',
                                                       '').replace(']', ''),
                                  domain_specified=False,
                                  domain_initial_dot=False,
                                  path='/',
                                  path_specified=True,
                                  secure=False,
                                  expires=None,
                                  discard=True,
                                  comment=None,
                                  comment_url=None,
                                  rest={'HttpOnly': None},
                                  rfc2109=False)
            cj.set_cookie(ck)
        else:
            group = ''
Beispiel #12
0
 def get_startupinfo():
     """Return subprocess.STARTUPINFO instance hiding the console window."""
     startupinfo = subprocess.STARTUPINFO()
     startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
     startupinfo.wShowWindow = subprocess.SW_HIDE
     return startupinfo
Beispiel #13
0
def call(
        airfoil,
        alfas='none',
        output='Cp',
        Reynolds=0,
        Mach=0,  # noqa C901
        plots=False,
        NACA=True,
        GDES=False,
        iteration=10,
        flap=None,
        PANE=False,
        NORM=True):
    """Call xfoil through Python.

    The input variables are:

    :param airfoil: if NACA is false, airfoil is the name of the plain
           filewhere the airfoil geometry is stored (variable airfoil).
           If NACA is True, airfoil is the naca series of the airfoil
           (i.e.: naca2244). By default NACA is False.
    
    :param alfas: list/array/float/int of angles of attack.
    
    :param output: defines the kind of output desired from xfoil.  There
           are four posssible choices (by default, Cp is chosen):
    
          - Cp: generates files with Pressure coefficients for
                desired alfas.
          - Dump: generates file with Velocity along surface, Delta
                  star,theta and Cf vs s,x,y for several alfas.
          - Polar: generates file with CL, CD, CM, CDp, Top_Xtr,
                   Bot_Xtr.
          - Alfa_L_0: generates a file with the value of the angle
                      of attack that lift is equal to zero.
          - Coordinates: returns the coordinates of a NACA airfoil.
    
    :param Reynolds: Reynolds number in case the simulation is for a
          viscous flow. In case not informed, the code will assume
          inviscid.

    :param Mach: Mach number in case the simulation has to take in
          account compressibility effects through the Prandtl-Glauert
          correlation. If not informed, the code will not use the
          correction. For logical reasons, if Mach is informed a
          Reynolds number different from zero must also be informed.

    :param  plots: the code is able to save in a .ps file all the plots
          of Cp vs.alfa. By default, this option is deactivated.

    :param NACA: Boolean variable that defines if the code imports an
          airfoil from a file or generates a NACA airfoil.

    :param GDES: XFOIL function that improves the airfoil shape in case
          the selected points do not provide a good shape. The CADD
          function is also used. For more information about these
          functions, use the XFOIL manual.

    :param iteration: changes how many times XFOIL will try to make the
          results converge. Speciallt important for viscous flows

    :param flap: determines if there is a flap. In case there is the
          expected input is [x_hinge, y_hinge, deflection(angles)].
          y_hinge is determined to be exactly in the middle between the
          upper and lower surfaces.

    :param PANE: if there are more than 495 surface points, the paneling
          method will not be used. Need to use the PANE subroutine to
          solve this. It will find the best points that represent the
          geometry (only 160 of them).

    :param NORM: For good results using the panel method, Xfoil
          requires normalized coordinates, so this option should
          always be True.

    :rtype: dictionary with outputs relevant to the specific output type.
            Usually x,y coordinates will be normalized.

    As a side note, it is much more eficient to run a single run with
    multiple angles of attack rather than multiple runs, each with a
    single angle of attack.

    Created on Sun Mar  9 14:58:25 2014

    Last update Fr Jul 13 15:38:40 2015

    @author: Pedro Leal (Based on Hakan Tiftikci's code)
    """

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #                               Functions
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def issueCmd(cmd, echo=False):
        """Submit a command through PIPE to the command lineself.

        (Therefore leading the commands to xfoil.)

        @author: Hakan Tiftikci
        """
        ps.stdin.write(cmd + '\n')
        if echo:
            print(cmd)

    def submit(output, alfa):
        """Submit job to xfoil and saves file.

        Standard output file= function_airfoil_alfa.txt, where alfa has
        4 digits, where two of them are for decimals. i.e.
        cp_naca2244_0200. Analysis for Pressure Coefficients for a
        naca2244 at an angle of degrees.

        Possible to output other results such as theta, delta star
        through the choice of the ouput, but not implemented here.

        @author: Pedro Leal (Based on Hakan Tiftikci's code)
        """
        if output == "Alfa_L_0":
            issueCmd('CL 0')

        else:
            # Submit job for given angle of attack
            issueCmd('ALFA %.4f' % (alfa, ))
            if plots is True:
                issueCmd('HARD')
                shutil.copyfile(
                    'plot.ps',
                    'plot_{!s}_{!s}_{!s}.ps'.format(output, airfoil, alfa))
            if output == 'Cp':
                # Creating the file with the Pressure Coefficients
                filename = file_name(airfoil, alfas, output)
                try:
                    os.remove(filename)
                except OSError:
                    pass
                # Before writing file, denormalize it
                issueCmd('CPWR %s' % filename)

            if output == 'Dump':
                # Creating the file with the Pressure Coefficients
                filename = file_name(airfoil, alfas, output)
                try:
                    os.remove(filename)
                except OSError:
                    pass

                issueCmd('DUMP %r' % filename)

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #                Characteristics of the simulation
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # By default the code considers the flow to be inviscid.
    Viscid = False
    if Reynolds != 0:
        Viscid = True
    # Is alpha given or not?(in case of Alfa_L_0, then alfas=False)
    if alfas != 'none':
        # Single or multiple runs?
        if type(alfas) == list or type(alfas) == np.ndarray:
            Multiple = True
        elif type(alfas) == int or type(alfas) == float or \
                type(alfas) == np.float64 or type(alfas) == np.float32:
            Multiple = False
    elif (output == "Alfa_L_0" or output == "Coordinates") and alfas == 'none':
        Multiple = False
    elif output == "Alfa_L_0" and alfas != 'none':
        raise Exception("To find alpha_L_0, alfas must not be defined")
    elif output != "Alfa_L_0" and alfas == 'none':
        raise Exception("To find anything except alpha_L_0, you need to "
                        "define the values for alfa")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #                           Start Xfoil
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    # """For communication with the xfoil through the command line the
    # Popen class from subprocess is used. stdin and stdout both
    # represent inputs and outputs with the process run on the command
    # line, in this case, xfoil.
    #
    # class Popen(args, bufsize=0, executable=None,
    #             stdin=None, stdout=None, stderr=None,
    #             preexec_fn=None, close_fds=False, shell=False,
    #             cwd=None, env=None, universal_newlines=False,
    #             startupinfo=None, creationflags=0):

    # The following keys avoid the xfoil pop-up
    # source: http://stackoverflow.com/questions/1765078/how-to-avoid-
    # console-window-with-pyw-file-containing-os-system-call
    startupinfo = sp.STARTUPINFO()
    startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
    # Random output variable to avoid writing stuff from xfoil on the
    # console
    sout = 0
    # Calling xfoil with Poper
    ps = sp.Popen(['xfoil.exe'],
                  stdin=sp.PIPE,
                  stdout=sout,
                  stderr=None,
                  startupinfo=startupinfo,
                  encoding='utf8')

    # Loading geometry
    if NORM is True:
        issueCmd('NORM')
    if NACA is False:
        issueCmd('load %s' % airfoil)
    else:
        issueCmd('%s' % airfoil)

    # Once you load a set of points in Xfoil you need to create a
    # name, however we do not need to give it a name
    issueCmd('')

    if PANE is True:
        issueCmd('PANE')
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #             Adapting points for better plots
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if GDES is True:
        issueCmd('GDES')  # enter GDES menu
        issueCmd('CADD')  # add points at corners
        issueCmd('')  # accept default input
        issueCmd('')  # accept default input
        issueCmd('')  # accept default input
        issueCmd('')  # accept default input
        issueCmd('PANEL')  # regenerate paneling
    # ==============================================================
    #                              Flaps
    # ===============================================================
    if flap is not None:
        issueCmd('GDES')  # enter GDES menu
        issueCmd('FLAP')  # enter FLAP menu
        issueCmd('%f' % flap[0])  # insert x location
        issueCmd('%f' % flap[1])  # insert y location
        issueCmd('%f' % flap[2])  # ainsesrt deflection in degrees
        issueCmd('eXec')  # set buffer airfoil as current airfoil
        issueCmd('')  # exit GDES menu
    # If output equals Coordinates, no analysis will be realized, only the
    # coordinates of the shape will be outputed
    if output == 'Coordinates':
        issueCmd('SAVE')
        issueCmd(output + '_' + airfoil)
        # In case there is alread a file with that name, it will replace it.
        # The yes stands for YES otherwise Xfoil will do nothing with it.
        issueCmd('Y')
    else:
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        # Opening OPER module in Xfoil
        issueCmd('OPER')

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #                Applying effects of vicosity
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        issueCmd('iter')
        issueCmd('%d' % iteration)

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #                Applying effects of vicosity
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if Viscid is True:
            # Defining the system as viscous
            issueCmd('v')
            # Defining Reynolds number
            issueCmd('%f' % Reynolds)

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #    Defining Mach number for Prandtl-Gauber correlation
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        # issueCmd('MACH {!s}'.format(Mach))
        issueCmd('MACH %s' % Mach)

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #                     Submitting
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if output == 'Polar' or output == 'Alfa_L_0':
            issueCmd('PACC')
            # All file names in this library are generated by the
            # filename functon.
            filename = file_name(airfoil, alfas, output)
            try:
                os.remove(filename)
            except OSError:
                pass

            issueCmd('%s' % filename)
            issueCmd('')

        # For several angles of attack
        if Multiple is True:
            for alfa in alfas:
                submit(output, alfa)

        # For only one angle of attack
        if Multiple is False:
            submit(output, alfas)

        # Exiting
        # From OPER mode
        issueCmd('')
    # From xfoil
    issueCmd('QUIT')
    # From stdin
    ps.stdin.close()
    # From popen
    ps.wait()
Beispiel #14
0
    def run(self):
        print("Welcome to thread " + self.getName())
        self.caller.output("[Compiling " + self.caller.file_name + "]")

        # Handle custom env variables
        if self.caller.env:
            old_env = os.environ
            if not _ST3:
                os.environ.update(
                    dict((k.encode(sys.getfilesystemencoding()), v)
                         for (k, v) in self.caller.env.items()))
            else:
                os.environ.update(self.caller.env.items())

        # Handle path; copied from exec.py
        if self.caller.path:
            # if we had an env, the old path is already backuped in the env
            if not self.caller.env:
                old_path = os.environ["PATH"]
            # The user decides in the build system  whether he wants to append $PATH
            # or tuck it at the front: "$PATH;C:\\new\\path", "C:\\new\\path;$PATH"
            # Handle differently in Python 2 and 3, to be safe:
            if not _ST3:
                os.environ["PATH"] = os.path.expandvars(
                    self.caller.path).encode(sys.getfilesystemencoding())
            else:
                os.environ["PATH"] = os.path.expandvars(self.caller.path)

        # Set up Windows-specific parameters
        if self.caller.plat == "windows":
            # make sure console does not come up
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        # Now, iteratively call the builder iterator
        #
        cmd_iterator = self.caller.builder.commands()
        try:
            for (cmd, msg) in cmd_iterator:

                # If there is a message, display it
                if msg:
                    self.caller.output(msg)

                # If there is nothing to be done, exit loop
                # (Avoids error with empty cmd_iterator)
                if cmd == "":
                    break

                if isinstance(cmd, strbase) or isinstance(cmd, list):
                    print(cmd)
                    # Now create a Popen object
                    try:
                        if self.caller.plat == "windows":
                            proc = subprocess.Popen(cmd,
                                                    startupinfo=startupinfo,
                                                    stderr=subprocess.STDOUT,
                                                    stdout=subprocess.PIPE,
                                                    cwd=self.caller.tex_dir)
                        elif self.caller.plat == "osx":
                            proc = subprocess.Popen(cmd,
                                                    stderr=subprocess.STDOUT,
                                                    stdout=subprocess.PIPE,
                                                    env=os.environ,
                                                    preexec_fn=os.setsid,
                                                    cwd=self.caller.tex_dir)
                        else:  # Must be linux
                            proc = subprocess.Popen(cmd,
                                                    stderr=subprocess.STDOUT,
                                                    stdout=subprocess.PIPE,
                                                    preexec_fn=os.setsid,
                                                    cwd=self.caller.tex_dir)
                    except:
                        self.caller.output("\n\nCOULD NOT COMPILE!\n\n")
                        self.caller.output("Attempted command:")
                        self.caller.output(" ".join(cmd))
                        self.caller.output("\nBuild engine: " +
                                           self.caller.builder.name)
                        self.caller.proc = None
                        print(traceback.format_exc())
                        return
                # Abundance of caution / for possible future extensions:
                elif isinstance(cmd, subprocess.Popen):
                    proc = cmd
                else:
                    # don't know what the command is
                    continue

                # Now actually invoke the command, making sure we allow for killing
                # First, save process handle into caller; then communicate (which blocks)
                with self.caller.proc_lock:
                    self.caller.proc = proc
                out, err = proc.communicate()
                self.caller.builder.set_output(
                    out.decode(self.caller.encoding, "ignore"))

                # Here the process terminated, but it may have been killed. If so, stop and don't read log
                # Since we set self.caller.proc above, if it is None, the process must have been killed.
                # TODO: clean up?
                with self.caller.proc_lock:
                    if not self.caller.proc:
                        print(proc.returncode)
                        self.caller.output(
                            "\n\n[User terminated compilation process]\n")
                        self.caller.finish(
                            False)  # We kill, so won't switch to PDF anyway
                        return
                # Here we are done cleanly:
                with self.caller.proc_lock:
                    self.caller.proc = None
                print("Finished normally")
                print(proc.returncode)
                # At this point, out contains the output from the current command;
                # we pass it to the cmd_iterator and get the next command, until completion
        except:
            self.caller.output("\n\nCOULD NOT COMPILE!\n\n")
            self.caller.output("\nBuild engine: " + self.caller.builder.name)
            self.caller.proc = None
            print(traceback.format_exc())
            return
        finally:
            # restore environment
            if self.caller.env:
                os.environ = old_env
            elif self.caller.path:
                os.environ['PATH'] = old_path

        # Clean up
        cmd_iterator.close()

        try:
            # Here we try to find the log file...
            # 1. Check the aux_directory if there is one
            # 2. Check the output_directory if there is one
            # 3. Assume the log file is in the same folder as the main file
            log_file_base = self.caller.tex_base + ".log"
            if self.caller.aux_directory is None:
                if self.caller.output_directory is None:
                    log_file = os.path.join(self.caller.tex_dir, log_file_base)
                else:
                    log_file = os.path.join(self.caller.output_directory,
                                            log_file_base)

                    if not os.path.exists(log_file):
                        log_file = os.path.join(self.caller.tex_dir,
                                                log_file_base)
            else:
                log_file = os.path.join(self.caller.aux_directory,
                                        log_file_base)

                if not os.path.exists(log_file):
                    if (self.caller.output_directory is not None
                            and self.caller.output_directory !=
                            self.caller.aux_directory):
                        log_file = os.path.join(self.caller.output_directory,
                                                log_file_base)

                    if not os.path.exists(log_file):
                        log_file = os.path.join(self.caller.tex_dir,
                                                log_file_base)

            # CHANGED 12-10-27. OK, here's the deal. We must open in binary mode
            # on Windows because silly MiKTeX inserts ASCII control characters in
            # over/underfull warnings. In particular it inserts EOFs, which
            # stop reading altogether; reading in binary prevents that. However,
            # that's not the whole story: if a FS character is encountered,
            # AND if we invoke splitlines on a STRING, it sadly breaks the line
            # in two. This messes up line numbers in error reports. If, on the
            # other hand, we invoke splitlines on a byte array (? whatever read()
            # returns), this does not happen---we only break at \n, etc.
            # However, we must still decode the resulting lines using the relevant
            # encoding.

            # Note to self: need to think whether we don't want to codecs.open
            # this, too... Also, we may want to move part of this logic to the
            # builder...
            with open(log_file, 'rb') as f:
                data = f.read()
        except IOError:
            self.caller.output([
                "",
                ""
                "Could not find log file {0}!".format(log_file_base),
            ])
            try:
                self.handle_std_outputs(out, err)
            except:
                # if out or err don't yet exist
                self.caller.finish(False)
        else:
            errors = []
            warnings = []
            badboxes = []

            try:
                (errors, warnings,
                 badboxes) = parseTeXlog.parse_tex_log(data,
                                                       self.caller.tex_dir)
                content = [""]
                if errors:
                    content.append("Errors:")
                    content.append("")
                    content.extend(errors)
                else:
                    content.append("No errors.")
                if warnings:
                    if errors:
                        content.extend(["", "Warnings:"])
                    else:
                        content[-1] = content[-1] + " Warnings:"
                    content.append("")
                    content.extend(warnings)
                else:
                    if errors:
                        content.append("")
                        content.append("No warnings.")
                    else:
                        content[-1] = content[-1] + " No warnings."

                if badboxes and self.caller.display_bad_boxes:
                    if warnings or errors:
                        content.extend(["", "Bad Boxes:"])
                    else:
                        content[-1] = content[-1] + " Bad Boxes:"
                    content.append("")
                    content.extend(badboxes)
                else:
                    if self.caller.display_bad_boxes:
                        if errors or warnings:
                            content.append("")
                            content.append("No bad boxes.")
                        else:
                            content[-1] = content[-1] + " No bad boxes."

                hide_panel = {
                 "always": True,
                 "no_errors": not errors,
                 "no_warnings": not errors and not warnings,
                 "no_badboxes": not errors and not warnings and \
                  (not self.caller.display_bad_boxes or not badboxes),
                 "never": False
                }.get(self.caller.hide_panel_level, False)

                if hide_panel:
                    # hide the build panel (ST2 api is not thread save)
                    if _ST3:
                        self.caller.window.run_command(
                            "hide_panel", {"panel": "output.latextools"})
                    else:
                        sublime.set_timeout(
                            lambda: self.caller.window.run_command(
                                "hide_panel", {"panel": "output.latextools"}),
                            10)
                    message = "build completed"
                    if errors:
                        message += " with errors"
                    if warnings:
                        if errors:
                            if badboxes and self.caller.display_bad_boxes:
                                message += ","
                            else:
                                message += " and"
                        else:
                            message += " with"
                        message += " warnings"
                    if badboxes and self.caller.display_bad_boxes:
                        if errors or warnings:
                            message += " and"
                        else:
                            message += " with"
                        message += " bad boxes"

                    if _ST3:
                        sublime.status_message(message)
                    else:
                        sublime.set_timeout(
                            lambda: sublime.status_message(message), 10)
            except Exception as e:
                # dumpt exception to console
                traceback.print_exc()

                content = ["", ""]
                content.append(
                    "LaTeXTools could not parse the TeX log file {0}".format(
                        log_file))
                content.append("(actually, we never should have gotten here)")
                content.append("")
                content.append("Python exception: {0!r}".format(e))
                content.append("")
                content.append(
                    "The full error description can be found on the console.")
                content.append("Please let us know on GitHub. Thanks!")

            self.caller.output(content)
            self.caller.output("\n\n[Done!]\n")
            self.caller.finish(len(errors) == 0)
Beispiel #15
0
##########################################################################

processingSrc = "rec.wav"
processingDst = "rec.mp3"
processingChain: List[List[str]] = []
recFiles: List[str] = []

processingChain = [
    ["lame", processingSrc, processingDst, "--noreplaygain", "--quiet"],
]

# don't show box on windows
si: Optional[Any]
if sys.platform == "win32":
    si = subprocess.STARTUPINFO()  # pytype: disable=module-attr
    try:
        si.dwFlags |= subprocess.STARTF_USESHOWWINDOW  # pytype: disable=module-attr
    except:
        # pylint: disable=no-member
        # python2.7+
        si.dwFlags |= (subprocess._subprocess.STARTF_USESHOWWINDOW)  # pytype: disable=module-attr
else:
    si = None


def retryWait(proc) -> Any:
    # osx throws interrupted system call errors frequently
    while 1:
        try:
            return proc.wait()
Beispiel #16
0
def start_elementumd(**kwargs):
    jsonrpc_failures = 0
    while jsonrpc_enabled() is False:
        jsonrpc_failures += 1
        log.warning(
            "Unable to connect to Kodi's JSON-RPC service, retrying...")
        if jsonrpc_failures > 1:
            time.sleep(5)
            if not jsonrpc_enabled(notify=True):
                log.error(
                    "Unable to reach Kodi's JSON-RPC service, aborting...")
                return False
            else:
                break
        time.sleep(3)

    elementum_dir, elementum_binary = get_elementum_binary()

    log.info("Binary dir: %s, item: %s " % (elementum_dir, elementum_binary))
    if elementum_dir is False or elementum_binary is False:
        return False

    lockfile = os.path.join(ADDON_PATH, ".lockfile")
    if os.path.exists(lockfile):
        log.warning("Existing process found from lockfile, killing...")
        try:
            with open(lockfile) as lf:
                pid = int(lf.read().rstrip(" \t\r\n\0"))
            os.kill(pid, 9)
        except OSError as e:
            if e.errno != 3:
                # Ignore:   OSError: [Errno 3] No such process
                log.error(repr(e))
        except Exception as e:
            log.error(repr(e))

        if binary_platform["os"] == "windows":
            try:
                library_lockfile = os.path.join(
                    py2_decode(translatePath(ADDON.getAddonInfo("profile"))),
                    "library.db.lock")
                log.warning("Removing library.db.lock file at %s ..." %
                            library_lockfile)
                os.remove(library_lockfile)
            except Exception as e:
                log.error(repr(e))

    SW_HIDE = 0
    STARTF_USESHOWWINDOW = 1

    args = [elementum_binary]
    kwargs["cwd"] = elementum_dir

    if binary_platform["os"] == "windows":
        args[0] = getWindowsShortPath(elementum_binary)
        kwargs["cwd"] = getWindowsShortPath(elementum_dir)
        si = subprocess.STARTUPINFO()
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_HIDE
        clear_fd_inherit_flags()
        kwargs["startupinfo"] = si
    else:
        env = os.environ.copy()
        env["LD_LIBRARY_PATH"] = "%s:%s" % (elementum_dir,
                                            env.get("LD_LIBRARY_PATH", ""))
        env["GODEBUG"] = "madvdontneed=1"
        kwargs["env"] = env
        kwargs["close_fds"] = True

    wait_counter = 1
    log.debug("Checking for visible")
    while xbmc.getCondVisibility(
            'Window.IsVisible(10140)') or xbmc.getCondVisibility(
                'Window.IsActive(10140)'):
        if wait_counter == 1:
            log.info(
                'Add-on settings currently opened, waiting before starting...')
        if wait_counter > 300:
            break
        time.sleep(1)
        wait_counter += 1

    log.info("elementumd: start args: %s, kw: %s" % (args, kwargs))

    if hasSubprocess:
        return subprocess.Popen(args, **kwargs)
    return False
Beispiel #17
0
def tx_make_process(txmanager, queue, thread_idx):
    """Function executed by threads to convert images to textures with txmake.

    Args:
    - queue (Queue.Queue): The task queue maintained by the main thread.
    """
    logger = txm_log()
    logger.debug('start')
    while not queue.empty():
        ui, txfile, txitem, args = queue.get()
        infile = args[-2]
        outfile = args[-1]

        if txfile.is_rtxplugin:
            queue.task_done()
            return

        logger.debug('%r', args)
        if not txfile.done_callback:
            logger.warning('Unexpected done callback = %r: %s',
                           txfile.done_callback, txfile.input_image)

        txfile.set_item_state(txitem, STATE_PROCESSING)
        txmanager.send_txmake_progress(txfile, txitem, txitem.state)
        start_t = time.time()
        err_msg = ''
        win_os = (platform.system() == 'Windows')
        sp_kwargs = {
            'stdin': subprocess.PIPE,
            'stdout': subprocess.PIPE,
            'stderr': subprocess.PIPE,
            'shell': False
        }
        if win_os:
            sp_kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
            sp_kwargs['startupinfo'] = subprocess.STARTUPINFO()
            sp_kwargs['startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW
        try:
            p = subprocess.Popen(args, **sp_kwargs)
        except Exception as err:
            logger.warning(' |_ failed to launch: %s\n    |_ args: %r', err,
                           args)
            txfile.set_item_state(txitem, STATE_ERROR)
        else:
            txmanager.subprocesses[thread_idx] = p
            lo = p.stdout.readline()
            le = p.stderr.readline()
            while lo or le:
                if lo:
                    logger.debug(lo)
                if le:
                    logger.debug(le)
                    err_msg += le
                lo = p.stdout.readline()
                le = p.stderr.readline()

        time.sleep(1.0)
        p.poll()  # get the return code

        if os.path.exists(outfile):
            stats = time.strftime('%Mm:%Ss',
                                  time.localtime(time.time() - start_t))
            txfile.set_item_state(txitem, STATE_EXISTS)
            logger.info('Converted in %s : %r', stats, outfile)

            # check time stamp for future dated input files
            # if time stamp is greater than "now", we
            # give the outfile the same time stamp as the
            # input outfile
            now_time = time.time()
            infile_time = os.path.getmtime(infile)
            if infile_time > now_time:
                logger.debug('Input file, %r, is from the future!', infile)
                os.utime(outfile, (infile_time, infile_time))
        else:
            if p.returncode in KILLED_SIGNALS:
                logger.debug('KILLED: %s', args)
                txfile.set_item_state(txitem, STATE_IN_QUEUE)
            else:
                txfile.set_item_state(txitem, STATE_ERROR)
                txfile.error_msg += err_msg
                logger.error('Failed to convert: %r', infile)
                logger.error('  |__ args: %r', args)
        txitem.update_file_size()
        txfile.update_file_size()

        # update txmanager and ui
        txmanager.send_txmake_progress(txfile, txitem, txitem.state)
        txmanager.subprocesses[thread_idx] = None

        # mark task done in task queue
        queue.task_done()

    logger.debug('empty queue = done')

    unblock(txmanager)
Beispiel #18
0
    def __init__(
            self,
            cmd,
            shell_cmd,
            user_input,
            env,
            listener,
            # "path" is an option in build systems
            path="",
            # "shell" is an options in build systems
            shell=False):

        if not shell_cmd and not cmd:
            raise ValueError("shell_cmd or cmd is required")

        if sys.version > '3' and (shell_cmd
                                  and not isinstance(shell_cmd, str)):
            raise ValueError("shell_cmd must be a string")

        self.listener = listener
        self.killed = False

        self.start_time = time.time()

        # Hide the console window on Windows
        startupinfo = None
        if os.name == "nt":
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        # Set temporary PATH to locate executable in cmd
        if path:
            old_path = os.environ["PATH"]
            # The user decides in the build system whether he wants to append $PATH
            # or tuck it at the front: "$PATH;C:\\new\\path", "C:\\new\\path;$PATH"
            os.environ["PATH"] = os.path.expandvars(path)

        proc_env = os.environ.copy()
        proc_env.update(env)
        for k, v in proc_env.items():
            proc_env[k] = os.path.expandvars(v)

        if shell_cmd and not sys.platform == "win32":
            echo_input = subprocess.Popen('echo "' + user_input + '"',
                                          stderr=subprocess.STDOUT,
                                          stdout=subprocess.PIPE,
                                          shell=True)

        if shell_cmd and sys.platform == "win32":
            # Since, Windows doesn't allow multiline echo, create concatenated echo statements
            split_input = user_input.split('\n')
            parsed_user_input = ""
            print(shell_cmd)
            for i in split_input:
                parsed_user_input += 'echo ' + i + "&"
            parsed_user_input = parsed_user_input[0:-1]
            echo_input = subprocess.Popen(parsed_user_input,
                                          stderr=subprocess.STDOUT,
                                          stdout=subprocess.PIPE,
                                          shell=True)
            # Use shell=True on Windows, so shell_cmd is passed through with the correct escaping
            self.proc = subprocess.Popen(shell_cmd,
                                         stdin=echo_input.stdout,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE,
                                         startupinfo=startupinfo,
                                         env=proc_env,
                                         shell=True)
        elif shell_cmd and sys.platform == "darwin":
            # Use a login shell on OSX, otherwise the users expected env vars won't be setup
            self.proc = subprocess.Popen(["/bin/bash", "-l", "-c", shell_cmd],
                                         stdin=echo_input.stdout,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE,
                                         startupinfo=startupinfo,
                                         env=proc_env,
                                         preexec_fn=os.setpgrp,
                                         shell=False)
        elif shell_cmd and (sys.platform == "linux" or sys.platform == "linux2"
                            or sys.platform == "linux3"):
            # Explicitly use /bin/bash on Linux, to keep Linux and OSX as
            # similar as possible. A login shell is explicitly not used for
            # linux, as it's not required
            self.proc = subprocess.Popen(["/bin/bash", "-c", shell_cmd],
                                         stdin=echo_input.stdout,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE,
                                         startupinfo=startupinfo,
                                         env=proc_env,
                                         preexec_fn=os.setpgrp,
                                         shell=False)
        else:
            # Old style build system, just do what it asks
            self.proc = subprocess.Popen(cmd,
                                         stdin=echo_input.stdout,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE,
                                         startupinfo=startupinfo,
                                         env=proc_env,
                                         shell=shell)

        if path:
            os.environ["PATH"] = old_path

        if self.proc.stdout:
            threading.Thread(target=self.read_stdout).start()

        if self.proc.stderr:
            threading.Thread(target=self.read_stderr).start()
Beispiel #19
0
    def run(self, edit):
        vsize = self.view.size()
        region = sublime.Region(0, vsize)
        src = self.view.substr(region)
        window = self.view.window()

        settings = sublime.load_settings('Crystal.sublime-settings')
        #command = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]
        command = [
            settings.get("crystal_cmd"), "tool", "format", "-", "--no-color"
        ]

        # for Windows Subsystem for Linux
        if os.name == "nt": command.insert(0, "wsl")

        popen_args = dict(args=command,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
        # Prevent flashing terminal windows
        if sys.platform.startswith('win'):
            popen_args['startupinfo'] = subprocess.STARTUPINFO()
            popen_args[
                'startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW

        proc = subprocess.Popen(**popen_args)
        stdout, stderr = proc.communicate(src.encode('utf-8'))
        stdout = stdout.decode('utf-8')
        stderr = stderr.decode('utf-8')
        exit = proc.returncode

        pos = 0
        if exit == 0:
            if not self.has_redo():
                for op, text in diff_match_patch().diff_main(src, stdout):
                    if op == diff_match_patch.DIFF_DELETE:
                        self.view.erase(edit,
                                        sublime.Region(pos, pos + len(text)))
                    if op == diff_match_patch.DIFF_INSERT:
                        self.view.insert(edit, pos, text)
                        pos += len(text)
                    if op == diff_match_patch.DIFF_EQUAL:
                        pos += len(text)

            self.view.erase_regions('crystal_errors')
            window.run_command("hide_panel")

        else:
            error_pos = None
            pattern = r"Error: Syntax error in .+?:(\d+): (.+)"
            match = re.match(pattern, stderr)
            if match:
                error_pos = int(match.group(1))
                error = match.group(2)
            else:
                error_pos = None
                error = stderr
            # error = json.loads(stderr)
            # error_pos = self.view.text_point(error[0]["line"] - 1, error[0]["column"] - 1)

            if error_pos:
                line_region = self.view.full_line(error_pos)
                self.view.add_regions('crystal_errors', [line_region],
                                      'comment', 'dot', sublime.HIDDEN)

            error_panel = window.create_output_panel('crystal_errors')
            # error_panel.run_command("append", {"characters":
            #   "Error at line %d, column %d: %s" % (error[0]["line"], error[0]["column"], error[0]['message'])
            # })

            if error_pos:
                error_panel.run_command("append", {
                    "characters":
                    "Error at line %d: %s" % (error_pos, error)
                })
            else:
                error_panel.run_command("append", {"characters": error})

            window.run_command("show_panel",
                               {"panel": "output.crystal_errors"})
Beispiel #20
0
def communicateWithCommandLine3(list0):
    shell = "powershell -Sta"

    plat = getplatform()
    #if(plat == "Windows"):
    #    shell = os.environ.get('COMSPEC') + ' ' + "/q /d /s /a /v:on /e:on /f:on"
    #else:
    #    shell = os.environ.get('SHELL')
    #print ( 'Running under', shell )

    global cmd_event
    cmd_event = threading.Event()
    global cmd_exit_flag
    cmd_exit_flag = 0

    startupinfo = None
    createflags = 0
    if (plat == 'Windows'):
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        startupinfo.dwFlags |= subprocess.STARTF_USESTDHANDLES
        startupinfo.wShowWindow = subprocess.SW_HIDE
        #createflags = subprocess.CREATE_NEW_CONSOLE

    p = subprocess.Popen(
        shell,
        shell=False,
        bufsize=-1,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        #if dont do this, windows stdout piory is high, stderr cant output full message
        #when split stdout and stderr, lots of app wont manager the output order
        stderr=subprocess.STDOUT,
        startupinfo=startupinfo,
        creationflags=createflags)
    read_thread = threading.Thread(target=powershell_read_thread_function,
                                   args=(p, ))
    read_thread.start()
    read_err_thread = threading.Thread(target=read_stderr_thread_function,
                                       args=(p, ))
    #read_err_thread.start()
    write_command_thread = threading.Thread(
        target=powershell_write_command_thread_function, args=(p, list0))
    write_command_thread.start()
    write_thread = threading.Thread(target=write_thread_function, args=(p, ))
    write_thread.start()
    # time.sleep(1)

    try:
        p.wait()
    except (KeyboardInterrupt):
        p.returncode = 1

    if (read_thread.isAlive()):
        stopThread(read_thread)
    if (read_err_thread.isAlive()):
        stopThread(read_err_thread)
    if (write_command_thread.isAlive()):
        stopThread(write_command_thread)
    if (write_thread.isAlive()):
        stopThread(write_thread)

    # time.sleep(1)
    return p.returncode
def strings(fi):
    """
    Extract text strings from selected region (the whole file if not selected)
    """
    if fi.getDocumentCount() == 0:
        return

    length = fi.getSelectionLength()
    offset = fi.getSelectionOffset()

    if length > 0:
        data = fi.getSelection()
    else:
        offset = 0
        data = fi.getDocument()

    # Do not show command prompt window
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    # Execute strings_dialog.py to show GUI
    # GUI portion is moved to external script to avoid hangup of FileInsight
    p = subprocess.Popen(["py.exe", "-3", "Parsing/strings_dialog.py"],
                         startupinfo=startupinfo,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE)

    # Receive parameters
    stdout_data, stderr_data = p.communicate()
    if stdout_data == "":
        return

    stdout_data = stdout_data.rstrip()
    (mode, min_len, postprocess, decode_hex) = stdout_data.split("\t")
    min_len = int(min_len)
    if decode_hex == "True":
        decode_hex = True
    else:
        decode_hex = False
    newdata = ""
    decoded = ""

    if mode == "ASCII + UTF-16":
        expression = "[ -~]{%d,}" % min_len
        matched = re.finditer(expression, data)
        newdata += "ASCII strings:\r\n"
        decoded += "Decoded ASCII strings:\r\n"

        if postprocess == "Remove duplicates":
            (plain_list,
             decoded_list) = strings_dedupe(matched, False, decode_hex)
            for s_plain in plain_list:
                newdata += s_plain + "\r\n"
            for s_decoded in decoded_list:
                decoded += s_decoded + "\r\n"
        else:
            for m in matched:
                (s_plain, s_decoded) = strings_decode(m.group(), decode_hex)

                if postprocess == "Show offset":
                    newdata += "0x%x: %s\r\n" % (offset + m.start(), s_plain)
                    if s_decoded != "":
                        decoded += "0x%x: %s -> %s\r\n" % (offset + m.start(),
                                                           s_plain, s_decoded)
                else:
                    newdata += s_plain + "\r\n"
                    if s_decoded != "":
                        decoded += "%s -> %s \r\n" % (s_plain, s_decoded)

        expression = "(?:(?:[ -~]\x00)|(?:\x00[ -~])){%d,}" % min_len
        matched = re.finditer(expression, data)
        newdata += "\r\nUTF-16 strings:\r\n"
        decoded += "\r\nDecoded UTF-16 strings:\r\n"

        if postprocess == "Remove duplicates":
            (plain_list,
             decoded_list) = strings_dedupe(matched, True, decode_hex)
            for s_plain in plain_list:
                newdata += s_plain + "\r\n"
            for s_decoded in decoded_list:
                decoded += s_decoded + "\r\n"
        else:
            for m in matched:
                s = re.sub("\x00+", "", m.group())
                (s_plain, s_decoded) = strings_decode(s, decode_hex)

                if postprocess == "Show offset":
                    newdata += "0x%x: %s\r\n" % (offset + m.start(), s_plain)
                    if s_decoded != "":
                        decoded += "0x%x: %s -> %s\r\n" % (offset + m.start(),
                                                           s_plain, s_decoded)
                else:
                    newdata += s_plain + "\r\n"
                    if s_decoded != "":
                        decoded += "%s -> %s \r\n" % (s_plain, s_decoded)
    elif mode == "ASCII":
        expression = "[ -~]{%d,}" % min_len
        matched = re.finditer(expression, data)
        newdata += "ASCII strings:\r\n"
        decoded += "Decoded ASCII strings:\r\n"

        if postprocess == "Remove duplicates":
            (plain_list,
             decoded_list) = strings_dedupe(matched, False, decode_hex)
            for s_plain in plain_list:
                newdata += s_plain + "\r\n"
            for s_decoded in decoded_list:
                decoded += s_decoded + "\r\n"
        else:
            for m in matched:
                (s_plain, s_decoded) = strings_decode(m.group(), decode_hex)

                if postprocess == "Show offset":
                    newdata += "0x%x: %s\r\n" % (offset + m.start(), s_plain)
                    if s_decoded != "":
                        decoded += "0x%x: %s -> %s\r\n" % (offset + m.start(),
                                                           s_plain, s_decoded)
                else:
                    newdata += s_plain + "\r\n"
                    if s_decoded != "":
                        decoded += "%s -> %s \r\n" % (s_plain, s_decoded)
    elif mode == "UTF-16":
        expression = "(?:(?:[ -~]\x00)|(?:\x00[ -~])){%d,}" % min_len
        matched = re.finditer(expression, data)
        newdata += "UTF-16 strings:\r\n"
        decoded += "Decoded UTF-16 strings:\r\n"

        if postprocess == "Remove duplicates":
            (plain_list,
             decoded_list) = strings_dedupe(matched, True, decode_hex)
            for s_plain in plain_list:
                newdata += s_plain + "\r\n"
            for s_decoded in decoded_list:
                decoded += s_decoded + "\r\n"
        else:
            for m in matched:
                s = re.sub("\x00+", "", m.group())
                (s_plain, s_decoded) = strings_decode(s, decode_hex)

                if postprocess == "Show offset":
                    newdata += "0x%x: %s\r\n" % (offset + m.start(), s_plain)
                    if s_decoded != "":
                        decoded += "0x%x: %s -> %s\r\n" % (offset + m.start(),
                                                           s_plain, s_decoded)
                else:
                    newdata += s_plain + "\r\n"
                    if s_decoded != "":
                        decoded += "%s -> %s \r\n" % (s_plain, s_decoded)

    fi.newDocument("Strings output", 0)  # Open a new tab with text mode
    if decode_hex:
        fi.setDocument(decoded + "\r\n" + newdata)
    else:
        fi.setDocument(newdata)

    if length > 0:
        print("Extracted text strings from offset %s to %s." %
              (hex(offset), hex(offset + length)))
    else:
        print("Extracted text strings from the whole file.")
Beispiel #22
0
 def get_startupinfo() -> subprocess.STARTUPINFO:  # pytype: disable=module-attr
     """Return subprocess.STARTUPINFO instance hiding the console window."""
     startupinfo = subprocess.STARTUPINFO()  # pytype: disable=module-attr
     startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW  # pytype: disable=module-attr
     startupinfo.wShowWindow = subprocess.SW_HIDE  # pytype: disable=module-attr
     return startupinfo
def binwalk_scan(fi):
    """
    Scan selected region (the whole file if not selected) to find embedded files
    """
    if fi.getDocumentCount() == 0:
        return

    time_start = time.time()

    length = fi.getSelectionLength()
    offset = fi.getSelectionOffset()

    offset_found = []
    if length > 0:
        data = fi.getSelection()
    else:
        offset = 0
        data = fi.getDocument()
        length = fi.getLength()

    # Create a temporary file
    fd, filepath = tempfile.mkstemp()
    handle = os.fdopen(fd, "wb")
    handle.write(data)
    handle.close()

    # Do not show command prompt window
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    # Execute binwalk_scan.py for scanning with binwalk
    p = subprocess.Popen(
        ["py.exe", "-3", "Parsing/binwalk_scan.py", filepath,
         str(offset)],
        startupinfo=startupinfo,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)

    # Receive scan result
    stdout_data, stderr_data = p.communicate()
    ret = p.wait()

    os.remove(filepath)  # Cleanup

    if ret == -1:
        print("binwalk is not installed.")
        print(
            "Please get it from https://github.com/ReFirmLabs/binwalk and install it (pip cannot be used to install binwalk)."
        )
        return

    if fi.getSelectionLength() > 0:
        print(
            'Scanned from offset %s to %s and the output is shown in the new "Binwalk output" tab.'
            % (hex(offset), hex(offset + length)))
    else:
        print(
            'Scanned the whole file and the output is shown in the new "Binwalk output" tab.'
        )

    for l in stdout_data.splitlines():
        offset_found.append(int(l.split()[1], 0))

    num_found = len(offset_found)
    if num_found == 0:
        print("No file has been detected.")

    print("Elapsed time (scan): %f (sec)" % (time.time() - time_start))
    time_start = time.time()

    if num_found > 0:
        if num_found > 100 and not bookmark_yesno_dialog(num_found):
            return  # "No" is clicked
        else:
            for i in range(0, num_found):
                if i + 1 == num_found:
                    fi.setBookmark(offset_found[i],
                                   offset + length - offset_found[i],
                                   hex(offset_found[i]), "#c8ffff")
                else:
                    fi.setBookmark(offset_found[i],
                                   offset_found[i + 1] - offset_found[i],
                                   hex(offset_found[i]), "#c8ffff")

            print("\r\nAdded bookmarks to the detected files.")
            print("Elapsed time (bookmark): %f (sec)" %
                  (time.time() - time_start))

        fi.newDocument("Binwalk output", 0)
        fi.setDocument(stdout_data)
import threading
import platform
from time import sleep

import logging
logger = logging.getLogger('GITIGNORE')

# Used for output suppression when calling subprocess functions; see
# http://stackoverflow.com/questions/10251391/suppressing-output-in-python-subprocess-call
devnull = open(os.devnull, 'w')

# Used to prevent a new command prompt window from popping up every time a new
# process is spawned on Windows. See
# https://docs.python.org/2/library/subprocess.html#subprocess.STARTUPINFO
if platform.system() == 'Windows':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
    startupinfo.wShowWindow = subprocess.SW_HIDE
else:
    startupinfo = None


def start():  # Gets invoked at the bottom of this file.
    """
    Regularly (every 5s) updates the file_exclude_patterns setting.
    """
    if is_first_launch():
        migrate_exclude_patterns()
        record_first_launch()

    def run():
Beispiel #25
0
def Ghostscript(tile, size, fp, scale=1):
    """Render an image using Ghostscript"""

    # Unpack decoder tile
    decoder, tile, offset, data = tile[0]
    length, bbox = data

    # Hack to support hi-res rendering
    scale = int(scale) or 1
    # orig_size = size
    # orig_bbox = bbox
    size = (size[0] * scale, size[1] * scale)
    # resolution is dependent on bbox and size
    res = (float((72.0 * size[0]) / (bbox[2] - bbox[0])),
           float((72.0 * size[1]) / (bbox[3] - bbox[1])))

    import subprocess
    import tempfile

    out_fd, outfile = tempfile.mkstemp()
    os.close(out_fd)

    infile_temp = None
    if hasattr(fp, 'name') and os.path.exists(fp.name):
        infile = fp.name
    else:
        in_fd, infile_temp = tempfile.mkstemp()
        os.close(in_fd)
        infile = infile_temp

        # Ignore length and offset!
        # Ghostscript can read it
        # Copy whole file to read in Ghostscript
        with open(infile_temp, 'wb') as f:
            # fetch length of fp
            fp.seek(0, 2)
            fsize = fp.tell()
            # ensure start position
            # go back
            fp.seek(0)
            lengthfile = fsize
            while lengthfile > 0:
                s = fp.read(min(lengthfile, 100 * 1024))
                if not s:
                    break
                lengthfile -= len(s)
                f.write(s)

    # Build Ghostscript command
    command = [
        "gs",
        "-q",  # quiet mode
        "-g%dx%d" % size,  # set output geometry (pixels)
        "-r%fx%f" % res,  # set input DPI (dots per inch)
        "-dBATCH",  # exit after processing
        "-dNOPAUSE",  # don't pause between pages
        "-dSAFER",  # safe mode
        "-sDEVICE=ppmraw",  # ppm driver
        "-sOutputFile=%s" % outfile,  # output file
        # adjust for image origin
        "-c",
        "%d %d translate" % (-bbox[0], -bbox[1]),
        "-f",
        infile,  # input file
        # showpage (see https://bugs.ghostscript.com/show_bug.cgi?id=698272)
        "-c",
        "showpage",
    ]

    if gs_windows_binary is not None:
        if not gs_windows_binary:
            raise WindowsError('Unable to locate Ghostscript on paths')
        command[0] = gs_windows_binary

    # push data through Ghostscript
    try:
        with open(os.devnull, 'w+b') as devnull:
            startupinfo = None
            if sys.platform.startswith('win'):
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            subprocess.check_call(command,
                                  stdin=devnull,
                                  stdout=devnull,
                                  startupinfo=startupinfo)
        im = Image.open(outfile)
        im.load()
    finally:
        try:
            os.unlink(outfile)
            if infile_temp:
                os.unlink(infile_temp)
        except OSError:
            pass

    return im.im.copy()
Beispiel #26
0
            if isinstance(item, list) or isinstance(item, tuple):
                for subitem in cls._flatten(item):
                    yield subitem

            else:
                yield item


# Reinitialize the CLI_LAME, CLI_SI, IS_WINDOWS, and IS_MACOSX constants
# on the base class, if necessary given the running operating system.

if sys.platform == 'win32':
    Service.CLI_DECODINGS.append('mbcs')
    Service.CLI_LAME = 'lame.exe'
    Service.CLI_MPLAYER = 'mplayer.exe'
    Service.CLI_SI = subprocess.STARTUPINFO()
    Service.IS_WINDOWS = True

    try:
        Service.CLI_SI.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    except AttributeError:
        try:
            Service.CLI_SI.dwFlags |= (
                subprocess._subprocess.  # workaround, pylint:disable=W0212
                STARTF_USESHOWWINDOW
            )

        except AttributeError:
            pass
Beispiel #27
0
    def setup_ccx(self, ccx_binary=None, ccx_binary_sig="CalculiX"):
        """Set Calculix binary path and validate its execution.

        Parameters
        ----------
        ccx_binary : str, optional
            It defaults to `None`. The path to the `ccx` binary. If it is `None`,
            the path is guessed.
        ccx_binary_sig : str, optional
            Defaults to 'CalculiX'. Expected output from `ccx` when run empty.

        Raises
        ------
        Exception
        """
        error_title = "No CalculiX binary ccx"
        error_message = ""
        from platform import system
        ccx_std_location = FreeCAD.ParamGet(
            "User parameter:BaseApp/Preferences/Mod/Fem/Ccx").GetBool(
                "UseStandardCcxLocation", True)
        if ccx_std_location:
            if system() == "Windows":
                ccx_path = FreeCAD.getHomePath() + "bin/ccx.exe"
                FreeCAD.ParamGet(
                    "User parameter:BaseApp/Preferences/Mod/Fem/Ccx"
                ).SetString("ccxBinaryPath", ccx_path)
                self.ccx_binary = ccx_path
            elif system() in ("Linux", "Darwin"):
                p1 = subprocess.Popen(["which", "ccx"], stdout=subprocess.PIPE)
                if p1.wait() == 0:
                    if sys.version_info.major >= 3:
                        ccx_path = p1.stdout.read().decode("utf8").split(
                            "\n")[0]
                    else:
                        ccx_path = p1.stdout.read().split("\n")[0]
                elif p1.wait() == 1:
                    error_message = (
                        "FEM: CalculiX binary ccx not found in "
                        "standard system binary path. "
                        "Please install ccx or set path to binary "
                        "in FEM preferences tab CalculiX.\n")
                    if FreeCAD.GuiUp:
                        QtGui.QMessageBox.critical(None, error_title,
                                                   error_message)
                    raise Exception(error_message)
                self.ccx_binary = ccx_path
        else:
            if not ccx_binary:
                self.ccx_prefs = FreeCAD.ParamGet(
                    "User parameter:BaseApp/Preferences/Mod/Fem/Ccx")
                ccx_binary = self.ccx_prefs.GetString("ccxBinaryPath", "")
                if not ccx_binary:
                    FreeCAD.ParamGet(
                        "User parameter:BaseApp/Preferences/Mod/Fem/Ccx"
                    ).SetBool("UseStandardCcxLocation", True)
                    error_message = (
                        "FEM: CalculiX binary ccx path not set at all. "
                        "The use of standard path was activated in "
                        "FEM preferences tab CalculiX. Please try again!\n")
                    if FreeCAD.GuiUp:
                        QtGui.QMessageBox.critical(None, error_title,
                                                   error_message)
                    raise Exception(error_message)
            self.ccx_binary = ccx_binary

        startup_info = None
        if system() == "Windows":
            # Windows workaround to avoid blinking terminal window
            startup_info = subprocess.STARTUPINFO()
            startup_info.dwFlags = subprocess.STARTF_USESHOWWINDOW
        ccx_stdout = None
        ccx_stderr = None
        try:
            p = subprocess.Popen([self.ccx_binary],
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 shell=False,
                                 startupinfo=startup_info)
            ccx_stdout, ccx_stderr = p.communicate()
            if ccx_binary_sig in str(ccx_stdout):
                self.ccx_binary_present = True
            else:
                raise Exception("FEM: wrong ccx binary")
                # since we raise an exception the try will fail and
                # the exception later with the error popup will be raised
                # TODO: I'm still able to break it.
                # If user doesn't give a file but a path without a file or
                # a file which is not a binary no exception at all is raised.
        except OSError as e:
            FreeCAD.Console.PrintError("{}\n".format(e))
            if e.errno == 2:
                error_message = (
                    "FEM: CalculiX binary ccx \'{}\' not found. "
                    "Please set the CalculiX binary ccx path in "
                    "FEM preferences tab CalculiX.\n".format(ccx_binary))
                if FreeCAD.GuiUp:
                    QtGui.QMessageBox.critical(None, error_title,
                                               error_message)
                raise Exception(error_message)
        except Exception as e:
            FreeCAD.Console.PrintError("{}\n".format(e))
            error_message = (
                "FEM: CalculiX ccx \'{}\' output \'{}\' doesn't "
                "contain expected phrase \'{}\'. "
                "There are some problems when running the ccx binary. "
                "Check if ccx runs standalone without FreeCAD.\n".format(
                    ccx_binary, ccx_stdout, ccx_binary_sig))
            if FreeCAD.GuiUp:
                QtGui.QMessageBox.critical(None, error_title, error_message)
            raise Exception(error_message)
Beispiel #28
0
    def executeGrass(commands, feedback, outputCommands=None):
        loglines = []
        loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output'))
        grassOutDone = False
        command, grassenv = Grass7Utils.prepareGrassExecution(commands)
        #QgsMessageLog.logMessage('exec: {}'.format(command), 'DEBUG', Qgis.Info)

        # For MS-Windows, we need to hide the console window.
        if isWindows():
            si = subprocess.STARTUPINFO()
            si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            si.wShowWindow = subprocess.SW_HIDE

        with subprocess.Popen(command,
                              shell=True if isMac() else False,
                              stdout=subprocess.PIPE,
                              stdin=subprocess.DEVNULL,
                              stderr=subprocess.STDOUT,
                              universal_newlines=True,
                              env=grassenv,
                              startupinfo=si if isWindows() else None) as proc:
            for line in iter(proc.stdout.readline, ''):
                if 'GRASS_INFO_PERCENT' in line:
                    try:
                        feedback.setProgress(
                            int(line[len('GRASS_INFO_PERCENT') + 2:]))
                    except:
                        pass
                else:
                    if 'r.out' in line or 'v.out' in line:
                        grassOutDone = True
                    loglines.append(line)
                    feedback.pushConsoleInfo(line)

        # Some GRASS scripts, like r.mapcalculator or r.fillnulls, call
        # other GRASS scripts during execution. This may override any
        # commands that are still to be executed by the subprocess, which
        # are usually the output ones. If that is the case runs the output
        # commands again.
        if not grassOutDone and outputCommands:
            command, grassenv = Grass7Utils.prepareGrassExecution(
                outputCommands)
            with subprocess.Popen(
                    command,
                    shell=True if isMac() else False,
                    stdout=subprocess.PIPE,
                    stdin=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT,
                    universal_newlines=True,
                    env=grassenv,
                    startupinfo=si if isWindows() else None) as proc:
                for line in iter(proc.stdout.readline, ''):
                    if 'GRASS_INFO_PERCENT' in line:
                        try:
                            feedback.setProgress(
                                int(line[len('GRASS_INFO_PERCENT') + 2:]))
                        except:
                            pass
                    else:
                        loglines.append(line)
                        feedback.pushConsoleInfo(line)

        if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_CONSOLE):
            QgsMessageLog.logMessage('\n'.join(loglines), 'Processing',
                                     Qgis.Info)
Beispiel #29
0
def convert_from_path(
    pdf_path,
    dpi=200,
    output_folder=None,
    first_page=None,
    last_page=None,
    fmt="ppm",
    jpegopt=None,
    thread_count=1,
    userpw=None,
    use_cropbox=False,
    strict=False,
    transparent=False,
    single_file=False,
    output_file=uuid_generator(),
    poppler_path=None,
    grayscale=False,
    size=None,
    paths_only=False,
    use_pdftocairo=False,
):
    """
        Description: Convert PDF to Image will throw whenever one of the condition is reached
        Parameters:
            pdf_path -> Path to the PDF that you want to convert
            dpi -> Image quality in DPI (default 200)
            output_folder -> Write the resulting images to a folder (instead of directly in memory)
            first_page -> First page to process
            last_page -> Last page to process before stopping
            fmt -> Output image format
            jpegopt -> jpeg options `quality`, `progressive`, and `optimize` (only for jpeg format)
            thread_count -> How many threads we are allowed to spawn for processing
            userpw -> PDF's password
            use_cropbox -> Use cropbox instead of mediabox
            strict -> When a Syntax Error is thrown, it will be raised as an Exception
            transparent -> Output with a transparent background instead of a white one.
            single_file -> Uses the -singlefile option from pdftoppm/pdftocairo
            output_file -> What is the output filename or generator
            poppler_path -> Path to look for poppler binaries
            grayscale -> Output grayscale image(s)
            size -> Size of the resulting image(s), uses the Pillow (width, height) standard
            paths_only -> Don't load image(s), return paths instead (requires output_folder)
            use_pdftocairo -> Use pdftocairo instead of pdftoppm, may help performance
    """

    if use_pdftocairo and fmt == "ppm":
        fmt = "png"

    # We make sure that if passed arguments are Path objects, they're converted to strings
    if isinstance(pdf_path, pathlib.PurePath):
        pdf_path = pdf_path.as_posix()

    if isinstance(output_folder, pathlib.PurePath):
        output_folder = output_folder.as_posix()

    if isinstance(poppler_path, pathlib.PurePath):
        poppler_path = poppler_path.as_posix()

    page_count = pdfinfo_from_path(pdf_path, userpw,
                                   poppler_path=poppler_path)["Pages"]

    # We start by getting the output format, the buffer processing function and if we need pdftocairo
    parsed_fmt, final_extension, parse_buffer_func, use_pdfcairo_format = _parse_format(
        fmt, grayscale)

    # We use pdftocairo is the format requires it OR we need a transparent output
    use_pdfcairo = (use_pdftocairo or use_pdfcairo_format
                    or (transparent and parsed_fmt in TRANSPARENT_FILE_TYPES))

    poppler_version = _get_poppler_version(
        "pdftocairo" if use_pdfcairo else "pdftoppm",
        poppler_path=poppler_path)

    if poppler_version <= 57:
        jpegopt = None

    # If output_file isn't a generator, it will be turned into one
    if not isinstance(output_file, types.GeneratorType) and not isinstance(
            output_file, ThreadSafeGenerator):
        if single_file:
            output_file = iter([output_file])
        else:
            output_file = counter_generator(output_file)

    if thread_count < 1:
        thread_count = 1

    if first_page is None:
        first_page = 1

    if last_page is None or last_page > page_count:
        last_page = page_count

    if first_page > last_page:
        return []

    auto_temp_dir = False
    if output_folder is None and use_pdfcairo:
        auto_temp_dir = True
        output_folder = tempfile.mkdtemp()

    # Recalculate page count based on first and last page
    page_count = last_page - first_page + 1

    if thread_count > page_count:
        thread_count = page_count

    reminder = page_count % thread_count
    current_page = first_page
    processes = []
    for _ in range(thread_count):
        thread_output_file = next(output_file)

        # Get the number of pages the thread will be processing
        thread_page_count = page_count // thread_count + int(reminder > 0)
        # Build the command accordingly
        args = _build_command(
            ["-r", str(dpi), pdf_path],
            output_folder,
            current_page,
            current_page + thread_page_count - 1,
            parsed_fmt,
            jpegopt,
            thread_output_file,
            userpw,
            use_cropbox,
            transparent,
            single_file,
            grayscale,
            size,
        )

        if use_pdfcairo:
            args = [_get_command_path("pdftocairo", poppler_path)] + args
        else:
            args = [_get_command_path("pdftoppm", poppler_path)] + args

        # Update page values
        current_page = current_page + thread_page_count
        reminder -= int(reminder > 0)
        # Add poppler path to LD_LIBRARY_PATH
        env = os.environ.copy()

        # Added for hiding console
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = subprocess.SW_HIDE

        if poppler_path is not None:
            env["LD_LIBRARY_PATH"] = poppler_path + ":" + env.get(
                "LD_LIBRARY_PATH", "")
        # Spawn the process and save its uuid
        processes.append((thread_output_file,
                          Popen(args,
                                env=env,
                                stdout=PIPE,
                                stderr=PIPE,
                                startupinfo=startupinfo)))

    images = []

    for uid, proc in processes:
        data, err = proc.communicate()

        if b"Syntax Error" in err and strict:
            raise PDFSyntaxError(err.decode("utf8", "ignore"))

        if output_folder is not None:
            images += _load_from_output_folder(output_folder,
                                               uid,
                                               final_extension,
                                               paths_only,
                                               in_memory=auto_temp_dir)
        else:
            images += parse_buffer_func(data)

    if auto_temp_dir:
        shutil.rmtree(output_folder)

    return images
Beispiel #30
0
def LINK(mname, murl, thumb):
    print "LIVE MURL IS =" + murl
    ok = True
    namelist = []
    urllist = []
    playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
    playlist.clear()
    if '_whole' in murl:
        link = main.OPEN_URL(murl)
        link = link.replace('\r',
                            '').replace('\n', '').replace('\t', '').replace(
                                '&nbsp;', '').replace('  ', '')
        part = re.findall('/([^/]+)ipad.mp4.m3u8', murl)[0]
        match = re.compile('BANDWIDTH=.+?' + part + '(.+?)_ipad.mp4.m3u8',
                           re.DOTALL).findall(link)
        for band in sorted(match):
            namelist.append(band)
        dialog = xbmcgui.Dialog()
        answer = dialog.select("Pick A Bandwidth", namelist)
        if answer != -1:
            nurl = murl.split('ipad.mp4.m3u8')[0]
            stream_url = nurl + namelist[int(
                answer
            )] + '_ipad.mp4.m3u8' + '|User-Agent=PS4 libhttp/1.76 (PlayStation 4)'
            NHLRESOLVE(mname, stream_url, thumb)
        else:
            return
    elif '/live/' in murl:
        import subprocess
        #jarfile = xbmc.translatePath('special://home/addons/plugin.video.phstreams/FuckNeulionV2.jar')
        #jarfile = selfAddon + '/resources/modules/FuckNeulionV2.jar'
        print "JARFILE IS = " + jarfile
        if 'Home' in mname:
            Side = 'home'
        if 'Away' in mname:
            Side = 'away'
        SelectGame = murl.split('x0xe')[1]
        murl = murl.split('x0xe')[0]
        startupinfo = None
        if os.name == 'nt':
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        command = ['java', '-jar', jarfile, SelectGame, Side]
        proxy_hack_process = subprocess.Popen(command,
                                              stdout=subprocess.PIPE,
                                              stderr=subprocess.STDOUT,
                                              startupinfo=startupinfo)
        if os.name == 'posix':
            # Run the proxy hack before playing the stream
            success = False

            success, output = FuckNeulionClient.request_proxy_hack(
                SelectGame, Side)

        xbmc.sleep(1000)
        link = main.OPEN_URL(murl)
        link = link.replace('\r',
                            '').replace('\n', '').replace('\t', '').replace(
                                '&nbsp;', '').replace('  ', '')
        part = re.findall('/([^/]+)ipad.m3u8', murl)[0]
        match = re.compile('BANDWIDTH=.+?' + part + '(.+?)_ipad.m3u8',
                           re.DOTALL).findall(link)
        for band in sorted(match):
            namelist.append(band)
        dialog = xbmcgui.Dialog()
        answer = dialog.select("Pick A Bandwidth", namelist)
        if answer != -1:
            nurl = murl.split('ipad.m3u8')[0]
            stream_url = nurl + namelist[int(
                answer
            )] + '_ipad.m3u8' + '|User-Agent=PS4 libhttp/1.76 (PlayStation 4)'
            NHLRESOLVE(mname, stream_url, thumb)
        else:
            return
    else:
        stream_url = murl + '|User-Agent=PS4 libhttp/1.76 (PlayStation 4)'
        NHLRESOLVE(mname, stream_url, thumb)