Beispiel #1
0
def main():
    parser = optparse.OptionParser()
    build_utils.AddDepfileOption(parser)
    parser.add_option('--src-dir',
                      action="append",
                      help='Directory containing .java files.')
    parser.add_option(
        '--src-jars',
        action="append",
        help='A list of source jars to include in addition to source files.')
    parser.add_option('--jar-path', help='Jar output path.')
    parser.add_option('--stamp', help='Path to touch on success.')

    options, _ = parser.parse_args()

    # A temporary directory to put the output of jar files.
    unzipped_jar_path = None
    if options.src_jars:
        unzipped_jar_path = tempfile.mkdtemp(
            dir=os.path.dirname(options.jar_path))
        jar_list = []
        for gn_list in options.src_jars:
            jar_list.extend(build_utils.ParseGnList(gn_list))

        for jar in jar_list:
            UnzipSourceJar(jar, unzipped_jar_path)

    src_dirs = []
    for src_dir in options.src_dir:
        src_dirs.extend(build_utils.ParseGnList(src_dir))
    if unzipped_jar_path:
        src_dirs += [unzipped_jar_path]

    for src_dir in src_dirs:
        JarSources(src_dir, options.jar_path)

    if options.depfile:
        input_paths = []
        for src_dir in src_dirs:
            for root, _, filenames in os.walk(src_dir):
                input_paths.extend(os.path.join(root, f) for f in filenames)
        build_utils.WriteDepfile(
            options.depfile, input_paths + build_utils.GetPythonDependencies())
    # Clean up temporary output directory.
    if unzipped_jar_path:
        build_utils.DeleteDirectory(unzipped_jar_path)

    if options.stamp:
        build_utils.Touch(options.stamp)
def main(argv):
    argv = build_utils.ExpandFileArgs(argv[1:])
    parser = argparse.ArgumentParser()
    parser.add_argument('--script',
                        required=True,
                        help='Path to the java binary wrapper script.')
    parser.add_argument('--input-jar', required=True)
    parser.add_argument('--output-jar', required=True)
    parser.add_argument('--direct-classpath-jars', required=True)
    parser.add_argument('--sdk-classpath-jars', required=True)
    parser.add_argument('--extra-classpath-jars',
                        dest='extra_jars',
                        action='append',
                        default=[],
                        help='Extra inputs, passed last to the binary script.')
    parser.add_argument('-v', '--verbose', action='store_true')
    parser.add_argument('--missing-classes-allowlist')
    _AddSwitch(parser, '--is-prebuilt')
    _AddSwitch(parser, '--enable-thread-annotations')
    _AddSwitch(parser, '--enable-check-class-path')
    args = parser.parse_args(argv)

    sdk_jars = build_utils.ParseGnList(args.sdk_classpath_jars)
    assert len(sdk_jars) > 0

    direct_jars = build_utils.ParseGnList(args.direct_classpath_jars)
    assert len(direct_jars) > 0

    extra_classpath_jars = []
    for a in args.extra_jars:
        extra_classpath_jars.extend(build_utils.ParseGnList(a))
    args.missing_classes_allowlist = build_utils.ParseGnList(
        args.missing_classes_allowlist)

    if args.verbose:
        verbose = '--verbose'
    else:
        verbose = '--not-verbose'

    cmd = ([
        args.script, args.input_jar, args.output_jar, verbose,
        args.is_prebuilt, args.enable_thread_annotations,
        args.enable_check_class_path
    ] + [str(len(args.missing_classes_allowlist))] +
           args.missing_classes_allowlist + [str(len(sdk_jars))] + sdk_jars +
           [str(len(direct_jars))] + direct_jars + extra_classpath_jars)
    subprocess.check_call(cmd)
def _ParseVariables(variables_arg, error_func):
  variables = {}
  for v in build_utils.ParseGnList(variables_arg):
    if '=' not in v:
      error_func('--variables argument must contain "=": ' + v)
    name, _, value = v.partition('=')
    variables[name] = value
  return variables
def _AppendParsedVariables(initial_variable_list, variables_arg, error_func):
    variables = initial_variable_list
    for v in build_utils.ParseGnList(variables_arg):
        if '=' not in v:
            error_func('--variables argument must contain "=": ' + v)
        name, _, value = v.partition('=')
        variables[name] = value
    return variables
def ExtractJars(options):
    # The paths of the files in the jar will be the same as they are passed in to
    # the command. Because of this, the command should be run in
    # options.classes_dir so the .class file paths in the jar are correct.
    jar_cwd = options.classes_dir
    build_utils.DeleteDirectory(jar_cwd)
    build_utils.MakeDirectory(jar_cwd)
    for jar in build_utils.ParseGnList(options.jars):
        jar_path = os.path.abspath(jar)
        jar_cmd = ['jar', 'xf', jar_path]
        build_utils.CheckOutput(jar_cmd, cwd=jar_cwd)
Beispiel #6
0
def _ParseVariables(variables_arg, error_func):
    variables = {}
    for v in build_utils.ParseGnList(variables_arg):
        if '=' not in v:
            error_func('--variables argument must contain "=": ' + v)
        name, _, value = v.partition('=')
        if value == 'True':
            # The python implementation of Mustache doesn't consider True to be a
            # truthy value. Wrap it up as a list to enable us to specify a boolean to
            # expand a template section.
            variables[name] = ['True']
        else:
            variables[name] = value
    return variables
def main():
    parser = argparse.ArgumentParser()
    build_utils.AddDepfileOption(parser)
    parser.add_argument(
        '--excluded-classes',
        help='A list of .class file patterns to exclude from the jar.')
    parser.add_argument('--src-search-dirs',
                        action='append',
                        help='A list of directories that should be searched'
                        ' for the source files.')
    parser.add_argument('--src-files',
                        action='append',
                        help='A list of source files to jar.')
    parser.add_argument(
        '--src-jars',
        action='append',
        help='A list of source jars to include in addition to source files.')
    parser.add_argument('--src-list-files',
                        action='append',
                        help='A list of files that contain a list of sources,'
                        ' e.g. a list of \'.sources\' files generated by GN.')
    parser.add_argument('--jar-path', help='Jar output path.', required=True)

    options = parser.parse_args()

    src_jars = []
    for gn_list in options.src_jars:
        src_jars.extend(build_utils.ParseGnList(gn_list))

    src_search_dirs = []
    for gn_src_search_dirs in options.src_search_dirs:
        src_search_dirs.extend(build_utils.ParseGnList(gn_src_search_dirs))

    src_list_files = []
    if options.src_list_files:
        for gn_src_list_file in options.src_list_files:
            src_list_files.extend(build_utils.ParseGnList(gn_src_list_file))

    src_files = []
    for gn_src_files in options.src_files:
        src_files.extend(build_utils.ParseGnList(gn_src_files))

    # Add files from --source_list_files
    for src_list_file in src_list_files:
        with open(src_list_file, 'r') as f:
            src_files.extend(f.read().splitlines())

    # Preprocess source files by removing any prefix that comes before
    # the Java package name.
    for i, s in enumerate(src_files):
        prefix_position = s.find(JAVA_PACKAGE_PREFIX)
        if prefix_position != -1:
            src_files[i] = s[prefix_position:]

    excluded_classes = []
    if options.excluded_classes:
        classes = build_utils.ParseGnList(options.excluded_classes)
        excluded_classes.extend(f.replace('.class', '.java') for f in classes)

    predicate = None
    if excluded_classes:
        predicate = lambda f: not build_utils.MatchesGlob(f, excluded_classes)

    # Create a dictionary that maps every source directory
    # to source files that it contains.
    dir_to_files_map = {}
    # Initialize the map.
    for src_search_dir in src_search_dirs:
        dir_to_files_map[src_search_dir] = []
    # Fill the map.
    for src_file in src_files:
        number_of_file_instances = 0
        for src_search_dir in src_search_dirs:
            target_path = os.path.join(src_search_dir, src_file)
            if os.path.isfile(target_path):
                number_of_file_instances += 1
                if not predicate or predicate(src_file):
                    dir_to_files_map[src_search_dir].append(target_path)
        if (number_of_file_instances > 1):
            raise Exception(
                'There is more than one instance of file %s in %s' %
                (src_file, src_search_dirs))
        if (number_of_file_instances < 1):
            raise Exception('Unable to find file %s in %s' %
                            (src_file, src_search_dirs))

    # Jar the sources from every source search directory.
    with build_utils.AtomicOutput(options.jar_path) as o, \
        zipfile.ZipFile(o, 'w', zipfile.ZIP_DEFLATED) as z:
        for src_search_dir in src_search_dirs:
            subpaths = dir_to_files_map[src_search_dir]
            if subpaths:
                build_utils.DoZip(subpaths, z, base_dir=src_search_dir)
            else:
                raise Exception(
                    'Directory %s does not contain any files and can be'
                    ' removed from the list of directories to search' %
                    src_search_dir)

        # Jar additional src jars
        if src_jars:
            build_utils.MergeZips(z, src_jars, compress=True)

    if options.depfile:
        deps = []
        for sources in dir_to_files_map.itervalues():
            deps.extend(sources)
        # Srcjar deps already captured in GN rules (no need to list them here).
        build_utils.WriteDepfile(options.depfile, options.jar_path, deps)
Beispiel #8
0
def main():
    parser = optparse.OptionParser()
    build_utils.AddDepfileOption(parser)
    parser.add_option('--src-search-dirs',
                      action="append",
                      help='A list of directories that should be searched'
                      ' for the source files.')
    parser.add_option('--src-files',
                      action="append",
                      help='A list of source files to jar.')
    parser.add_option(
        '--src-jars',
        action="append",
        help='A list of source jars to include in addition to source files.')
    parser.add_option('--src-list-files',
                      action="append",
                      help='A list of files that contain a list of sources,'
                      ' e.g. a list of \'.sources\' files generated by GN.')
    parser.add_option('--jar-path', help='Jar output path.')
    parser.add_option('--stamp', help='Path to touch on success.')

    options, _ = parser.parse_args()

    # A temporary directory to put the output of jar files.
    unzipped_jar_path = None
    if options.src_jars:
        unzipped_jar_path = tempfile.mkdtemp(
            dir=os.path.dirname(options.jar_path))
        jar_list = []
        for gn_list in options.src_jars:
            jar_list.extend(build_utils.ParseGnList(gn_list))

        for jar in jar_list:
            UnzipSourceJar(jar, unzipped_jar_path)

    src_search_dirs = []
    for gn_src_search_dirs in options.src_search_dirs:
        src_search_dirs.extend(build_utils.ParseGnList(gn_src_search_dirs))

    src_list_files = []
    if options.src_list_files:
        for gn_src_list_file in options.src_list_files:
            src_list_files.extend(build_utils.ParseGnList(gn_src_list_file))

    src_files = []
    for gn_src_files in options.src_files:
        src_files.extend(build_utils.ParseGnList(gn_src_files))

    # Add files from --source_list_files
    for src_list_file in src_list_files:
        with open(src_list_file, 'r') as f:
            src_files.extend(f.read().splitlines())

    # Preprocess source files by removing any prefix that comes before
    # the Java package name.
    for i, s in enumerate(src_files):
        prefix_position = s.find(JAVA_PACKAGE_PREFIX)
        if prefix_position != -1:
            src_files[i] = s[prefix_position:]

    # Create a dictionary that maps every source directory
    # to source files that it contains.
    dir_to_files_map = {}
    # Initialize the map.
    for src_search_dir in src_search_dirs:
        dir_to_files_map[src_search_dir] = []
    # Fill the map.
    for src_file in src_files:
        number_of_file_instances = 0
        for src_search_dir in src_search_dirs:
            if os.path.isfile(os.path.join(src_search_dir, src_file)):
                number_of_file_instances += 1
                dir_to_files_map[src_search_dir].append(src_file)
        if (number_of_file_instances > 1):
            raise Exception(
                'There is more than one instance of file %s in %s' %
                (src_file, src_search_dirs))
        if (number_of_file_instances < 1):
            raise Exception('Unable to find file %s in %s' %
                            (src_file, src_search_dirs))

    # Delete the old output file if any.
    if os.path.isfile(options.jar_path):
        os.remove(options.jar_path)

    # Jar the sources from every source search directory.
    for src_search_dir in src_search_dirs:
        if len(dir_to_files_map[src_search_dir]) > 0:
            JarSources(src_search_dir, dir_to_files_map[src_search_dir],
                       options.jar_path)
        else:
            raise Exception(
                'Directory %s does not contain any files and can be'
                ' removed from the list of directories to search' %
                src_search_dir)

    # Jar additional src jars
    if unzipped_jar_path:
        JarSources(unzipped_jar_path, ['.'], options.jar_path)

    if options.depfile:
        deps = []
        for src_dir in src_search_dirs:
            for root, _, filenames in os.walk(src_dir):
                deps.extend(os.path.join(root, f) for f in filenames)
        # Srcjar deps already captured in GN rules (no need to list them here).
        build_utils.WriteDepfile(options.depfile, options.jar_path, deps)

    # Clean up temporary output directory.
    if unzipped_jar_path:
        build_utils.DeleteDirectory(unzipped_jar_path)

    if options.stamp:
        build_utils.Touch(options.stamp)