pobsrc/pobsrc.py
#!/usr/bin/env python3
import os,sys,requests,zipfile,sys
from configparser import ConfigParser
from os.path import exists
pobSrcConfDir='.pobsrc'+os.sep
pobSrcConf=pobSrcConfDir+'pobsrc.conf'
def getProjectDir():
currDir=directory_path = os.getcwd()
return str(os.path.basename(currDir))
def checkIfConfigIsOk():
if not os.path.exists(pobSrcConf):
print("Configuration not found! Please run 'pobsrc init' ")
sys.exit(1)
conf = ConfigParser()
conf.read(pobSrcConf)
if (not conf.has_section('project') ):
print("Configuration not found! Please run 'pobsrc init' ")
sys.exit(1)
def getConfigDirPath():
from os.path import expanduser
home = expanduser("~")
if sys.platform == 'win32':
configDir=home+"\\AppData\\Local\\pobsrc\\"
elif sys.platform == 'linux':
configDir=home+"/.config/pobsrc/"
elif sys.platform== "darwin":
configDir=home+"/.config/pobsrc/"
else:
configDir=home+"/.config/pobsrc/"
return configDir
def getPref(p):
remote=''
dirname=getConfigDirPath()
filename=dirname+os.sep+'pobsrc.ini'
if not os.path.exists(dirname):
return ''
if os.path.exists(filename):
conf = ConfigParser()
conf.read(filename)
if (conf.has_section('preferences') and conf.has_option('preferences', p)):
remote= conf.get('preferences',p)
if not remote:
return ''
else:
return str(remote)
defaultSourceDestRoot=getPref('defaultSourceDestRoot')
blankConf='''
[project]
projectName=%s
projectSourceDestRoot=%s
[options]
excludeFile=exclusions.txt
rcloneBinPath=rclone
rcloneCommand=sync
''' % (getProjectDir() , defaultSourceDestRoot)
blankPreferences='''
[preferences]
defaultSourceDestRoot=
'''
if sys.argv[1] == 'pref':
dirname=getConfigDirPath()
filename=dirname+os.sep+'pobsrc.ini'
if not os.path.exists(dirname):
os.makedirs(dirname)
if not os.path.exists(filename):
f = open(filename, "w")
f.write(blankPreferences)
f.close()
if sys.argv[1] =='init':
if not os.path.exists(pobSrcConfDir):
os.makedirs(pobSrcConfDir)
if not exists(pobSrcConf):
print ('init...')
print ('Create blank %s' % pobSrcConf)
f = open(pobSrcConf, "w")
f.write(blankConf)
f.close()
print("Please edit it (%s)" % pobSrcConf)
else:
print ('%s exists!' % pobSrcConf)
if not exists('./exclusions.txt'):
f = open("./exclusions.txt", "w")
f.write('')
f.close()
else:
print ('exclusions.txt exists, not overwritten.')
if sys.argv[1] =='clone':
url = sys.argv[2]
dest =os.path.basename(url)
filename=os.path.splitext(dest)[0]
with open (dest, "wb") as f:
f.write(requests.get(url).content)
if not os.path.exists(filename):
os.makedirs(filename)
pzip = zipfile.ZipFile(dest)
pzip.extractall(filename)
pzip.close()
os.remove(dest)
if sys.argv[1] =='put':
checkIfConfigIsOk()
conf = ConfigParser()
conf.read(pobSrcConf)
projectName=conf.get('project','projectName')
projectSourceDestRoot=conf.get('project','projectSourceDestRoot')
excludeFile=conf.get('options','excludeFile')
rcloneBinPath=conf.get('options','rcloneBinPath')
rcloneCommand=conf.get('options','rcloneCommand')
cmd=rcloneBinPath+' '+rcloneCommand+' ../'+projectName+' '+projectSourceDestRoot+'/'+projectName+' --exclude-from '+excludeFile
os.system(cmd)