|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import argparse |
9 | | -import json |
10 | 9 | import logging |
11 | 10 | import os |
12 | 11 | import re |
13 | 12 | from carousel import __version__ |
14 | 13 | import sys |
| 14 | +from dulwich import porcelain, config as gitconfig |
15 | 15 |
|
16 | 16 | # set up logging |
17 | 17 | logging.basicConfig(level=logging.INFO) |
|
20 | 20 | # constants |
21 | 21 | CWD = os.getcwd() |
22 | 22 | OKAY = r'[\w\d]' |
23 | | -LAYERS = ['simulations', 'outputs', 'calculations', 'formulas', 'data'] |
24 | | -DFLT_MODEL = dict.fromkeys(LAYERS) |
25 | | -DFLT = 'my_model.json' |
26 | | -MODELS = 'models' |
27 | | -PATHS = [MODELS] + LAYERS |
28 | 23 | INIT_CONTENT = """ |
29 | 24 | import os |
30 | 25 |
|
31 | 26 | __version__ = '0.1' |
32 | | -__author__ = 'your name' |
33 | | -__email__ = 'your.name@company.com' |
| 27 | +__author__ = '%s' |
| 28 | +__email__ = '%s' |
34 | 29 |
|
35 | | -PKG_PATH = os.path.abspath(os.path.dirname(__file__)) |
36 | | -PROJ_PATH = os.path.dirname(PKG_PATH) |
| 30 | +PROJ_PATH = os.path.abspath(os.path.dirname(__file__)) |
| 31 | +# TODO: change PROJ_PATH to MODELPATH everywhere |
37 | 32 | """ |
38 | 33 | DESCRIPTION = """ |
39 | | -Creates a Carousel project. See documentation for more information. |
| 34 | +Create a Carousel project file structure. See documentation for more detail. |
40 | 35 | """ |
| 36 | +GIT_GLOBAL = os.path.expanduser(os.path.join('~', '.gitconfig')) |
| 37 | +UNKNOWN = 'unknown' |
| 38 | +USERNAME = (os.environ.get('USERNAME', UNKNOWN) if sys.platform == 'win32' |
| 39 | + else os.environ.get('USER', UNKNOWN)) # default username |
| 40 | +HOSTNAME = os.uname()[1] |
| 41 | +USEREMAIL = '%s@%s' % (USERNAME, HOSTNAME) |
| 42 | + |
| 43 | + |
| 44 | +def get_gitconfig(git_path, section, name): |
| 45 | + try: |
| 46 | + return gitconfig.ConfigFile.from_path(git_path).get(section, name) |
| 47 | + except (IOError, KeyError): |
| 48 | + return None |
| 49 | + |
41 | 50 |
|
42 | 51 | # run from command line |
43 | 52 | if __name__ == '__main__': |
44 | 53 | parser = argparse.ArgumentParser(description=DESCRIPTION) |
45 | 54 | parser.add_argument('project', help='name of Carousel project to create') |
46 | 55 | parser.add_argument('--version', action='version', |
47 | 56 | version=('%(prog)s' + ' %s' % __version__)) |
| 57 | + parser.add_argument('-g', '--git', action='store_true', |
| 58 | + help='initialize Git repository for project') |
| 59 | + parser.add_argument( |
| 60 | + '-f', '--folders', action='append', default=['data'], |
| 61 | + help=('create layer folders in project package, list each folder' |
| 62 | + ' separately, can be used more than once, default is "data"')) |
| 63 | + parser.add_argument('--author', help="Project author's full name", |
| 64 | + default=USERNAME) |
| 65 | + parser.add_argument('--email', help="Project author's email", |
| 66 | + default=USEREMAIL) |
48 | 67 | args = parser.parse_args() |
49 | 68 | # exit with error if no project name specified |
50 | 69 | if len(sys.argv) < 2: |
|
63 | 82 | sys.exit('The path, %s, already exists.' % project_name) |
64 | 83 | os.mkdir(project_name) # make project folder |
65 | 84 | LOGGER.info('Project created at path, %s.', project_name) |
66 | | - # make project file structure |
67 | | - for p in PATHS + [project_pkg]: |
68 | | - os.mkdir(os.path.join(project_name, p)) |
69 | | - LOGGER.debug('created folder: %s', p) |
70 | 85 | # make project package |
| 86 | + os.mkdir(os.path.join(project_name, project_pkg)) # make project package |
| 87 | + LOGGER.info('Project package, %s, created.', project_pkg) |
71 | 88 | pkg_init = os.path.join(project_name, project_pkg, '__init__.py') |
| 89 | + # try to get user info from git config |
| 90 | + username = useremail = None |
| 91 | + if args.author == USERNAME: |
| 92 | + username = get_gitconfig(GIT_GLOBAL, 'user', 'name') |
| 93 | + if username: |
| 94 | + args.author = username |
| 95 | + if args.email == USEREMAIL: |
| 96 | + useremail = get_gitconfig(GIT_GLOBAL, 'user', 'email') |
| 97 | + if useremail: |
| 98 | + args.email = useremail |
72 | 99 | with open(pkg_init, 'w') as init: |
73 | 100 | init.write('"""\nThis is the %s package.\n"""\n' % project_pkg) |
74 | | - init.write(INIT_CONTENT) |
75 | | - # make default model |
76 | | - with open(os.path.join(project_name, MODELS, DFLT), 'w') as dflt: |
77 | | - json.dump(DFLT_MODEL, dflt, indent=2) |
78 | | - LOGGER.debug('created %s in %s/%s', DFLT, project_name, MODELS) |
| 101 | + init.write(INIT_CONTENT % (args.author, args.email)) |
| 102 | + LOGGER.info('Package file created: %s.', pkg_init) |
| 103 | + if args.git: |
| 104 | + repo = porcelain.init(project_name) |
| 105 | + LOGGER.info('Project Git repository initialized: %s.', repo) |
| 106 | + porcelain.add(repo, os.path.relpath(pkg_init, project_name)) |
| 107 | + LOGGER.info('Project package added to index') |
| 108 | + # if global git config username and email not set, use args if specified |
| 109 | + kwargs = {} |
| 110 | + if not username: |
| 111 | + username = args.author |
| 112 | + if not useremail: |
| 113 | + useremail = args.email |
| 114 | + if not os.path.exists(GIT_GLOBAL): |
| 115 | + conf = repo.get_config() |
| 116 | + conf.set('user', 'name', username) |
| 117 | + conf.set('user', 'email', useremail) |
| 118 | + conf.write_to_path() |
| 119 | + else: |
| 120 | + kwargs['author'] = '%s <%s>' % (username, useremail) |
| 121 | + sha1 = porcelain.commit(repo, message='initial dump', **kwargs) |
| 122 | + LOGGER.info('Project initial commit: %s.', sha1) |
| 123 | + porcelain.log(repo, outstream=logging.root.handlers[0].stream) |
| 124 | + # make project layer folders |
| 125 | + for fp in args.folders: |
| 126 | + os.mkdir(os.path.join(project_name, project_pkg, fp)) |
| 127 | + LOGGER.info('created folder: %s', fp) |
| 128 | + LOGGER.info('Carousel quickstart completed.') |
0 commit comments