dev-data.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """ Populate the pagure db with some dev data. """
  4. from __future__ import print_function, unicode_literals
  5. import argparse
  6. import os
  7. import sys
  8. import tempfile
  9. import pygit2
  10. import shutil
  11. from sqlalchemy import create_engine, MetaData
  12. import pagure
  13. import tests
  14. from pagure.lib import create_session
  15. from pagure.lib.login import generate_hashed_value
  16. from pagure.lib.model import create_default_status
  17. from pagure.lib.repo import PagureRepo
  18. '''
  19. Usage:
  20. python dev-data.py --init
  21. python dev-data.py --clean
  22. python dev-data.py --populate
  23. python dev-data.py --all
  24. '''
  25. _config = pagure.config.reload_config()
  26. def init_database():
  27. DB_URL = _config['DB_URL']
  28. # create the table if it doesnt exist
  29. pagure.lib.model.create_tables(
  30. DB_URL,
  31. _config.get('PATH_ALEMBIC_INI', None),
  32. acls=_config.get('ACLS', {}),
  33. debug=True)
  34. engine = create_engine('%s' % DB_URL, echo=True)
  35. metadata = MetaData(engine)
  36. metadata.reflect(bind=engine)
  37. return engine, metadata
  38. def empty_dev_db(metadata, engine):
  39. print('')
  40. print('')
  41. print('WARNING: Deleting all data from ', _config['DB_URL'])
  42. # Dangerous: this will wipe the data from the table but keep the schema
  43. print('')
  44. response = raw_input('Do you want to continue? (yes/no) ')
  45. if 'yes'.startswith(response.lower()):
  46. for tbl in reversed(metadata.sorted_tables):
  47. if tbl.fullname != 'acls':
  48. engine.execute(tbl.delete())
  49. else:
  50. exit("Aborting.")
  51. def insert_data(session, username, user_email):
  52. _config['EMAIL_SEND'] = False
  53. _config['TESTING'] = True
  54. # Populate with default statuses
  55. create_default_status(session)
  56. print('Default statuses populated')
  57. ######################################
  58. # tags
  59. item = pagure.lib.model.Tag(
  60. tag='tag1',
  61. )
  62. session.add(item)
  63. session.commit()
  64. ######################################
  65. # Users
  66. # Create a couple of users
  67. pingou = item = pagure.lib.model.User(
  68. user='pingou',
  69. fullname='PY C',
  70. password=generate_hashed_value(u'testing123'),
  71. token=None,
  72. default_email='bar@pingou.com',
  73. )
  74. session.add(item)
  75. session.commit()
  76. print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
  77. foo = item = pagure.lib.model.User(
  78. user='foo',
  79. fullname='foo bar',
  80. password=generate_hashed_value(u'testing123'),
  81. token=None,
  82. default_email='foo@bar.com',
  83. )
  84. session.add(item)
  85. session.commit()
  86. print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
  87. you = item = pagure.lib.model.User(
  88. user=username,
  89. fullname=username,
  90. password=generate_hashed_value(u'testing123'),
  91. token=None,
  92. default_email=user_email,
  93. )
  94. session.add(item)
  95. session.commit()
  96. print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
  97. ######################################
  98. # pagure_group
  99. item = pagure.lib.model.PagureGroup(
  100. group_name='admin',
  101. group_type='admin',
  102. user_id=pingou.id,
  103. display_name='admin',
  104. description='Admin Group',
  105. )
  106. session.add(item)
  107. session.commit()
  108. print('Created "admin" group. Pingou is a member.')
  109. # Add a couple of groups so that we can list them
  110. item = pagure.lib.model.PagureGroup(
  111. group_name='group',
  112. group_type='user',
  113. user_id=pingou.id,
  114. display_name='group group',
  115. description='this is a group group',
  116. )
  117. session.add(item)
  118. session.commit()
  119. print('Created "group" group. Pingou is a member.')
  120. item = pagure.lib.model.PagureGroup(
  121. group_name='rel-eng',
  122. group_type='user',
  123. user_id=pingou.id,
  124. display_name='Release Engineering',
  125. description='The group of release engineers',
  126. )
  127. session.add(item)
  128. session.commit()
  129. print('Created "rel-eng" group. Pingou is a member.')
  130. ######################################
  131. # projects
  132. import shutil
  133. # delete folder from local instance to start from a clean slate
  134. if os.path.exists(_config['GIT_FOLDER']):
  135. shutil.rmtree(_config['GIT_FOLDER'])
  136. # Create projects
  137. item = project1 = pagure.lib.model.Project(
  138. user_id=pingou.id,
  139. name='test',
  140. is_fork=False,
  141. parent_id=None,
  142. description='test project #1',
  143. hook_token='aaabbbccc',
  144. )
  145. item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  146. session.add(item)
  147. session.flush()
  148. tests.create_locks(session, item)
  149. item = project2 = pagure.lib.model.Project(
  150. user_id=pingou.id,
  151. name='test2',
  152. is_fork=False,
  153. parent_id=None,
  154. description='test project #2',
  155. hook_token='aaabbbddd',
  156. )
  157. item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  158. session.add(item)
  159. item = project3 = pagure.lib.model.Project(
  160. user_id=pingou.id,
  161. name='test3',
  162. is_fork=False,
  163. parent_id=None,
  164. description='namespaced test project',
  165. hook_token='aaabbbeee',
  166. namespace='somenamespace',
  167. )
  168. item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  169. session.add(item)
  170. session.commit()
  171. tests.create_projects_git(_config['GIT_FOLDER'], bare=True)
  172. add_content_git_repo(
  173. os.path.join(_config['GIT_FOLDER'], 'test.git'))
  174. tests.add_readme_git_repo(
  175. os.path.join(_config['GIT_FOLDER'], 'test.git'))
  176. # Add some content to the git repo
  177. add_content_git_repo(
  178. os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
  179. 'test.git'))
  180. tests.add_readme_git_repo(
  181. os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
  182. 'test.git'))
  183. tests.add_commit_git_repo(
  184. os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
  185. 'test.git'), ncommits=10)
  186. ######################################
  187. # user_emails
  188. item = pagure.lib.model.UserEmail(
  189. user_id=pingou.id,
  190. email='bar@pingou.com')
  191. session.add(item)
  192. item = pagure.lib.model.UserEmail(
  193. user_id=pingou.id,
  194. email='foo@pingou.com')
  195. session.add(item)
  196. item = pagure.lib.model.UserEmail(
  197. user_id=foo.id,
  198. email='foo@bar.com')
  199. session.add(item)
  200. item = pagure.lib.model.UserEmail(
  201. user_id=you.id,
  202. email=user_email)
  203. session.add(item)
  204. session.commit()
  205. ######################################
  206. # user_emails_pending
  207. email_pend = pagure.lib.model.UserEmailPending(
  208. user_id=pingou.id,
  209. email='foo@fp.o',
  210. token='abcdef',
  211. )
  212. session.add(email_pend)
  213. session.commit()
  214. ######################################
  215. # issues
  216. # Add an issue and tag it so that we can list them
  217. item = pagure.lib.model.Issue(
  218. id=1001,
  219. uid='foobar',
  220. project_id=project1.id,
  221. title='Problem with jenkins build',
  222. content='For some reason the tests fail at line:24',
  223. user_id=pingou.id,
  224. )
  225. session.add(item)
  226. session.commit()
  227. item = pagure.lib.model.Issue(
  228. id=1002,
  229. uid='foobar2',
  230. project_id=project1.id,
  231. title='Unit tests failing',
  232. content='Need to fix code for the unit tests to '
  233. 'pass so jenkins build can complete.',
  234. user_id=pingou.id,
  235. )
  236. session.add(item)
  237. session.commit()
  238. item = pagure.lib.model.Issue(
  239. id=1003,
  240. uid='foobar3',
  241. project_id=project1.id,
  242. title='Segfault during execution',
  243. content='Index out of bounds for variable i?',
  244. user_id=you.id,
  245. )
  246. session.add(item)
  247. session.commit()
  248. ######################################
  249. # pagure_user_group
  250. group = pagure.lib.search_groups(session, pattern=None,
  251. group_name="rel-eng", group_type=None)
  252. item = pagure.lib.model.PagureUserGroup(
  253. user_id=pingou.id,
  254. group_id=group.id
  255. )
  256. session.add(item)
  257. session.commit()
  258. group = pagure.lib.search_groups(session, pattern=None,
  259. group_name="admin", group_type=None)
  260. item = pagure.lib.model.PagureUserGroup(
  261. user_id=you.id,
  262. group_id=group.id
  263. )
  264. session.add(item)
  265. session.commit()
  266. group = pagure.lib.search_groups(session, pattern=None,
  267. group_name="group", group_type=None)
  268. item = pagure.lib.model.PagureUserGroup(
  269. user_id=foo.id,
  270. group_id=group.id
  271. )
  272. session.add(item)
  273. session.commit()
  274. ######################################
  275. # projects_groups
  276. group = pagure.lib.search_groups(session, pattern=None,
  277. group_name="rel-eng", group_type=None)
  278. repo = pagure.lib.get_authorized_project(session, 'test')
  279. item = pagure.lib.model.ProjectGroup(
  280. project_id=repo.id,
  281. group_id=group.id,
  282. access="commit"
  283. )
  284. session.add(item)
  285. session.commit()
  286. group = pagure.lib.search_groups(session, pattern=None,
  287. group_name="admin", group_type=None)
  288. repo = pagure.lib.get_authorized_project(session, 'test2')
  289. item = pagure.lib.model.ProjectGroup(
  290. project_id=repo.id,
  291. group_id=group.id,
  292. access="admin"
  293. )
  294. session.add(item)
  295. session.commit()
  296. ######################################
  297. # pull_requests
  298. repo = pagure.lib.get_authorized_project(session, 'test')
  299. forked_repo = pagure.lib.get_authorized_project(session, 'test')
  300. req = pagure.lib.new_pull_request(
  301. session=session,
  302. repo_from=forked_repo,
  303. branch_from='master',
  304. repo_to=repo,
  305. branch_to='master',
  306. title='Fixing code for unittest',
  307. user=username
  308. )
  309. session.commit()
  310. ######################################
  311. # tokens
  312. tests.create_tokens(session, user_id=pingou.id, project_id=project1.id)
  313. ######################################
  314. # user_projects
  315. repo = pagure.lib.get_authorized_project(session, 'test')
  316. item = pagure.lib.model.ProjectUser(
  317. project_id=repo.id,
  318. user_id=foo.id,
  319. access="commit"
  320. )
  321. session.add(item)
  322. session.commit()
  323. repo = pagure.lib.get_authorized_project(session, 'test2')
  324. item = pagure.lib.model.ProjectUser(
  325. project_id=repo.id,
  326. user_id=you.id,
  327. access="commit"
  328. )
  329. session.add(item)
  330. session.commit()
  331. ######################################
  332. # issue_comments
  333. item = pagure.lib.model.IssueComment(
  334. user_id=pingou.id,
  335. issue_uid='foobar',
  336. comment='We may need to adjust the unittests instead of the code.',
  337. )
  338. session.add(item)
  339. session.commit()
  340. ######################################
  341. # issue_to_issue
  342. repo = pagure.lib.get_authorized_project(session, 'test')
  343. all_issues = pagure.lib.search_issues(session, repo)
  344. pagure.lib.add_issue_dependency(session, all_issues[0],
  345. all_issues[1], 'pingou')
  346. ######################################
  347. # pull_request_comments
  348. user = pagure.lib.search_user(session, username='pingou')
  349. # only 1 pull request available atm
  350. pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
  351. item = pagure.lib.model.PullRequestComment(
  352. pull_request_uid=pr.uid,
  353. user_id=user.id,
  354. comment="+1 for me. Btw, could you rebase before you merge?",
  355. notification=0
  356. )
  357. session.add(item)
  358. session.commit()
  359. ######################################
  360. # pull_request_flags
  361. # only 1 pull request available atm
  362. pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
  363. item = pagure.lib.model.PullRequestFlag(
  364. uid="random_pr_flag_uid",
  365. pull_request_uid=pr.uid,
  366. user_id=pingou.id,
  367. username=pingou.user,
  368. percent=80,
  369. comment="Jenkins build passes",
  370. url=str(pr.id),
  371. status="Open"
  372. )
  373. session.add(item)
  374. session.commit()
  375. ######################################
  376. # tags_issues
  377. repo = pagure.lib.get_authorized_project(session, 'test')
  378. issues = pagure.lib.search_issues(session, repo)
  379. item = pagure.lib.model.TagIssue(
  380. issue_uid=issues[0].uid,
  381. tag='tag1',
  382. )
  383. session.add(item)
  384. session.commit()
  385. ######################################
  386. # tokens_acls
  387. tests.create_tokens_acl(session)
  388. ######################################
  389. # Fork a project
  390. # delete fork data
  391. fork_proj_location = "forks/foo/test.git"
  392. try:
  393. shutil.rmtree(os.path.join(_config['GIT_FOLDER'],
  394. fork_proj_location))
  395. except:
  396. print('git folder already deleted')
  397. try:
  398. shutil.rmtree(os.path.join(_config['DOCS_FOLDER'],
  399. fork_proj_location))
  400. except:
  401. print('docs folder already deleted')
  402. try:
  403. shutil.rmtree(os.path.join(_config['TICKETS_FOLDER'],
  404. fork_proj_location))
  405. except:
  406. print('tickets folder already deleted')
  407. try:
  408. shutil.rmtree(os.path.join(_config['REQUESTS_FOLDER'],
  409. fork_proj_location))
  410. except:
  411. print('requests folder already deleted')
  412. repo = pagure.lib.get_authorized_project(session, 'test')
  413. result = pagure.lib.fork_project(session, 'foo', repo)
  414. if result == 'Repo "test" cloned to "foo/test"':
  415. session.commit()
  416. def add_content_git_repo(folder, branch='master'):
  417. """ Create some content for the specified git repo. """
  418. if not os.path.exists(folder):
  419. os.makedirs(folder)
  420. brepo = pygit2.init_repository(folder, bare=True)
  421. newfolder = tempfile.mkdtemp(prefix='pagure-tests')
  422. repo = pygit2.clone_repository(folder, newfolder)
  423. # Create a file in that git repo
  424. with open(os.path.join(newfolder, 'sources'), 'w') as stream:
  425. stream.write('foo\n bar')
  426. repo.index.add('sources')
  427. repo.index.write()
  428. parents = []
  429. commit = None
  430. try:
  431. commit = repo.revparse_single(
  432. 'HEAD' if branch == 'master' else branch)
  433. except KeyError:
  434. pass
  435. if commit:
  436. parents = [commit.oid.hex]
  437. # Commits the files added
  438. tree = repo.index.write_tree()
  439. author = pygit2.Signature(
  440. 'Alice Author', 'alice@authors.tld')
  441. committer = pygit2.Signature(
  442. 'Cecil Committer', 'cecil@committers.tld')
  443. repo.create_commit(
  444. 'refs/heads/%s' % branch, # the name of the reference to update
  445. author,
  446. committer,
  447. 'Add sources file for testing',
  448. # binary string representing the tree object ID
  449. tree,
  450. # list of binary strings representing parents of the new commit
  451. parents,
  452. )
  453. parents = []
  454. commit = None
  455. try:
  456. commit = repo.revparse_single(
  457. 'HEAD' if branch == 'master' else branch)
  458. except KeyError:
  459. pass
  460. if commit:
  461. parents = [commit.oid.hex]
  462. subfolder = os.path.join('folder1', 'folder2')
  463. if not os.path.exists(os.path.join(newfolder, subfolder)):
  464. os.makedirs(os.path.join(newfolder, subfolder))
  465. # Create a file in that git repo
  466. with open(os.path.join(newfolder, subfolder, 'file'), 'w') as stream:
  467. stream.write('foo\n bar\nbaz')
  468. repo.index.add(os.path.join(subfolder, 'file'))
  469. repo.index.write()
  470. # Commits the files added
  471. tree = repo.index.write_tree()
  472. author = pygit2.Signature(
  473. 'Alice Author', 'alice@authors.tld')
  474. committer = pygit2.Signature(
  475. 'Cecil Committer', 'cecil@committers.tld')
  476. repo.create_commit(
  477. 'refs/heads/%s' % branch, # the name of the reference to update
  478. author,
  479. committer,
  480. 'Add some directory and a file for more testing',
  481. # binary string representing the tree object ID
  482. tree,
  483. # list of binary strings representing parents of the new commit
  484. parents
  485. )
  486. # Push to origin
  487. ori_remote = repo.remotes[0]
  488. master_ref = repo.lookup_reference(
  489. 'HEAD' if branch == 'master' else 'refs/heads/%s' % branch).resolve()
  490. refname = '%s:%s' % (master_ref.name, master_ref.name)
  491. PagureRepo.push(ori_remote, refname)
  492. shutil.rmtree(newfolder)
  493. if __name__ == "__main__":
  494. desc = "Run the dev database initialization/insertion/deletion " \
  495. "script for db located " + str(_config['DB_URL'])
  496. parser = argparse.ArgumentParser(prog="dev-data", description=desc)
  497. parser.add_argument('-i', '--init', action="store_true",
  498. help="Create the dev db")
  499. parser.add_argument('-p', '--populate', action="store_true",
  500. help="Add test data to the db")
  501. parser.add_argument('-d', '--delete', action="store_true",
  502. help="Wipe the dev db")
  503. parser.add_argument('-a', '--all', action="store_true",
  504. help="Create, Wipe, Populate the dev db")
  505. args = parser.parse_args()
  506. # forcing the user to choose
  507. if not any(vars(args).values()):
  508. parser.error('No arguments provided.')
  509. if args.init or args.delete or args.all:
  510. eng, meta = init_database()
  511. if args.delete or args.all:
  512. empty_dev_db(meta, eng)
  513. if args.populate or args.all:
  514. session = create_session(_config['DB_URL'])
  515. invalid_option = ['pingou', 'bar@pingou.com', 'foo', 'foo@bar.com']
  516. print("")
  517. user_name = raw_input(
  518. "Enter your username so we can add you into the test data: ")
  519. while user_name in invalid_option:
  520. print("Reserved names: " + str(invalid_option))
  521. user_name = raw_input(
  522. "Enter your username so we can add you into the test data: ")
  523. if not user_name.replace(" ", ""):
  524. user_name = 'pythagoras'
  525. print("")
  526. user_email = raw_input("Enter your user email: ")
  527. while user_email in invalid_option:
  528. print("Reserved names: " + str(invalid_option))
  529. user_email = raw_input("Enter your user email: ")
  530. if not user_email.replace(" ", ""):
  531. user_email = 'pythagoras@math.com'
  532. insert_data(session, user_name, user_email)