|
#!/usr/bin/env python |
|
|
|
|
|
import argparse |
|
import ConfigParser |
|
import logging |
|
import os |
|
import subprocess |
|
import sys |
|
|
|
|
|
NOTIFY = (logging.INFO + logging.WARN) / 2 |
|
|
|
LOGLEVELS = [logging.DEBUG, |
|
logging.INFO, |
|
NOTIFY, |
|
logging.WARN, |
|
logging.ERROR, |
|
logging.FATAL] |
|
|
|
README = 'README.rst' |
|
|
|
|
|
def loglevel_for_integer(level): |
|
levels = LOGLEVELS |
|
if level < 0: |
|
return LOGLEVELS[0] |
|
if level >= len(LOGLEVELS): |
|
return LOGLEVELS[-1] |
|
return LOGLEVELS[level] |
|
|
|
|
|
def main(): |
|
|
|
conf_parser = argparse.ArgumentParser( |
|
# Turn off help, so we print all options in response to -h |
|
add_help=False) |
|
conf_parser.add_argument("-c", "--configfile", |
|
default='~/.initproject.conf', |
|
help="Specify configuration file", |
|
metavar="FILE") |
|
args, remaining_argv = conf_parser.parse_known_args() |
|
config = ConfigParser.SafeConfigParser() |
|
config.read([os.path.expanduser(args.configfile)]) |
|
defaults = {} |
|
for section in config.sections(): |
|
defaults.update(('{0}_{1}'.format(section, varname), value) |
|
for varname, value in config.items(section)) |
|
|
|
parser = argparse.ArgumentParser( |
|
# Inherit options from config_parser |
|
parents=[conf_parser], |
|
# print script description with -h/--help |
|
#description=__doc__, |
|
# Don't mess with format of description |
|
#formatter_class=argparse.RawDescriptionHelpFormatter |
|
) |
|
|
|
parser.set_defaults(**defaults) |
|
|
|
parser.add_argument( |
|
'projectname', nargs='?') |
|
|
|
parser.add_argument( |
|
'--python-virtualenv', |
|
dest='python_virtualenv', |
|
action='store_true') |
|
|
|
parser.add_argument( |
|
'--git', |
|
dest='vcs_git', |
|
action='store_true') |
|
|
|
parser.add_argument( |
|
'--gist', |
|
dest='repository_gist', |
|
action='store_true') |
|
|
|
parser.add_argument( |
|
'--github-login', |
|
dest='github_login') |
|
|
|
parser.add_argument( |
|
'--github-password', |
|
dest='github_password') |
|
|
|
parser.add_argument( |
|
'--email', |
|
dest='user_email') |
|
|
|
parser.add_argument( |
|
'--readme', |
|
dest='files_readme', |
|
action='store_true') |
|
|
|
parser.add_argument( |
|
'--dump-config', |
|
dest='dumpconfig', |
|
action='store_true') |
|
|
|
parser.add_argument( |
|
'-v', '--verbose', |
|
action='count', |
|
dest='verbose', |
|
default=0, |
|
help="Increase verbosity") |
|
|
|
parser.add_argument( |
|
'-q', '--quiet', |
|
action='count', |
|
dest='quiet', |
|
default=0, |
|
help='Decrease verbosity') |
|
|
|
parser.add_argument( |
|
'--verbosity', |
|
dest='operation_verbosity', |
|
default=2, |
|
help='Set verbosity') |
|
|
|
args = parser.parse_args(remaining_argv) |
|
|
|
global logger |
|
args.operation_verbosity = args.operation_verbosity + args.verbose - args.quiet |
|
loglevel = loglevel_for_integer(4 - args.operation_verbosity) |
|
logging.basicConfig() |
|
logger = logging.getLogger('initproject') |
|
logger.setLevel(loglevel) |
|
|
|
if args.dumpconfig: |
|
for option, value in args._get_kwargs(): |
|
if '_' in option: |
|
section, varname = option.split('_', 1) |
|
if not config.has_section(section): |
|
config.add_section(section) |
|
config.set(section, varname, unicode(value)) |
|
config.write(sys.stdout) |
|
sys.exit(0) |
|
|
|
if not args.projectname: |
|
parser.error('project name argument missing') |
|
|
|
files = [] |
|
project_dir = os.path.join(os.getcwd(), args.projectname) |
|
in_project_dir = lambda *a: os.path.join(project_dir, *a) |
|
|
|
def run_in_project_dir(cmd): |
|
old_dir = os.getcwd() |
|
os.chdir(project_dir) |
|
subprocess.call(cmd.split()) |
|
os.chdir(old_dir) |
|
|
|
try: |
|
os.mkdir(args.projectname) |
|
except OSError as e: |
|
if e.errno != 17: |
|
# not a "File exists" exception |
|
raise |
|
|
|
if args.python_virtualenv: |
|
import virtualenv |
|
virtualenv.logger = virtualenv.Logger([(loglevel, sys.stdout)]) |
|
virtualenv.create_environment(args.projectname, use_distribute=True) |
|
|
|
if args.files_readme: |
|
if not os.path.exists(in_project_dir(README)): |
|
line = (len(args.projectname) + 2) * '=' |
|
open(in_project_dir(README), 'w').write( |
|
'{0}\n {1}\n{0}\n'.format(line, args.projectname)) |
|
files.append(README) |
|
|
|
if args.vcs_git or args.repository_gist: |
|
run_in_project_dir('git init') |
|
email = args.user_email.format(**args.__dict__) |
|
run_in_project_dir('git config user.email {0}'.format(email)) |
|
|
|
if args.repository_gist: |
|
if files: |
|
from github3.api import Github |
|
github = Github(args.github_login, args.github_password) |
|
files = dict( |
|
(filename, {'content': open(in_project_dir(filename)).read()}) |
|
for filename in files) |
|
gist = github.gists.create_gist(args.projectname, files=files) |
|
logger.log(NOTIFY, 'Created gist {0} at {1}'.format(gist.id, gist.git_push_url)) |
|
run_in_project_dir('git remote add origin {0}'.format(gist.git_push_url)) |
|
run_in_project_dir('git fetch origin') |
|
run_in_project_dir('git reset --hard origin/master') |
|
else: |
|
logger.warn("Can't create an empty Gist. Try adding a readme file.") |
|
|
|
if __name__ == '__main__': |
|
main() |