contribute.py 3.22 KB
Newer Older
Laurent Heirendt's avatar
Laurent Heirendt committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import click
import datetime
import os
from distutils.dir_util import copy_tree
from shutil import copyfile

@click.command()
@click.option('--date', default=datetime.datetime.today().strftime('%Y-%m-%d'), help='Date of the presentation - format: YYYY-MM-DD')
@click.option('--name', default='myPresentation', help='Short name of the presentation.')

def main(date, name):
    """Copies the template folder"""

    # validate the date
    validateDate(date)

    # get the directory
    mainDir = date[:4]

    # get the root directory
    rootDir = os.getcwd()

Laurent Heirendt's avatar
Laurent Heirendt committed
23
24
25
26
    # create a root directory if not existing
    if not os.path.exists(mainDir):
        os.mkdir(mainDir)

Laurent Heirendt's avatar
Laurent Heirendt committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    # generate the full name of the presentation
    fullName = date + "_" + name

    # generate the full path
    fullPath = os.path.join(os.getcwd(), mainDir, fullName)
    slidesPath = os.path.join(fullPath, 'slides')

    # print out a summary
    click.echo(' > Date: {0}' . format(date))
    click.echo(' > Name: {0}' . format(name))
    click.echo(' > Directory: {0}' . format(fullPath))
    click.echo(' > Root directory: {0}' . format(rootDir))

    # create the directory
    if not os.path.exists(fullPath):
        os.mkdir(fullPath)
        # create an empty slides folder inside
        if not os.path.exists(slidesPath):
            os.mkdir(slidesPath)

        click.echo(' > Directory for slides {0} created.' . format(slidesPath))
    else:
        click.echo(' > Directory for slides{0} already exists.' . format(slidesPath))

    # change to the root directory of the presentation
    os.chdir(fullPath)

    # generate the symlink to the theme
    createSymlink('../../theme', 'theme')
    createSymlink('../../theme/package.json', 'package.json')

    # copy the contents of the template folder
    if not os.path.isfile(os.path.join(fullPath, 'slides', 'index.md')):
        copy_tree(os.path.join(rootDir, 'template', 'slides'), slidesPath)
        click.echo(' > Template slides copied.')
    else:
        click.echo(' > Slide deck already exists.')

    # copy the Gruntfile
    if not os.path.isfile(os.path.join(fullPath, 'Gruntfile.coffee')):
        copyfile(os.path.join(rootDir, 'template', 'Gruntfile.coffee'), os.path.join(fullPath, 'Gruntfile.coffee'))
        click.echo(' > Gruntfile copied.')
    else:
        click.echo(' > Gruntfile already exists.')


    # launch the presentation deck
    if not os.path.exists(os.path.join(fullPath, 'node_modules')):
        click.echo(' > Installing dependencies ... ')
        os.system('yarn add -g grunt-cli generator-reveal')
        os.system('yarn')
        click.echo(' > All dependencies installed.')
    else:
        click.echo(' > All dependencies already installed.')

    # launch the server
    os.system('grunt server')

def createSymlink(src, dst):
    if not os.path.islink(dst):
        os.symlink(src, dst)
        click.echo(' > Symlink to {0} created.' . format(dst))
    else:
        click.echo(' > Symlink to {0} already exists.' . format(dst))


def validateDate(input):
    """Validate a date string to fit the ISO format"""
    try:
        datetime.datetime.strptime(input, '%Y-%m-%d')
    except ValueError:
        print('The date {} is invalid'.format(input))
        raise

if __name__ == '__main__':
    main()