-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.py
43 lines (33 loc) · 1.2 KB
/
github.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
# pip install GitPython==3.*
from git import GitCommandError, Repo
from programlib import Program
GIT_USER = 'Vadim Liventsev'
GIT_EMAIL = '[email protected]'
GITHUB_USERNAME = 'vadim0x60'
GITHUB_PASSWORD = 'GITHUB_PASSWORD'
GITHUB_REPO = 'programlib.git'
github_remote = f'https://{GITHUB_USERNAME}:{GITHUB_PASSWORD}@github.com/{GITHUB_USERNAME}/{GITHUB_REPO}'
def upload_file(repo, filename, message=None):
if not message:
message = f'added {filename}'
repo.index.add(filename)
repo.index.commit(message)
repo.remotes.origin.pull()
repo.remotes.origin.push()
def ensure_correct_repo(remote, path):
try:
repo = Repo.clone_from(remote, path)
except GitCommandError:
repo = Repo(path)
assert repo.remotes.origin.url == remote
repo.config_writer().set_value('user', 'name', GIT_USER).release()
repo.config_writer().set_value('user', 'email', GIT_EMAIL).release()
return repo
# Create a program
program = Program(source='print("Hello, world!")', language='Python')
# Test it
assert program.run() == ['Hello, world!']
# Upload it to GitHub
repo = ensure_correct_repo(github_remote, 'programs')
program.save('programs/hello.py')
upload_file(repo, 'hello.py')