4
\$\begingroup\$

This Python script does following activities:

  • clone repo, as provided from the list

  • creating a new branch "jenkinsMigrate"

  • rename Jenkinsfile to Jenkinsfile.migrate

  • push code to GitHub in a new branch.

NOTE: credentials were configured already.

import git
import os
import subprocess

gitUrl = "https://*****"
cwd = "*****"

with open("migrateList.txt", "r") as file:
    for line in file:
        #configure gitUri for each repository
        gitUri = gitUrl + line.strip() + ".git"
        try:
            global repo
            repo = git.Repo.clone_from(gitUri, cwd + line.strip())
        except:
            print(" directory already available")
        os.chdir(cwd + line.strip())

        #checkout new branch for migration
        repo.git.checkout('-b', "jenkinsMigrate")

        subprocess.call(["git", "mv", "Jenkinsfile", "Jenkinsfile.migrate"])
        repo.git.add(update=True)
        repo.index.commit("jenkins migration")
        origin = repo.remote(name='origin')

        #push new branch to github
        subprocess.call(["git","push", "--set-upstream", "origin", "jenkinsMigrate"])
        subprocess.call(["cd", ".."])

Sample text file:

$ cat migrateList.txt 
repo1
repo2
repo3
repo4

This is a working code, but all I am looking for is to maintain consistency in the commands that I use. eg: For some of the git commands that I have used are from gitpython module where as others are invoked with shell commands. Apart from these any other suggestions are welcome.

Thanks!

UPDATE 1: python - 2.7.10

gitPython module

\$\endgroup\$
  • 1
    \$\begingroup\$ What version of Python are you using? \$\endgroup\$ – Ludisposed Mar 18 at 9:55
  • \$\begingroup\$ Which git pacakage are you using? Probably not git, but maybe GitPython? \$\endgroup\$ – Graipher Mar 18 at 14:49
4
\$\begingroup\$

Review

  • Stick to the PEP8 style guide

    1. Functions and variables should be snake_case
    2. Constants like gitUrl should be UPPER_SNAKE_CASE
  • There is a function for getting the current working directory

    os.getcwd() Depending on your use case this might be useful

  • If you are using Python3.5+ you should change the older subprocces.call with the newer subprocess.run

  • Don't catch bare Exceptions

    If you now what exceptions are going to be caught it is better to define them

\$\endgroup\$
2
\$\begingroup\$

Use Python 3.x

Python 2.7 will retire in 2020 (end of support) so I'd sugest you migrate your project to a newer version.

Other aspects apart from what @Ludisposed already mentioned:

  • you're using line.strip() in several places so you might want to assign it to a new variable and use that instead.
  • declare the migrateList as a constant at the top of the file (under your imports). That way it will be easier to be modified.
  • use f-strings (Python > 3.6). E.g: f"{gitUrl}{line.strip()}.git"
  • don't use globals. The reason they are bad is that they allow functions/variables to have hidden (as in non-obvious and undeclared) and thus hard to understand side effects. Also, this can lead to Spaghetti code.
  • PEP8: leave an empty space after # in your comments. E.g: # configure gitUri for each repository
  • PEP8:

Imports should be grouped in the following order:

  • Standard library imports.
  • Related third party imports.
  • Local application/library specific imports.
  • You should put a blank line between each group of imports.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.