# -*- coding: utf-8 -*-
#!/usr/bin/python
##-------------------------------------------------------------------
## @copyright 2017 DennyZhang.com
## Licensed under MIT
##   https://raw.githubusercontent.com/DennyZhang/devops_public/master/LICENSE
##
## File : git_create_tag.py
## Author : Denny <denny@dennyzhang.com>
## Description : Create monthly git tag for all repos in your projects
## Dependency:
##        pip install GitPython
##
## Sample:
##        python git_create_tag.py --tag_name "2017-08-01" --delete_tag_already_exists \
##               --git_list_file "/tmp/git_list.txt"
##
##        Sample of git_list_file
##                   git@bitbucket.org:dennyzhang/devops.git
##                   git@bitbucket.org:dennyzhang/frontend.git
##                   git@bitbucket.org:dennyzhang/backend.git
## --
## Created : <2017-03-24>
## Updated: Time-stamp: <2017-05-20 00:12:12>
##-------------------------------------------------------------------
import os, sys
import logging
import argparse

import datetime
# Install package first: pip install GitPython
import git

# TODO:
log_file = "/var/log/%s.log" % (os.path.basename(__file__).rstrip('\.py'))
logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s %(message)s')
logging.getLogger().addHandler(logging.StreamHandler())

# TODO: handle with git passphrase
def get_repo_name(repo_url):
    # https://github.com/DennyZhang/devops_public.git -> devops_public
    # git@github.com:DennyZhang/devops_public.git -> devops_public
    l = repo_url.split(".")
    return l[-2].split("/")[-1]

def git_create_tag(git_repo, tag_name, delete_tag_already_exists):
    # https://gitpython.readthedocs.io/en/stable/tutorial.html#meet-the-repo-type
    if tag_name in git_repo.tags:
        if delete_tag_already_exists is True:
            logging.info("Tag(%s) already exists, delete it first. Git repo: %s" % \
                         (tag_name, git_repo.working_dir))
            git_repo.delete_tag(tag_name)
            git_repo.git.execute(["git", "push", "--delete", "origin", tag_name])
        else:
            logging.warn("Tag(%s) already exists, skip current process.")
            return True

    # TODO: check return code
    logging.info("Create local tag(%s)" % (tag_name))
    git_repo.create_tag(tag_name, message = "Automatically create git tag.")
    logging.info("Push local tag(%s) to remote" % (tag_name))
    git_repo.remotes.origin.push(tag_name)
    return True

def git_list_create_tag(working_dir, git_list_file, tag_name, delete_tag_already_exists):
    git_list = []
    with open(git_list_file,'r') as f:
        for row in f:
            row = row.strip()
            if row == "" or row.startswith('#'):
                continue
            git_list.append(row)

    for repo_url in git_list:
        code_dir = "%s/%s" % (working_dir, get_repo_name(repo_url))
        git_repo = None
        if os.path.exists(code_dir):
            # re-use current code folder
            git_repo = git.Repo(code_dir)
        else:
            git_repo = git.Repo.clone_from(repo_url, code_dir)

        git_create_tag(git_repo, tag_name, delete_tag_already_exists)
    return True

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
##        python git_create_tag.py -tag_name "2017-08-01" --delete_tag_already_exists \
##               --git_list_file /tmp/git_list.txt"
    parser.add_argument('--tag_name', required=False, default='', \
                        help="What tag name to create for git repos", type=str)
    parser.add_argument('--git_list_file', required=False, default="", \
                        help="The file should specify a list of git repos", type=str)
    parser.add_argument('--delete_tag_already_exists', dest='delete_tag_already_exists', \
                        action='store_true', default=False, \
                        help="If enabled, we will delete existing tag if it already exists")
    parser.add_argument('--working_dir', required=False, default="/tmp", \
                        help="Working directory for creating git tags", type=str)

    l = parser.parse_args()
    tag_name = l.tag_name
    delete_tag_already_exists = l.delete_tag_already_exists
    git_list_file = l.git_list_file
    working_dir = l.working_dir

    if tag_name == '':
       tag_name = datetime.datetime.utcnow().strftime("%Y-%m-%d")

    if git_list_create_tag(working_dir, git_list_file, tag_name, delete_tag_already_exists) is True:
        logging.info("OK: Action is done successfully.")
        sys.exit(0)
    else:
        logging.error("ERROR: Action has failed.")
        sys.exit(0)
## File : git_create_tag.py ends

@@ -976,23 +976,8 @@
 sys%0A
-import logging%0A
 impo
@@ -988,16 +988,16 @@
 rgparse%0A
+
 %0Aimport 
@@ -1068,242 +1068,8 @@
 it%0A%0A
-# TODO:%0Alog_file = %22/var/log/%25s.log%22 %25 (os.path.basename(__file__).rstrip('%5C.py'))%0Alogging.basicConfig(filename=log_file, level=logging.DEBUG, format='%25(asctime)s %25(message)s')%0Alogging.getLogger().addHandler(logging.StreamHandler())%0A%0A
 # TO
@@ -1568,28 +1568,21 @@
         
-logging.info
+print
 (%22Tag(%25s
@@ -1634,23 +1634,16 @@
 %25s%22 %25 %5C%0A
-       
         
@@ -1840,22 +1840,24 @@
     
-logging.warn(%22
+print(%22Warning: 
 Tag(
@@ -1959,28 +1959,21 @@
 ode%0A    
-logging.info
+print
 (%22Create
@@ -2083,28 +2083,21 @@
 .%22)%0A    
-logging.info
+print
 (%22Push l
@@ -4211,20 +4211,13 @@
     
-logging.info
+print
 (%22OK
@@ -4291,21 +4291,13 @@
     
-logging.error
+print
 (%22ER

remove logging