Last active
December 22, 2019 17:19
-
-
Save lemon32767/cd8121d24fb3dddfdd6fd3e6ee6de26b to your computer and use it in GitHub Desktop.
neocities pkg archival tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import sys, os, subprocess, re | |
if len(sys.argv) < 2: | |
print("""\ | |
usage: neocpkg <command> [neocities website name | website/pkg] | |
commands: | |
init\t create a repo here | |
push\t push repo pkg | |
pull\t download repo pkg | |
clone\t clone repo | |
""".format()) | |
sys.exit(1) | |
pkgbasename = None | |
pkgname = None | |
pkgdir = None | |
def retrieve(): | |
globals()['pkgbasename'] = os.path.basename(os.getcwd()) | |
globals()['pkgname'] = "neocpkg@"+pkgbasename | |
globals()['pkgdir'] = os.getcwd() | |
def shcall(arg): | |
print(" ".join(arg)) | |
return subprocess.call(arg) | |
def cmd_init(): | |
retrieve() | |
wbname = None | |
try: | |
wbname = sys.argv[2] | |
except IndexError: | |
print("missing website name") | |
sys.exit(6) | |
f = open(".neocpkg", "w") | |
f.write(wbname) | |
f.close() | |
def get_wbname(): | |
retrieve() | |
wbname = None | |
try: | |
with open(".neocpkg") as f: | |
wbname = f.read() | |
except FileNotFoundError: | |
print("run 'neocpkg init' first") | |
sys.exit(3) | |
return wbname | |
def cmd_push(): | |
retrieve() | |
wbname = get_wbname() | |
ignore = [".git/"] | |
try: | |
gitignore = open(".gitignore") | |
ignore += gitignore.readlines() | |
gitignore.close() | |
except FileNotFoundError: | |
pass | |
ignore = ["-x"] + list(map(lambda fs: re.sub('/$', '/*', fs.replace("\n","")), ignore)) + ["@"] | |
# print(ignore) | |
archivepath = "/tmp/"+pkgname+".zip" | |
#cmd = "zip \"{}\" {} -9qr ./".format(archivepath, " ".join(ignore)) | |
zcmd = ["zip", archivepath] + ignore + ["-9qr", "./"] | |
if shcall(zcmd) != 0: | |
print("subprocess finished with non-zero exit code; aborting") | |
sys.exit(4) | |
with open(archivepath, "rb") as f: | |
bytes = f.read() | |
with open(archivepath+".txt", "wb") as f2: | |
f2.write(b'?' + bytes) | |
ncmd = ["neocities", "upload", "-d", "pkg", archivepath+".txt"] | |
if shcall(ncmd) != 0: | |
print("subprocess finished with non-zero exit code; aborting") | |
os.remove(archivepath) | |
os.remove(archivepath+".txt") | |
sys.exit(4) | |
os.remove(archivepath) | |
os.remove(archivepath+".txt") | |
def cmd_pull(): | |
retrieve() | |
url = "https://{}.neocities.org/pkg/{}.zip.txt".format(get_wbname(), pkgname) | |
archivepath = "/tmp/"+pkgname+".zip" | |
ccmd = ["curl", url, "--output", archivepath+".txt"] | |
if shcall(ccmd) != 0: | |
print("subprocess finished with non-zero exit code; aborting") | |
sys.exit(4) | |
with open(archivepath+".txt", "rb") as txt: | |
with open(archivepath, "wb") as zipp: | |
txt.read(1) #ignore dummy byte | |
zipp.write(txt.read()) | |
os.remove(archivepath+".txt") | |
if shcall(["unzip", archivepath]) != 0: | |
os.remove(archivepath) | |
print("subprocess finished with non-zero exit code; aborting") | |
sys.exit(4) | |
os.remove(archivepath) | |
def cmd_clone(): | |
pkgname = None | |
wbname = None | |
try: | |
wbname = sys.argv[2].split("/")[0] | |
pkgname = sys.argv[2].split("/")[1] | |
except: | |
print("usage: neocpkg clone <website name>/<pkg name>") | |
sys.exit(8) | |
os.mkdir(pkgname) | |
os.chdir(pkgname) | |
sys.argv[2] = wbname | |
cmd_init() | |
cmd_pull() | |
command_table = { | |
'init': cmd_init, | |
'push': cmd_push, | |
'pull': cmd_pull, | |
'clone': cmd_clone | |
} | |
command = sys.argv[1] | |
try: | |
cmd = command_table[command] | |
sys.exit(cmd()) | |
except KeyError: | |
print("invalid command: {}".format(command)) | |
sys.exit(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment