Ejemplo n.º 1
0
 def write_attrs(self, context_attrs):
     for k, v in context_attrs.iteritems():
         if isinstance(v, (list, set)):
             self.write_mozbuild_list(k, alphabetical_sorted(v))
         elif isinstance(v, dict):
             self.write_mozbuild_dict(k, v)
         else:
             self.write_mozbuild_value(k, v)
Ejemplo n.º 2
0
 def write_mozbuild_list(self, key, value):
     if value:
         self.write("\n")
         self.write(self.indent + key)
         self.write(" += [\n    " + self.indent)
         self.write((",\n    " + self.indent).join(
             alphabetical_sorted(self.mb_serialize(v) for v in value)))
         self.write("\n")
         self.write_ln("]")
 def write_mozbuild_list(self, key, value):
     if value:
         self.write('\n')
         self.write(self.indent + key)
         self.write(' += [\n    ' + self.indent)
         self.write((',\n    ' + self.indent).join(
             alphabetical_sorted(self.mb_serialize(v) for v in value)))
         self.write('\n')
         self.write_ln(']')
Ejemplo n.º 4
0
 def write_attrs(self, context_attrs):
     for k in sorted(context_attrs.keys()):
         v = context_attrs[k]
         if isinstance(v, (list, set)):
             self.write_mozbuild_list(k, alphabetical_sorted(v))
         elif isinstance(v, dict):
             self.write_mozbuild_dict(k, v)
         else:
             self.write_mozbuild_value(k, v)
Ejemplo n.º 5
0
 def write_attrs(self, context_attrs):
     for k in sorted(context_attrs.keys()):
         v = context_attrs[k]
         if isinstance(v, (list, set)):
             self.write_mozbuild_list(k, alphabetical_sorted(v))
         elif isinstance(v, dict):
             self.write_mozbuild_dict(k, v)
         else:
             self.write_mozbuild_value(k, v)
Ejemplo n.º 6
0
def edit_moz_build_file_to_add_file(
    normalized_mozbuild_filename,
    unnormalized_filename_to_add,
    unnormalized_list_of_files,
):
    """
    This function edits the moz.build file in-place

    I had _really_ hoped to replace this whole damn thing with something that adds a
    node to the AST, dumps the AST out, and then runs black on the file but there are
    some issues:
      - third party moz.build files (or maybe all moz.build files) aren't always run
        through black
      - dumping the ast out losing comments
    """

    # add the file into the list, and then sort it in the same way the moz.build validator
    # expects
    unnormalized_list_of_files.append(unnormalized_filename_to_add)
    unnormalized_list_of_files = alphabetical_sorted(unnormalized_list_of_files)

    # we're going to add our file by doing a find/replace of an adjacent file in the list
    indx_of_addition = unnormalized_list_of_files.index(unnormalized_filename_to_add)
    indx_of_addition
    if indx_of_addition == 0:
        target_indx = 1
        replace_before = False
    else:
        target_indx = indx_of_addition - 1
        replace_before = True

    find_str = unnormalized_list_of_files[target_indx]

    # We will only perform the first replacement. This is because sometimes there's moz.build
    # code like:
    #   SOURCES += ['file.cpp']
    #   SOURCES['file.cpp'].flags += ['-Winline']
    # If we replaced every time we found the target, we would be inserting into that second
    # line.
    did_replace = False

    # FileInput is a strange class that lets you edit a file in-place, but does so by hijacking
    # stdout, so you just print() the output you want as you go through
    file = fileinput.FileInput(normalized_mozbuild_filename, inplace=True)
    for line in file:
        if not did_replace and find_str in line:
            did_replace = True

            # Okay, we found the line we need to edit, now we need to be ugly about it
            # Grab the type of quote used in this moz.build file: single or double
            quote_type = line[line.index(find_str) - 1]

            if "[" not in line:
                # We'll want to put our new file onto its own line
                newline_to_add = "\n"
                # And copy the indentation of the line we're adding adjacent to
                indent_value = line[0 : line.index(quote_type)]
            else:
                # This is frustrating, we have the start of the array here. We aren't
                # going to be able to indent things onto a newline properly. We're just
                # going to have to stick it in on the same line.
                newline_to_add = ""
                indent_value = ""

            find_str = "%s%s%s" % (quote_type, find_str, quote_type)
            if replace_before:
                replacement_tuple = (
                    find_str,
                    newline_to_add,
                    indent_value,
                    quote_type,
                    unnormalized_filename_to_add,
                    quote_type,
                )
                replace_str = "%s,%s%s%s%s%s" % replacement_tuple
            else:
                replacement_tuple = (
                    quote_type,
                    unnormalized_filename_to_add,
                    quote_type,
                    newline_to_add,
                    indent_value,
                    find_str,
                )
                replace_str = "%s%s%s,%s%s%s" % replacement_tuple

            line = line.replace(find_str, replace_str)

        print(line, end="")  # line has its own newline on it, don't add a second
    file.close()