-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateProjectDirectories.py
executable file
·222 lines (192 loc) · 5.61 KB
/
createProjectDirectories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python
from __future__ import (absolute_import, division,
print_function)
import argparse
import sys
import os
import shutil
import subprocess
usage = '''
Creates a directory and subdirectories for a version controlled project.
Starts either a mercurial (hg) or a git (default) repo.
Project structure follows roughly the suggestions from:
A Quick Guide to Organizing Computational Biology Projects
http://dx.doi.org/10.1371/journal.pcbi.1000424
Directory structure:
Project/
.git/
data/
doc/
scripts/
results/
Notes:
By default it assumes that the repo will live online in bitbucket, and the user name is also set to "adomingues". Use option -r to change this and/or edit directly in the code. If bitbucket-cli is installed (pip install --user bitbucket-cli) it will also create the repo.
'''
def getArgs():
"""Parse sys.argv"""
parser = argparse.ArgumentParser(
description=usage,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'-p', '--project',
required=True,
type=str,
help='Project name. The root of the directory structure'
)
parser.add_argument(
'-vc', '--versionControl',
required=True,
type=str,
default="git",
help='Options: hg or git. Starts version control with list of ignored files included.'
)
parser.add_argument(
'-r', '--repository',
required=False,
type=str,
default="bitbucket",
help='Options: github or bitbucket. Adds an online repo to thee VCS definitions.'
)
args = parser.parse_args()
return args
def writeIgnore(fout):
ignores = [
'syntax: glob',
'*.orig',
'*.rej',
'*~',
'*.o',
'tests/*.err',
'data/*',
'results/*',
'Oler_raw_data/*',
'*temp*',
'*.rda',
'*.RData',
'*.gff',
'*.gff3',
'*.pdf',
'*.PDF',
'*.aux',
'*.dvi',
'*.log',
'*.nav',
'*.out',
'*.snm',
'*.toc',
'*svg',
'*Rhistory',
'*DS_Store',
'*.bed',
'*.gtf',
'*.loci',
'*.stats',
'*.tracking',
'*.loci',
'*.kate-swp',
'*.svg',
'*.xlsx',
'<<<<<<< local',
'=======',
'*.xls',
'*.backup',
'*.gz',
'*.rdb',
'*.rdx',
'*.bbl',
'*.blg',
'*.ods',
'*.zip',
' ',
'.bpipe/*',
'commandlog.txt',
'*.sublime-project',
'*.sublime-workspace',
'>>>>>>> other',
' ',
'syntax: regexp',
'.*\#.*\#$'
]
with open(fout, 'w') as f:
f.write('\n'.join(ignores))
def createFolders(project):
folders = ['data', 'doc', 'scripts', 'results']
if not os.path.exists(project):
os.makedirs(project)
os.chdir(project)
for folder in folders:
if not os.path.exists(folder):
os.makedirs(folder)
print("Template project directories are created.")
def startVC(project, vcs, repo):
if vcs == "git":
os.system("git init")
ignore = '.gitignore'
elif vcs == "hg":
os.system("hg init")
ignore = '.hgignore'
if repo == 'bitbucket':
repo_name = (
"default = https://adomingues@bitbucket.org/adomingues/" +
project)
hgrc_file = open(".hg/hgrc", 'w')
hgrc_file.write("[paths]\n")
hgrc_file.write(repo_name+"\n")
hgrc_file.close()
else:
pass
writeIgnore(ignore)
def cmdExists(cmd):
'''
Source: https://stackoverflow.com/a/11069822/1274242
'''
return subprocess.call("type " + cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
def createBickBucketRepo(project, vcs, repo):
print("Creating online repo..")
cmd = "bitbucket"
bit_arguments = ("bitbucket create --private --protocol ssh --scm git " + project).split(" ")
git_add_remote = ("git remote add origin https://adomingues@bitbucket.org/adomingues/" + project + ".git").split(" ")
git_push = ("git push -u origin master").split(" ")
if repo == "bitbucket" and cmdExists(cmd) and vcs == "git":
call = [bit_arguments]
subprocess.check_call(bit_arguments)
os.chdir(project)
subprocess.call(git_add_remote)
print('Next steps:')
print('1. Add files with "git add <files>"')
print('2. git commit -m "Initial commit."')
print('3. git push -u origin master')
def createDocs(project):
print('Creating minimal docs for project')
docs = ["doc/" + project + ".md",
"README.md"
]
for doc in docs:
try:
with open(doc, 'w'):
pass
except:
print('Something went wrong! Could not create file')
sys.exit(0)
def main():
current_dir = os.getcwd()
args = getArgs()
project = args.project
repo = args.repository
print("The project's name is: ", project)
if args.versionControl == "git":
vcs = args.versionControl
elif args.versionControl == "hg":
vcs = args.versionControl
else:
print('No version control system provided.')
createFolders(project)
createDocs(project)
startVC(project, vcs, repo)
os.chdir(current_dir)
print("Changing directory to: " + os.getcwd())
createBickBucketRepo(project, vcs, repo)
if __name__ == "__main__":
main()