-
Notifications
You must be signed in to change notification settings - Fork 0
/
timb.py
executable file
·163 lines (141 loc) · 5.11 KB
/
timb.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
'''
USE CASE:
You have a local git repo that you wish to push to both your own git server,
github and bitbucket.
This script
* Reads your local ~/.netrc
* Gets repo name and description from arguments
* Builds a git repo locally
* Adds a README.md and a LICENCE. Commits the changes.
* Builds a git repo hosted via your remote git server
* Adds a git hook for automatically pushing to configured remotes from your
remote
* Adds a remote for github on your remote server.
* Adds a remote for bitbucket on your remote server.
* Creates a repo at GitHub.
* Creates a repo at bitbucket.
* Pushes to remote.
'''
import ConfigParser
import os
import sys
from git import Repo
from shutil import copyfile
from paramiko import client
from paramiko import AutoAddPolicy
from textwrap import dedent
import commands
import requests
import json
# Get HOME
HOME = os.getenv('HOME')
RCFILE = ("%s/.timbrc" % HOME)
# Read the variables from the local config file
CONFIG = ConfigParser.ConfigParser()
CONFIG.read(RCFILE)
GITDIR = CONFIG.get("grb", "GITDIR")
GITHUB_API_URL = CONFIG.get("grb", "GITHUB_API_URL")
GITHUBUSER = CONFIG.get("grb", "GITHUBUSER")
BITBUCKET_API_URL = CONFIG.get("grb", "BITBUCKET_API_URL")
BITBUCKETUSER = CONFIG.get("grb", "BITBUCKETUSER")
GITSERVER = CONFIG.get("grb", "GITSERVER")
GITREMOTEDIR = CONFIG.get("grb", "GITREMOTEDIR")
LICENSE = CONFIG.get("grb", "LICENSE")
# Set the repo directory:
REPONAME = sys.argv[1]
REPODIR = GITDIR + "/" + REPONAME
# Set the repo description
DESCRIPTION = sys.argv[2]
# Set the remote values
REMOTES = dedent("""
[remote "github"]
url = [email protected]:%s/%s.git
fetch = +refs/heads/*:refs/remotes/github/*
autopush = true
[remote "bitbucket"]
url = [email protected]:%s/%s.git
fetch = +refs/heads/*:refs/remotes/bitbucket/*
autopush = true""" % (
GITHUBUSER, REPONAME, BITBUCKETUSER, REPONAME))
HOOK = dedent("""
#!/bin/bash
for remote in $(git remote); do
if [ "$(git config "remote.${remote}.autopush")" = "true" ]; then
git push --all "$remote"
fi
done""")
def localrepo():
''' Creates and initialises the git repoa.'''
if not os.path.exists(REPODIR):
# Initialise the repo
Repo.init(REPODIR)
# Add the description
description = "%s/.git/description" % REPODIR
with open(description, "w") as text_file:
text_file.write(DESCRIPTION)
# Add the LICENSE file
dst = "%s/LICENSE" % REPODIR
copyfile(LICENSE, dst)
repo = Repo.init(REPODIR)
repo.index.add([dst])
repo.index.commit("Added LICENSE.")
# Add the README file
readme = "%s/README.md" % REPODIR
with open(readme, "w") as text_file:
text_file.write(
"# %s \nThis is the initial README for the %s git repo."
% (REPONAME, REPONAME))
repo.index.add([readme])
repo.index.commit("Added README.")
# Adds the remote git repo as origin
remoteurl = "ssh://%s%s/%s" % (GITSERVER, GITREMOTEDIR, REPONAME)
repo.create_remote('origin', remoteurl)
# Push the initial content
print "Pushing the initial content."
origin = repo.remotes.origin
origin.push("--all")
else:
print "Directory %s already exists" % REPODIR
def remoterepo():
'''Builds a git repo hosted via remote git server'''
# Throw in an if exists here as per localrepo
sshclient = client.SSHClient()
# sftpclient = sftp_file.SFTPFile(sftp, handle, mode='r', bufsize=-1)
sshclient.load_system_host_keys()
sshclient.set_missing_host_key_policy(AutoAddPolicy())
sshclient.connect(GITSERVER)
# rexists(sftpclient, "%s/%s" % (GITREMOTEDIR, REPONAME))
print "Creating %s on %s" % (REPONAME, GITSERVER)
sshclient.exec_command('mkdir %s/%s' % (GITREMOTEDIR, REPONAME))
sshclient.exec_command('git init --bare %s/%s' % (GITREMOTEDIR, REPONAME))
sshclient.exec_command(
'echo %s > %s/%s/description' % (DESCRIPTION, GITREMOTEDIR, REPONAME))
# Adds a remote for github and bitbucket.
sshclient.exec_command(
'echo -e %s >> %s/%s/config' % (
commands.mkarg(REMOTES), GITREMOTEDIR, REPONAME))
# Adds a remote git hook for automatically pushing to configured remotes.
sshclient.exec_command(
'echo -e %s >> %s/%s/hooks/post-receive' % (
commands.mkarg(HOOK), GITREMOTEDIR, REPONAME))
sshclient.exec_command(
'chmod u+x %s/%s/hooks/post-receive' % (
GITREMOTEDIR, REPONAME))
def socialrepos():
'''Creates a repo at Github and Bitbucket.'''
print "Creating the repo at Github"
payload = json.dumps({'name': REPONAME, 'description': DESCRIPTION})
req = requests.post(
'%s' % GITHUB_API_URL, payload)
print "Creating the repo at Bitbucket"
payload = json.dumps({"description": "%s" % DESCRIPTION})
req = requests.post(
'%s/%s/%s' % (BITBUCKET_API_URL, BITBUCKETUSER, REPONAME), payload)
# Description not working for Bitbucket...
def main():
'''Run the main program'''
remoterepo()
socialrepos()
localrepo()
main()