Mentions légales du service

Skip to content
Snippets Groups Projects
Commit f120c07d authored by Florent Bouchez Tichadou's avatar Florent Bouchez Tichadou
Browse files

changing script to handle single files

parent 4ea71787
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,7 @@ import sys
import shutil
import logging
import argparse
from pathlib import Path
from string import Template
......@@ -50,7 +51,7 @@ TEST_DIR = os.path.join(AGDBENTURES_DIR, "test_directory")
# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument(
"source_dir",
"source",
help="The path to the level source directory. The file main.c is the entry point of all levels.",
)
......@@ -104,8 +105,21 @@ def create_dir(dir_path: str, clear=False):
logging.debug(f"Created {dir_path}")
SOURCE_LEVEL_DIR = args.source_dir
MAIN_SOURCE_FILE = os.path.join(SOURCE_LEVEL_DIR, "main.c")
# Check if source is a directory or a file
if os.path.isfile(args.source):
SOURCE_LEVEL_DIR = None
MAIN_SOURCE_FILE = args.source
elif os.path.isdir(args.source):
SOURCE_LEVEL_DIR = os.path.basename(args.source)
MAIN_SOURCE_FILE = os.path.join(SOURCE_LEVEL_DIR, "main.c")
if not os.path.isfile(MAIN_SOURCE_FILE):
print(f"Error: cannot find {MAIN_SOURCE_FILE}")
exit(1)
else:
print(f"Error: {args.source} is not a valid directory or .c file")
exit(1)
logging.debug(f"Source file: {MAIN_SOURCE_FILE} in {SOURCE_LEVEL_DIR}")
if args.dev:
......@@ -142,6 +156,32 @@ def copy_with_unifdef(
apply_unifdef(in_file, out_file, args.defines + defines, args.udefines + udefines)
def process_file(src_file: str, defines: list[str] = [], udefines: list[str] = []):
print (src_file)
# abs_path = os.path.abspath(os.path.join(src_dir, src_file))
out_file = os.path.abspath(os.path.join(DEST_DIR, os.path.basename(src_file)))
if args.dev:
# in development mode, just symlink the files
# os.symlink(abs_path, out_file)
os.link(src_file, out_file)
logging.debug(f"linked named {out_file} to {src_file}")
else:
# if the file a .c or .h apply unifdef
if src_file.endswith(".c") or src_file.endswith(".h"):
copy_with_unifdef(src_file, out_file, defines, udefines)
logging.debug(f"Applied unifdef to {src_file}")
elif os.path.isfile(src_file):
# simple copy to the destination directory
shutil.copy(src_file, DEST_DIR)
logging.debug(f"Copied {src_file}")
def process_directory(src_dir: str, defines: list[str] = [], udefines: list[str] = []):
"""Applies copy_with_unifdef to all the files in a directory.
In development mode, creates symbolic links and don't apply unifdef"""
......@@ -152,44 +192,36 @@ def process_directory(src_dir: str, defines: list[str] = [], udefines: list[str]
if os.path.basename(file_path).startswith("_") or \
os.path.basename(file_path).startswith("."):
continue
process_file(file_path, defines, udefines)
abs_path = os.path.abspath(os.path.join(src_dir, file_path))
out_file = os.path.abspath(os.path.join(DEST_DIR, os.path.basename(file_path)))
if args.dev:
# in development mode, just symlink the files
# os.symlink(abs_path, out_file)
os.link(abs_path, out_file)
logging.debug(f"linked named {out_file} to {abs_path}")
else:
# if the file a .c or .h apply unifdef
if file_path.endswith(".c") or file_path.endswith(".h"):
copy_with_unifdef(abs_path, out_file, defines, udefines)
logging.debug(f"Applied unifdef to {file_path}")
elif os.path.isfile(abs_path):
# simple copy to the destination directory
shutil.copy(abs_path, DEST_DIR)
logging.debug(f"Copied {file_path}")
# === copy level files
if SOURCE_LEVEL_DIR:
process_directory(SOURCE_LEVEL_DIR)
else:
process_file(MAIN_SOURCE_FILE)
p=Path(MAIN_SOURCE_FILE)
p.rename(p.with_suffix('.py'))
# PYTHON_FILE=
# p = Path('mysequence.fasta')
process_file(str(p))
# === copy level files
process_directory(SOURCE_LEVEL_DIR)
logging.debug(
f"Prepared level files with #defines {args.defines} and negation {args.udefines}"
)
# === copy engine files
SOURCE_ENGINE_DIR = os.path.join(AGDBENTURES_DIR, "engines", metadata["engine_name"])
logging.debug(f"Source engine dir {SOURCE_ENGINE_DIR}")
process_directory(SOURCE_ENGINE_DIR)
if metadata["engine_name"] != "none":
SOURCE_ENGINE_DIR = os.path.join(AGDBENTURES_DIR, "engines", metadata["engine_name"])
logging.debug(f"Source engine dir {SOURCE_ENGINE_DIR}")
logging.debug(
f"Prepared engine files with #defines {args.defines} and negation {args.udefines}"
)
process_directory(SOURCE_ENGINE_DIR)
logging.debug(
f"Prepared engine files with #defines {args.defines} and negation {args.udefines}"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment