def replace_file_lines(fname, *args): """Reads all the lines from the given file and applies the provided replacement patterns to it. Replacement patterns are formatted (compiled_regex, replacement). Every replacement pattern must be used, or this function will raise a CalledProcessError. Patterns can be used more than once. Earlier replacement patterns will short-circuit later ones: First pattern: (a, a) Second pattern: (a, c) The second pattern will never match, since the first pattern is identical to it. """ replacement_succeeded = [False for arg in args] with open(fname, 'r') as infile, open(fname + '.tmp', 'w') as outfile: for line in infile: for i, pattern in enumerate(args): # This will raise an IndexError if the args are not structured properly regex, replacement = pattern[0], pattern[1] (outline, replaced) = regex.subn(replacement, line) if replaced > 0: replacement_succeeded[i] = True break outfile.write(outline) if all(replacement_succeeded): os_helper.check_call('mv {} {}'.format(fname + '.tmp', fname)) else: error_output = "Didn't find all provided patterns in {}\n\tPatterns:".format( fname) for arg in args: error_output += \ "\n\tpattern: {} replacement: {}".format(arg[0], arg[1]) print RED + error_output + END raise subprocess.CalledProcessError(1, cmd="replace_file_lines", output=error_output)
def create_release_branch(version_string, commit_hash=None, internal=False, test=False): """Creates a branch named 'release-$version_string' from the provided commit_hash Uses the tip of master if no commit_hash is present. """ if commit_hash is None: if not test: git_helper.checkout("master") git_helper.pull() commit_hash = git_helper.current_hash() internal_prefix = "internal-" if internal else "" release_branch = "{}release-{}".format(internal_prefix, version_string) # Check for existence of branch on origin, fail if it exists. release_branch_found = os_helper.check_output( "git ls-remote --heads origin | grep refs/heads/{}".format( release_branch)) if test or not release_branch_found: os_helper.check_call("git checkout -b {} {}".format( release_branch, commit_hash)) return release_branch else: raise subprocess.CalledProcessError( 1, cmd="create_release_branch({})".format(release_branch), output="Release branch {} exists on the origin remote.".format( release_branch))
def update_mopub_sdk_submodules(external_only=False): """Pulls the latest MoPub Android and iOS SDK releases, both internal and external""" os_helper.check_call( 'git --git-dir mopub-android-sdk/.git pull origin master') os_helper.check_call('git --git-dir mopub-ios-sdk/.git pull origin master') if not external_only: os_helper.check_call( 'git --git-dir mopub-android/.git pull origin master') os_helper.check_call('git --git-dir mopub-ios/.git pull origin master')
def build_wrappers_and_export_unity_package(branch_name): """Runs the build.sh script.""" os_helper.check_call("./scripts/build.sh")
def commit_all_changes(branch_name, commit_message=None, extra_args=""): """Commits all file changes with an optional commit message.""" message = commit_message or "Commit generated by release.py" os_helper.check_call('git commit {} -am "{}"'.format(extra_args, message)) return git_helper.current_hash()
def recover_mopub_sdk_submodules(branch): """Brings back the Android and iOS submodules""" os_helper.check_call('git submodule init') os_helper.check_call('git submodule update --recursive')
def reset_mopub_sdk_submodules(): """Resets the git state of the Android and iOS submodules""" os_helper.check_call('git --git-dir mopub-android-sdk/.git checkout .') os_helper.check_call('git --git-dir mopub-android-sdk/.git clean -df') os_helper.check_call('git --git-dir mopub-ios-sdk/.git checkout .') os_helper.check_call('git --git-dir mopub-ios-sdk/.git clean -df')