Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files_to_ignore to ensure that only files that we want to copy over are actually moved #289

Merged
merged 16 commits into from
Sep 26, 2020
8 changes: 8 additions & 0 deletions abcclassroom/example-data/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ clone_dir: cloned_repos
# course_dir unless you enter an absolute path (i.e. starting with '/' on
# Linux or OS X or with 'C:' on Windows).
template_dir: assignment_repos

# A list of files that you do not want to be copied from your assignment
# release directory to the github template repo. These may be system files
# Checkpoints or other files that are created by various tools and operating
# system functions. Add as many as you wish to the list below!
files_to_ignore:
- .DS_Store
- .ipynb_checkpoints
48 changes: 46 additions & 2 deletions abcclassroom/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def new_update_template(args):

Creates an assignment entry in the config file if one does not already
exist.

Parameters
----------
args : command line arguments
"""

print("Loading configuration from config.yml")
config = cf.get_config()

Expand Down Expand Up @@ -61,6 +66,23 @@ def new_update_template(args):
def create_or_update_remote(
template_repo_path, organization, repo_name, token
):
"""
Push template repo to github creating a new repository or update the
repo's contents

Parameters
----------
template_repo_path : string
The path to the template repo on your local computer.
organization : string
The name of the organization where your GitHub Classroom lives.
repo_name : string
The name of the template repository to create on GitHub
token : github token
Used to authenticate with GitHub via the API. Created by running
``abc-init``

"""
remote_exists = github.remote_repo_exists(organization, repo_name, token)
if not remote_exists:
print("Creating remote repo {}".format(repo_name))
Expand Down Expand Up @@ -166,12 +188,25 @@ def create_template_dir(config, assignment, mode="fail"):
def copy_assignment_files(config, template_repo, assignment):
"""Copy all of the files from the course_materials/release directory for the
assignment into the template repo directory.

Parameters
----------
config: ordered dictionary
Config file returned by ``get_config()`` that contains paths to the
course directory, github organization and other custom options
template_repo: os path object
Absolute path to the template repository where the assignment files
will be copied
assignment: string
name of the assignment being copied

"""

course_dir = cf.get_config_option(config, "course_directory", True)
materials_dir = cf.get_config_option(config, "course_materials", True)
parent_path = utils.get_abspath(materials_dir, course_dir)
release_dir = Path(parent_path, "release", assignment)

if not release_dir.is_dir():
print(
"release directory {} does not exist; exiting\n".format(
Expand All @@ -188,13 +223,22 @@ def copy_assignment_files(config, template_repo, assignment):
template_repo.relative_to(course_dir)
)
)
for file in all_files:
# Get a list of files to ignore - maybe our default config has some
# could have some defaults - then remove all files that we want to ignore
files_to_ignore = cf.get_config_option(config, "files_to_ignore", True)
files_to_move = set(all_files).difference(files_to_ignore)

for file in files_to_move:
fpath = Path(release_dir, file)
print(" {}".format(fpath.relative_to(course_dir)))
# overwrites if fpath exists in template_repo
# TODO: Note that as written here, moving directories will fail so
# we may want to revisit this
shutil.copy(fpath, template_repo)
nfiles += 1
print("Copied {} files".format(nfiles))

print("Copied {} files to your assignment directory!".format(nfiles))
print("The files copied include: {}".format(files_to_move))


def create_extra_files(config, template_repo, assignment):
Expand Down
1 change: 1 addition & 0 deletions abcclassroom/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ def default_config():
"template_dir": "test_template",
"course_materials": "nbgrader",
"clone_dir": "cloned-repos",
"files_to_ignore": [".DS_Store", ".ipynb_checkpoints"],
}
return config
18 changes: 15 additions & 3 deletions abcclassroom/tests/test_assignment_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ def test_move_git_dir(default_config, tmp_path):

# Tests for copy_assignment_files method
def test_copy_assignment_files(default_config, tmp_path):
# test that contents are the same for target and source directory
""""Test that files are moved to the template repo directory and that
ignored files are NOT moved.
"""
default_config["course_directory"] = tmp_path
assignment = "assignment1"

files_to_ignore = default_config["files_to_ignore"]
# first, set up the test course materials directory
# and create some temporary files
cmpath = Path(
Expand All @@ -105,16 +107,26 @@ def test_copy_assignment_files(default_config, tmp_path):
cmpath.mkdir(parents=True)
cmpath.joinpath("file1.txt").touch()
cmpath.joinpath("file2.txt").touch()
cmpath.joinpath(".DS_Store").touch()

template_repo = abctemplate.create_template_dir(default_config, assignment)
abctemplate.copy_assignment_files(
default_config, template_repo, assignment
)
# Test that both text files have been moved to the template dir but that
# the system .DS_Store is not there
for afile in os.listdir(cmpath):
if afile not in files_to_ignore:
assert afile in os.listdir(template_repo)
else:
print(afile)
assert afile not in os.listdir(template_repo)

assert os.listdir(cmpath).sort() == os.listdir(template_repo).sort()
lwasser marked this conversation as resolved.
Show resolved Hide resolved


def test_copy_assignment_files_fails_nodir(default_config, tmp_path):
# test that fails if course_materials dir does not exist
"""Test that fails if course_materials dir does not exist"""
default_config["course_directory"] = tmp_path
assignment = "assignment1"
template_repo = abctemplate.create_template_dir(default_config, assignment)
Expand Down