From 18d5c4d40416c605c94223458a0d3c62ee830db3 Mon Sep 17 00:00:00 2001 From: "steven.liatti" <steven@liatti.ch> Date: Wed, 14 Oct 2020 18:22:39 +0200 Subject: [PATCH] Continue refactoring with argparse, update #2 --- .gitignore | 1 + scripts/clone_all_repos_in_group.py | 47 +++++++++++++++-------------- scripts/create_group.py | 9 ++---- scripts/create_repo_for_students.py | 11 +++---- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 1d17dae..4bb4026 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .venv +.vscode \ No newline at end of file diff --git a/scripts/clone_all_repos_in_group.py b/scripts/clone_all_repos_in_group.py index 719f4da..667761a 100755 --- a/scripts/clone_all_repos_in_group.py +++ b/scripts/clone_all_repos_in_group.py @@ -1,30 +1,33 @@ #!/usr/bin/env python3 import os -import sys -import json import requests import subprocess +import argparse -if len(sys.argv) < 4: - print('Usage: ' + sys.argv[0] + ' <token> <group_id> <directory> <until_date>') - exit(1) +parser = argparse.ArgumentParser() +parser.add_argument( + "token", metavar="TOKEN", help="Create a token here: https://gitedu.hesge.ch/profile/personal_access_tokens") +parser.add_argument( + "group_id", metavar="GROUP_ID", help="The group id (int) of the projects.") +parser.add_argument( + "directory", metavar="DIRECTORY", help="Local directory where clone all repositories.") +parser.add_argument( + "-u", "--until_date", help="Do a git checkout for all repositories at given date, format \"YYYY-MM-DD hh:mm\" (optional).") +args = parser.parse_args() -directory = sys.argv[3] try: - os.mkdir(directory) + os.mkdir(args.directory) except OSError: - print("Creation of the directory '%s' failed, exit\n" % directory) + print("Creation of the directory '%s' failed, exit\n" % args.directory) exit(1) -token = sys.argv[1] -group_id = sys.argv[2] - base_url = 'https://gitedu.hesge.ch/api/v4/' params = {'simple': 'true', 'per_page': 100} -headers = {'PRIVATE-TOKEN': token} +headers = {'PRIVATE-TOKEN': args.token} -repositories = requests.get(base_url + '/groups/' + group_id + '/projects', params=params, headers=headers).json() +repositories = requests.get(base_url + '/groups/' + args.group_id + + '/projects', params=params, headers=headers).json() if 'message' in repositories: print('Error retrieving repositories: ' + repositories['message']) exit(1) @@ -41,21 +44,21 @@ for repo in repositories: members_names = '' for member in members: - members_names += "'" + member['name'] + "' (" + member['username'] + '), ' + members_names += member['username'] + ', ' print('Members: ' + members_names) print('Web url: ' + web_url) - print('Cloning in "' + directory + '/' + repo['path'] + '"') + print('Cloning in "' + args.directory + '/' + repo['path'] + '"') - subprocess.run(["git", "clone", "-q", ssh_url_to_repo, directory + '/' + repo['path']]) - if len(sys.argv) > 4: - until_date = sys.argv[4] + subprocess.run(["git", "clone", "-q", ssh_url_to_repo, + args.directory + '/' + repo['path']]) + if args.until_date: commit_id = subprocess.check_output([ - "git","rev-list", "-n", "1", "--before=\"" + until_date + "\"", - "master"], cwd=directory + '/' + repo['path']).decode('utf-8').rstrip() + "git", "rev-list", "-n", "1", "--before=\"" + args.until_date + "\"", + "master"], cwd=args.directory + '/' + repo['path']).decode('utf-8').rstrip() subprocess.run( ["git", "checkout", "-q", str(commit_id)], - cwd=directory + '/' + repo['path']) + cwd=args.directory + '/' + repo['path']) print("Checkout at " + str(commit_id) + "\n") else: - print() \ No newline at end of file + print() diff --git a/scripts/create_group.py b/scripts/create_group.py index 6a9e3e1..209e4c3 100755 --- a/scripts/create_group.py +++ b/scripts/create_group.py @@ -1,16 +1,13 @@ #!/usr/bin/env python3 -import sys -import json -import requests -import subprocess import argparse +import requests parser = argparse.ArgumentParser() parser.add_argument( - "token", help="Create a token here: https://gitedu.hesge.ch/profile/personal_access_tokens") + "token", metavar="TOKEN", help="Create a token here: https://gitedu.hesge.ch/profile/personal_access_tokens") parser.add_argument( - "group_name", help="The group name.") + "group_name", metavar="GROUP_NAME", help="The group name.") parser.add_argument( "--visibility", help="Group visibility. By default private.") args = parser.parse_args() diff --git a/scripts/create_repo_for_students.py b/scripts/create_repo_for_students.py index e67278e..89480aa 100755 --- a/scripts/create_repo_for_students.py +++ b/scripts/create_repo_for_students.py @@ -1,18 +1,15 @@ #!/usr/bin/env python3 -import sys -import json -import requests -import subprocess import argparse +import requests parser = argparse.ArgumentParser() parser.add_argument( - "token", help="Create a token here: https://gitedu.hesge.ch/profile/personal_access_tokens") + "token", metavar="TOKEN", help="Create a token here: https://gitedu.hesge.ch/profile/personal_access_tokens") parser.add_argument( - "group_id", help="The group id (int) where to store the created new project.") + "group_id", metavar="GROUP_ID", help="The group id (int) where to store the created new project.") parser.add_argument( - "emails", help="Emails list of students working in this project, separated by commas (email1,email2).") + "emails", metavar="EMAILS", help="Emails list of students working in this project, separated by commas (email1,email2).") parser.add_argument( "-n", "--name", help="The project name. If blank, take the first student name (from email) as name.") parser.add_argument("-i", "--import_url", -- GitLab