1
0

dev-data.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #!/usr/bin/env python
  2. """ Populate the pagure db with some dev data. """
  3. import sys
  4. import os
  5. import pagure
  6. import tests
  7. from sqlalchemy import create_engine, MetaData
  8. import argparse
  9. '''
  10. Usage:
  11. python dev-data.py --init
  12. python dev-data.py --clean
  13. python dev-data.py --populate
  14. python dev-data.py --all
  15. '''
  16. def init_database():
  17. DB_URL = pagure.APP.config['DB_URL']
  18. # create the table if it doesnt exist
  19. pagure.lib.model.create_tables(
  20. DB_URL,
  21. pagure.APP.config.get('PATH_ALEMBIC_INI', None),
  22. acls=pagure.APP.config.get('ACLS', {}),
  23. debug=True)
  24. engine = create_engine('%s' % DB_URL, echo=True)
  25. metadata = MetaData(engine)
  26. metadata.reflect(bind=engine)
  27. return engine, metadata
  28. def empty_dev_db(metadata, engine):
  29. print('')
  30. print('')
  31. print('WARNING: Deleting all data from ', pagure.APP.config['DB_URL'])
  32. # Dangerous: this will wipe the data from the table but keep the schema
  33. print('')
  34. response = raw_input('Do you want to continue yes or no? ')
  35. if 'yes'.startswith(response.lower()):
  36. for tbl in reversed(metadata.sorted_tables):
  37. if tbl.fullname != 'acls':
  38. engine.execute(tbl.delete())
  39. def insert_data(session, username, user_email):
  40. pagure.APP.config['EMAIL_SEND'] = False
  41. pagure.APP.config['TESTING'] = True
  42. ######################################
  43. # tags
  44. item = pagure.lib.model.Tag(
  45. tag='tag1',
  46. )
  47. session.add(item)
  48. session.commit()
  49. ######################################
  50. # Users
  51. # Create a couple of users
  52. item = pagure.lib.model.User(
  53. user='pingou',
  54. fullname='PY C',
  55. password='foo',
  56. default_email='bar@pingou.com',
  57. )
  58. session.add(item)
  59. session.commit()
  60. item = pagure.lib.model.User(
  61. user='foo',
  62. fullname='foo bar',
  63. password='foo',
  64. default_email='foo@bar.com',
  65. )
  66. session.add(item)
  67. session.commit()
  68. item = pagure.lib.model.User(
  69. user=username,
  70. fullname=username,
  71. password='foo',
  72. default_email=user_email,
  73. )
  74. session.add(item)
  75. session.commit()
  76. ######################################
  77. # pagure_group
  78. item = pagure.lib.model.PagureGroup(
  79. group_name='admin',
  80. user_id=1,
  81. )
  82. session.add(item)
  83. session.commit()
  84. # Add a couple of groups so that we can list them
  85. item = pagure.lib.model.PagureGroup(
  86. group_name='group',
  87. group_type='user',
  88. user_id=1, # pingou
  89. )
  90. session.add(item)
  91. session.commit()
  92. item = pagure.lib.model.PagureGroup(
  93. group_name='rel-eng',
  94. group_type='user',
  95. user_id=1, # pingou
  96. )
  97. session.add(item)
  98. session.commit()
  99. ######################################
  100. # projects
  101. import shutil
  102. # delete folder from local instance to start from a clean slate
  103. if os.path.exists(pagure.APP.config['GIT_FOLDER']):
  104. shutil.rmtree(pagure.APP.config['GIT_FOLDER'])
  105. tests.create_projects(session)
  106. tests.create_projects_git(pagure.APP.config['GIT_FOLDER'], bare=True)
  107. tests.add_content_git_repo(
  108. os.path.join(pagure.APP.config['GIT_FOLDER'], 'test.git'))
  109. tests.add_readme_git_repo(
  110. os.path.join(pagure.APP.config['GIT_FOLDER'], 'test.git'))
  111. # Add some content to the git repo
  112. tests.add_content_git_repo(
  113. os.path.join(pagure.APP.config['GIT_FOLDER'], 'forks', 'pingou',
  114. 'test.git'))
  115. tests.add_readme_git_repo(
  116. os.path.join(pagure.APP.config['GIT_FOLDER'], 'forks', 'pingou',
  117. 'test.git'))
  118. tests.add_commit_git_repo(
  119. os.path.join(pagure.APP.config['GIT_FOLDER'], 'forks', 'pingou',
  120. 'test.git'), ncommits=10)
  121. ######################################
  122. # user_emails
  123. item = pagure.lib.model.UserEmail(
  124. user_id=1,
  125. email='bar@pingou.com')
  126. session.add(item)
  127. item = pagure.lib.model.UserEmail(
  128. user_id=1,
  129. email='foo@pingou.com')
  130. session.add(item)
  131. item = pagure.lib.model.UserEmail(
  132. user_id=2,
  133. email='foo@bar.com')
  134. session.add(item)
  135. item = pagure.lib.model.UserEmail(
  136. user_id=3,
  137. email=user_email)
  138. session.add(item)
  139. session.commit()
  140. ######################################
  141. # user_emails_pending
  142. user = pagure.lib.search_user(session, username='pingou')
  143. email_pend = pagure.lib.model.UserEmailPending(
  144. user_id=user.id,
  145. email='foo@fp.o',
  146. token='abcdef',
  147. )
  148. session.add(email_pend)
  149. session.commit()
  150. ######################################
  151. # issues
  152. # Add an issue and tag it so that we can list them
  153. item = pagure.lib.model.Issue(
  154. id=1,
  155. uid='foobar',
  156. project_id=1,
  157. title='Problem with jenkins build',
  158. content='For some reason the tests fail at line:24',
  159. user_id=1, # pingou
  160. )
  161. session.add(item)
  162. session.commit()
  163. item = pagure.lib.model.Issue(
  164. id=2,
  165. uid='foobar2',
  166. project_id=1,
  167. title='Unit tests failing',
  168. content='Need to fix code for the unit tests to '
  169. 'pass so jenkins build can complete.',
  170. user_id=1, # pingou
  171. )
  172. session.add(item)
  173. session.commit()
  174. user = pagure.lib.search_user(session, username=username)
  175. item = pagure.lib.model.Issue(
  176. id=3,
  177. uid='foobar3',
  178. project_id=1,
  179. title='Segfault during execution',
  180. content='Index out of bounds for variable i?',
  181. user_id=user.id, # current user
  182. )
  183. session.add(item)
  184. session.commit()
  185. ######################################
  186. # pagure_user_group
  187. group = pagure.lib.search_groups(session, pattern=None,
  188. group_name="rel-eng", group_type=None)
  189. user = pagure.lib.search_user(session, username='pingou')
  190. item = pagure.lib.model.PagureUserGroup(
  191. user_id=user.id,
  192. group_id=group.id
  193. )
  194. session.add(item)
  195. session.commit()
  196. user = pagure.lib.search_user(session, username=username)
  197. group = pagure.lib.search_groups(session, pattern=None,
  198. group_name="admin", group_type=None)
  199. item = pagure.lib.model.PagureUserGroup(
  200. user_id=user.id,
  201. group_id=group.id
  202. )
  203. session.add(item)
  204. session.commit()
  205. user = pagure.lib.search_user(session, username='foo')
  206. group = pagure.lib.search_groups(session, pattern=None,
  207. group_name="group", group_type=None)
  208. item = pagure.lib.model.PagureUserGroup(
  209. user_id=user.id,
  210. group_id=group.id
  211. )
  212. session.add(item)
  213. session.commit()
  214. ######################################
  215. # projects_groups
  216. group = pagure.lib.search_groups(session, pattern=None,
  217. group_name="rel-eng", group_type=None)
  218. repo = pagure.lib.get_project(session, 'test')
  219. item = pagure.lib.model.ProjectGroup(
  220. project_id=repo.id,
  221. group_id=group.id
  222. )
  223. session.add(item)
  224. session.commit()
  225. group = pagure.lib.search_groups(session, pattern=None,
  226. group_name="admin", group_type=None)
  227. repo = pagure.lib.get_project(session, 'test2')
  228. item = pagure.lib.model.ProjectGroup(
  229. project_id=repo.id,
  230. group_id=group.id
  231. )
  232. session.add(item)
  233. session.commit()
  234. ######################################
  235. # pull_requests
  236. repo = pagure.lib.get_project(session, 'test')
  237. forked_repo = pagure.lib.get_project(session, 'test')
  238. req = pagure.lib.new_pull_request(
  239. session=session,
  240. repo_from=forked_repo,
  241. branch_from='master',
  242. repo_to=repo,
  243. branch_to='master',
  244. title='Fixing code for unittest',
  245. user=username,
  246. requestfolder=None,
  247. )
  248. session.commit()
  249. ######################################
  250. # tokens
  251. tests.create_tokens(session)
  252. ######################################
  253. # user_projects
  254. user = pagure.lib.search_user(session, username='foo')
  255. repo = pagure.lib.get_project(session, 'test')
  256. item = pagure.lib.model.ProjectUser(
  257. project_id=repo.id,
  258. user_id=user.id
  259. )
  260. session.add(item)
  261. session.commit()
  262. user = pagure.lib.search_user(session, username=username)
  263. repo = pagure.lib.get_project(session, 'test2')
  264. item = pagure.lib.model.ProjectUser(
  265. project_id=repo.id,
  266. user_id=user.id
  267. )
  268. session.add(item)
  269. session.commit()
  270. ######################################
  271. # issue_comments
  272. item = pagure.lib.model.IssueComment(
  273. user_id=1,
  274. issue_uid='foobar',
  275. comment='We may need to adjust the unittests instead of the code.',
  276. )
  277. session.add(item)
  278. session.commit()
  279. ######################################
  280. # issue_to_issue
  281. repo = pagure.lib.get_project(session, 'test')
  282. all_issues = pagure.lib.search_issues(session, repo)
  283. pagure.lib.add_issue_dependency(session, all_issues[0],
  284. all_issues[1], 'pingou',
  285. pagure.APP.config['GIT_FOLDER'])
  286. ######################################
  287. # pull_request_comments
  288. user = pagure.lib.search_user(session, username='pingou')
  289. # only 1 pull request available atm
  290. pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
  291. item = pagure.lib.model.PullRequestComment(
  292. pull_request_uid=pr.uid,
  293. user_id=user.id,
  294. comment="+1 for me. Btw, could you rebase before you merge?",
  295. notification=0
  296. )
  297. session.add(item)
  298. session.commit()
  299. ######################################
  300. # pull_request_flags
  301. user = pagure.lib.search_user(session, username='pingou')
  302. # only 1 pull request available atm
  303. pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
  304. item = pagure.lib.model.PullRequestFlag(
  305. uid="random_pr_flag_uid",
  306. pull_request_uid=pr.uid,
  307. user_id=user.id,
  308. username=user.user,
  309. percent=80,
  310. comment="Jenkins build passes",
  311. url=str(pr.id)
  312. )
  313. session.add(item)
  314. session.commit()
  315. ######################################
  316. # tags_issues
  317. repo = pagure.lib.get_project(session, 'test')
  318. issues = pagure.lib.search_issues(session, repo)
  319. item = pagure.lib.model.TagIssue(
  320. issue_uid=issues[0].uid,
  321. tag='Blocker',
  322. )
  323. session.add(item)
  324. session.commit()
  325. ######################################
  326. # tokens_acls
  327. tests.create_tokens_acl(session)
  328. ######################################
  329. # Fork a project
  330. # delete fork data
  331. fork_proj_location = "forks/foo/test.git"
  332. try:
  333. shutil.rmtree(os.path.join(pagure.APP.config['GIT_FOLDER'],
  334. fork_proj_location))
  335. except:
  336. print('git folder already deleted')
  337. try:
  338. shutil.rmtree(os.path.join(pagure.APP.config['DOCS_FOLDER'],
  339. fork_proj_location))
  340. except:
  341. print('docs folder already deleted')
  342. try:
  343. shutil.rmtree(os.path.join(pagure.APP.config['TICKETS_FOLDER'],
  344. fork_proj_location))
  345. except:
  346. print('tickets folder already deleted')
  347. try:
  348. shutil.rmtree(os.path.join(pagure.APP.config['REQUESTS_FOLDER'],
  349. fork_proj_location))
  350. except:
  351. print('requests folder already deleted')
  352. repo = pagure.lib.get_project(session, 'test')
  353. result = pagure.lib.fork_project(session, 'foo', repo,
  354. pagure.APP.config['GIT_FOLDER'],
  355. pagure.APP.config['DOCS_FOLDER'],
  356. pagure.APP.config['TICKETS_FOLDER'],
  357. pagure.APP.config['REQUESTS_FOLDER'])
  358. if result == 'Repo "test" cloned to "foo/test"':
  359. session.commit()
  360. if __name__ == "__main__":
  361. desc = "Run the dev database initialization/insertion/deletion " \
  362. "script for db located " + str(pagure.APP.config['DB_URL'])
  363. parser = argparse.ArgumentParser(prog="dev-data", description=desc)
  364. parser.add_argument('-i', '--init', action="store_true",
  365. help="Create the dev db")
  366. parser.add_argument('-p', '--populate', action="store_true",
  367. help="Add test data to the db")
  368. parser.add_argument('-d', '--delete', action="store_true",
  369. help="Wipe the dev db")
  370. parser.add_argument('-a', '--all', action="store_true",
  371. help="Create, Wipe, Populate the dev db")
  372. args = parser.parse_args()
  373. # forcing the user to choose
  374. if not any(vars(args).values()):
  375. parser.error('No arguments provided.')
  376. if args.init or args.delete or args.all:
  377. eng, meta = init_database()
  378. if args.delete or args.all:
  379. empty_dev_db(meta, eng)
  380. if args.populate or args.all:
  381. session = pagure.SESSION
  382. invalid_option = ['pingou', 'bar@pingou.com', 'foo', 'foo@bar.com']
  383. print("")
  384. user_name = raw_input(
  385. "Enter your username so we can add you into the test data: ")
  386. while user_name in invalid_option:
  387. print("Reserved names: " + str(invalid_option))
  388. user_name = raw_input(
  389. "Enter your username so we can add you into the test data: ")
  390. if not user_name.replace(" ", ""):
  391. user_name = 'pythagoras'
  392. print("")
  393. user_email = raw_input("Enter your user email: ")
  394. while user_email in invalid_option:
  395. print("Reserved names: " + str(invalid_option))
  396. user_email = raw_input("Enter your user email: ")
  397. if not user_email.replace(" ", ""):
  398. user_email = 'pythagoras@math.com'
  399. insert_data(session, user_name, user_email)