81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
if __name__ == '__main__' :
|
|
|
|
import subprocess
|
|
from pathlib import Path
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import json
|
|
|
|
VERSION = 'v0.1'
|
|
|
|
configFile = open("config.json",'r')
|
|
|
|
config = json.loads(configFile.readall())
|
|
|
|
configFile.close()
|
|
|
|
parser = argparse.ArgumentParser(description="A Python project preparer for Linux")
|
|
|
|
parser.add_argument('-n','--name',default=config['defaultDirectoryName'],help='The project name')
|
|
parser.add_argument('-p','--path',default=False,help='The path to create the project in')
|
|
parser.add_argument('-f','--filename',default=config['defaultFileName'],help="Initial file name")
|
|
parser.add_argument('--file-preset',default=config['defaultPreset'],help='Choose a preset for the init file: print | class | import')
|
|
parser.add_argument('-g','--git',action='store_true',help='If "git init" should be used')
|
|
parser.add_argument('--version',action='store_true',help='Print the current version')
|
|
|
|
args = parser.parse_args(sys.argv[1:])
|
|
|
|
if args.version :
|
|
print(f"Linux Python Preparer {VERSION}")
|
|
os._exit(0)
|
|
|
|
presetContent = ''
|
|
presetFile = Path().cwd().joinpath(f"{config['presetPath']}/{args.file_preset}.py")
|
|
|
|
if not presetFile.exists() and args.file_preset == '' :
|
|
raise Exception(f"Preset {args.file_preset} does not exist!")
|
|
else :
|
|
presetContent = presetFile.read_text()
|
|
|
|
if not args.path :
|
|
path = Path().cwd()
|
|
else :
|
|
dest = args.path if args.path[0]!='~' else Path.home().as_posix() + args.path[1:]
|
|
path = Path(dest)
|
|
|
|
if not path.exists() and path.is_file() :
|
|
raise Exception('Path does not exist')
|
|
|
|
counter = 0
|
|
|
|
while True :
|
|
checkablePath = args.name if counter == 0 else args.name + str(counter)
|
|
subpath = path.joinpath(checkablePath)
|
|
if subpath.exists() :
|
|
counter += 1
|
|
else :
|
|
break
|
|
|
|
if counter != 0 :
|
|
path = Path(path.absolute().as_posix() + f"/{args.name}" + str(counter))
|
|
else :
|
|
path = Path(path.absolute().as_posix() + f"/{args.name}")
|
|
|
|
path.mkdir()
|
|
print('Directory was created.')
|
|
|
|
file = Path(path.absolute().as_posix() + f"/{args.filename}")
|
|
file.touch(mode=0o750)
|
|
print('File was created.')
|
|
|
|
if presetContent :
|
|
file.write_text(presetContent)
|
|
print(f"Preset {args.file_preset} was implemented.")
|
|
|
|
os.chdir(path.absolute())
|
|
if args.git :
|
|
subprocess.run(['git','init'])
|
|
|
|
print('The process was completed successfully.')
|