Example #1
0
def main(sys_args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-s", "--settings_path", dest="settings_path", default=None)
    parser.add_argument("-d", "--directory", dest="directory", default=None)
    parser.add_argument(
        "-c", "--changelog", help="also generate debian/changelog",
        dest="changelog", action="store_true",
        default=False)

    sys_args = sys_args if sys_args is not None else sys.argv[1:]
    args = parser.parse_args(sys_args)
    directory = args.directory or os.getcwd()

    settings_path = args.settings_path or os.path.join(directory, "deb.json")
    with open(settings_path, "rb") as settings_fp:
        settings = json.loads(settings_fp.read().decode("utf8"))

    initialize_debian_folder(settings, directory)

    if args.changelog:
        from debfolder import git_helpers
        project = settings["project"]
        changelog = []
        for commit in git_helpers.parse_commits(cwd=directory):
            log_line = git_helpers.generate_changelog_entry(project, commit)
            changelog.append(log_line)

        changelog_path = os.path.join(directory, "debian", "changelog")
        with open(changelog_path, "wb") as changelog_fp:
            changelog_fp.write("\n".join(changelog).encode("utf8"))
    def test_parse_commits_allows_custom_cwd_and_entries(self):
        self.add_git_log_format_result("%cn <%ce>", entries=1)
        self.add_git_log_format_result("%ct", entries=1)
        self.add_git_log_format_result("%h", entries=1)
        self.add_git_log_format_result("%s", entries=1)

        commits = git_helpers.parse_commits(cwd="/path", entries=1)

        self.assertEqual([{
            "committer": "Joe <*****@*****.**>",
            "hash": "hash",
            "timestamp": "1439654861",
            "message": "Commit message."
        }], commits)

        self.assert_all_calls_made()
        self.assert_cwd_equals("/path")
    def test_parse_commits_allows_custom_cwd_and_entries(self):
        self.add_git_log_format_result("%cn <%ce>", entries=1)
        self.add_git_log_format_result("%ct", entries=1)
        self.add_git_log_format_result("%h", entries=1)
        self.add_git_log_format_result("%s", entries=1)

        commits = git_helpers.parse_commits(cwd="/path", entries=1)

        self.assertEqual([{
            "committer": "Joe <*****@*****.**>",
            "hash": "hash",
            "timestamp": "1439654861",
            "message": "Commit message."
        }], commits)

        self.assert_all_calls_made()
        self.assert_cwd_equals("/path")
    def test_parse_commits_extracts_commits_from_git_repository(self):
        self.add_git_log_format_result("%cn <%ce>")
        self.add_git_log_format_result("%ct")
        self.add_git_log_format_result("%h")
        self.add_git_log_format_result("%s")

        commits = git_helpers.parse_commits()
        self.assertEqual([{
            "committer": "Joe <*****@*****.**>",
            "hash": "hash",
            "timestamp": "1439654861",
            "message": "Commit message."
        }, {
            "committer": "Jane <*****@*****.**>",
            "hash": "hash2",
            "timestamp": "1439615320",
            "message": "Other commit message."
        }], commits)

        self.assert_all_calls_made()
    def test_parse_commits_extracts_commits_from_git_repository(self):
        self.add_git_log_format_result("%cn <%ce>")
        self.add_git_log_format_result("%ct")
        self.add_git_log_format_result("%h")
        self.add_git_log_format_result("%s")

        commits = git_helpers.parse_commits()
        self.assertEqual([{
            "committer": "Joe <*****@*****.**>",
            "hash": "hash",
            "timestamp": "1439654861",
            "message": "Commit message."
        }, {
            "committer": "Jane <*****@*****.**>",
            "hash": "hash2",
            "timestamp": "1439615320",
            "message": "Other commit message."
        }], commits)

        self.assert_all_calls_made()