boop.py 1.13 KB
Newer Older
1
2
3
#!/bin/env python3

import os
4
import sys
5
6
7
import shutil


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ProgramError(Exception):
    """Exception raised during the program execution.

    Examples:

    >>> try:
    ...     raise ProgramError("Oops")
    ... except ProgramError as e:
    ...     print(e.args[0])
    Oops
    """

    pass


23
def main():
24
25
26
27
28
29
    # Check that content directory exists on the filesystem
    content_path = os.path.join(os.curdir, "content")
    if not os.path.isdir(content_path):
        raise ProgramError("./content directory does not exist")

    # Make sure output dir doesn't exist (shutil will create it after)
30
31
    output_path = os.path.join(os.curdir, "output")
    if os.path.isdir(output_path):
32
        # it should be a directory...
33
        shutil.rmtree(output_path)
34
35
36
    elif os.path.exists(output_path):
        # ... but it also can be a simple file if user created it manually!
        os.remove(output_path)
37
38

    # And copy the files from ./content to ./output
39
    shutil.copytree(content_path, output_path)
40
41
42


if __name__ == "__main__":
43
44
45
46
47
    try:
        main()
        print("Boop!")
    except ProgramError as e:
        print(e.args[0], file=sys.stderr)