This repository was archived by the owner on Mar 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgithub.py
More file actions
89 lines (84 loc) · 2.92 KB
/
Copy pathgithub.py
File metadata and controls
89 lines (84 loc) · 2.92 KB
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
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010 MatToufoutu
#
"""
virtualenvwrapper.project plugin for github repositories
"""
from __future__ import print_function
import os
from git import *
from github2.client import Github
def get_environment():
"""
Get required environment variables.
"""
env_is_ok = True
github_user = os.environ.get('GITHUB_USER')
if github_user is None:
print('GITHUB_USER not found')
print('Add \'export GITHUB_USER="username"\' to your .bashrc')
env_is_ok = False
api_token = os.environ.get('GITHUB_API_TOKEN')
if api_token is None:
print('GITHUB_API_TOKEN not found')
print('Add \'export GITHUB_API_TOKEN"=api_token"\' to your .bashrc')
env_is_ok = False
if not env_is_ok:
return None
return (github_user, api_token)
def template(args):
"""
Creates a GitHub repository into the project directory,
if the GitHub project still exists, clone the repository.
"""
prj_name = args[0]
print('Initializing git repository and creating GitHub project for %s' % prj_name)
src_dir = os.path.join(os.getcwd(), 'src')
env_vars = get_environment()
if env_vars is None:
print('Repository was not created')
return
username, api_token = env_vars
github = Github(username, api_token)
try:
github.repos.show("%s/%s" % (username, prj_name))
repo_exists = True
except RuntimeError:
repo_exists = False
if repo_exists:
print('Repository already exists on GitHub')
print('[C]lone or [A]bort? ', end='')
choice = raw_input().lower()
if choice == 'c':
print('Cloning repository from %s' % git_url)
try:
# delete src folder to prevent error when cloning (will be re-created by the clone command)
if os.path.exists(src_dir):
os.rmdir(src_dir)
Repo.clone_from(git_url, src_dir)
repo_created = True
except GitCommandError:
print('An error occured while cloning the repository')
if os.path.exists(os.path.join(src_dir, '.git')):
os.rmdir(os.path.join(src_dir, '.git'))
repo_created = False
else:
repo_created = False
else:
prj_desc = raw_input('Project description (default=None): ')
prj_url = raw_input('Project homepage (default=None): ')
github.repos.create(
name = prj_name,
description = prj_desc if prj_desc else None,
homepage = prj_url if prj_url else None,
)
repo = Repo.init(src_dir)
repo.create_remote('origin', git_url)
repo_created = True
if repo_created:
print('Repository was successfuly created')
else:
print('Repository was not created')
return