-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarcheck.py
executable file
·82 lines (65 loc) · 2.23 KB
/
starcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import subprocess
import traceback
from github import Github
import requests
# Configuration
move_path = "/tmp/starchecker/old/"
directory = "/tmp/starchecker/repos/"
token = ""
def clone_repository(repo_url, destination):
try:
subprocess.call(["git", "clone", repo_url, destination])
print(f"Cloned repository: {repo_url}")
return True
except subprocess.CalledProcessError:
print(f"Failed to clone repository: {repo_url}")
return False
def update_repository(directory):
try:
os.chdir(directory)
subprocess.call(["git", "config", "pull.rebase", "true"])
subprocess.call(["git", "checkout", "origin/master", "."])
subprocess.call(["git", "reset", "--hard"])
subprocess.call(["git", "pull"])
print(f"Updated repository: {directory}")
return True
except subprocess.CalledProcessError:
print(f"Failed to update repository: {directory}")
return False
def move_repository(source, destination):
try:
subprocess.call(["mv", source, destination])
print(f"Moved repository from {source} to {destination}")
return True
except subprocess.CalledProcessError:
print(f"Failed to move repository from {source} to {destination}")
return False
def check_git_repo_existence(repo_url):
try:
response = requests.head(repo_url)
return response.status_code == 200
except requests.RequestException:
return False
try:
# Authenticate with GitHub
g = Github(token)
# Get starred repositories
starred_repos = g.get_user().get_starred()
# Clone or update repositories
for repo in starred_repos:
repo_name = repo.name
repo_directory = os.path.join(directory, repo_name)
if not os.path.exists(repo_directory):
if clone_repository(repo.clone_url, repo_directory):
continue
else:
if update_repository(repo_directory):
continue
# Move repository if unable to clone/update
move_repository(repo_directory, move_path)
except Exception as e:
print(f"An error occurred: {e}")
print(traceback.format_exc())