Skip to content

Commit

Permalink
use shutil to zip
Browse files Browse the repository at this point in the history
  • Loading branch information
donno2048 authored Jan 16, 2024
1 parent 07250c1 commit 215be52
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions gitback/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os import chdir, mkdir, rmdir, getcwd
from os.path import exists
from subprocess import Popen, PIPE
from zipfile import ZipFile
from shutil import make_archive
from argparse import ArgumentParser
from urllib.request import urlopen
from json import loads
Expand All @@ -21,8 +21,7 @@ def backup()->None:
if args.path is not None: chdir(args.path)
username, git_command = args.username if args.username is not None else input("Your GitHub username: "), ' clone '
name = 'backup' if args.name is None else args.name
if exists(name+'.zip') and args.zip: exit(f'You already have a {name} zip, please move it somewhere else until the process is done')
if args.zip: backup = ZipFile(name+'.zip', 'w', 8)
if exists(name + '.zip') and args.zip: exit(f'You already have a {name} zip, please move it somewhere else until the process is done')
try: mkdir(name)
except FileExistsError:
try:
Expand All @@ -39,12 +38,12 @@ def backup()->None:
while len(result := loads(urlopen(f"https://api.github.com/users/{username}/gists?type=all&per_page=100&page={i}").read())):
for gist in result:
Popen('git' + git_command + gist_clone_command + gist["id"], shell = True, stdout = PIPE).wait()
if args.zip: backup.write(getcwd() + '/' + gist["id"], arcname = gist["id"])
i += 1
if not args.gists:
i = 0
while len(result := loads(urlopen(f"https://api.github.com/users/{username}/repos?type=all&per_page=100&page={i}").read())):
for repo in result:
Popen('git' + git_command + repo_clone_command + repo["full_name"], shell = True, stdout = PIPE).wait()
if args.zip: backup.write(getcwd() + '/' + repo["name"], arcname = repo["name"])
i += 1
chdir('..')
make_archive(name + '.zip', 'zip', name)

0 comments on commit 215be52

Please sign in to comment.