예제 #1
0
파일: api.py 프로젝트: dimagi/formtranslate
def form_translate(input_data, operation):
    """
    Utility for interacting with the form_translate jar,
    which provides functionality for a number of different useful form tools:
        - converting a form to an xsd file
        - turning a form into a more readable format
        - generating a list of translations as an exportable .csv file
        - form validation
        - etc.

    does this by calling
       java -jar form_translate.jar $operation < form.xml > output

    """
    with subprocess_context() as subprocess:
        location = config.get_form_translate_jar_location()

        p = subprocess.Popen(['java', '-Xmx128m', '-jar', location, operation],
                             stdin=PIPE,
                             stdout=PIPE,
                             stderr=PIPE)
        stdout, stderr = p.communicate(input_data)
        exit_code = p.wait()

        return ShellResult(stdout=stdout.decode('utf-8'),
                           stderr=stderr.decode('utf-8'),
                           exit_code=exit_code)
예제 #2
0
def validate_xpath(xpath, allow_case_hashtags=False):
    with subprocess_context() as subprocess:
        path = get_xpath_validator_path()
        if allow_case_hashtags:
            cmd = ['node', path, '--allow-case-hashtags']
        else:
            cmd = ['node', path]
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate(xpath.encode('utf-8'))
        exit_code = p.wait()
        if exit_code == 0:
            return XpathValidationResponse(is_valid=True, message=None)
        elif exit_code == 1:
            return XpathValidationResponse(is_valid=False, message=stdout)
        else:
            raise XpathValidationError(
                "{path} failed with exit code {exit_code}:\n{stderr}"
                .format(path=path, exit_code=exit_code, stderr=stderr))
예제 #3
0
def validate_xpath(xpath, allow_case_hashtags=False):
    with subprocess_context() as subprocess:
        path = get_xpath_validator_path()
        if allow_case_hashtags:
            cmd = ['node', path, '--allow-case-hashtags']
        else:
            cmd = ['node', path]
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate(xpath.encode('utf-8'))
        exit_code = p.wait()
        if exit_code == 0:
            return XpathValidationResponse(is_valid=True, message=None)
        elif exit_code == 1:
            return XpathValidationResponse(is_valid=False, message=stdout)
        else:
            raise XpathValidationError(
                "{path} failed with exit code {exit_code}:\n{stderr}"
                .format(path=path, exit_code=exit_code, stderr=stderr))
예제 #4
0
def validate_xpath(xpath, allow_case_hashtags=False):
    with subprocess_context() as subprocess:
        path = get_xpath_validator_path()
        if allow_case_hashtags:
            cmd = ['node', path, '--allow-case-hashtags']
        else:
            cmd = ['node', path]
        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        # Collapse whitespace. '\r' mysteriously causes the process to hang in python 3.
        stdout, stderr = p.communicate(
            re.sub(r'\s+', ' ', xpath).encode('utf-8'))
        exit_code = p.wait()
    if exit_code == 0:
        return XpathValidationResponse(is_valid=True, message=None)
    elif exit_code == 1:
        return XpathValidationResponse(is_valid=False, message=stdout)
    else:
        raise XpathValidationError(
            "{path} failed with exit code {exit_code}:\n{stderr}".format(
                path=path, exit_code=exit_code, stderr=stderr))
예제 #5
0
파일: api.py 프로젝트: dimagi/formtranslate
def form_translate(input_data, operation):
    """
    Utility for interacting with the form_translate jar,
    which provides functionality for a number of different useful form tools:
        - converting a form to an xsd file
        - turning a form into a more readable format
        - generating a list of translations as an exportable .csv file
        - form validation
        - etc.

    does this by calling
       java -jar form_translate.jar $operation < form.xml > output

    """
    with subprocess_context() as subprocess:
        location = config.get_form_translate_jar_location()

        p = subprocess.Popen(['java', '-Xmx128m', '-jar', location, operation],
                             stdin=PIPE, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate(input_data)
        exit_code = p.wait()

        return ShellResult(stdout=stdout.decode('utf-8'), stderr=stderr.decode('utf-8'),
                           exit_code=exit_code)
예제 #6
0
def sign_jar(jad, jar, use_j2me_endpoint=False):
    if not (hasattr(jad, 'update') and hasattr(jad, 'render')):
        jad = JadDict.from_jad(jad, use_j2me_endpoint=use_j2me_endpoint)
    ''' run jadTool on the newly created JAR '''
    key_store = settings.JAR_SIGN['key_store']
    key_alias = settings.JAR_SIGN['key_alias']
    store_pass = settings.JAR_SIGN['store_pass']
    key_pass = settings.JAR_SIGN['key_pass']
    jad_tool = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                            'JadTool.jar')
    # remove traces of former jar signings, if any
    jad.update({
        "MIDlet-Certificate-1-1": None,
        "MIDlet-Certificate-1-2": None,
        "MIDlet-Certificate-1-3": None,
        "MIDlet-Jar-RSA-SHA1": None,
        "MIDlet-Permissions": None
    })
    line_ending = jad.line_ending
    # save jad and jar to actual files
    with NamedTemporaryFile('w', suffix='.jad', delete=False) as jad_file:
        with NamedTemporaryFile('wb', suffix='.jar', delete=False) as jar_file:

            jad_file.write(jad.render())
            jar_file.write(jar)

            jad_file.flush()
            jar_file.flush()

            step_one = 'java -jar "%s" -addjarsig -jarfile "%s" -alias %s -keystore "%s" -storepass %s -keypass %s -inputjad "%s" -outputjad "%s"' % \
                            (jad_tool, jar_file.name, key_alias, key_store, store_pass, key_pass, jad_file.name, jad_file.name)

            step_two = 'java -jar "%s" -addcert -alias %s -keystore "%s" -storepass %s -inputjad "%s" -outputjad "%s"' % \
                            (jad_tool, key_alias, key_store, store_pass, jad_file.name, jad_file.name)

            with subprocess_context() as subprocess:
                for step in (step_one, step_two):
                    p = subprocess.Popen(shlex.split(step),
                                         stdout=PIPE,
                                         stderr=PIPE,
                                         shell=False)
                    _, stderr = p.communicate()
                    if stderr.strip():
                        raise Exception(stderr)

            with open(jad_file.name) as f:
                txt = f.read()
                jad = JadDict.from_jad(txt,
                                       use_j2me_endpoint=use_j2me_endpoint)

            try:
                os.unlink(jad_file.name)
                os.unlink(jar_file.name)
            except Exception:
                pass

    jad.update({
        "MIDlet-Permissions":
        "javax.microedition.io.Connector.file.read,"
        "javax.microedition.io.Connector.ssl,"
        "javax.microedition.io.Connector.file.write,"
        "javax.microedition.io.Connector.comm,"
        "javax.microedition.io.Connector.http,"
        "javax.microedition.io.Connector.https,"
        "javax.microedition.io.Connector.sms,"
        "javax.wireless.messaging.sms.send,"
        "javax.microedition.media.control.VideoControl.getSnapshot"
    })
    jad.line_ending = line_ending

    return jad.render()
예제 #7
0
def sign_jar(jad, jar, use_j2me_endpoint=False):
    if not (hasattr(jad, 'update') and hasattr(jad, 'render')):
        jad = JadDict.from_jad(jad, use_j2me_endpoint=use_j2me_endpoint)

    ''' run jadTool on the newly created JAR '''
    key_store   = settings.JAR_SIGN['key_store']
    key_alias   = settings.JAR_SIGN['key_alias']
    store_pass  = settings.JAR_SIGN['store_pass']
    key_pass    = settings.JAR_SIGN['key_pass']
    jad_tool    = os.path.join(os.path.abspath(os.path.dirname(__file__)),'JadTool.jar')
    # remove traces of former jar signings, if any
    jad.update({
        "MIDlet-Certificate-1-1" : None,
        "MIDlet-Certificate-1-2" : None,
        "MIDlet-Certificate-1-3" : None,
        "MIDlet-Jar-RSA-SHA1" : None,
        "MIDlet-Permissions" : None
    })
    line_ending = jad.line_ending
    # save jad and jar to actual files
    with NamedTemporaryFile('w', suffix='.jad', delete=False) as jad_file:
        with NamedTemporaryFile('wb', suffix='.jar', delete=False) as jar_file:

            jad_file.write(jad.render())
            jar_file.write(jar)

            jad_file.flush()
            jar_file.flush()
            
            step_one = 'java -jar "%s" -addjarsig -jarfile "%s" -alias %s -keystore "%s" -storepass %s -keypass %s -inputjad "%s" -outputjad "%s"' % \
                            (jad_tool, jar_file.name, key_alias, key_store, store_pass, key_pass, jad_file.name, jad_file.name)

            step_two = 'java -jar "%s" -addcert -alias %s -keystore "%s" -storepass %s -inputjad "%s" -outputjad "%s"' % \
                            (jad_tool, key_alias, key_store, store_pass, jad_file.name, jad_file.name)

            with subprocess_context() as subprocess:
                for step in (step_one, step_two):
                    p = subprocess.Popen(shlex.split(step), stdout=PIPE, stderr=PIPE, shell=False)
                    _, stderr = p.communicate()
                    if stderr.strip():
                        raise Exception(stderr)

            with open(jad_file.name) as f:
                txt = f.read()
                jad = JadDict.from_jad(txt, use_j2me_endpoint=use_j2me_endpoint)
            
            try:
                os.unlink(jad_file.name)
                os.unlink(jar_file.name)
            except Exception:
                pass
    
    jad.update({
        "MIDlet-Permissions" :
            "javax.microedition.io.Connector.file.read,"
            "javax.microedition.io.Connector.ssl,"
            "javax.microedition.io.Connector.file.write,"
            "javax.microedition.io.Connector.comm,"
            "javax.microedition.io.Connector.http,"
            "javax.microedition.io.Connector.https,"
            "javax.microedition.io.Connector.sms,"
            "javax.wireless.messaging.sms.send,"
            "javax.microedition.media.control.VideoControl.getSnapshot"
    })
    jad.line_ending = line_ending

    return jad.render()