This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
Kosmos/Builder/common.py
2020-05-06 06:36:43 -04:00

78 lines
2.3 KiB
Python

#
# Kosmos Builder
# Copyright (C) 2020 Nichole Mattera
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import enum
import glob
import os
import re
import shutil
import uuid
class Command(enum.Enum):
Kosmos = 0
SDSetup = 1
KosmosMinimal = 2
class GitService(enum.Enum):
GitHub = 0
GitLab = 1
SourceForge = 2
def generate_temp_path():
return os.path.join(os.getcwd(), 'tmp', str(uuid.uuid4()))
def delete_path(path):
if os.path.exists(path):
if os.path.isfile(path):
os.remove(path)
else:
shutil.rmtree(path)
def copy_module_file(module_name, file_name, destination):
return shutil.copyfile(os.path.join(os.getcwd(), 'Modules', module_name, file_name), destination)
def copy_module_folder(module_name, folder_name, destination):
return shutil.copytree(os.path.join(os.getcwd(), 'Modules', module_name, folder_name), destination)
def find_file(pattern):
return glob.glob(pattern, recursive=False)
def sed(pattern, replace, file_path):
lines = []
with open(file_path, 'r') as text_file:
lines += text_file.readlines()
with open(file_path, 'w') as text_file:
for line in lines:
text_file.write(re.sub(pattern, replace, line))
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
def move_contents_of_folder(source, dest):
files = os.listdir(source)
for f in files:
if os.path.isdir(os.path.join(source, f)):
mkdir(os.path.join(dest, f))
move_contents_of_folder(os.path.join(source, f), os.path.join(dest, f))
else:
shutil.move(os.path.join(source, f), dest)