Exemplo n.º 1
0
def commitFile(config, path="../"):
    git = Repo(path).git
    git.add(".")
    defaultMsg = "{} {}".format(config['name'],
                                timeStamp.strftime("%Y-%m-%d %H:%M:%S"))
    commitMsg = get("Commit message", default=defaultMsg)
    git.commit("-m", commitMsg)
    return git.log("--pretty=format:%H", "-1")
Exemplo n.º 2
0
class MonitorHandler(PatternMatchingEventHandler):
    def __init__(self, *args, **kwargs):
        super(MonitorHandler, self).__init__(*args, **kwargs)
        self.git = Repo(os.getcwd()).git

    def on_created(self, event):
        print("created")

    def on_deleted(self, event):
        print("deleted")

    def on_modified(self, event):
        print("Adding files to git")
        self.git.add('./')
        print("Commiting files to git")
        self.git.commit('-m', 'Added new image')
        print("Pushing files to remote repository")
        self.git.push()
Exemplo n.º 3
0
def solved_and_commit(index):
    collection = get_collection()
    problem = collection.find_one({'index': index})
    if not problem:
        raise Exception('problem %s not found' % index)

    solution_file = regular_file(index, problem['title'])
    code_file = index.lower() + '.cpp'

    # move files
    to_move = {}
    path_map = {'cpp': 'cpp', 'md': 'solutions'}
    for f in [solution_file, code_file]:
        if not os.path.exists(f):
            warnings.warn('solutions file <%s> not exists' % f)
            continue

        suffix = f.rsplit('.', 1)[-1]
        if suffix in path_map:
            path = os.path.join(path_map[suffix], f)
            to_move[f] = path
        else:
            warnings.warn('<%s> stay here' % f)

    print('Plan to Move:\n')
    for src, dst in to_move.items():
        print('\t %s ---> %s' % (src, dst))
    input('\nReady to Move?')

    for src, dst in to_move.items():
        shutil.move(src, dst)

    # update solved tag and  update index table?
    collection.update_one({'index': index}, {'$set': {'solved': True}})
    update_index_table()

    # git commit and push
    git = Repo('../').git
    print(git.add('.'))
    print(git.status())

    default_summary = '%s %s, Solved' % (index, problem['title'])
    commit = input('\nCheck git status, and Input Summary: [%s]\n' %
                   default_summary)
    commit = commit or default_summary
    print(git.commit('-m', commit))

    input('Ready to Push?')
    print(git.push())
Exemplo n.º 4
0
class Repository:
    def __init__(self, submissions_directory):
        self.submissions_directory = submissions_directory
        self.submission_json_path = \
          os.path.join(self.submissions_directory, "submissions.json")
        self.readme_path = os.path.join(self.submissions_directory,
                                        "README.md")
        self.author = config.get_author()
        if not os.path.exists(self.submissions_directory):
            self.init()
        self.git = Repo(self.submissions_directory).git
        self.submissions = config.load_submissions_data(
            self.submission_json_path)

    def init(self):
        if not os.path.exists(self.submissions_directory):
            git = Repo.init(self.submissions_directory).git
            git.config("user.email", config.get_author_email())
            git.config("user.name", config.get_author_name())
            shutil.copy2(str(config.RESOURCES_DIR.joinpath("readme.template")),
                         self.readme_path)
            git.add("README.md")
            date = datetime.now().strftime('%b/%d/%Y %H:%M')
            git.commit(message="Initial commit with README.md",
                       date="{}".format(date),
                       author=self.author)

    def add(self, file_path):
        self.git.add(os.path.abspath(file_path))
        self.git.add(os.path.abspath(self.readme_path))
        self.git.add(os.path.abspath(self.submission_json_path))

    def commit(self, commit_message, timestamp):
        self.git.commit(message=commit_message,
                        date=timestamp,
                        author=self.author)

    def push(self, force_push=False):
        remote_url = config.get_remote_url()
        if not remote_url:
            print(
                "\U00002757",
                "The remote git repository url is not defined, skipping push")
        else:
            try:
                self.git.remote("add", "origin", remote_url)
            except GitCommandError:
                pass
            args = ["origin", "master"]
            if force_push:
                args.insert(0, "-f")
            self.git.push(*args)
            print(
                "\U0001F44C",
                "The updates were automatically pushed to the remote repository"
            )
Exemplo n.º 5
0
def solved_and_commit(index):
    leetcode = get_db()
    problem = leetcode.find_one({'index': index})
    if not problem:
        raise Exception('problem %s not found' % index)

    title = '%s. %s' % (index, problem['title'])
    files = [title + '.md', title + '.py']

    # move files
    to_move = {}
    path_map = {'py': 'python', 'md': 'solutions'}
    for f in files:
        if not os.path.exists(f):
            warnings.warn('solution file <%s> not exists' % f)
            continue

        suffix = f.rsplit('.', 1)[-1]
        if suffix in path_map:
            path = os.path.join(path_map[suffix], f)
            to_move[f] = path
        else:
            warnings.warn('<%s> stay here' % f)

    print('Plan to Move:\n')
    for src, dst in to_move.items():
        print('\t %s ---> %s' % (src, dst))
    input('\nReady to Move?')

    for src, dst in to_move.items():
        shutil.move(src, dst)

    # udpate readme and tables
    generate_readme()
    generate_tables_by_tag()

    # git commit and push
    git = Repo('.').git
    print(git.add('.'))
    print(git.status())

    default_summary = '%s, Solved' % title
    commit = input('\nCheck git status, and Input Summary: [%s]\n' % default_summary)
    commit = commit or default_summary
    print(git.commit('-m', commit))

    input('Ready to Push?')
    print(git.push())
Exemplo n.º 6
0
import JDTools
import os

from git import Repo

# Commit Changes
JDTools.commit()

# GITHUB Repo
repo_path = os.path.join(os.environ['GITHUB_WORKSPACE'])

repo = Repo(repo_path).git

changes = repo.status('--porcelain').strip()
if (len(changes) > 0):
    repo.add('-A')
    repo.commit(m="构建码表")
    repo.push()
Exemplo n.º 7
0
def release_kuber(context, app, version):
    # check whether the app belongs to the context
    if app not in config.app_dict.get(context, []):
        log.error('Provided app does not belong to the context')
        return

    git = Repo('{}/..'.format(os.getcwd())).git
    git.checkout('master')
    git.pull()

    try:
        # # check whether a container with app name and provided version number exists
        # container_tags = subprocess.check_output(['gsutil', 'ls', '{}/{}'.format(config.gs_path_to_app, app)])
        # container_exist = False
        #
        # for tag in container_tags.split('\n'):
        #     # tag sample:
        #     # gs://artifacts.sojern-platform.appspot.com/containers/repositories/library/applier/tag_2.3.11
        #     version_list = re.findall('\d+\.\d+\.\d+', tag.split('/')[-1])
        #     if version_list and version_list[0] == version:
        #         container_exist = True
        #         break
        #
        # if not container_exist:
        #     log.error('The container with provided app and version number does not exist')
        #     return

        # modify app.yml file with new version
        yaml_file = '{}.yml'.format(app)

        with open(yaml_file) as f:
            yaml_dict = yaml.load(f)

        _update_yaml_file(yaml_dict, version)

        with open(yaml_file, 'w') as f:
            yaml.dump(yaml_dict, f, default_flow_style=False)

        # get replication controller name, sample output of this command:
        # CONTROLLER              CONTAINER (S)     IMAGES            SELECTOR     REPLICAS
        # backend-controller      applier           applier_2.3.11    applier      3
        rc_name = subprocess.check_output(['kubectl', '--context={}'.format(context), 'get', 'rc',
                                           '--selector=run={}'.format(app)]).split('\n')[1].split()[0]

        print 'controller name:', rc_name
        # run rolling update
        exit_code = subprocess.call(['kubectl', '--context=vagrant', 'rolling-update',
                                     '{}'.format(rc_name), '-f', yaml_file])

        # if rolling update succeeds, commit changes in Git repo and push back to master
        if exit_code == 0:
            log.info('Rolling update {} to {} successfully'.format(app, version))
            git.add(yaml_file)
            git.commit(m='bump {} to {}'.format(app, version))
            git.push()
        else:
            log.error('Errors in rolling update command, exit code:{}'.format(exit_code))
            git.checkout('.')

    except Exception as e:
        git.checkout('.')
        log.exception('Exception:{}'.format(e))
Exemplo n.º 8
0
def push(update, context):
    if update.effective_user.username not in ALLOWED_USER:
        return -1

    if 'requested_code' not in context.user_data:
        REPLY(update, '正在构建,请稍候\.\.\.')

        TYPING(update)

        try:
            JDTools.commit()

            repo = Repo('.').git

            changes = repo.status('--porcelain').strip()
            if (len(changes) > 0):
                repo.add('-A')
                repo.commit(m="更新码表\n%s" % "\n".join(LOG_STATUS))
                repo.push()

                REPLY(update, '构建完毕,Push成功')
            else:
                REPLY(update, '构建完毕,码表无改动')

            LOG_STATUS.clear()
        except Exception as e:
            REPLY(update, '构建失败')
            raise e
            return -1

        TYPING(update)

        try:
            # try load session
            auth_provider.load_session()
            auth_provider.refresh_token()
        except:
            # get new session
            auth_url = client.auth_provider.get_auth_url(redirect_uri)
            REPLY(update, "请登录OneDrive:\n %s\n并输入CODE" % auth_url,
                  ParseMode.HTML)
            context.user_data['requested_code'] = True
            return 12
    else:
        context.user_data.clear()
        code = update.message.text
        try:
            client.auth_provider.authenticate(code, redirect_uri,
                                              client_secret)
            auth_provider.save_session()
        except:
            REPLY(update, "OneDrive登录失败")
            return -1

    try:
        TYPING(update)
        client.item(drive='me', path=os.environ['ONEDRIVE_PATH']).children[
            'xkjd27c.cizu.dict.yaml'].upload('./rime/xkjd27c.cizu.dict.yaml')
        client.item(drive='me', path=os.environ['ONEDRIVE_PATH']).children[
            'xkjd27c.danzi.dict.yaml'].upload('./rime/xkjd27c.danzi.dict.yaml')
        client.item(drive='me', path=os.environ['ONEDRIVE_PATH']
                    ).children['xkjd27c.chaojizici.dict.yaml'].upload(
                        './rime/xkjd27c.chaojizici.dict.yaml')
        REPLY(update, "OneDrive上传成功")
    except Exception as e:
        REPLY(update, "OneDrive上传失败: \n%s" % e, ParseMode.HTML)

    return -1
def release_kuber(context, app, version):
    # check whether the app belongs to the context
    if app not in config.app_dict.get(context, []):
        log.error('Provided app does not belong to the context')
        return

    to_kuber = os.path.join(os.getcwd(), 'hellonode')
    git = Repo(os.path.abspath('{}/..'.format(to_kuber))).git
    git = Repo('{}'.format(os.getcwd())).git
    git.checkout('master')
    git.pull()

    try:
        try:
        # check whether a container with app name and provided version number exists
            print version
            version = version if version.startswith('v') else 'v'+version
            print version
            container_tags = subprocess.check_output(['gsutil', 'ls', '{}/{}/tag_{}'.format(
                                config.gs_path_to_app, app, version)])
            log.info('App and version check successful')
        except:
            log.error('The container with provided app and version number does not exist')
            return

        # modify app.yml file with new version
        yaml_file = '{}.yml'.format(app)

        print os.getcwd()
        with open(yaml_file) as f:
            yaml_dict = yaml.load(f)

        _update_yaml_file(yaml_dict, version)

        with open(yaml_file, 'w') as f:
            yaml.dump(yaml_dict, f, default_flow_style=False)

        # get replication controller name, sample output of this command:
        # CONTROLLER              CONTAINER (S)     IMAGES            SELECTOR     REPLICAS
        # backend-controller      applier           applier_2.3.11    applier      3
        rc_name = subprocess.check_output(['kubectl','get', 'deployment',
                                           '{}'.format(app)]).split('\n')[1].split()[0]

        log.info('Applying New Configuration')
        # run rolling update
        exit_code = subprocess.call(['kubectl', 'apply', '-f', yaml_file])

        # if rolling update succeeds, commit changes in Git repo and push back to master
        if exit_code == 0:
            log.info('Updated {} to {} successfully'.format(app, version))
            git.add(yaml_file)
            commit_message = ' bump {} to {}'.format(app, version)
            git.commit(m=commit_message)
            log.info(commit_message)
            git.push()
        else:
            log.error('Errors in updating deployment, exit code:{}'.format(exit_code))
            git.checkout('.')

    except Exception as e:
        git.checkout('.')
        log.exception('Exception:{}'.format(e))
Exemplo n.º 10
0
if run_res.returncode != 0:
    err_exit(f"Error synchronize {configs_uri} with local repo")

working_branch_name = f"{repo_conf_path.replace('/', '-')}-{int(time.time())}"
os.chdir(git_repo)

git = Repo('.').git
# синхронизируем изменения с origin
print("Update changes from origin repo...")
git.checkout(master_branch)
git.pull("origin")
# Очищаем ссылки на удалённые в origin ветки
git.remote('prune', 'origin')
# создаём новую рабочую ветку и добавляем все изменения забранные с удалённого сервера
git.checkout('-b', working_branch_name)
git.add(repo_conf_path.split('/')[0])

git_status = ''
if git.diff('--cached'):
    git_status = git.status()
    print("Changes in configs:\n", git_status)
else:
    print("No any configs changes detected, nothing to commit")
    git.checkout(master_branch)
    git.branch('-d', working_branch_name)
    exit(2)

git.commit('-m', commit_msg)

# get GitHub repo name (git remote show -n origin / Fetch URL:)
github_repo = os.environ.get("GITHUB_REPO")