def git_apply_patch_file(patch_path, patch_dir): """ Apply |patch_path| to files in |patch_dir|. """ patch_name = os.path.basename(patch_path) sys.stdout.write('\nApply %s in %s\n' % (patch_name, patch_dir)) if not os.path.isfile(patch_path): sys.stdout.write('... patch file does not exist.\n') return 'fail' patch_string = open(patch_path, 'rb').read() if sys.platform == 'win32': # Convert the patch to Unix line endings. This is necessary to avoid # whitespace errors with git apply. patch_string = patch_string.replace('\r\n', '\n') # Git apply fails silently if not run relative to a respository root. if not is_checkout(patch_dir): sys.stdout.write('... patch directory is not a repository root.\n') return 'fail' # Output patch contents. cmd = '%s apply -p0 --numstat' % git_exe result = exec_cmd(cmd, patch_dir, patch_string) write_indented_output(result['out'].replace('<stdin>', patch_name)) # Reverse check to see if the patch has already been applied. cmd = '%s apply -p0 --reverse --check' % git_exe result = exec_cmd(cmd, patch_dir, patch_string) if result['err'].find('error:') < 0: sys.stdout.write('... already applied (skipping).\n') return 'skip' # Normal check to see if the patch can be applied cleanly. cmd = '%s apply -p0 --check' % git_exe result = exec_cmd(cmd, patch_dir, patch_string) if result['err'].find('error:') >= 0: sys.stdout.write('... failed to apply:\n') write_indented_output(result['err'].replace('<stdin>', patch_name)) return 'fail' # Apply the patch file. This should always succeed because the previous # command succeeded. cmd = '%s apply -p0' % git_exe result = exec_cmd(cmd, patch_dir, patch_string) if result['err'] == '': sys.stdout.write('... successfully applied.\n') else: sys.stdout.write('... successfully applied (with warnings):\n') write_indented_output(result['err'].replace('<stdin>', patch_name)) return 'apply'
def get_url(path='.'): """ Returns the origin url for the specified path. """ cmd = "%s config --get remote.origin.url" % git_exe result = exec_cmd(cmd, path) if result['out'] != '': return result['out'].strip() return 'Unknown'
def get_commit_number(path='.', branch='HEAD'): """ Returns the number of commits in the specified branch/tag/hash. """ cmd = "%s rev-list --count %s" % (git_exe, branch) result = exec_cmd(cmd, path) if result['out'] != '': return result['out'].strip() return '0'
def get_hash(path='.', branch='HEAD'): """ Returns the git hash for the specified branch/tag/hash. """ cmd = "%s rev-parse %s" % (git_exe, branch) result = exec_cmd(cmd, path) if result['out'] != '': return result['out'].strip() return 'Unknown'
def get_commit_number(path = '.', branch = 'HEAD'): """ Returns the number of commits in the specified branch/tag/hash. """ if is_ue_fork(path): return read_ue_fork_file(path)['revision'] else: cmd = "git rev-list --count %s" % branch result = exec_cmd(cmd, path) if result['out'] != '': return result['out'].strip() return '0'
def get_url(path = '.'): """ Returns the origin url for the specified path. """ if is_ue_fork(path): return read_ue_fork_file(path)['url'] else: cmd = "git config --get remote.origin.url" result = exec_cmd(cmd, path) if result['out'] != '': return result['out'].strip() return 'Unknown'
def get_hash(path = '.', branch = 'HEAD'): """ Returns the git hash for the specified branch/tag/hash. """ if is_ue_fork(path): return read_ue_fork_file(path)['commit'] else: cmd = "git rev-parse %s" % branch result = exec_cmd(cmd, path) if result['out'] != '': return result['out'].strip() return 'Unknown'
def get_svn_revision(path = '.', branch = 'HEAD'): """ Returns the SVN revision associated with the specified path and git branch/tag/hash. """ svn_rev = "None" cmd = "git log --grep=^git-svn-id: -n 1 %s" % (branch) result = exec_cmd(cmd, path) if result['err'] == '': for line in result['out'].split('\n'): if line.find("git-svn-id") > 0: svn_rev = line.split("@")[1].split()[0] break return svn_rev
def yapf_format(file_name, file_contents): # Reads .style.yapf in the root_dir when specifying contents via stdin. result = exec_cmd("%s %s/yapf" % (sys.executable, script_dir), root_dir, file_contents) if result['err'] != '': print "yapf error: %s" % result['err'] if result['out'] != '': output = result['out'] if sys.platform == 'win32': # Convert to Unix line endings. output = output.replace("\r", "") return output return None
def clang_format(file_name, file_contents): # -assume-filename is necessary to find the .clang-format file and determine # the language when specifying contents via stdin. result = exec_cmd("%s -assume-filename=%s" % (clang_format_exe, file_name), \ root_dir, file_contents) if result['err'] != '': print "clang-format error: %s" % result['err'] if result['out'] != '': output = result['out'] if sys.platform == 'win32': # Convert to Unix line endings. output = output.replace("\r", "") return output return None
def get_changed_files(path, hash): """ Retrieves the list of changed files. """ if hash == 'unstaged': cmd = "%s diff --name-only" % git_exe elif hash == 'staged': cmd = "%s diff --name-only --cached" % git_exe else: cmd = "%s diff-tree --no-commit-id --name-only -r %s" % (git_exe, hash) result = exec_cmd(cmd, path) if result['out'] != '': files = result['out'] if sys.platform == 'win32': # Convert to Unix line endings. files = files.replace('\r\n', '\n') return files.strip().split("\n") return []
def get_svn_info(path = '.'): """ Retrieves the URL and revision from svn info. """ url = 'None' rev = 'None' cmd = "svn info --xml %s" % (path) is_http = path[0:4] == 'http' if is_http or os.path.exists(path): result = exec_cmd(cmd, path if not is_http else '.') if result['err'] == '': tree = ET.ElementTree(ET.fromstring(result['out'])) entry = tree.getroot().find('entry') url = entry.find('url').text rev = entry.attrib['revision'] else: raise Exception("Failed to execute svn info: %s" % (result['err'])) return {'url': url, 'revision': rev}
def get_undefined_symbols(file): """ Returns the undefined symbols imported by |file|. """ symbols = [] # Each symbol line has a value like: # cef_sandbox.a:cef_sandbox.o: _memcpy cmdline = 'nm -u -A %s' % file result = exec_cmd(cmdline, os.path.join(cef_dir, 'tools')) if len(result['err']) > 0: raise Exception('ERROR: nm failed: %s' % result['err']) for line in result['out'].split('\n'): if line.find(': ') < 0: continue symbol = line[line.rfind(': ') + 2:] symbols.append(symbol) return symbols
def get_exported_symbols(file): """ Returns the global symbols exported by |file|. """ symbols = [] # Each symbol line has a value like: # 0000000000000000 T _cef_sandbox_initialize cmdline = 'nm -g -U %s' % file result = exec_cmd(cmdline, os.path.join(cef_dir, 'tools')) if len(result['err']) > 0: raise Exception('ERROR: nm failed: %s' % result['err']) for line in result['out'].split('\n'): if line.find(' T ') < 0: continue symbol = line[line.rfind(' ') + 1:] symbols.append(symbol) return symbols
# Retrieve the list of paths modified by the patch file. patch_paths = extract_paths(patch_file) # List of paths added by the patch file. added_paths = [] if not options.resave: # Revert any changes to existing files in the patch. for patch_path in patch_paths: patch_path_abs = os.path.abspath(os.path.join(patch_root_abs, \ patch_path)) if os.path.exists(patch_path_abs): msg('Reverting changes to %s' % patch_path_abs) cmd = 'git checkout -- %s' % (patch_path_abs) result = exec_cmd(cmd, patch_root_abs) if result['err'] != '': msg('Failed to revert file: %s' % result['err']) msg('Deleting file %s' % patch_path_abs) os.remove(patch_path_abs) added_paths.append(patch_path_abs) if result['out'] != '': sys.stdout.write(result['out']) else: msg('Skipping non-existing file %s' % patch_path_abs) added_paths.append(patch_path_abs) if not options.revert: # Apply the patch file. msg('Applying patch to %s' % patch_root_abs) result = exec_cmd('patch -p0', patch_root_abs, patch_file)
# Retrieve the list of paths modified by the patch file. patch_paths = extract_paths(patch_file) # List of paths added by the patch file. added_paths = [] if not options.resave: # Revert any changes to existing files in the patch. for patch_path in patch_paths: patch_path_abs = os.path.abspath(os.path.join(patch_root_abs, \ patch_path)) if os.path.exists(patch_path_abs): msg('Reverting changes to %s' % patch_path_abs) cmd = 'git checkout -- %s' % (patch_path_abs) result = exec_cmd(cmd, patch_root_abs) if result['err'] != '': msg('Failed to revert file: %s' % result['err']) msg('Deleting file %s' % patch_path_abs) os.remove(patch_path_abs) added_paths.append(patch_path_abs) if result['out'] != '': sys.stdout.write(result['out']) else: msg('Skipping non-existing file %s' % patch_path_abs) added_paths.append(patch_path_abs) if not options.revert: # Apply the patch file. msg('Applying patch to %s' % patch_root_abs) patch_string = open(patch_file, 'rb').read()
patch_root_abs = os.path.abspath(os.path.join(cef_dir, patch_root)) # Retrieve the list of paths modified by the patch file. patch_paths = extract_paths(patch_file) if not options.resave: # Revert any changes to existing files in the patch. for patch_path in patch_paths: patch_path_abs = os.path.abspath(os.path.join(patch_root_abs, \ patch_path)) msg('Reverting changes to %s' % patch_path_abs) if src_is_git: cmd = 'git checkout -- %s' % (patch_path_abs) else: cmd = 'svn revert %s' % (patch_path_abs) result = exec_cmd(cmd, patch_root_abs) if result['err'] != '': raise Exception('Failed to revert file: %s' % result['err']) if result['out'] != '': sys.stdout.write(result['out']) # Apply the patch file. msg('Applying patch to %s' % patch_root_abs) result = exec_cmd('patch -p0 < %s' % patch_file, patch_root_abs) if result['err'] != '': raise Exception('Failed to apply patch file: %s' % result['err']) sys.stdout.write(result['out']) if result['out'].find('FAILED') != -1: warn('Failed to apply %s, fix manually and run with --resave' % \ patch['name']) continue
# Revert any changes to existing files in the patch. for patch_path in patch_paths: patch_path_abs = os.path.abspath(os.path.join(patch_root_abs, \ patch_path)) if os.path.exists(patch_path_abs): if options.backup: backup_path_abs = patch_path_abs + backup_ext if not os.path.exists(backup_path_abs): msg('Creating backup of %s' % patch_path_abs) copy_file(patch_path_abs, backup_path_abs) else: msg('Skipping backup of %s' % patch_path_abs) msg('Reverting changes to %s' % patch_path_abs) cmd = 'git checkout -- %s' % (patch_path_abs) result = exec_cmd(cmd, patch_root_abs) if result['err'] != '': msg('Failed to revert file: %s' % result['err']) msg('Deleting file %s' % patch_path_abs) os.remove(patch_path_abs) added_paths.append(patch_path_abs) if result['out'] != '': sys.stdout.write(result['out']) else: msg('Skipping non-existing file %s' % patch_path_abs) added_paths.append(patch_path_abs) if not options.revert: # Chromium files are occasionally (incorrectly) checked in with Windows # line endings. This will cause the patch tool to fail when attempting # to patch those files on Posix systems. Convert any such files to Posix
patch_root_abs = os.path.abspath(os.path.join(cef_dir, patch_root)) # Retrieve the list of paths modified by the patch file. patch_paths = extract_paths(patch_file) if not options.resave: # Revert any changes to existing files in the patch. for patch_path in patch_paths: patch_path_abs = os.path.abspath(os.path.join(patch_root_abs, \ patch_path)) msg('Reverting changes to %s' % patch_path_abs) if src_is_git: cmd = 'git checkout -- %s' % (patch_path_abs) else: cmd = 'svn revert %s' % (patch_path_abs) result = exec_cmd(cmd, patch_root_abs) if result['err'] != '': raise Exception('Failed to revert file: %s' % result['err']) if result['out'] != '': sys.stdout.write(result['out']) # Apply the patch file. msg('Applying patch to %s' % patch_root_abs) result = exec_cmd('patch -p0', patch_root_abs, patch_file) if result['err'] != '': raise Exception('Failed to apply patch file: %s' % result['err']) sys.stdout.write(result['out']) if result['out'].find('FAILED') != -1: warn('Failed to apply %s, fix manually and run with --resave' % \ patch['name']) continue
def is_ancestor(path='.', commit1='HEAD', commit2='master'): """ Returns whether |commit1| is an ancestor of |commit2|. """ cmd = "%s merge-base --is-ancestor %s %s" % (git_exe, commit1, commit2) result = exec_cmd(cmd, path) return result['ret'] == 0