1
0

dev-data.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. display_name='admin',
  82. description='Admin Group',
  83. )
  84. session.add(item)
  85. session.commit()
  86. # Add a couple of groups so that we can list them
  87. item = pagure.lib.model.PagureGroup(
  88. group_name='group',
  89. group_type='user',
  90. user_id=1, # pingou
  91. display_name='group group',
  92. description='this is a group group',
  93. )
  94. session.add(item)
  95. session.commit()
  96. item = pagure.lib.model.PagureGroup(
  97. group_name='rel-eng',
  98. group_type='user',
  99. user_id=1, # pingou
  100. display_name='Release Engineering',
  101. description='The group of release engineers',
  102. )
  103. session.add(item)
  104. session.commit()
  105. ######################################
  106. # projects
  107. import shutil
  108. # delete folder from local instance to start from a clean slate
  109. if os.path.exists(pagure.APP.config['GIT_FOLDER']):
  110. shutil.rmtree(pagure.APP.config['GIT_FOLDER'])
  111. tests.create_projects(session)
  112. tests.create_projects_git(pagure.APP.config['GIT_FOLDER'], bare=True)
  113. tests.add_content_git_repo(
  114. os.path.join(pagure.APP.config['GIT_FOLDER'], 'test.git'))
  115. tests.add_readme_git_repo(
  116. os.path.join(pagure.APP.config['GIT_FOLDER'], 'test.git'))
  117. # Add some content to the git repo
  118. tests.add_content_git_repo(
  119. os.path.join(pagure.APP.config['GIT_FOLDER'], 'forks', 'pingou',
  120. 'test.git'))
  121. tests.add_readme_git_repo(
  122. os.path.join(pagure.APP.config['GIT_FOLDER'], 'forks', 'pingou',
  123. 'test.git'))
  124. tests.add_commit_git_repo(
  125. os.path.join(pagure.APP.config['GIT_FOLDER'], 'forks', 'pingou',
  126. 'test.git'), ncommits=10)
  127. ######################################
  128. # user_emails
  129. item = pagure.lib.model.UserEmail(
  130. user_id=1,
  131. email='bar@pingou.com')
  132. session.add(item)
  133. item = pagure.lib.model.UserEmail(
  134. user_id=1,
  135. email='foo@pingou.com')
  136. session.add(item)
  137. item = pagure.lib.model.UserEmail(
  138. user_id=2,
  139. email='foo@bar.com')
  140. session.add(item)
  141. item = pagure.lib.model.UserEmail(
  142. user_id=3,
  143. email=user_email)
  144. session.add(item)
  145. session.commit()
  146. ######################################
  147. # user_emails_pending
  148. user = pagure.lib.search_user(session, username='pingou')
  149. email_pend = pagure.lib.model.UserEmailPending(
  150. user_id=user.id,
  151. email='foo@fp.o',
  152. token='abcdef',
  153. )
  154. session.add(email_pend)
  155. session.commit()
  156. ######################################
  157. # issues
  158. # Add an issue and tag it so that we can list them
  159. item = pagure.lib.model.Issue(
  160. id=1,
  161. uid='foobar',
  162. project_id=1,
  163. title='Problem with jenkins build',
  164. content='For some reason the tests fail at line:24',
  165. user_id=1, # pingou
  166. )
  167. session.add(item)
  168. session.commit()
  169. item = pagure.lib.model.Issue(
  170. id=2,
  171. uid='foobar2',
  172. project_id=1,
  173. title='Unit tests failing',
  174. content='Need to fix code for the unit tests to '
  175. 'pass so jenkins build can complete.',
  176. user_id=1, # pingou
  177. )
  178. session.add(item)
  179. session.commit()
  180. user = pagure.lib.search_user(session, username=username)
  181. item = pagure.lib.model.Issue(
  182. id=3,
  183. uid='foobar3',
  184. project_id=1,
  185. title='Segfault during execution',
  186. content='Index out of bounds for variable i?',
  187. user_id=user.id, # current user
  188. )
  189. session.add(item)
  190. session.commit()
  191. ######################################
  192. # pagure_user_group
  193. group = pagure.lib.search_groups(session, pattern=None,
  194. group_name="rel-eng", group_type=None)
  195. user = pagure.lib.search_user(session, username='pingou')
  196. item = pagure.lib.model.PagureUserGroup(
  197. user_id=user.id,
  198. group_id=group.id
  199. )
  200. session.add(item)
  201. session.commit()
  202. user = pagure.lib.search_user(session, username=username)
  203. group = pagure.lib.search_groups(session, pattern=None,
  204. group_name="admin", group_type=None)
  205. item = pagure.lib.model.PagureUserGroup(
  206. user_id=user.id,
  207. group_id=group.id
  208. )
  209. session.add(item)
  210. session.commit()
  211. user = pagure.lib.search_user(session, username='foo')
  212. group = pagure.lib.search_groups(session, pattern=None,
  213. group_name="group", group_type=None)
  214. item = pagure.lib.model.PagureUserGroup(
  215. user_id=user.id,
  216. group_id=group.id
  217. )
  218. session.add(item)
  219. session.commit()
  220. ######################################
  221. # projects_groups
  222. group = pagure.lib.search_groups(session, pattern=None,
  223. group_name="rel-eng", group_type=None)
  224. repo = pagure.get_authorized_project(session, 'test')
  225. item = pagure.lib.model.ProjectGroup(
  226. project_id=repo.id,
  227. group_id=group.id
  228. )
  229. session.add(item)
  230. session.commit()
  231. group = pagure.lib.search_groups(session, pattern=None,
  232. group_name="admin", group_type=None)
  233. repo = pagure.get_authorized_project(session, 'test2')
  234. item = pagure.lib.model.ProjectGroup(
  235. project_id=repo.id,
  236. group_id=group.id
  237. )
  238. session.add(item)
  239. session.commit()
  240. ######################################
  241. # pull_requests
  242. repo = pagure.get_authorized_project(session, 'test')
  243. forked_repo = pagure.get_authorized_project(session, 'test')
  244. req = pagure.lib.new_pull_request(
  245. session=session,
  246. repo_from=forked_repo,
  247. branch_from='master',
  248. repo_to=repo,
  249. branch_to='master',
  250. title='Fixing code for unittest',
  251. user=username,
  252. requestfolder=None,
  253. )
  254. session.commit()
  255. ######################################
  256. # tokens
  257. tests.create_tokens(session)
  258. ######################################
  259. # user_projects
  260. user = pagure.lib.search_user(session, username='foo')
  261. repo = pagure.get_authorized_project(session, 'test')
  262. item = pagure.lib.model.ProjectUser(
  263. project_id=repo.id,
  264. user_id=user.id
  265. )
  266. session.add(item)
  267. session.commit()
  268. user = pagure.lib.search_user(session, username=username)
  269. repo = pagure.get_authorized_project(session, 'test2')
  270. item = pagure.lib.model.ProjectUser(
  271. project_id=repo.id,
  272. user_id=user.id
  273. )
  274. session.add(item)
  275. session.commit()
  276. ######################################
  277. # issue_comments
  278. item = pagure.lib.model.IssueComment(
  279. user_id=1,
  280. issue_uid='foobar',
  281. comment='We may need to adjust the unittests instead of the code.',
  282. )
  283. session.add(item)
  284. session.commit()
  285. ######################################
  286. # issue_to_issue
  287. repo = pagure.get_authorized_project(session, 'test')
  288. all_issues = pagure.lib.search_issues(session, repo)
  289. pagure.lib.add_issue_dependency(session, all_issues[0],
  290. all_issues[1], 'pingou',
  291. pagure.APP.config['GIT_FOLDER'])
  292. ######################################
  293. # pull_request_comments
  294. user = pagure.lib.search_user(session, username='pingou')
  295. # only 1 pull request available atm
  296. pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
  297. item = pagure.lib.model.PullRequestComment(
  298. pull_request_uid=pr.uid,
  299. user_id=user.id,
  300. comment="+1 for me. Btw, could you rebase before you merge?",
  301. notification=0
  302. )
  303. session.add(item)
  304. session.commit()
  305. ######################################
  306. # pull_request_flags
  307. user = pagure.lib.search_user(session, username='pingou')
  308. # only 1 pull request available atm
  309. pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
  310. item = pagure.lib.model.PullRequestFlag(
  311. uid="random_pr_flag_uid",
  312. pull_request_uid=pr.uid,
  313. user_id=user.id,
  314. username=user.user,
  315. percent=80,
  316. comment="Jenkins build passes",
  317. url=str(pr.id)
  318. )
  319. session.add(item)
  320. session.commit()
  321. ######################################
  322. # tags_issues
  323. repo = pagure.get_authorized_project(session, 'test')
  324. issues = pagure.lib.search_issues(session, repo)
  325. item = pagure.lib.model.TagIssue(
  326. issue_uid=issues[0].uid,
  327. tag='Blocker',
  328. )
  329. session.add(item)
  330. session.commit()
  331. ######################################
  332. # tokens_acls
  333. tests.create_tokens_acl(session)
  334. ######################################
  335. # Fork a project
  336. # delete fork data
  337. fork_proj_location = "forks/foo/test.git"
  338. try:
  339. shutil.rmtree(os.path.join(pagure.APP.config['GIT_FOLDER'],
  340. fork_proj_location))
  341. except:
  342. print('git folder already deleted')
  343. try:
  344. shutil.rmtree(os.path.join(pagure.APP.config['DOCS_FOLDER'],
  345. fork_proj_location))
  346. except:
  347. print('docs folder already deleted')
  348. try:
  349. shutil.rmtree(os.path.join(pagure.APP.config['TICKETS_FOLDER'],
  350. fork_proj_location))
  351. except:
  352. print('tickets folder already deleted')
  353. try:
  354. shutil.rmtree(os.path.join(pagure.APP.config['REQUESTS_FOLDER'],
  355. fork_proj_location))
  356. except:
  357. print('requests folder already deleted')
  358. repo = pagure.get_authorized_project(session, 'test')
  359. result = pagure.lib.fork_project(session, 'foo', repo,
  360. pagure.APP.config['GIT_FOLDER'],
  361. pagure.APP.config['DOCS_FOLDER'],
  362. pagure.APP.config['TICKETS_FOLDER'],
  363. pagure.APP.config['REQUESTS_FOLDER'])
  364. if result == 'Repo "test" cloned to "foo/test"':
  365. session.commit()
  366. if __name__ == "__main__":
  367. desc = "Run the dev database initialization/insertion/deletion " \
  368. "script for db located " + str(pagure.APP.config['DB_URL'])
  369. parser = argparse.ArgumentParser(prog="dev-data", description=desc)
  370. parser.add_argument('-i', '--init', action="store_true",
  371. help="Create the dev db")
  372. parser.add_argument('-p', '--populate', action="store_true",
  373. help="Add test data to the db")
  374. parser.add_argument('-d', '--delete', action="store_true",
  375. help="Wipe the dev db")
  376. parser.add_argument('-a', '--all', action="store_true",
  377. help="Create, Wipe, Populate the dev db")
  378. args = parser.parse_args()
  379. # forcing the user to choose
  380. if not any(vars(args).values()):
  381. parser.error('No arguments provided.')
  382. if args.init or args.delete or args.all:
  383. eng, meta = init_database()
  384. if args.delete or args.all:
  385. empty_dev_db(meta, eng)
  386. if args.populate or args.all:
  387. session = pagure.SESSION
  388. invalid_option = ['pingou', 'bar@pingou.com', 'foo', 'foo@bar.com']
  389. print("")
  390. user_name = raw_input(
  391. "Enter your username so we can add you into the test data: ")
  392. while user_name in invalid_option:
  393. print("Reserved names: " + str(invalid_option))
  394. user_name = raw_input(
  395. "Enter your username so we can add you into the test data: ")
  396. if not user_name.replace(" ", ""):
  397. user_name = 'pythagoras'
  398. print("")
  399. user_email = raw_input("Enter your user email: ")
  400. while user_email in invalid_option:
  401. print("Reserved names: " + str(invalid_option))
  402. user_email = raw_input("Enter your user email: ")
  403. if not user_email.replace(" ", ""):
  404. user_email = 'pythagoras@math.com'
  405. insert_data(session, user_name, user_email)