您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

169 行
5.1 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # called from wnf.py
  23. # lib/wnf.py --install [rootpassword] [dbname] [source]
  24. import os,sys
  25. class Installer:
  26. def __init__(self, root_login, root_password):
  27. import webnotes
  28. import webnotes.db
  29. self.root_password = root_password
  30. from webnotes.model.db_schema import DbManager
  31. self.conn = webnotes.db.Database(user=root_login, password=root_password)
  32. webnotes.conn=self.conn
  33. webnotes.session= {'user':'Administrator'}
  34. self.dbman = DbManager(self.conn)
  35. def import_from_db(self, target, source_path='', password = 'admin', verbose=0):
  36. """
  37. a very simplified version, just for the time being..will eventually be deprecated once the framework stabilizes.
  38. """
  39. import conf
  40. # delete user (if exists)
  41. self.dbman.delete_user(target)
  42. # create user and db
  43. self.dbman.create_user(target, conf.db_password)
  44. if verbose: print "Created user %s" % target
  45. # create a database
  46. self.dbman.create_database(target)
  47. if verbose: print "Created database %s" % target
  48. # grant privileges to user
  49. self.dbman.grant_all_privileges(target,target)
  50. if verbose: print "Granted privileges to user %s and database %s" % (target, target)
  51. # flush user privileges
  52. self.dbman.flush_privileges()
  53. self.conn.use(target)
  54. # import in target
  55. if verbose: print "Starting database import..."
  56. # get the path of the sql file to import
  57. source_given = True
  58. if not source_path:
  59. source_given = False
  60. source_path = os.path.join(os.path.sep.join(os.path.abspath(webnotes.__file__).split(os.path.sep)[:-3]), 'data', 'Framework.sql')
  61. self.dbman.restore_database(target, source_path, self.root_password)
  62. if verbose: print "Imported from database %s" % source_path
  63. # fresh app
  64. if 'Framework.sql' in source_path:
  65. from webnotes.model.sync import sync_core_doctypes
  66. print "Building tables from core module..."
  67. sync_core_doctypes()
  68. self.install_core()
  69. # framework cleanups
  70. self.framework_cleanups(target)
  71. if verbose: print "Ran framework startups on %s" % target
  72. return target
  73. def framework_cleanups(self, target):
  74. """create framework internal tables"""
  75. import webnotes
  76. self.create_sessions_table()
  77. self.create_scheduler_log()
  78. self.create_doctype_cache()
  79. self.create_session_cache()
  80. # set the basic passwords
  81. webnotes.conn.begin()
  82. webnotes.conn.sql("""update tabProfile set password = password('admin')
  83. where name='Administrator'""")
  84. webnotes.conn.commit()
  85. def create_sessions_table(self):
  86. """create sessions table"""
  87. import webnotes
  88. self.dbman.drop_table('tabSessions')
  89. webnotes.conn.sql("""CREATE TABLE `tabSessions` (
  90. `user` varchar(40) DEFAULT NULL,
  91. `sid` varchar(120) DEFAULT NULL,
  92. `sessiondata` longtext,
  93. `ipaddress` varchar(16) DEFAULT NULL,
  94. `lastupdate` datetime DEFAULT NULL,
  95. `status` varchar(20) DEFAULT NULL,
  96. KEY `sid` (`sid`)
  97. ) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
  98. def create_scheduler_log(self):
  99. import webnotes
  100. self.dbman.drop_table('__SchedulerLog')
  101. webnotes.conn.sql("""create table __SchedulerLog (
  102. `timestamp` timestamp,
  103. method varchar(200),
  104. error text
  105. ) engine=MyISAM""")
  106. def create_doctype_cache(self):
  107. import webnotes
  108. self.dbman.drop_table('__DocTypeCache')
  109. webnotes.conn.sql("""create table `__DocTypeCache` (
  110. name VARCHAR(120),
  111. modified DATETIME,
  112. content TEXT,
  113. server_code_compiled TEXT)""")
  114. def create_session_cache(self):
  115. import webnotes
  116. self.dbman.drop_table('__SessionCache')
  117. webnotes.conn.sql("""create table `__SessionCache` (
  118. user VARCHAR(120),
  119. country VARCHAR(120),
  120. cache LONGTEXT)""")
  121. def install_core(self):
  122. """create install docs"""
  123. import webnotes
  124. from core import install_docs
  125. from webnotes.model.doc import Document
  126. from webnotes.modules import reload_doc
  127. webnotes.conn.begin()
  128. for data in install_docs:
  129. d = Document(data['doctype'])
  130. d.fields.update(data)
  131. d.save()
  132. print 'Created %(doctype)s %(name)s' % d.fields
  133. # login page
  134. reload_doc('core', 'page', 'login_page')
  135. print 'Loaded Login Page'
  136. webnotes.conn.commit()