Home > other >  How do I parse json to run git commands from python?
How do I parse json to run git commands from python?

Time:10-20

My git repo is not getting checked out, I am new to python and I don't know why

My input arguments are in json and I need to read them and to checkout my git repo

Can someone help get what I am doing wrong?

class GitCheckout:
    def parse_json(self, text):
        self.json_dict = json.loads(text)
        self.extract_data()

    def extract_data(self):
        des = self.json_dict["dest"]
        src = self.json_dict["src"]
        ver = self.json_dict["ver"]
        self.generate_git_cmd(str(des), str(src), str(ver))

    def generate_git_cmd(self, des, src, ver):
        if os.path.isdir(des):
            stdout = self.run_cmd(['rm', '-rf', des])
            print(stdout)
        else:
            self.run_cmd(['mkdir', '-p', des])
        stdout = self.run_cmd(['git', 'clone', '--no-checkout', src, des])
        stdout = self.run_cmd(['--wd', des, 'git', 'checkout', '--detach', ver])

    def run_cmd(self, cmd):
        p = subprocess.run(cmd, shell=True, capture_output=True)
        p.stdout = p.stdout.decode("utf-8")
        return p.stdout

    def main(self):
        obj = GitCheckout()
        args = sys.argv
        obj.parse_json(self, args)

    if __name__ == '__main__':
        main

CodePudding user response:

Your indentation is incorrect, your main() function isn't defined properly, and you aren't actually calling your main() function. Indentation should be like so (note the last two blocks are outdented):

class GitCheckout:
    def parse_json(self, text):
        self.json_dict = json.loads(text)
        self.extract_data()

    def extract_data(self):
        des = self.json_dict["dest"]
        src = self.json_dict["src"]
        ver = self.json_dict["ver"]
        self.generate_git_cmd(str(des), str(src), str(ver))

    def generate_git_cmd(self, des, src, ver):
        if os.path.isdir(des):
            stdout = self.run_cmd(['rm', '-rf', des])
            print(stdout)
        else:
            self.run_cmd(['mkdir', '-p', des])
        stdout = self.run_cmd(['git', 'clone', '--no-checkout', src, des])

    def run_cmd(self, cmd):
        p = subprocess.run(cmd, shell=True, capture_output=True)
        p.stdout = p.stdout.decode("utf-8")
        return p.stdout

def main():
    obj = GitCheckout()
    args = sys.argv
    obj.parse_json(args)

if __name__ == '__main__':
    main()

Notice that I removed the self parameter from main(), as it isn't part of the class definition. I also added parentheses () to actually call main() in the last line.

CodePudding user response:

main should be a call to a main function, thus it needs to be main().

Also the whole if block is indented causing it to be part of the class definition, you should unindent it for it to be in the general scope of the file.

Also, main function is defined as a method of the class GitCheckout, it can only be called from an object of the class. you should unindent it and remove self from the parameters.

Also, self is passed implicitly, no need to pass it explicitly.

Try improving:

class GitCheckout:
    def parse_json(self, text):
        self.json_dict = json.loads(text)
        self.extract_data()

    def extract_data(self):
        des = self.json_dict["dest"]
        src = self.json_dict["src"]
        ver = self.json_dict["ver"]
        self.generate_git_cmd(str(des), str(src), str(ver))

    def generate_git_cmd(self, des, src, ver):
        if os.path.isdir(des):
            stdout = self.run_cmd(['rm', '-rf', des])
            print(stdout)
        else:
            self.run_cmd(['mkdir', '-p', des])
        stdout = self.run_cmd(['git', 'clone', '--no-checkout', src, des])

    def run_cmd(self, cmd):
        p = subprocess.run(cmd, shell=True, capture_output=True)
        p.stdout = p.stdout.decode("utf-8")
        return p.stdout

def main():
    obj = GitCheckout()
    args = sys.argv
    obj.parse_json(args)

if __name__ == '__main__':
    main()
  • Related