Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3.  
  4. """Get Drupal system table records for modules with MySQL.
  5.  
  6. Usage:
  7. 1. Change parameters for database in config_mysql in the top of code.
  8. 2. Run this as a script: python this_script_name.pyj
  9.  
  10. Exported info:
  11. - project
  12. - module name
  13. - status
  14. - version
  15.  
  16. Sample output:
  17. ...
  18. views_date_format_sql,views_date_format_sql,0,7.x-3.1
  19. views_flipped_table,views_flipped_table,0,7.x-1.0
  20. views_php,views_php,0,7.x-1.0-alpha1
  21. views_rules,views_rules,0,7.x-1.0
  22. views_rules,views_rules_test,0,7.x-1.0
  23. ...
  24. """
  25.  
  26.  
  27. import sys
  28. import csv
  29. import pymysql
  30. import phpserialize
  31.  
  32.  
  33. # Change this config parameters to match your environment.
  34. config_mysql = {
  35. # Example:
  36. # host: localhost
  37. # user: taro
  38. # password: taro_the_dog
  39. # db: drupal7
  40. # charset: utf8, utf8mb4, latin1
  41. 'host': 'YOUR_DB_HOST',
  42. 'user': 'YOUR_DB_USERNAME',
  43. 'password': 'YOUR_DB_PASSWORD',
  44. 'db': 'YOUR_DB_NAME',
  45. 'charset': 'YOUR_DB_CHARSET',
  46. }
  47.  
  48.  
  49. def main():
  50. """Main function when being run as a script.
  51. """
  52. modules = select_drupal_system_table_flattened()
  53. print_rows_as_csv(modules)
  54.  
  55.  
  56. def connect():
  57. """Connect to a MySQL database table.
  58. """
  59. config_mysql_instance = config_mysql.copy()
  60. config_mysql_instance['cursorclass'] = pymysql.cursors.DictCursor
  61.  
  62. connection = pymysql.connect(**config_mysql_instance)
  63. return connection
  64.  
  65.  
  66. def select(sql, params, single=False):
  67. """Run SELECT query.
  68.  
  69. Usage:
  70. select('SELECT `uid`, `pass` FROM `users`', (), False)
  71. select("SELECT `name`, `type`, `status`, `info` FROM `system`", (), True)
  72. """
  73. connection = connect()
  74. try:
  75. with connection.cursor() as cursor:
  76. cursor.execute(sql, params)
  77. if single:
  78. result = cursor.fetchone()
  79. else:
  80. result = cursor.fetchall()
  81. return result
  82. finally:
  83. connection.close()
  84.  
  85.  
  86. def select_phpunserialize(sql, params, phpload_columns):
  87. """Run SELECT query and unserialize selected columns.
  88.  
  89. Usage:
  90. select_phpunserialize(
  91. "SELECT `name`,`type`,`status`, `info` FROM `system` WHERE `type`= %s",
  92. ('module'), ['info'])
  93. """
  94. result_raw = select(sql, params)
  95. def _map(row):
  96. for k, v in row.items():
  97. if k in phpload_columns:
  98. row[k] = phpserialize.loads(v)
  99. return row
  100. return list(map(_map, result_raw))
  101.  
  102.  
  103. def select_drupal_system_table_flattened():
  104. """Fetch all records for module in `system` table.
  105. """
  106. sql = "SELECT `name`,`type`,`status`,`info` FROM `system` WHERE `type` = %s"
  107. params = ('module', )
  108. phpload_columns = ['info']
  109. modules = select_phpunserialize(sql, params, phpload_columns)
  110.  
  111. def _flatten(row):
  112. return map(_to_str, [
  113. row['info'].get(b'project'),
  114. row['name'],
  115. row['status'],
  116. row['info'][b'version']])
  117.  
  118. def _to_str(value):
  119. if value is None:
  120. return ''
  121. if isinstance(value, bytes):
  122. return value.decode()
  123. else:
  124. return value
  125.  
  126. return map(_flatten, modules)
  127.  
  128.  
  129. def print_rows_as_csv(rows):
  130. """Print out rows of list in csv format.
  131. """
  132. writer = csv.writer(sys.stdout)
  133. writer.writerows(rows)
  134.  
  135.  
  136. if __name__ == '__main__':
  137. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement