def parse_args(): parser = argparse.ArgumentParser(description='Apply patches to a git repo') parser.add_argument('-f', '--force', default=False, action='store_true', help='Do not stop on the first failed patch.') parser.add_argument('--project-root', required=False, default=git.get_repo_root(os.path.abspath(__file__)), help='A folder to resolve repos relative paths against') parser.add_argument('-R', '--reverse', default=False, action='store_true', help='Apply patches in reverse.') parser.add_argument('-r', '--repo', help='Path to a repository root folder.') paths_group = parser.add_mutually_exclusive_group(required=True) paths_group.add_argument('-d', '--directory', help='Path to a directory with patches. \ If present, -p/--patch is ignored.') paths_group.add_argument('-p', '--patch', nargs='*', help='Path(s) to a patch file(s).') args = parser.parse_args() # Additional rules. if args.patch is not None and args.repo is None: parser.error("Repository path (-r/--repo) is required \ when you supply patches list.") return args
def parse_args(): parser = argparse.ArgumentParser(description='Apply patches to a git repo') parser.add_argument('-f', '--force', default=False, action='store_true', help='Do not stop on the first failed patch.') parser.add_argument( '--project-root', required=False, default=git.get_repo_root(os.path.abspath(__file__)), help='Parent folder to resolve repos relative paths against') parser.add_argument('-R', '--reverse', default=False, action='store_true', help='Apply patches in reverse.') parser.add_argument('-r', '--repo', help='Path to a repository root folder.') paths_group = parser.add_mutually_exclusive_group(required=True) paths_group.add_argument( '-d', '--directory', help= 'Path to a directory with patches. If present, -p/--patch is ignored.') paths_group.add_argument('-p', '--patch', nargs='*', help='Path(s) to a patch file(s).') args = parser.parse_args() # Additional rules. if args.patch is not None and args.repo is None: parser.error( "Repository path (-r/--repo) is required when you supply patches list." ) return args
def _get_repo_root(filename): """Returns the root of the repository containing the given file. Args: filename: A source file or directory in the repository. """ # CMake builds can be run out of source tree, so use the directory containing # a source file as the starting point for the calculation. if os.path.isdir(filename): dir_in_repo = filename else: dir_in_repo = os.path.dirname(filename) starting_dir = os.getcwd() os.chdir(dir_in_repo) root_dir = git.get_repo_root() os.chdir(starting_dir) return root_dir
def default_args(): """Returns a default list of directories to scan. """ toplevel = git.get_repo_root() return [os.path.join(toplevel, dirname) for dirname in _DEFAULT_DIRS]
def main(): """Read an binary input file and output to a C/C++ source file as an array. """ if not git.is_within_repo(): raise ImportError( "This script must be run from within a Git repository.") args = arg_parser.parse_args() input_file = args.input input_file_base = os.path.splitext(args.input)[0] output_source = args.output_source if not output_source: output_source = input_file_base + ".cc" logging.debug("Using default --output_source='%s'", output_source) output_header = args.output_header if not output_header: output_header = input_file_base + ".h" logging.debug("Using default --output_header='%s'", output_header) absolute_dir = path.dirname(output_header) relative_dir = path.relpath(absolute_dir, git.get_repo_root()) relative_header_path = path.join(relative_dir, path.basename(output_header)) identifier_base = sub("[^0-9a-zA-Z]+", "_", path.basename(input_file_base)) array_name = args.array if not array_name: array_name = identifier_base + "_data" logging.debug("Using default --array='%s'", array_name) array_size_name = args.array_size if not array_size_name: array_size_name = identifier_base + "_size" logging.debug("Using default --array_size='%s'", array_size_name) fileid = args.filename_identifier if not fileid: fileid = identifier_base + "_filename" logging.debug("Using default --filename_identifier='%s'", fileid) filename = args.filename if filename is None: # but not if it's the empty string filename = path.basename(input_file) logging.debug("Using default --filename='%s'", filename) header_guard = args.header_guard if not header_guard: header_guard = sub("[^0-9a-zA-Z]+", "_", relative_header_path).upper() + '_' # Avoid double underscores to stay compliant with the Standard. header_guard = sub("[_]+", "_", header_guard) logging.debug("Using default --header_guard='%s'", header_guard) namespace = args.cpp_namespace namespaces = namespace.split("::") if namespace else [] with open(input_file, "rb") as infile: input_bytes = bytearray(infile.read()) logging.debug("Read %d bytes from %s", len(input_bytes), input_file) header_text = "\n".join( header(header_guard, namespaces, array_name, array_size_name, fileid)) source_text = "\n".join( source(namespaces, array_name, array_size_name, fileid, filename, input_bytes, relative_header_path)) with open(output_header, "w") as hdr: hdr.write(header_text) logging.debug("Wrote header file %s", output_header) with open(output_source, "w") as src: src.write(source_text) logging.debug("Wrote source file %s", output_source)